compiler/libec: Fix for sizeof to be of uintsize type
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50       bool backOutputLineNumbers = outputLineNumbers;
51       outputLineNumbers = false;
52
53       if(exp)
54          OutputExpression(exp, f);
55       f.Seek(0, start);
56       count = strlen(string);
57       count += f.Read(string + count, 1, 1023);
58       string[count] = '\0';
59       delete f;
60
61       outputLineNumbers = backOutputLineNumbers;
62    }
63 }
64
65 Type ProcessTemplateParameterType(TemplateParameter param)
66 {
67    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
68    {
69       // TOFIX: Will need to free this Type
70       if(!param.baseType)
71       {
72          if(param.dataTypeString)
73             param.baseType = ProcessTypeString(param.dataTypeString, false);
74          else
75             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
76       }
77       return param.baseType;
78    }
79    return null;
80 }
81
82 bool NeedCast(Type type1, Type type2)
83 {
84    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
85
86    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
87    {
88       return false;
89    }
90
91    if(type1.kind == type2.kind)
92    {
93       switch(type1.kind)
94       {
95          case _BoolType:
96          case charType:
97          case shortType:
98          case intType:
99          case int64Type:
100          case intPtrType:
101          case intSizeType:
102             if(type1.passAsTemplate && !type2.passAsTemplate)
103                return true;
104             return type1.isSigned != type2.isSigned;
105          case classType:
106             return type1._class != type2._class;
107          case pointerType:
108             return NeedCast(type1.type, type2.type);
109          default:
110             return true; //false; ????
111       }
112    }
113    return true;
114 }
115
116 static void ReplaceClassMembers(Expression exp, Class _class)
117 {
118    if(exp.type == identifierExp && exp.identifier)
119    {
120       Identifier id = exp.identifier;
121       Context ctx;
122       Symbol symbol = null;
123       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
124       {
125          // First, check if the identifier is declared inside the function
126          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
127          {
128             symbol = (Symbol)ctx.symbols.FindString(id.string);
129             if(symbol) break;
130          }
131       }
132
133       // If it is not, check if it is a member of the _class
134       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
135       {
136          Property prop = eClass_FindProperty(_class, id.string, privateModule);
137          Method method = null;
138          DataMember member = null;
139          ClassProperty classProp = null;
140          if(!prop)
141          {
142             method = eClass_FindMethod(_class, id.string, privateModule);
143          }
144          if(!prop && !method)
145             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
146          if(!prop && !method && !member)
147          {
148             classProp = eClass_FindClassProperty(_class, id.string);
149          }
150          if(prop || method || member || classProp)
151          {
152             // Replace by this.[member]
153             exp.type = memberExp;
154             exp.member.member = id;
155             exp.member.memberType = unresolvedMember;
156             exp.member.exp = QMkExpId("this");
157             //exp.member.exp.loc = exp.loc;
158             exp.addedThis = true;
159          }
160          else if(_class && _class.templateParams.first)
161          {
162             Class sClass;
163             for(sClass = _class; sClass; sClass = sClass.base)
164             {
165                if(sClass.templateParams.first)
166                {
167                   ClassTemplateParameter param;
168                   for(param = sClass.templateParams.first; param; param = param.next)
169                   {
170                      if(param.type == expression && !strcmp(param.name, id.string))
171                      {
172                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
173
174                         if(argExp)
175                         {
176                            Declarator decl;
177                            OldList * specs = MkList();
178
179                            FreeIdentifier(exp.member.member);
180
181                            ProcessExpressionType(argExp);
182
183                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
184
185                            exp.expType = ProcessType(specs, decl);
186
187                            // *[expType] *[argExp]
188                            exp.type = bracketsExp;
189                            exp.list = MkListOne(MkExpOp(null, '*',
190                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
191                         }
192                      }
193                   }
194                }
195             }
196          }
197       }
198    }
199 }
200
201 ////////////////////////////////////////////////////////////////////////
202 // PRINTING ////////////////////////////////////////////////////////////
203 ////////////////////////////////////////////////////////////////////////
204
205 public char * PrintInt(int64 result)
206 {
207    char temp[100];
208    if(result > MAXINT)
209       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
210    else
211       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
212    if(result > MAXINT || result < MININT)
213       strcat(temp, "LL");
214    return CopyString(temp);
215 }
216
217 public char * PrintUInt(uint64 result)
218 {
219    char temp[100];
220    if(result > MAXDWORD)
221       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
222    else if(result > MAXINT)
223       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
224    else
225       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
226    return CopyString(temp);
227 }
228
229 public char * PrintInt64(int64 result)
230 {
231    char temp[100];
232    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
233    return CopyString(temp);
234 }
235
236 public char * PrintUInt64(uint64 result)
237 {
238    char temp[100];
239    if(result > MAXINT64)
240       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
241    else
242       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
243    return CopyString(temp);
244 }
245
246 public char * PrintHexUInt(uint64 result)
247 {
248    char temp[100];
249    if(result > MAXDWORD)
250       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
251    else
252       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
253    if(result > MAXDWORD)
254       strcat(temp, "LL");
255    return CopyString(temp);
256 }
257
258 public char * PrintHexUInt64(uint64 result)
259 {
260    char temp[100];
261    if(result > MAXDWORD)
262       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
263    else
264       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
265    return CopyString(temp);
266 }
267
268 public char * PrintShort(short result)
269 {
270    char temp[100];
271    sprintf(temp, "%d", (unsigned short)result);
272    return CopyString(temp);
273 }
274
275 public char * PrintUShort(unsigned short result)
276 {
277    char temp[100];
278    if(result > 32767)
279       sprintf(temp, "0x%X", (int)result);
280    else
281       sprintf(temp, "%d", (int)result);
282    return CopyString(temp);
283 }
284
285 public char * PrintChar(char result)
286 {
287    char temp[100];
288    if(result > 0 && isprint(result))
289       sprintf(temp, "'%c'", result);
290    else if(result < 0)
291       sprintf(temp, "%d", (int)result);
292    else
293       //sprintf(temp, "%#X", result);
294       sprintf(temp, "0x%X", (unsigned char)result);
295    return CopyString(temp);
296 }
297
298 public char * PrintUChar(unsigned char result)
299 {
300    char temp[100];
301    sprintf(temp, "0x%X", result);
302    return CopyString(temp);
303 }
304
305 public char * PrintFloat(float result)
306 {
307    char temp[350];
308    if(result.isInf)
309    {
310       if(result.signBit)
311          strcpy(temp, "-inf");
312       else
313          strcpy(temp, "inf");
314    }
315    else if(result.isNan)
316    {
317       if(result.signBit)
318          strcpy(temp, "-nan");
319       else
320          strcpy(temp, "nan");
321    }
322    else
323       sprintf(temp, "%.16ff", result);
324    return CopyString(temp);
325 }
326
327 public char * PrintDouble(double result)
328 {
329    char temp[350];
330    if(result.isInf)
331    {
332       if(result.signBit)
333          strcpy(temp, "-inf");
334       else
335          strcpy(temp, "inf");
336    }
337    else if(result.isNan)
338    {
339       if(result.signBit)
340          strcpy(temp, "-nan");
341       else
342          strcpy(temp, "nan");
343    }
344    else
345       sprintf(temp, "%.16f", result);
346    return CopyString(temp);
347 }
348
349 ////////////////////////////////////////////////////////////////////////
350 ////////////////////////////////////////////////////////////////////////
351
352 //public Operand GetOperand(Expression exp);
353
354 #define GETVALUE(name, t) \
355    public bool GetOp##name(Operand op2, t * value2) \
356    {                                                        \
357       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
358       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
359       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
360       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
361       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
362       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
363       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
364       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
365       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
366       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
367       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
368       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
369       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
370       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
371       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
372       else                                                                          \
373          return false;                                                              \
374       return true;                                                                  \
375    } \
376    public bool Get##name(Expression exp, t * value2) \
377    {                                                        \
378       Operand op2 = GetOperand(exp);                        \
379       return GetOp##name(op2, value2); \
380    }
381
382 // To help the deubugger currently not preprocessing...
383 #define HELP(x) x
384
385 GETVALUE(Int, HELP(int));
386 GETVALUE(UInt, HELP(unsigned int));
387 GETVALUE(Int64, HELP(int64));
388 GETVALUE(UInt64, HELP(uint64));
389 GETVALUE(IntPtr, HELP(intptr));
390 GETVALUE(UIntPtr, HELP(uintptr));
391 GETVALUE(IntSize, HELP(intsize));
392 GETVALUE(UIntSize, HELP(uintsize));
393 GETVALUE(Short, HELP(short));
394 GETVALUE(UShort, HELP(unsigned short));
395 GETVALUE(Char, HELP(char));
396 GETVALUE(UChar, HELP(unsigned char));
397 GETVALUE(Float, HELP(float));
398 GETVALUE(Double, HELP(double));
399
400 void ComputeExpression(Expression exp);
401
402 void ComputeClassMembers(Class _class, bool isMember)
403 {
404    DataMember member = isMember ? (DataMember) _class : null;
405    Context context = isMember ? null : SetupTemplatesContext(_class);
406    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
407                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
408    {
409       int c;
410       int unionMemberOffset = 0;
411       int bitFields = 0;
412
413       /*
414       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
415          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
416       */
417
418       if(member)
419       {
420          member.memberOffset = 0;
421          if(targetBits < sizeof(void *) * 8)
422             member.structAlignment = 0;
423       }
424       else if(targetBits < sizeof(void *) * 8)
425          _class.structAlignment = 0;
426
427       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
428       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
429          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
430
431       if(!member && _class.destructionWatchOffset)
432          _class.memberOffset += sizeof(OldList);
433
434       // To avoid reentrancy...
435       //_class.structSize = -1;
436
437       {
438          DataMember dataMember;
439          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
440          {
441             if(!dataMember.isProperty)
442             {
443                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
444                {
445                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
446                   /*if(!dataMember.dataType)
447                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
448                      */
449                }
450             }
451          }
452       }
453
454       {
455          DataMember dataMember;
456          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
457          {
458             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
459             {
460                if(!isMember && _class.type == bitClass && dataMember.dataType)
461                {
462                   BitMember bitMember = (BitMember) dataMember;
463                   uint64 mask = 0;
464                   int d;
465
466                   ComputeTypeSize(dataMember.dataType);
467
468                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
469                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
470
471                   _class.memberOffset = bitMember.pos + bitMember.size;
472                   for(d = 0; d<bitMember.size; d++)
473                   {
474                      if(d)
475                         mask <<= 1;
476                      mask |= 1;
477                   }
478                   bitMember.mask = mask << bitMember.pos;
479                }
480                else if(dataMember.type == normalMember && dataMember.dataType)
481                {
482                   int size;
483                   int alignment = 0;
484
485                   // Prevent infinite recursion
486                   if(dataMember.dataType.kind != classType ||
487                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
488                      _class.type != structClass)))
489                      ComputeTypeSize(dataMember.dataType);
490
491                   if(dataMember.dataType.bitFieldCount)
492                   {
493                      bitFields += dataMember.dataType.bitFieldCount;
494                      size = 0;
495                   }
496                   else
497                   {
498                      if(bitFields)
499                      {
500                         int size = (bitFields + 7) / 8;
501
502                         if(isMember)
503                         {
504                            // TESTING THIS PADDING CODE
505                            if(alignment)
506                            {
507                               member.structAlignment = Max(member.structAlignment, alignment);
508
509                               if(member.memberOffset % alignment)
510                                  member.memberOffset += alignment - (member.memberOffset % alignment);
511                            }
512
513                            dataMember.offset = member.memberOffset;
514                            if(member.type == unionMember)
515                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
516                            else
517                            {
518                               member.memberOffset += size;
519                            }
520                         }
521                         else
522                         {
523                            // TESTING THIS PADDING CODE
524                            if(alignment)
525                            {
526                               _class.structAlignment = Max(_class.structAlignment, alignment);
527
528                               if(_class.memberOffset % alignment)
529                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
530                            }
531
532                            dataMember.offset = _class.memberOffset;
533                            _class.memberOffset += size;
534                         }
535                         bitFields = 0;
536                      }
537                      size = dataMember.dataType.size;
538                      alignment = dataMember.dataType.alignment;
539                   }
540
541                   if(isMember)
542                   {
543                      // TESTING THIS PADDING CODE
544                      if(alignment)
545                      {
546                         member.structAlignment = Max(member.structAlignment, alignment);
547
548                         if(member.memberOffset % alignment)
549                            member.memberOffset += alignment - (member.memberOffset % alignment);
550                      }
551
552                      dataMember.offset = member.memberOffset;
553                      if(member.type == unionMember)
554                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
555                      else
556                      {
557                         member.memberOffset += size;
558                      }
559                   }
560                   else
561                   {
562                      // TESTING THIS PADDING CODE
563                      if(alignment)
564                      {
565                         _class.structAlignment = Max(_class.structAlignment, alignment);
566
567                         if(_class.memberOffset % alignment)
568                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
569                      }
570
571                      dataMember.offset = _class.memberOffset;
572                      _class.memberOffset += size;
573                   }
574                }
575                else
576                {
577                   int alignment;
578
579                   ComputeClassMembers((Class)dataMember, true);
580                   alignment = dataMember.structAlignment;
581
582                   if(isMember)
583                   {
584                      if(alignment)
585                      {
586                         if(member.memberOffset % alignment)
587                            member.memberOffset += alignment - (member.memberOffset % alignment);
588
589                         member.structAlignment = Max(member.structAlignment, alignment);
590                      }
591                      dataMember.offset = member.memberOffset;
592                      if(member.type == unionMember)
593                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
594                      else
595                         member.memberOffset += dataMember.memberOffset;
596                   }
597                   else
598                   {
599                      if(alignment)
600                      {
601                         if(_class.memberOffset % alignment)
602                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
603                         _class.structAlignment = Max(_class.structAlignment, alignment);
604                      }
605                      dataMember.offset = _class.memberOffset;
606                      _class.memberOffset += dataMember.memberOffset;
607                   }
608                }
609             }
610          }
611          if(bitFields)
612          {
613             int alignment = 0;
614             int size = (bitFields + 7) / 8;
615
616             if(isMember)
617             {
618                // TESTING THIS PADDING CODE
619                if(alignment)
620                {
621                   member.structAlignment = Max(member.structAlignment, alignment);
622
623                   if(member.memberOffset % alignment)
624                      member.memberOffset += alignment - (member.memberOffset % alignment);
625                }
626
627                if(member.type == unionMember)
628                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
629                else
630                {
631                   member.memberOffset += size;
632                }
633             }
634             else
635             {
636                // TESTING THIS PADDING CODE
637                if(alignment)
638                {
639                   _class.structAlignment = Max(_class.structAlignment, alignment);
640
641                   if(_class.memberOffset % alignment)
642                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
643                }
644                _class.memberOffset += size;
645             }
646             bitFields = 0;
647          }
648       }
649       if(member && member.type == unionMember)
650       {
651          member.memberOffset = unionMemberOffset;
652       }
653
654       if(!isMember)
655       {
656          /*if(_class.type == structClass)
657             _class.size = _class.memberOffset;
658          else
659          */
660
661          if(_class.type != bitClass)
662          {
663             int extra = 0;
664             if(_class.structAlignment)
665             {
666                if(_class.memberOffset % _class.structAlignment)
667                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
668             }
669             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
670             if(!member)
671             {
672                Property prop;
673                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
674                {
675                   if(prop.isProperty && prop.isWatchable)
676                   {
677                      prop.watcherOffset = _class.structSize;
678                      _class.structSize += sizeof(OldList);
679                   }
680                }
681             }
682
683             // Fix Derivatives
684             {
685                OldLink derivative;
686                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
687                {
688                   Class deriv = derivative.data;
689
690                   if(deriv.computeSize)
691                   {
692                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
693                      deriv.offset = /*_class.offset + */_class.structSize;
694                      deriv.memberOffset = 0;
695                      // ----------------------
696
697                      deriv.structSize = deriv.offset;
698
699                      ComputeClassMembers(deriv, false);
700                   }
701                }
702             }
703          }
704       }
705    }
706    if(context)
707       FinishTemplatesContext(context);
708 }
709
710 public void ComputeModuleClasses(Module module)
711 {
712    Class _class;
713    OldLink subModule;
714
715    for(subModule = module.modules.first; subModule; subModule = subModule.next)
716       ComputeModuleClasses(subModule.data);
717    for(_class = module.classes.first; _class; _class = _class.next)
718       ComputeClassMembers(_class, false);
719 }
720
721
722 public int ComputeTypeSize(Type type)
723 {
724    uint size = type ? type.size : 0;
725    if(!size && type && !type.computing)
726    {
727       type.computing = true;
728       switch(type.kind)
729       {
730          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
731          case charType: type.alignment = size = sizeof(char); break;
732          case intType: type.alignment = size = sizeof(int); break;
733          case int64Type: type.alignment = size = sizeof(int64); break;
734          case intPtrType: type.alignment = size = targetBits / 8; break;
735          case intSizeType: type.alignment = size = targetBits / 8; break;
736          case longType: type.alignment = size = sizeof(long); break;
737          case shortType: type.alignment = size = sizeof(short); break;
738          case floatType: type.alignment = size = sizeof(float); break;
739          case doubleType: type.alignment = size = sizeof(double); break;
740          case classType:
741          {
742             Class _class = type._class ? type._class.registered : null;
743
744             if(_class && _class.type == structClass)
745             {
746                // Ensure all members are properly registered
747                ComputeClassMembers(_class, false);
748                type.alignment = _class.structAlignment;
749                size = _class.structSize;
750                if(type.alignment && size % type.alignment)
751                   size += type.alignment - (size % type.alignment);
752
753             }
754             else if(_class && (_class.type == unitClass ||
755                    _class.type == enumClass ||
756                    _class.type == bitClass))
757             {
758                if(!_class.dataType)
759                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
760                size = type.alignment = ComputeTypeSize(_class.dataType);
761             }
762             else
763                size = type.alignment = targetBits / 8; // sizeof(Instance *);
764             break;
765          }
766          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
767          case arrayType:
768             if(type.arraySizeExp)
769             {
770                ProcessExpressionType(type.arraySizeExp);
771                ComputeExpression(type.arraySizeExp);
772                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType &&
773                   type.arraySizeExp.expType.kind != shortType &&
774                   type.arraySizeExp.expType.kind != charType &&
775                   type.arraySizeExp.expType.kind != longType &&
776                   type.arraySizeExp.expType.kind != int64Type &&
777                   type.arraySizeExp.expType.kind != intSizeType &&
778                   type.arraySizeExp.expType.kind != intPtrType &&
779                   type.arraySizeExp.expType.kind != enumType &&
780                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
781                {
782                   Location oldLoc = yylloc;
783                   // bool isConstant = type.arraySizeExp.isConstant;
784                   char expression[10240];
785                   expression[0] = '\0';
786                   type.arraySizeExp.expType = null;
787                   yylloc = type.arraySizeExp.loc;
788                   if(inCompiler)
789                      PrintExpression(type.arraySizeExp, expression);
790                   Compiler_Error($"Array size not constant int (%s)\n", expression);
791                   yylloc = oldLoc;
792                }
793                GetInt(type.arraySizeExp, &type.arraySize);
794             }
795             else if(type.enumClass)
796             {
797                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
798                {
799                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
800                }
801                else
802                   type.arraySize = 0;
803             }
804             else
805             {
806                // Unimplemented auto size
807                type.arraySize = 0;
808             }
809
810             size = ComputeTypeSize(type.type) * type.arraySize;
811             if(type.type)
812                type.alignment = type.type.alignment;
813
814             break;
815          case structType:
816          {
817             Type member;
818             for(member = type.members.first; member; member = member.next)
819             {
820                uint addSize = ComputeTypeSize(member);
821
822                member.offset = size;
823                if(member.alignment && size % member.alignment)
824                   member.offset += member.alignment - (size % member.alignment);
825                size = member.offset;
826
827                type.alignment = Max(type.alignment, member.alignment);
828                size += addSize;
829             }
830             if(type.alignment && size % type.alignment)
831                size += type.alignment - (size % type.alignment);
832             break;
833          }
834          case unionType:
835          {
836             Type member;
837             for(member = type.members.first; member; member = member.next)
838             {
839                uint addSize = ComputeTypeSize(member);
840
841                member.offset = size;
842                if(member.alignment && size % member.alignment)
843                   member.offset += member.alignment - (size % member.alignment);
844                size = member.offset;
845
846                type.alignment = Max(type.alignment, member.alignment);
847                size = Max(size, addSize);
848             }
849             if(type.alignment && size % type.alignment)
850                size += type.alignment - (size % type.alignment);
851             break;
852          }
853          case templateType:
854          {
855             TemplateParameter param = type.templateParameter;
856             Type baseType = ProcessTemplateParameterType(param);
857             if(baseType)
858             {
859                size = ComputeTypeSize(baseType);
860                type.alignment = baseType.alignment;
861             }
862             else
863                type.alignment = size = sizeof(uint64);
864             break;
865          }
866          case enumType:
867          {
868             type.alignment = size = sizeof(enum { test });
869             break;
870          }
871          case thisClassType:
872          {
873             type.alignment = size = targetBits / 8; //sizeof(void *);
874             break;
875          }
876       }
877       type.size = size;
878       type.computing = false;
879    }
880    return size;
881 }
882
883
884 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
885 {
886    // This function is in need of a major review when implementing private members etc.
887    DataMember topMember = isMember ? (DataMember) _class : null;
888    uint totalSize = 0;
889    uint maxSize = 0;
890    int alignment, 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    uint totalSize = 0;
1017    DataMember member;
1018    Context context = isMember ? null : SetupTemplatesContext(_class);
1019
1020    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1021       DeclareMembers(_class.base, false);
1022
1023    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1024    {
1025       if(!member.isProperty)
1026       {
1027          switch(member.type)
1028          {
1029             case normalMember:
1030             {
1031                /*
1032                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1033                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1034                   DeclareStruct(member.dataType._class.string, false);
1035                   */
1036                if(!member.dataType && member.dataTypeString)
1037                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1038                if(member.dataType)
1039                   DeclareType(member.dataType, false, false);
1040                break;
1041             }
1042             case unionMember:
1043             case structMember:
1044             {
1045                DeclareMembers((Class)member, true);
1046                break;
1047             }
1048          }
1049       }
1050    }
1051    if(context)
1052       FinishTemplatesContext(context);
1053
1054    return topMember ? topMember.memberID : _class.memberID;
1055 }
1056
1057 void DeclareStruct(char * name, bool skipNoHead)
1058 {
1059    External external = null;
1060    Symbol classSym = FindClass(name);
1061
1062    if(!inCompiler || !classSym) return;
1063
1064    // We don't need any declaration for bit classes...
1065    if(classSym.registered &&
1066       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1067       return;
1068
1069    /*if(classSym.registered.templateClass)
1070       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1071    */
1072
1073    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1074    {
1075       // Add typedef struct
1076       Declaration decl;
1077       OldList * specifiers, * declarators;
1078       OldList * declarations = null;
1079       char structName[1024];
1080       Specifier spec = null;
1081       external = (classSym.registered && classSym.registered.type == structClass) ?
1082          classSym.pointerExternal : classSym.structExternal;
1083
1084       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1085       // Moved this one up because DeclareClass done later will need it
1086
1087       classSym.declaring++;
1088
1089       if(strchr(classSym.string, '<'))
1090       {
1091          if(classSym.registered.templateClass)
1092          {
1093             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1094             classSym.declaring--;
1095          }
1096          return;
1097       }
1098
1099       //if(!skipNoHead)
1100          DeclareMembers(classSym.registered, false);
1101
1102       structName[0] = 0;
1103       FullClassNameCat(structName, name, false);
1104
1105       if(external && external.declaration && external.declaration.specifiers)
1106       {
1107          for(spec = external.declaration.specifiers->first; spec; spec = spec.next)
1108          {
1109             if(spec.type == structSpecifier || spec.type == unionSpecifier)
1110                break;
1111          }
1112       }
1113
1114       /*if(!external)
1115          external = MkExternalDeclaration(null);*/
1116
1117       if(!skipNoHead && (!spec || !spec.definitions))
1118       {
1119          bool addedPadding = false;
1120          classSym.declaredStructSym = true;
1121
1122          declarations = MkList();
1123
1124          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1125
1126          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1127          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1128
1129          if(!declarations->count || (declarations->count == 1 && addedPadding))
1130          {
1131             FreeList(declarations, FreeClassDef);
1132             declarations = null;
1133          }
1134       }
1135       if(skipNoHead || declarations)
1136       {
1137          if(spec)
1138          {
1139             if(declarations)
1140                spec.definitions = declarations;
1141
1142             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1143             {
1144                // TODO: Fix this
1145                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1146
1147                // DANGER
1148                if(classSym.structExternal)
1149                   ast->Move(classSym.structExternal, curExternal.prev);
1150                ast->Move(classSym.pointerExternal, curExternal.prev);
1151
1152                classSym.id = curExternal.symbol.idCode;
1153                classSym.idCode = curExternal.symbol.idCode;
1154                // external = classSym.pointerExternal;
1155                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1156             }
1157          }
1158          else
1159          {
1160             if(!external)
1161                external = MkExternalDeclaration(null);
1162
1163             specifiers = MkList();
1164             declarators = MkList();
1165             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1166
1167             /*
1168             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1169             ListAdd(declarators, MkInitDeclarator(d, null));
1170             */
1171             external.declaration = decl = MkDeclaration(specifiers, declarators);
1172             if(decl.symbol && !decl.symbol.pointerExternal)
1173                decl.symbol.pointerExternal = external;
1174
1175             // For simple classes, keep the declaration as the external to move around
1176             if(classSym.registered && classSym.registered.type == structClass)
1177             {
1178                char className[1024];
1179                strcpy(className, "__ecereClass_");
1180                FullClassNameCat(className, classSym.string, true);
1181                MangleClassName(className);
1182
1183                // Testing This
1184                DeclareClass(classSym, className);
1185
1186                external.symbol = classSym;
1187                classSym.pointerExternal = external;
1188                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1189                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1190             }
1191             else
1192             {
1193                char className[1024];
1194                strcpy(className, "__ecereClass_");
1195                FullClassNameCat(className, classSym.string, true);
1196                MangleClassName(className);
1197
1198                // TOFIX: TESTING THIS...
1199                classSym.structExternal = external;
1200                DeclareClass(classSym, className);
1201                external.symbol = classSym;
1202             }
1203
1204             //if(curExternal)
1205                ast->Insert(curExternal ? curExternal.prev : null, external);
1206          }
1207       }
1208
1209       classSym.declaring--;
1210    }
1211    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1212    {
1213       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1214       // Moved this one up because DeclareClass done later will need it
1215
1216       // TESTING THIS:
1217       classSym.declaring++;
1218
1219       //if(!skipNoHead)
1220       {
1221          if(classSym.registered)
1222             DeclareMembers(classSym.registered, false);
1223       }
1224
1225       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1226       {
1227          // TODO: Fix this
1228          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1229
1230          // DANGER
1231          if(classSym.structExternal)
1232             ast->Move(classSym.structExternal, curExternal.prev);
1233          ast->Move(classSym.pointerExternal, curExternal.prev);
1234
1235          classSym.id = curExternal.symbol.idCode;
1236          classSym.idCode = curExternal.symbol.idCode;
1237          // external = classSym.pointerExternal;
1238          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1239       }
1240
1241       classSym.declaring--;
1242    }
1243    //return external;
1244 }
1245
1246 void DeclareProperty(Property prop, char * setName, char * getName)
1247 {
1248    Symbol symbol = prop.symbol;
1249    char propName[1024];
1250
1251    strcpy(setName, "__ecereProp_");
1252    FullClassNameCat(setName, prop._class.fullName, false);
1253    strcat(setName, "_Set_");
1254    // strcat(setName, prop.name);
1255    FullClassNameCat(setName, prop.name, true);
1256
1257    strcpy(getName, "__ecereProp_");
1258    FullClassNameCat(getName, prop._class.fullName, false);
1259    strcat(getName, "_Get_");
1260    FullClassNameCat(getName, prop.name, true);
1261    // strcat(getName, prop.name);
1262
1263    strcpy(propName, "__ecereProp_");
1264    FullClassNameCat(propName, prop._class.fullName, false);
1265    strcat(propName, "_");
1266    FullClassNameCat(propName, prop.name, true);
1267    // strcat(propName, prop.name);
1268
1269    // To support "char *" property
1270    MangleClassName(getName);
1271    MangleClassName(setName);
1272    MangleClassName(propName);
1273
1274    if(prop._class.type == structClass)
1275       DeclareStruct(prop._class.fullName, false);
1276
1277    if(!symbol || curExternal.symbol.idCode < symbol.id)
1278    {
1279       bool imported = false;
1280       bool dllImport = false;
1281       if(!symbol || symbol._import)
1282       {
1283          if(!symbol)
1284          {
1285             Symbol classSym;
1286             if(!prop._class.symbol)
1287                prop._class.symbol = FindClass(prop._class.fullName);
1288             classSym = prop._class.symbol;
1289             if(classSym && !classSym._import)
1290             {
1291                ModuleImport module;
1292
1293                if(prop._class.module)
1294                   module = FindModule(prop._class.module);
1295                else
1296                   module = mainModule;
1297
1298                classSym._import = ClassImport
1299                {
1300                   name = CopyString(prop._class.fullName);
1301                   isRemote = prop._class.isRemote;
1302                };
1303                module.classes.Add(classSym._import);
1304             }
1305             symbol = prop.symbol = Symbol { };
1306             symbol._import = (ClassImport)PropertyImport
1307             {
1308                name = CopyString(prop.name);
1309                isVirtual = false; //prop.isVirtual;
1310                hasSet = prop.Set ? true : false;
1311                hasGet = prop.Get ? true : false;
1312             };
1313             if(classSym)
1314                classSym._import.properties.Add(symbol._import);
1315          }
1316          imported = true;
1317          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1318          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1319             prop._class.module.importType != staticImport)
1320             dllImport = true;
1321       }
1322
1323       if(!symbol.type)
1324       {
1325          Context context = SetupTemplatesContext(prop._class);
1326          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1327          FinishTemplatesContext(context);
1328       }
1329
1330       // Get
1331       if(prop.Get)
1332       {
1333          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1334          {
1335             Declaration decl;
1336             OldList * specifiers, * declarators;
1337             Declarator d;
1338             OldList * params;
1339             Specifier spec;
1340             External external;
1341             Declarator typeDecl;
1342             bool simple = false;
1343
1344             specifiers = MkList();
1345             declarators = MkList();
1346             params = MkList();
1347
1348             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1349                MkDeclaratorIdentifier(MkIdentifier("this"))));
1350
1351             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1352             //if(imported)
1353             if(dllImport)
1354                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1355
1356             {
1357                Context context = SetupTemplatesContext(prop._class);
1358                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1359                FinishTemplatesContext(context);
1360             }
1361
1362             // Make sure the simple _class's type is declared
1363             for(spec = specifiers->first; spec; spec = spec.next)
1364             {
1365                if(spec.type == nameSpecifier /*SpecifierClass*/)
1366                {
1367                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1368                   {
1369                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1370                      symbol._class = classSym.registered;
1371                      if(classSym.registered && classSym.registered.type == structClass)
1372                      {
1373                         DeclareStruct(spec.name, false);
1374                         simple = true;
1375                      }
1376                   }
1377                }
1378             }
1379
1380             if(!simple)
1381                d = PlugDeclarator(typeDecl, d);
1382             else
1383             {
1384                ListAdd(params, MkTypeName(specifiers,
1385                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1386                specifiers = MkList();
1387             }
1388
1389             d = MkDeclaratorFunction(d, params);
1390
1391             //if(imported)
1392             if(dllImport)
1393                specifiers->Insert(null, MkSpecifier(EXTERN));
1394             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1395                specifiers->Insert(null, MkSpecifier(STATIC));
1396             if(simple)
1397                ListAdd(specifiers, MkSpecifier(VOID));
1398
1399             ListAdd(declarators, MkInitDeclarator(d, null));
1400
1401             decl = MkDeclaration(specifiers, declarators);
1402
1403             external = MkExternalDeclaration(decl);
1404             ast->Insert(curExternal.prev, external);
1405             external.symbol = symbol;
1406             symbol.externalGet = external;
1407
1408             ReplaceThisClassSpecifiers(specifiers, prop._class);
1409
1410             if(typeDecl)
1411                FreeDeclarator(typeDecl);
1412          }
1413          else
1414          {
1415             // Move declaration higher...
1416             ast->Move(symbol.externalGet, curExternal.prev);
1417          }
1418       }
1419
1420       // Set
1421       if(prop.Set)
1422       {
1423          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1424          {
1425             Declaration decl;
1426             OldList * specifiers, * declarators;
1427             Declarator d;
1428             OldList * params;
1429             Specifier spec;
1430             External external;
1431             Declarator typeDecl;
1432
1433             declarators = MkList();
1434             params = MkList();
1435
1436             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1437             if(!prop.conversion || prop._class.type == structClass)
1438             {
1439                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1440                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1441             }
1442
1443             specifiers = MkList();
1444
1445             {
1446                Context context = SetupTemplatesContext(prop._class);
1447                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1448                   MkDeclaratorIdentifier(MkIdentifier("value")));
1449                FinishTemplatesContext(context);
1450             }
1451             ListAdd(params, MkTypeName(specifiers, d));
1452
1453             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1454             //if(imported)
1455             if(dllImport)
1456                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1457             d = MkDeclaratorFunction(d, params);
1458
1459             // Make sure the simple _class's type is declared
1460             for(spec = specifiers->first; spec; spec = spec.next)
1461             {
1462                if(spec.type == nameSpecifier /*SpecifierClass*/)
1463                {
1464                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1465                   {
1466                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1467                      symbol._class = classSym.registered;
1468                      if(classSym.registered && classSym.registered.type == structClass)
1469                         DeclareStruct(spec.name, false);
1470                   }
1471                }
1472             }
1473
1474             ListAdd(declarators, MkInitDeclarator(d, null));
1475
1476             specifiers = MkList();
1477             //if(imported)
1478             if(dllImport)
1479                specifiers->Insert(null, MkSpecifier(EXTERN));
1480             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1481                specifiers->Insert(null, MkSpecifier(STATIC));
1482
1483             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1484             if(!prop.conversion || prop._class.type == structClass)
1485                ListAdd(specifiers, MkSpecifier(VOID));
1486             else
1487                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1488
1489             decl = MkDeclaration(specifiers, declarators);
1490
1491             external = MkExternalDeclaration(decl);
1492             ast->Insert(curExternal.prev, external);
1493             external.symbol = symbol;
1494             symbol.externalSet = external;
1495
1496             ReplaceThisClassSpecifiers(specifiers, prop._class);
1497          }
1498          else
1499          {
1500             // Move declaration higher...
1501             ast->Move(symbol.externalSet, curExternal.prev);
1502          }
1503       }
1504
1505       // Property (for Watchers)
1506       if(!symbol.externalPtr)
1507       {
1508          Declaration decl;
1509          External external;
1510          OldList * specifiers = MkList();
1511
1512          if(imported)
1513             specifiers->Insert(null, MkSpecifier(EXTERN));
1514          else
1515             specifiers->Insert(null, MkSpecifier(STATIC));
1516
1517          ListAdd(specifiers, MkSpecifierName("Property"));
1518
1519          {
1520             OldList * list = MkList();
1521             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1522                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1523
1524             if(!imported)
1525             {
1526                strcpy(propName, "__ecerePropM_");
1527                FullClassNameCat(propName, prop._class.fullName, false);
1528                strcat(propName, "_");
1529                // strcat(propName, prop.name);
1530                FullClassNameCat(propName, prop.name, true);
1531
1532                MangleClassName(propName);
1533
1534                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1535                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1536             }
1537             decl = MkDeclaration(specifiers, list);
1538          }
1539
1540          external = MkExternalDeclaration(decl);
1541          ast->Insert(curExternal.prev, external);
1542          external.symbol = symbol;
1543          symbol.externalPtr = external;
1544       }
1545       else
1546       {
1547          // Move declaration higher...
1548          ast->Move(symbol.externalPtr, curExternal.prev);
1549       }
1550
1551       symbol.id = curExternal.symbol.idCode;
1552    }
1553 }
1554
1555 // ***************** EXPRESSION PROCESSING ***************************
1556 public Type Dereference(Type source)
1557 {
1558    Type type = null;
1559    if(source)
1560    {
1561       if(source.kind == pointerType || source.kind == arrayType)
1562       {
1563          type = source.type;
1564          source.type.refCount++;
1565       }
1566       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1567       {
1568          type = Type
1569          {
1570             kind = charType;
1571             refCount = 1;
1572          };
1573       }
1574       // Support dereferencing of no head classes for now...
1575       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1576       {
1577          type = source;
1578          source.refCount++;
1579       }
1580       else
1581          Compiler_Error($"cannot dereference type\n");
1582    }
1583    return type;
1584 }
1585
1586 static Type Reference(Type source)
1587 {
1588    Type type = null;
1589    if(source)
1590    {
1591       type = Type
1592       {
1593          kind = pointerType;
1594          type = source;
1595          refCount = 1;
1596       };
1597       source.refCount++;
1598    }
1599    return type;
1600 }
1601
1602 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1603 {
1604    Identifier ident = member.identifiers ? member.identifiers->first : null;
1605    bool found = false;
1606    DataMember dataMember = null;
1607    Method method = null;
1608    bool freeType = false;
1609
1610    yylloc = member.loc;
1611
1612    if(!ident)
1613    {
1614       if(curMember)
1615       {
1616          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1617          if(*curMember)
1618          {
1619             found = true;
1620             dataMember = *curMember;
1621          }
1622       }
1623    }
1624    else
1625    {
1626       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1627       DataMember _subMemberStack[256];
1628       int _subMemberStackPos = 0;
1629
1630       // FILL MEMBER STACK
1631       if(!thisMember)
1632          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1633       if(thisMember)
1634       {
1635          dataMember = thisMember;
1636          if(curMember && thisMember.memberAccess == publicAccess)
1637          {
1638             *curMember = thisMember;
1639             *curClass = thisMember._class;
1640             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1641             *subMemberStackPos = _subMemberStackPos;
1642          }
1643          found = true;
1644       }
1645       else
1646       {
1647          // Setting a method
1648          method = eClass_FindMethod(_class, ident.string, privateModule);
1649          if(method && method.type == virtualMethod)
1650             found = true;
1651          else
1652             method = null;
1653       }
1654    }
1655
1656    if(found)
1657    {
1658       Type type = null;
1659       if(dataMember)
1660       {
1661          if(!dataMember.dataType && dataMember.dataTypeString)
1662          {
1663             //Context context = SetupTemplatesContext(dataMember._class);
1664             Context context = SetupTemplatesContext(_class);
1665             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1666             FinishTemplatesContext(context);
1667          }
1668          type = dataMember.dataType;
1669       }
1670       else if(method)
1671       {
1672          // This is for destination type...
1673          if(!method.dataType)
1674             ProcessMethodType(method);
1675          //DeclareMethod(method);
1676          // method.dataType = ((Symbol)method.symbol)->type;
1677          type = method.dataType;
1678       }
1679
1680       if(ident && ident.next)
1681       {
1682          for(ident = ident.next; ident && type; ident = ident.next)
1683          {
1684             if(type.kind == classType)
1685             {
1686                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1687                if(!dataMember)
1688                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1689                if(dataMember)
1690                   type = dataMember.dataType;
1691             }
1692             else if(type.kind == structType || type.kind == unionType)
1693             {
1694                Type memberType;
1695                for(memberType = type.members.first; memberType; memberType = memberType.next)
1696                {
1697                   if(!strcmp(memberType.name, ident.string))
1698                   {
1699                      type = memberType;
1700                      break;
1701                   }
1702                }
1703             }
1704          }
1705       }
1706
1707       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1708       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1709       {
1710          int id = 0;
1711          ClassTemplateParameter curParam = null;
1712          Class sClass;
1713          for(sClass = _class; sClass; sClass = sClass.base)
1714          {
1715             id = 0;
1716             if(sClass.templateClass) sClass = sClass.templateClass;
1717             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1718             {
1719                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1720                {
1721                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1722                   {
1723                      if(sClass.templateClass) sClass = sClass.templateClass;
1724                      id += sClass.templateParams.count;
1725                   }
1726                   break;
1727                }
1728                id++;
1729             }
1730             if(curParam) break;
1731          }
1732
1733          if(curParam)
1734          {
1735             ClassTemplateArgument arg = _class.templateArgs[id];
1736             if(arg.dataTypeString)
1737             {
1738                // FreeType(type);
1739                type = ProcessTypeString(arg.dataTypeString, false);
1740                freeType = true;
1741                if(type && _class.templateClass)
1742                   type.passAsTemplate = true;
1743                if(type)
1744                {
1745                   // type.refCount++;
1746                   /*if(!exp.destType)
1747                   {
1748                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1749                      exp.destType.refCount++;
1750                   }*/
1751                }
1752             }
1753          }
1754       }
1755       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1756       {
1757          Class expClass = type._class.registered;
1758          Class cClass = null;
1759          int c;
1760          int paramCount = 0;
1761          int lastParam = -1;
1762
1763          char templateString[1024];
1764          ClassTemplateParameter param;
1765          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1766          for(cClass = expClass; cClass; cClass = cClass.base)
1767          {
1768             int p = 0;
1769             if(cClass.templateClass) cClass = cClass.templateClass;
1770             for(param = cClass.templateParams.first; param; param = param.next)
1771             {
1772                int id = p;
1773                Class sClass;
1774                ClassTemplateArgument arg;
1775                for(sClass = cClass.base; sClass; sClass = sClass.base)
1776                {
1777                   if(sClass.templateClass) sClass = sClass.templateClass;
1778                   id += sClass.templateParams.count;
1779                }
1780                arg = expClass.templateArgs[id];
1781
1782                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1783                {
1784                   ClassTemplateParameter cParam;
1785                   //int p = numParams - sClass.templateParams.count;
1786                   int p = 0;
1787                   Class nextClass;
1788                   if(sClass.templateClass) sClass = sClass.templateClass;
1789
1790                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1791                   {
1792                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1793                      p += nextClass.templateParams.count;
1794                   }
1795
1796                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1797                   {
1798                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1799                      {
1800                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1801                         {
1802                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1803                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1804                            break;
1805                         }
1806                      }
1807                   }
1808                }
1809
1810                {
1811                   char argument[256];
1812                   argument[0] = '\0';
1813                   /*if(arg.name)
1814                   {
1815                      strcat(argument, arg.name.string);
1816                      strcat(argument, " = ");
1817                   }*/
1818                   switch(param.type)
1819                   {
1820                      case expression:
1821                      {
1822                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1823                         char expString[1024];
1824                         OldList * specs = MkList();
1825                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1826                         Expression exp;
1827                         char * string = PrintHexUInt64(arg.expression.ui64);
1828                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1829                         delete string;
1830
1831                         ProcessExpressionType(exp);
1832                         ComputeExpression(exp);
1833                         expString[0] = '\0';
1834                         PrintExpression(exp, expString);
1835                         strcat(argument, expString);
1836                         //delete exp;
1837                         FreeExpression(exp);
1838                         break;
1839                      }
1840                      case identifier:
1841                      {
1842                         strcat(argument, arg.member.name);
1843                         break;
1844                      }
1845                      case TemplateParameterType::type:
1846                      {
1847                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1848                            strcat(argument, arg.dataTypeString);
1849                         break;
1850                      }
1851                   }
1852                   if(argument[0])
1853                   {
1854                      if(paramCount) strcat(templateString, ", ");
1855                      if(lastParam != p - 1)
1856                      {
1857                         strcat(templateString, param.name);
1858                         strcat(templateString, " = ");
1859                      }
1860                      strcat(templateString, argument);
1861                      paramCount++;
1862                      lastParam = p;
1863                   }
1864                   p++;
1865                }
1866             }
1867          }
1868          {
1869             int len = strlen(templateString);
1870             if(templateString[len-1] == '<')
1871                len--;
1872             else
1873             {
1874                if(templateString[len-1] == '>')
1875                   templateString[len++] = ' ';
1876                templateString[len++] = '>';
1877             }
1878             templateString[len++] = '\0';
1879          }
1880          {
1881             Context context = SetupTemplatesContext(_class);
1882             if(freeType) FreeType(type);
1883             type = ProcessTypeString(templateString, false);
1884             freeType = true;
1885             FinishTemplatesContext(context);
1886          }
1887       }
1888
1889       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1890       {
1891          ProcessExpressionType(member.initializer.exp);
1892          if(!member.initializer.exp.expType)
1893          {
1894             if(inCompiler)
1895             {
1896                char expString[10240];
1897                expString[0] = '\0';
1898                PrintExpression(member.initializer.exp, expString);
1899                ChangeCh(expString, '\n', ' ');
1900                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1901             }
1902          }
1903          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1904          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1905          {
1906             Compiler_Error($"incompatible instance method %s\n", ident.string);
1907          }
1908       }
1909       else if(member.initializer)
1910       {
1911          /*
1912          FreeType(member.exp.destType);
1913          member.exp.destType = type;
1914          if(member.exp.destType)
1915             member.exp.destType.refCount++;
1916          ProcessExpressionType(member.exp);
1917          */
1918
1919          ProcessInitializer(member.initializer, type);
1920       }
1921       if(freeType) FreeType(type);
1922    }
1923    else
1924    {
1925       if(_class && _class.type == unitClass)
1926       {
1927          if(member.initializer)
1928          {
1929             /*
1930             FreeType(member.exp.destType);
1931             member.exp.destType = MkClassType(_class.fullName);
1932             ProcessExpressionType(member.initializer, type);
1933             */
1934             Type type = MkClassType(_class.fullName);
1935             ProcessInitializer(member.initializer, type);
1936             FreeType(type);
1937          }
1938       }
1939       else
1940       {
1941          if(member.initializer)
1942          {
1943             //ProcessExpressionType(member.exp);
1944             ProcessInitializer(member.initializer, null);
1945          }
1946          if(ident)
1947          {
1948             if(method)
1949             {
1950                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1951             }
1952             else if(_class)
1953             {
1954                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1955                if(inCompiler)
1956                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1957             }
1958          }
1959          else if(_class)
1960             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1961       }
1962    }
1963 }
1964
1965 void ProcessInstantiationType(Instantiation inst)
1966 {
1967    yylloc = inst.loc;
1968    if(inst._class)
1969    {
1970       MembersInit members;
1971       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1972       Class _class;
1973
1974       /*if(!inst._class.symbol)
1975          inst._class.symbol = FindClass(inst._class.name);*/
1976       classSym = inst._class.symbol;
1977       _class = classSym ? classSym.registered : null;
1978
1979       // DANGER: Patch for mutex not declaring its struct when not needed
1980       if(!_class || _class.type != noHeadClass)
1981          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1982
1983       afterExternal = afterExternal ? afterExternal : curExternal;
1984
1985       if(inst.exp)
1986          ProcessExpressionType(inst.exp);
1987
1988       inst.isConstant = true;
1989       if(inst.members)
1990       {
1991          DataMember curMember = null;
1992          Class curClass = null;
1993          DataMember subMemberStack[256];
1994          int subMemberStackPos = 0;
1995
1996          for(members = inst.members->first; members; members = members.next)
1997          {
1998             switch(members.type)
1999             {
2000                case methodMembersInit:
2001                {
2002                   char name[1024];
2003                   static uint instMethodID = 0;
2004                   External external = curExternal;
2005                   Context context = curContext;
2006                   Declarator declarator = members.function.declarator;
2007                   Identifier nameID = GetDeclId(declarator);
2008                   char * unmangled = nameID ? nameID.string : null;
2009                   Expression exp;
2010                   External createdExternal = null;
2011
2012                   if(inCompiler)
2013                   {
2014                      char number[16];
2015                      //members.function.dontMangle = true;
2016                      strcpy(name, "__ecereInstMeth_");
2017                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2018                      strcat(name, "_");
2019                      strcat(name, nameID.string);
2020                      strcat(name, "_");
2021                      sprintf(number, "_%08d", instMethodID++);
2022                      strcat(name, number);
2023                      nameID.string = CopyString(name);
2024                   }
2025
2026                   // Do modifications here...
2027                   if(declarator)
2028                   {
2029                      Symbol symbol = declarator.symbol;
2030                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2031
2032                      if(method && method.type == virtualMethod)
2033                      {
2034                         symbol.method = method;
2035                         ProcessMethodType(method);
2036
2037                         if(!symbol.type.thisClass)
2038                         {
2039                            if(method.dataType.thisClass && currentClass &&
2040                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2041                            {
2042                               if(!currentClass.symbol)
2043                                  currentClass.symbol = FindClass(currentClass.fullName);
2044                               symbol.type.thisClass = currentClass.symbol;
2045                            }
2046                            else
2047                            {
2048                               if(!_class.symbol)
2049                                  _class.symbol = FindClass(_class.fullName);
2050                               symbol.type.thisClass = _class.symbol;
2051                            }
2052                         }
2053                         // TESTING THIS HERE:
2054                         DeclareType(symbol.type, true, true);
2055
2056                      }
2057                      else if(classSym)
2058                      {
2059                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2060                            unmangled, classSym.string);
2061                      }
2062                   }
2063
2064                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2065                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2066
2067                   if(nameID)
2068                   {
2069                      FreeSpecifier(nameID._class);
2070                      nameID._class = null;
2071                   }
2072
2073                   if(inCompiler)
2074                   {
2075
2076                      Type type = declarator.symbol.type;
2077                      External oldExternal = curExternal;
2078
2079                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2080                      // *** It was commented out for problems such as
2081                      /*
2082                            class VirtualDesktop : Window
2083                            {
2084                               clientSize = Size { };
2085                               Timer timer
2086                               {
2087                                  bool DelayExpired()
2088                                  {
2089                                     clientSize.w;
2090                                     return true;
2091                                  }
2092                               };
2093                            }
2094                      */
2095                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2096
2097                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2098
2099                      /*
2100                      if(strcmp(declarator.symbol.string, name))
2101                      {
2102                         printf("TOCHECK: Look out for this\n");
2103                         delete declarator.symbol.string;
2104                         declarator.symbol.string = CopyString(name);
2105                      }
2106
2107                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2108                      {
2109                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2110                         excludedSymbols->Remove(declarator.symbol);
2111                         globalContext.symbols.Add((BTNode)declarator.symbol);
2112                         if(strstr(declarator.symbol.string), "::")
2113                            globalContext.hasNameSpace = true;
2114
2115                      }
2116                      */
2117
2118                      //curExternal = curExternal.prev;
2119                      //afterExternal = afterExternal->next;
2120
2121                      //ProcessFunction(afterExternal->function);
2122
2123                      //curExternal = afterExternal;
2124                      {
2125                         External externalDecl;
2126                         externalDecl = MkExternalDeclaration(null);
2127                         ast->Insert(oldExternal.prev, externalDecl);
2128
2129                         // Which function does this process?
2130                         if(createdExternal.function)
2131                         {
2132                            ProcessFunction(createdExternal.function);
2133
2134                            //curExternal = oldExternal;
2135
2136                            {
2137                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2138
2139                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2140                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2141
2142                               //externalDecl = MkExternalDeclaration(decl);
2143
2144                               //***** ast->Insert(external.prev, externalDecl);
2145                               //ast->Insert(curExternal.prev, externalDecl);
2146                               externalDecl.declaration = decl;
2147                               if(decl.symbol && !decl.symbol.pointerExternal)
2148                                  decl.symbol.pointerExternal = externalDecl;
2149
2150                               // Trying this out...
2151                               declarator.symbol.pointerExternal = externalDecl;
2152                            }
2153                         }
2154                      }
2155                   }
2156                   else if(declarator)
2157                   {
2158                      curExternal = declarator.symbol.pointerExternal;
2159                      ProcessFunction((FunctionDefinition)members.function);
2160                   }
2161                   curExternal = external;
2162                   curContext = context;
2163
2164                   if(inCompiler)
2165                   {
2166                      FreeClassFunction(members.function);
2167
2168                      // In this pass, turn this into a MemberInitData
2169                      exp = QMkExpId(name);
2170                      members.type = dataMembersInit;
2171                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2172
2173                      delete unmangled;
2174                   }
2175                   break;
2176                }
2177                case dataMembersInit:
2178                {
2179                   if(members.dataMembers && classSym)
2180                   {
2181                      MemberInit member;
2182                      Location oldyyloc = yylloc;
2183                      for(member = members.dataMembers->first; member; member = member.next)
2184                      {
2185                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2186                         if(member.initializer && !member.initializer.isConstant)
2187                            inst.isConstant = false;
2188                      }
2189                      yylloc = oldyyloc;
2190                   }
2191                   break;
2192                }
2193             }
2194          }
2195       }
2196    }
2197 }
2198
2199 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2200 {
2201    // OPTIMIZATIONS: TESTING THIS...
2202    if(inCompiler)
2203    {
2204       if(type.kind == functionType)
2205       {
2206          Type param;
2207          if(declareParams)
2208          {
2209             for(param = type.params.first; param; param = param.next)
2210                DeclareType(param, declarePointers, true);
2211          }
2212          DeclareType(type.returnType, declarePointers, true);
2213       }
2214       else if(type.kind == pointerType && declarePointers)
2215          DeclareType(type.type, declarePointers, false);
2216       else if(type.kind == classType)
2217       {
2218          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2219             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2220       }
2221       else if(type.kind == structType || type.kind == unionType)
2222       {
2223          Type member;
2224          for(member = type.members.first; member; member = member.next)
2225             DeclareType(member, false, false);
2226       }
2227       else if(type.kind == arrayType)
2228          DeclareType(type.arrayType, declarePointers, false);
2229    }
2230 }
2231
2232 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2233 {
2234    ClassTemplateArgument * arg = null;
2235    int id = 0;
2236    ClassTemplateParameter curParam = null;
2237    Class sClass;
2238    for(sClass = _class; sClass; sClass = sClass.base)
2239    {
2240       id = 0;
2241       if(sClass.templateClass) sClass = sClass.templateClass;
2242       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2243       {
2244          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2245          {
2246             for(sClass = sClass.base; sClass; sClass = sClass.base)
2247             {
2248                if(sClass.templateClass) sClass = sClass.templateClass;
2249                id += sClass.templateParams.count;
2250             }
2251             break;
2252          }
2253          id++;
2254       }
2255       if(curParam) break;
2256    }
2257    if(curParam)
2258    {
2259       arg = &_class.templateArgs[id];
2260       if(arg && param.type == type)
2261          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2262    }
2263    return arg;
2264 }
2265
2266 public Context SetupTemplatesContext(Class _class)
2267 {
2268    Context context = PushContext();
2269    context.templateTypesOnly = true;
2270    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2271    {
2272       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2273       for(; param; param = param.next)
2274       {
2275          if(param.type == type && param.identifier)
2276          {
2277             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2278             curContext.templateTypes.Add((BTNode)type);
2279          }
2280       }
2281    }
2282    else if(_class)
2283    {
2284       Class sClass;
2285       for(sClass = _class; sClass; sClass = sClass.base)
2286       {
2287          ClassTemplateParameter p;
2288          for(p = sClass.templateParams.first; p; p = p.next)
2289          {
2290             //OldList * specs = MkList();
2291             //Declarator decl = null;
2292             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2293             if(p.type == type)
2294             {
2295                TemplateParameter param = p.param;
2296                TemplatedType type;
2297                if(!param)
2298                {
2299                   // ADD DATA TYPE HERE...
2300                   p.param = param = TemplateParameter
2301                   {
2302                      identifier = MkIdentifier(p.name), type = p.type,
2303                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2304                   };
2305                }
2306                type = TemplatedType { key = (uintptr)p.name, param = param };
2307                curContext.templateTypes.Add((BTNode)type);
2308             }
2309          }
2310       }
2311    }
2312    return context;
2313 }
2314
2315 public void FinishTemplatesContext(Context context)
2316 {
2317    PopContext(context);
2318    FreeContext(context);
2319    delete context;
2320 }
2321
2322 public void ProcessMethodType(Method method)
2323 {
2324    if(!method.dataType)
2325    {
2326       Context context = SetupTemplatesContext(method._class);
2327
2328       method.dataType = ProcessTypeString(method.dataTypeString, false);
2329
2330       FinishTemplatesContext(context);
2331
2332       if(method.type != virtualMethod && method.dataType)
2333       {
2334          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2335          {
2336             if(!method._class.symbol)
2337                method._class.symbol = FindClass(method._class.fullName);
2338             method.dataType.thisClass = method._class.symbol;
2339          }
2340       }
2341
2342       // Why was this commented out? Working fine without now...
2343
2344       /*
2345       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2346          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2347          */
2348    }
2349
2350    /*
2351    if(type)
2352    {
2353       char * par = strstr(type, "(");
2354       char * classOp = null;
2355       int classOpLen = 0;
2356       if(par)
2357       {
2358          int c;
2359          for(c = par-type-1; c >= 0; c++)
2360          {
2361             if(type[c] == ':' && type[c+1] == ':')
2362             {
2363                classOp = type + c - 1;
2364                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2365                {
2366                   classOp--;
2367                   classOpLen++;
2368                }
2369                break;
2370             }
2371             else if(!isspace(type[c]))
2372                break;
2373          }
2374       }
2375       if(classOp)
2376       {
2377          char temp[1024];
2378          int typeLen = strlen(type);
2379          memcpy(temp, classOp, classOpLen);
2380          temp[classOpLen] = '\0';
2381          if(temp[0])
2382             _class = eSystem_FindClass(module, temp);
2383          else
2384             _class = null;
2385          method.dataTypeString = new char[typeLen - classOpLen + 1];
2386          memcpy(method.dataTypeString, type, classOp - type);
2387          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2388       }
2389       else
2390          method.dataTypeString = type;
2391    }
2392    */
2393 }
2394
2395
2396 public void ProcessPropertyType(Property prop)
2397 {
2398    if(!prop.dataType)
2399    {
2400       Context context = SetupTemplatesContext(prop._class);
2401       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2402       FinishTemplatesContext(context);
2403    }
2404 }
2405
2406 public void DeclareMethod(Method method, char * name)
2407 {
2408    Symbol symbol = method.symbol;
2409    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2410    {
2411       bool imported = false;
2412       bool dllImport = false;
2413
2414       if(!method.dataType)
2415          method.dataType = ProcessTypeString(method.dataTypeString, false);
2416
2417       if(!symbol || symbol._import || method.type == virtualMethod)
2418       {
2419          if(!symbol || method.type == virtualMethod)
2420          {
2421             Symbol classSym;
2422             if(!method._class.symbol)
2423                method._class.symbol = FindClass(method._class.fullName);
2424             classSym = method._class.symbol;
2425             if(!classSym._import)
2426             {
2427                ModuleImport module;
2428
2429                if(method._class.module && method._class.module.name)
2430                   module = FindModule(method._class.module);
2431                else
2432                   module = mainModule;
2433                classSym._import = ClassImport
2434                {
2435                   name = CopyString(method._class.fullName);
2436                   isRemote = method._class.isRemote;
2437                };
2438                module.classes.Add(classSym._import);
2439             }
2440             if(!symbol)
2441             {
2442                symbol = method.symbol = Symbol { };
2443             }
2444             if(!symbol._import)
2445             {
2446                symbol._import = (ClassImport)MethodImport
2447                {
2448                   name = CopyString(method.name);
2449                   isVirtual = method.type == virtualMethod;
2450                };
2451                classSym._import.methods.Add(symbol._import);
2452             }
2453             if(!symbol)
2454             {
2455                // Set the symbol type
2456                /*
2457                if(!type.thisClass)
2458                {
2459                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2460                }
2461                else if(type.thisClass == (void *)-1)
2462                {
2463                   type.thisClass = null;
2464                }
2465                */
2466                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2467                symbol.type = method.dataType;
2468                if(symbol.type) symbol.type.refCount++;
2469             }
2470             /*
2471             if(!method.thisClass || strcmp(method.thisClass, "void"))
2472                symbol.type.params.Insert(null,
2473                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2474             */
2475          }
2476          if(!method.dataType.dllExport)
2477          {
2478             imported = true;
2479             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2480                dllImport = true;
2481          }
2482       }
2483
2484       /* MOVING THIS UP
2485       if(!method.dataType)
2486          method.dataType = ((Symbol)method.symbol).type;
2487          //ProcessMethodType(method);
2488       */
2489
2490       if(method.type != virtualMethod && method.dataType)
2491          DeclareType(method.dataType, true, true);
2492
2493       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2494       {
2495          // We need a declaration here :)
2496          Declaration decl;
2497          OldList * specifiers, * declarators;
2498          Declarator d;
2499          Declarator funcDecl;
2500          External external;
2501
2502          specifiers = MkList();
2503          declarators = MkList();
2504
2505          //if(imported)
2506          if(dllImport)
2507             ListAdd(specifiers, MkSpecifier(EXTERN));
2508          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2509             ListAdd(specifiers, MkSpecifier(STATIC));
2510
2511          if(method.type == virtualMethod)
2512          {
2513             ListAdd(specifiers, MkSpecifier(INT));
2514             d = MkDeclaratorIdentifier(MkIdentifier(name));
2515          }
2516          else
2517          {
2518             d = MkDeclaratorIdentifier(MkIdentifier(name));
2519             //if(imported)
2520             if(dllImport)
2521                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2522             {
2523                Context context = SetupTemplatesContext(method._class);
2524                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2525                FinishTemplatesContext(context);
2526             }
2527             funcDecl = GetFuncDecl(d);
2528
2529             if(dllImport)
2530             {
2531                Specifier spec, next;
2532                for(spec = specifiers->first; spec; spec = next)
2533                {
2534                   next = spec.next;
2535                   if(spec.type == extendedSpecifier)
2536                   {
2537                      specifiers->Remove(spec);
2538                      FreeSpecifier(spec);
2539                   }
2540                }
2541             }
2542
2543             // Add this parameter if not a static method
2544             if(method.dataType && !method.dataType.staticMethod)
2545             {
2546                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2547                {
2548                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2549                   TypeName thisParam = MkTypeName(MkListOne(
2550                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2551                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2552                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2553                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2554
2555                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2556                   {
2557                      TypeName param = funcDecl.function.parameters->first;
2558                      funcDecl.function.parameters->Remove(param);
2559                      FreeTypeName(param);
2560                   }
2561
2562                   if(!funcDecl.function.parameters)
2563                      funcDecl.function.parameters = MkList();
2564                   funcDecl.function.parameters->Insert(null, thisParam);
2565                }
2566             }
2567             // Make sure we don't have empty parameter declarations for static methods...
2568             /*
2569             else if(!funcDecl.function.parameters)
2570             {
2571                funcDecl.function.parameters = MkList();
2572                funcDecl.function.parameters->Insert(null,
2573                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2574             }*/
2575          }
2576          // TESTING THIS:
2577          ProcessDeclarator(d);
2578
2579          ListAdd(declarators, MkInitDeclarator(d, null));
2580
2581          decl = MkDeclaration(specifiers, declarators);
2582
2583          ReplaceThisClassSpecifiers(specifiers, method._class);
2584
2585          // Keep a different symbol for the function definition than the declaration...
2586          if(symbol.pointerExternal)
2587          {
2588             Symbol functionSymbol { };
2589
2590             // Copy symbol
2591             {
2592                *functionSymbol = *symbol;
2593                functionSymbol.string = CopyString(symbol.string);
2594                if(functionSymbol.type)
2595                   functionSymbol.type.refCount++;
2596             }
2597
2598             excludedSymbols->Add(functionSymbol);
2599             symbol.pointerExternal.symbol = functionSymbol;
2600          }
2601          external = MkExternalDeclaration(decl);
2602          if(curExternal)
2603             ast->Insert(curExternal ? curExternal.prev : null, external);
2604          external.symbol = symbol;
2605          symbol.pointerExternal = external;
2606       }
2607       else if(ast)
2608       {
2609          // Move declaration higher...
2610          ast->Move(symbol.pointerExternal, curExternal.prev);
2611       }
2612
2613       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2614    }
2615 }
2616
2617 char * ReplaceThisClass(Class _class)
2618 {
2619    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2620    {
2621       bool first = true;
2622       int p = 0;
2623       ClassTemplateParameter param;
2624       int lastParam = -1;
2625
2626       char className[1024];
2627       strcpy(className, _class.fullName);
2628       for(param = _class.templateParams.first; param; param = param.next)
2629       {
2630          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2631          {
2632             if(first) strcat(className, "<");
2633             if(!first) strcat(className, ", ");
2634             if(lastParam + 1 != p)
2635             {
2636                strcat(className, param.name);
2637                strcat(className, " = ");
2638             }
2639             strcat(className, param.name);
2640             first = false;
2641             lastParam = p;
2642          }
2643          p++;
2644       }
2645       if(!first)
2646       {
2647          int len = strlen(className);
2648          if(className[len-1] == '>') className[len++] = ' ';
2649          className[len++] = '>';
2650          className[len++] = '\0';
2651       }
2652       return CopyString(className);
2653    }
2654    else
2655       return CopyString(_class.fullName);
2656 }
2657
2658 Type ReplaceThisClassType(Class _class)
2659 {
2660    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2661    {
2662       bool first = true;
2663       int p = 0;
2664       ClassTemplateParameter param;
2665       int lastParam = -1;
2666       char className[1024];
2667       strcpy(className, _class.fullName);
2668
2669       for(param = _class.templateParams.first; param; param = param.next)
2670       {
2671          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2672          {
2673             if(first) strcat(className, "<");
2674             if(!first) strcat(className, ", ");
2675             if(lastParam + 1 != p)
2676             {
2677                strcat(className, param.name);
2678                strcat(className, " = ");
2679             }
2680             strcat(className, param.name);
2681             first = false;
2682             lastParam = p;
2683          }
2684          p++;
2685       }
2686       if(!first)
2687       {
2688          int len = strlen(className);
2689          if(className[len-1] == '>') className[len++] = ' ';
2690          className[len++] = '>';
2691          className[len++] = '\0';
2692       }
2693       return MkClassType(className);
2694       //return ProcessTypeString(className, false);
2695    }
2696    else
2697    {
2698       return MkClassType(_class.fullName);
2699       //return ProcessTypeString(_class.fullName, false);
2700    }
2701 }
2702
2703 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2704 {
2705    if(specs != null && _class)
2706    {
2707       Specifier spec;
2708       for(spec = specs.first; spec; spec = spec.next)
2709       {
2710          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2711          {
2712             spec.type = nameSpecifier;
2713             spec.name = ReplaceThisClass(_class);
2714             spec.symbol = FindClass(spec.name); //_class.symbol;
2715          }
2716       }
2717    }
2718 }
2719
2720 // Returns imported or not
2721 bool DeclareFunction(GlobalFunction function, char * name)
2722 {
2723    Symbol symbol = function.symbol;
2724    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2725    {
2726       bool imported = false;
2727       bool dllImport = false;
2728
2729       if(!function.dataType)
2730       {
2731          function.dataType = ProcessTypeString(function.dataTypeString, false);
2732          if(!function.dataType.thisClass)
2733             function.dataType.staticMethod = true;
2734       }
2735
2736       if(inCompiler)
2737       {
2738          if(!symbol)
2739          {
2740             ModuleImport module = FindModule(function.module);
2741             // WARNING: This is not added anywhere...
2742             symbol = function.symbol = Symbol {  };
2743
2744             if(module.name)
2745             {
2746                if(!function.dataType.dllExport)
2747                {
2748                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2749                   module.functions.Add(symbol._import);
2750                }
2751             }
2752             // Set the symbol type
2753             {
2754                symbol.type = ProcessTypeString(function.dataTypeString, false);
2755                if(!symbol.type.thisClass)
2756                   symbol.type.staticMethod = true;
2757             }
2758          }
2759          imported = symbol._import ? true : false;
2760          if(imported && function.module != privateModule && function.module.importType != staticImport)
2761             dllImport = true;
2762       }
2763
2764       DeclareType(function.dataType, true, true);
2765
2766       if(inCompiler)
2767       {
2768          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2769          {
2770             // We need a declaration here :)
2771             Declaration decl;
2772             OldList * specifiers, * declarators;
2773             Declarator d;
2774             Declarator funcDecl;
2775             External external;
2776
2777             specifiers = MkList();
2778             declarators = MkList();
2779
2780             //if(imported)
2781                ListAdd(specifiers, MkSpecifier(EXTERN));
2782             /*
2783             else
2784                ListAdd(specifiers, MkSpecifier(STATIC));
2785             */
2786
2787             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2788             //if(imported)
2789             if(dllImport)
2790                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2791
2792             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2793             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2794             if(function.module.importType == staticImport)
2795             {
2796                Specifier spec;
2797                for(spec = specifiers->first; spec; spec = spec.next)
2798                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2799                   {
2800                      specifiers->Remove(spec);
2801                      FreeSpecifier(spec);
2802                      break;
2803                   }
2804             }
2805
2806             funcDecl = GetFuncDecl(d);
2807
2808             // Make sure we don't have empty parameter declarations for static methods...
2809             if(funcDecl && !funcDecl.function.parameters)
2810             {
2811                funcDecl.function.parameters = MkList();
2812                funcDecl.function.parameters->Insert(null,
2813                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2814             }
2815
2816             ListAdd(declarators, MkInitDeclarator(d, null));
2817
2818             {
2819                Context oldCtx = curContext;
2820                curContext = globalContext;
2821                decl = MkDeclaration(specifiers, declarators);
2822                curContext = oldCtx;
2823             }
2824
2825             // Keep a different symbol for the function definition than the declaration...
2826             if(symbol.pointerExternal)
2827             {
2828                Symbol functionSymbol { };
2829                // Copy symbol
2830                {
2831                   *functionSymbol = *symbol;
2832                   functionSymbol.string = CopyString(symbol.string);
2833                   if(functionSymbol.type)
2834                      functionSymbol.type.refCount++;
2835                }
2836
2837                excludedSymbols->Add(functionSymbol);
2838
2839                symbol.pointerExternal.symbol = functionSymbol;
2840             }
2841             external = MkExternalDeclaration(decl);
2842             if(curExternal)
2843                ast->Insert(curExternal.prev, external);
2844             external.symbol = symbol;
2845             symbol.pointerExternal = external;
2846          }
2847          else
2848          {
2849             // Move declaration higher...
2850             ast->Move(symbol.pointerExternal, curExternal.prev);
2851          }
2852
2853          if(curExternal)
2854             symbol.id = curExternal.symbol.idCode;
2855       }
2856    }
2857    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2858 }
2859
2860 void DeclareGlobalData(GlobalData data)
2861 {
2862    Symbol symbol = data.symbol;
2863    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2864    {
2865       if(inCompiler)
2866       {
2867          if(!symbol)
2868             symbol = data.symbol = Symbol { };
2869       }
2870       if(!data.dataType)
2871          data.dataType = ProcessTypeString(data.dataTypeString, false);
2872       DeclareType(data.dataType, true, true);
2873       if(inCompiler)
2874       {
2875          if(!symbol.pointerExternal)
2876          {
2877             // We need a declaration here :)
2878             Declaration decl;
2879             OldList * specifiers, * declarators;
2880             Declarator d;
2881             External external;
2882
2883             specifiers = MkList();
2884             declarators = MkList();
2885
2886             ListAdd(specifiers, MkSpecifier(EXTERN));
2887             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2888             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2889
2890             ListAdd(declarators, MkInitDeclarator(d, null));
2891
2892             decl = MkDeclaration(specifiers, declarators);
2893             external = MkExternalDeclaration(decl);
2894             if(curExternal)
2895                ast->Insert(curExternal.prev, external);
2896             external.symbol = symbol;
2897             symbol.pointerExternal = external;
2898          }
2899          else
2900          {
2901             // Move declaration higher...
2902             ast->Move(symbol.pointerExternal, curExternal.prev);
2903          }
2904
2905          if(curExternal)
2906             symbol.id = curExternal.symbol.idCode;
2907       }
2908    }
2909 }
2910
2911 class Conversion : struct
2912 {
2913    Conversion prev, next;
2914    Property convert;
2915    bool isGet;
2916    Type resultType;
2917 };
2918
2919 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2920 {
2921    if(source && dest)
2922    {
2923       // Property convert;
2924
2925       if(source.kind == templateType && dest.kind != templateType)
2926       {
2927          Type type = ProcessTemplateParameterType(source.templateParameter);
2928          if(type) source = type;
2929       }
2930
2931       if(dest.kind == templateType && source.kind != templateType)
2932       {
2933          Type type = ProcessTemplateParameterType(dest.templateParameter);
2934          if(type) dest = type;
2935       }
2936
2937       if(dest.classObjectType == typedObject)
2938       {
2939          if(source.classObjectType != anyObject)
2940             return true;
2941          else
2942          {
2943             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2944             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2945             {
2946                return true;
2947             }
2948          }
2949       }
2950       else
2951       {
2952          if(source.classObjectType == anyObject)
2953             return true;
2954          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2955             return true;
2956       }
2957
2958       if((dest.kind == structType && source.kind == structType) ||
2959          (dest.kind == unionType && source.kind == unionType))
2960       {
2961          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2962              (source.members.first && source.members.first == dest.members.first))
2963             return true;
2964       }
2965
2966       if(dest.kind == ellipsisType && source.kind != voidType)
2967          return true;
2968
2969       if(dest.kind == pointerType && dest.type.kind == voidType &&
2970          ((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))
2971          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2972
2973          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2974
2975          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2976          return true;
2977       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2978          ((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))
2979          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2980
2981          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2982
2983          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2984          return true;
2985
2986       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2987       {
2988          if(source._class.registered && source._class.registered.type == unitClass)
2989          {
2990             if(conversions != null)
2991             {
2992                if(source._class.registered == dest._class.registered)
2993                   return true;
2994             }
2995             else
2996             {
2997                Class sourceBase, destBase;
2998                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2999                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
3000                if(sourceBase == destBase)
3001                   return true;
3002             }
3003          }
3004          // Don't match enum inheriting from other enum if resolving enumeration values
3005          // TESTING: !dest.classObjectType
3006          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
3007             (enumBaseType ||
3008                (!source._class.registered || source._class.registered.type != enumClass) ||
3009                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
3010             return true;
3011          else
3012          {
3013             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
3014             if(enumBaseType &&
3015                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
3016                ((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)
3017             {
3018                if(eClass_IsDerived(dest._class.registered, source._class.registered))
3019                {
3020                   return true;
3021                }
3022             }
3023          }
3024       }
3025
3026       // JUST ADDED THIS...
3027       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3028          return true;
3029
3030       if(doConversion)
3031       {
3032          // Just added this for Straight conversion of ColorAlpha => Color
3033          if(source.kind == classType)
3034          {
3035             Class _class;
3036             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3037             {
3038                Property convert;
3039                for(convert = _class.conversions.first; convert; convert = convert.next)
3040                {
3041                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3042                   {
3043                      Conversion after = (conversions != null) ? conversions.last : null;
3044
3045                      if(!convert.dataType)
3046                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3047                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3048                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3049                         MatchTypes(convert.dataType, dest, conversions, null, null,
3050                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3051                               convert.dataType.kind == classType, false, true))
3052                      {
3053                         if(!conversions && !convert.Get)
3054                            return true;
3055                         else if(conversions != null)
3056                         {
3057                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3058                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3059                               (dest.kind != classType || dest._class.registered != _class.base))
3060                               return true;
3061                            else
3062                            {
3063                               Conversion conv { convert = convert, isGet = true };
3064                               // conversions.Add(conv);
3065                               conversions.Insert(after, conv);
3066                               return true;
3067                            }
3068                         }
3069                      }
3070                   }
3071                }
3072             }
3073          }
3074
3075          // MOVING THIS??
3076
3077          if(dest.kind == classType)
3078          {
3079             Class _class;
3080             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3081             {
3082                Property convert;
3083                for(convert = _class.conversions.first; convert; convert = convert.next)
3084                {
3085                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3086                   {
3087                      // Conversion after = (conversions != null) ? conversions.last : null;
3088
3089                      if(!convert.dataType)
3090                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3091                      // Just added this equality check to prevent recursion.... Make it safer?
3092                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3093                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3094                      {
3095                         if(!conversions && !convert.Set)
3096                            return true;
3097                         else if(conversions != null)
3098                         {
3099                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3100                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3101                               (source.kind != classType || source._class.registered != _class.base))
3102                               return true;
3103                            else
3104                            {
3105                               // *** Testing this! ***
3106                               Conversion conv { convert = convert };
3107                               conversions.Add(conv);
3108                               //conversions.Insert(after, conv);
3109                               return true;
3110                            }
3111                         }
3112                      }
3113                   }
3114                }
3115             }
3116             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3117             {
3118                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3119                   (source.kind != classType || source._class.registered.type != structClass))
3120                   return true;
3121             }*/
3122
3123             // TESTING THIS... IS THIS OK??
3124             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3125             {
3126                if(!dest._class.registered.dataType)
3127                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3128                // Only support this for classes...
3129                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3130                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3131                {
3132                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false))
3133                   {
3134                      return true;
3135                   }
3136                }
3137             }
3138          }
3139
3140          // Moved this lower
3141          if(source.kind == classType)
3142          {
3143             Class _class;
3144             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3145             {
3146                Property convert;
3147                for(convert = _class.conversions.first; convert; convert = convert.next)
3148                {
3149                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3150                   {
3151                      Conversion after = (conversions != null) ? conversions.last : null;
3152
3153                      if(!convert.dataType)
3154                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3155                      if(convert.dataType != source &&
3156                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3157                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true))
3158                      {
3159                         if(!conversions && !convert.Get)
3160                            return true;
3161                         else if(conversions != null)
3162                         {
3163                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3164                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3165                               (dest.kind != classType || dest._class.registered != _class.base))
3166                               return true;
3167                            else
3168                            {
3169                               Conversion conv { convert = convert, isGet = true };
3170
3171                               // conversions.Add(conv);
3172                               conversions.Insert(after, conv);
3173                               return true;
3174                            }
3175                         }
3176                      }
3177                   }
3178                }
3179             }
3180
3181             // TESTING THIS... IS THIS OK??
3182             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3183             {
3184                if(!source._class.registered.dataType)
3185                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3186                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3187                {
3188                   if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, source._class.registered.dataType.kind == classType, source._class.registered.dataType.kind == classType, false, false))
3189                      return true;
3190                   // For bool to be accepted by byte, short, etc.
3191                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false))
3192                      return true;
3193                }
3194             }
3195          }
3196       }
3197
3198       if(source.kind == classType || source.kind == subClassType)
3199          ;
3200       else if(dest.kind == source.kind &&
3201          (dest.kind != structType && dest.kind != unionType &&
3202           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3203           return true;
3204       // RECENTLY ADDED THESE
3205       else if(dest.kind == doubleType && source.kind == floatType)
3206          return true;
3207       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3208          return true;
3209       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3210          return true;
3211       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3212          return true;
3213       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3214          return true;
3215       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3216          return true;
3217       else if(source.kind == enumType &&
3218          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3219           return true;
3220       else if(dest.kind == enumType && !isConversionExploration &&
3221          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3222           return true;
3223       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3224               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3225       {
3226          Type paramSource, paramDest;
3227
3228          if(dest.kind == methodType)
3229             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3230          if(source.kind == methodType)
3231             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3232
3233          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3234          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3235          if(dest.kind == methodType)
3236             dest = dest.method.dataType;
3237          if(source.kind == methodType)
3238             source = source.method.dataType;
3239
3240          paramSource = source.params.first;
3241          if(paramSource && paramSource.kind == voidType) paramSource = null;
3242          paramDest = dest.params.first;
3243          if(paramDest && paramDest.kind == voidType) paramDest = null;
3244
3245
3246          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3247             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3248          {
3249             // Source thisClass must be derived from destination thisClass
3250             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3251                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3252             {
3253                if(paramDest && paramDest.kind == classType)
3254                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3255                else
3256                   Compiler_Error($"method class should not take an object\n");
3257                return false;
3258             }
3259             paramDest = paramDest.next;
3260          }
3261          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3262          {
3263             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3264             {
3265                if(dest.thisClass)
3266                {
3267                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3268                   {
3269                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3270                      return false;
3271                   }
3272                }
3273                else
3274                {
3275                   // THIS WAS BACKWARDS:
3276                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3277                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3278                   {
3279                      if(owningClassDest)
3280                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3281                      else
3282                         Compiler_Error($"overriding class expected to be derived from method class\n");
3283                      return false;
3284                   }
3285                }
3286                paramSource = paramSource.next;
3287             }
3288             else
3289             {
3290                if(dest.thisClass)
3291                {
3292                   // Source thisClass must be derived from destination thisClass
3293                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3294                   {
3295                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3296                      return false;
3297                   }
3298                }
3299                else
3300                {
3301                   // THIS WAS BACKWARDS TOO??
3302                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3303                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3304                   {
3305                      //if(owningClass)
3306                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3307                      //else
3308                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3309                      return false;
3310                   }
3311                }
3312             }
3313          }
3314
3315
3316          // Source return type must be derived from destination return type
3317          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3318          {
3319             Compiler_Warning($"incompatible return type for function\n");
3320             return false;
3321          }
3322
3323          // Check parameters
3324
3325          for(; paramDest; paramDest = paramDest.next)
3326          {
3327             if(!paramSource)
3328             {
3329                //Compiler_Warning($"not enough parameters\n");
3330                Compiler_Error($"not enough parameters\n");
3331                return false;
3332             }
3333             {
3334                Type paramDestType = paramDest;
3335                Type paramSourceType = paramSource;
3336                Type type = paramDestType;
3337
3338                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3339                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3340                   paramSource.kind != templateType)
3341                {
3342                   int id = 0;
3343                   ClassTemplateParameter curParam = null;
3344                   Class sClass;
3345                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3346                   {
3347                      id = 0;
3348                      if(sClass.templateClass) sClass = sClass.templateClass;
3349                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3350                      {
3351                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3352                         {
3353                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3354                            {
3355                               if(sClass.templateClass) sClass = sClass.templateClass;
3356                               id += sClass.templateParams.count;
3357                            }
3358                            break;
3359                         }
3360                         id++;
3361                      }
3362                      if(curParam) break;
3363                   }
3364
3365                   if(curParam)
3366                   {
3367                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3368                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3369                   }
3370                }
3371
3372                // paramDest must be derived from paramSource
3373                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3374                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3375                {
3376                   char type[1024];
3377                   type[0] = 0;
3378                   PrintType(paramDest, type, false, true);
3379                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3380
3381                   if(paramDestType != paramDest)
3382                      FreeType(paramDestType);
3383                   return false;
3384                }
3385                if(paramDestType != paramDest)
3386                   FreeType(paramDestType);
3387             }
3388
3389             paramSource = paramSource.next;
3390          }
3391          if(paramSource)
3392          {
3393             Compiler_Error($"too many parameters\n");
3394             return false;
3395          }
3396          return true;
3397       }
3398       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3399       {
3400          return true;
3401       }
3402       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3403          (source.kind == arrayType || source.kind == pointerType))
3404       {
3405          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3406             return true;
3407       }
3408    }
3409    return false;
3410 }
3411
3412 static void FreeConvert(Conversion convert)
3413 {
3414    if(convert.resultType)
3415       FreeType(convert.resultType);
3416 }
3417
3418 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3419                               char * string, OldList conversions)
3420 {
3421    BTNamedLink link;
3422
3423    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3424    {
3425       Class _class = link.data;
3426       if(_class.type == enumClass)
3427       {
3428          OldList converts { };
3429          Type type { };
3430          type.kind = classType;
3431
3432          if(!_class.symbol)
3433             _class.symbol = FindClass(_class.fullName);
3434          type._class = _class.symbol;
3435
3436          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3437          {
3438             NamedLink value;
3439             Class enumClass = eSystem_FindClass(privateModule, "enum");
3440             if(enumClass)
3441             {
3442                Class baseClass;
3443                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3444                {
3445                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3446                   for(value = e.values.first; value; value = value.next)
3447                   {
3448                      if(!strcmp(value.name, string))
3449                         break;
3450                   }
3451                   if(value)
3452                   {
3453                      FreeExpContents(sourceExp);
3454                      FreeType(sourceExp.expType);
3455
3456                      sourceExp.isConstant = true;
3457                      sourceExp.expType = MkClassType(baseClass.fullName);
3458                      //if(inCompiler)
3459                      {
3460                         char constant[256];
3461                         sourceExp.type = constantExp;
3462                         if(!strcmp(baseClass.dataTypeString, "int"))
3463                            sprintf(constant, "%d",(int)value.data);
3464                         else
3465                            sprintf(constant, "0x%X",(int)value.data);
3466                         sourceExp.constant = CopyString(constant);
3467                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3468                      }
3469
3470                      while(converts.first)
3471                      {
3472                         Conversion convert = converts.first;
3473                         converts.Remove(convert);
3474                         conversions.Add(convert);
3475                      }
3476                      delete type;
3477                      return true;
3478                   }
3479                }
3480             }
3481          }
3482          if(converts.first)
3483             converts.Free(FreeConvert);
3484          delete type;
3485       }
3486    }
3487    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3488       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3489          return true;
3490    return false;
3491 }
3492
3493 public bool ModuleVisibility(Module searchIn, Module searchFor)
3494 {
3495    SubModule subModule;
3496
3497    if(searchFor == searchIn)
3498       return true;
3499
3500    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3501    {
3502       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3503       {
3504          if(ModuleVisibility(subModule.module, searchFor))
3505             return true;
3506       }
3507    }
3508    return false;
3509 }
3510
3511 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3512 {
3513    Module module;
3514
3515    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3516       return true;
3517    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3518       return true;
3519    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3520       return true;
3521
3522    for(module = mainModule.application.allModules.first; module; module = module.next)
3523    {
3524       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3525          return true;
3526    }
3527    return false;
3528 }
3529
3530 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3531 {
3532    Type source;
3533    Type realDest = dest;
3534    Type backupSourceExpType = null;
3535    Expression computedExp = sourceExp;
3536    dest.refCount++;
3537
3538    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3539       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3540    {
3541       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3542       ComputeExpression(computedExp /*sourceExp*/);
3543    }
3544
3545    source = sourceExp.expType;
3546
3547    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3548    {
3549       if(computedExp != sourceExp)
3550       {
3551          FreeExpression(computedExp);
3552          computedExp = sourceExp;
3553       }
3554       FreeType(dest);
3555       return true;
3556    }
3557
3558    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3559    {
3560        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3561        {
3562           Class sourceBase, destBase;
3563           for(sourceBase = source._class.registered;
3564               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3565               sourceBase = sourceBase.base);
3566           for(destBase = dest._class.registered;
3567               destBase && destBase.base && destBase.base.type != systemClass;
3568               destBase = destBase.base);
3569           //if(source._class.registered == dest._class.registered)
3570           if(sourceBase == destBase)
3571           {
3572             if(computedExp != sourceExp)
3573             {
3574                FreeExpression(computedExp);
3575                computedExp = sourceExp;
3576             }
3577             FreeType(dest);
3578             return true;
3579          }
3580       }
3581    }
3582
3583    if(source)
3584    {
3585       OldList * specs;
3586       bool flag = false;
3587       int64 value = MAXINT;
3588
3589       source.refCount++;
3590
3591       if(computedExp.type == constantExp)
3592       {
3593          if(source.isSigned)
3594             value = strtoll(computedExp.constant, null, 0);
3595          else
3596             value = strtoull(computedExp.constant, null, 0);
3597       }
3598       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3599       {
3600          if(source.isSigned)
3601             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3602          else
3603             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3604       }
3605       if(computedExp != sourceExp)
3606       {
3607          FreeExpression(computedExp);
3608          computedExp = sourceExp;
3609       }
3610
3611       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3612          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3613       {
3614          FreeType(source);
3615          source = Type { kind = intType, isSigned = false, refCount = 1 };
3616       }
3617
3618       if(dest.kind == classType)
3619       {
3620          Class _class = dest._class ? dest._class.registered : null;
3621
3622          if(_class && _class.type == unitClass)
3623          {
3624             if(source.kind != classType)
3625             {
3626                Type tempType { };
3627                Type tempDest, tempSource;
3628
3629                for(; _class.base.type != systemClass; _class = _class.base);
3630                tempSource = dest;
3631                tempDest = tempType;
3632
3633                tempType.kind = classType;
3634                if(!_class.symbol)
3635                   _class.symbol = FindClass(_class.fullName);
3636
3637                tempType._class = _class.symbol;
3638                tempType.truth = dest.truth;
3639                if(tempType._class)
3640                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3641
3642                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3643                backupSourceExpType = sourceExp.expType;
3644                sourceExp.expType = dest; dest.refCount++;
3645                //sourceExp.expType = MkClassType(_class.fullName);
3646                flag = true;
3647
3648                delete tempType;
3649             }
3650          }
3651
3652
3653          // Why wasn't there something like this?
3654          if(_class && _class.type == bitClass && source.kind != classType)
3655          {
3656             if(!dest._class.registered.dataType)
3657                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3658             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3659             {
3660                FreeType(source);
3661                FreeType(sourceExp.expType);
3662                source = sourceExp.expType = MkClassType(dest._class.string);
3663                source.refCount++;
3664
3665                //source.kind = classType;
3666                //source._class = dest._class;
3667             }
3668          }
3669
3670          // Adding two enumerations
3671          /*
3672          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3673          {
3674             if(!source._class.registered.dataType)
3675                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3676             if(!dest._class.registered.dataType)
3677                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3678
3679             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3680             {
3681                FreeType(source);
3682                source = sourceExp.expType = MkClassType(dest._class.string);
3683                source.refCount++;
3684
3685                //source.kind = classType;
3686                //source._class = dest._class;
3687             }
3688          }*/
3689
3690          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3691          {
3692             OldList * specs = MkList();
3693             Declarator decl;
3694             char string[1024];
3695
3696             ReadString(string, sourceExp.string);
3697             decl = SpecDeclFromString(string, specs, null);
3698
3699             FreeExpContents(sourceExp);
3700             FreeType(sourceExp.expType);
3701
3702             sourceExp.type = classExp;
3703             sourceExp._classExp.specifiers = specs;
3704             sourceExp._classExp.decl = decl;
3705             sourceExp.expType = dest;
3706             dest.refCount++;
3707
3708             FreeType(source);
3709             FreeType(dest);
3710             if(backupSourceExpType) FreeType(backupSourceExpType);
3711             return true;
3712          }
3713       }
3714       else if(source.kind == classType)
3715       {
3716          Class _class = source._class ? source._class.registered : null;
3717
3718          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3719          {
3720             /*
3721             if(dest.kind != classType)
3722             {
3723                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3724                if(!source._class.registered.dataType)
3725                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3726
3727                FreeType(dest);
3728                dest = MkClassType(source._class.string);
3729                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3730                //   dest = MkClassType(source._class.string);
3731             }
3732             */
3733
3734             if(dest.kind != classType)
3735             {
3736                Type tempType { };
3737                Type tempDest, tempSource;
3738
3739                if(!source._class.registered.dataType)
3740                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3741
3742                for(; _class.base.type != systemClass; _class = _class.base);
3743                tempDest = source;
3744                tempSource = tempType;
3745                tempType.kind = classType;
3746                tempType._class = FindClass(_class.fullName);
3747                tempType.truth = source.truth;
3748                tempType.classObjectType = source.classObjectType;
3749
3750                if(tempType._class)
3751                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3752
3753                // PUT THIS BACK TESTING UNITS?
3754                if(conversions.last)
3755                {
3756                   ((Conversion)(conversions.last)).resultType = dest;
3757                   dest.refCount++;
3758                }
3759
3760                FreeType(sourceExp.expType);
3761                sourceExp.expType = MkClassType(_class.fullName);
3762                sourceExp.expType.truth = source.truth;
3763                sourceExp.expType.classObjectType = source.classObjectType;
3764
3765                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3766
3767                if(!sourceExp.destType)
3768                {
3769                   FreeType(sourceExp.destType);
3770                   sourceExp.destType = sourceExp.expType;
3771                   if(sourceExp.expType)
3772                      sourceExp.expType.refCount++;
3773                }
3774                //flag = true;
3775                //source = _class.dataType;
3776
3777
3778                // TOCHECK: TESTING THIS NEW CODE
3779                if(!_class.dataType)
3780                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3781                FreeType(dest);
3782                dest = MkClassType(source._class.string);
3783                dest.truth = source.truth;
3784                dest.classObjectType = source.classObjectType;
3785
3786                FreeType(source);
3787                source = _class.dataType;
3788                source.refCount++;
3789
3790                delete tempType;
3791             }
3792          }
3793       }
3794
3795       if(!flag)
3796       {
3797          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3798          {
3799             FreeType(source);
3800             FreeType(dest);
3801             return true;
3802          }
3803       }
3804
3805       // Implicit Casts
3806       /*
3807       if(source.kind == classType)
3808       {
3809          Class _class = source._class.registered;
3810          if(_class.type == unitClass)
3811          {
3812             if(!_class.dataType)
3813                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3814             source = _class.dataType;
3815          }
3816       }*/
3817
3818       if(dest.kind == classType)
3819       {
3820          Class _class = dest._class ? dest._class.registered : null;
3821          bool fittingValue = false;
3822          if(_class && _class.type == enumClass)
3823          {
3824             Class enumClass = eSystem_FindClass(privateModule, "enum");
3825             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3826             if(c && value >= 0 && value <= c.largest)
3827                fittingValue = true;
3828          }
3829
3830          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3831             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3832          {
3833             if(_class.type == normalClass || _class.type == noHeadClass)
3834             {
3835                Expression newExp { };
3836                *newExp = *sourceExp;
3837                if(sourceExp.destType) sourceExp.destType.refCount++;
3838                if(sourceExp.expType)  sourceExp.expType.refCount++;
3839                sourceExp.type = castExp;
3840                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3841                sourceExp.cast.exp = newExp;
3842                FreeType(sourceExp.expType);
3843                sourceExp.expType = null;
3844                ProcessExpressionType(sourceExp);
3845
3846                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3847                if(!inCompiler)
3848                {
3849                   FreeType(sourceExp.expType);
3850                   sourceExp.expType = dest;
3851                }
3852
3853                FreeType(source);
3854                if(inCompiler) FreeType(dest);
3855
3856                if(backupSourceExpType) FreeType(backupSourceExpType);
3857                return true;
3858             }
3859
3860             if(!_class.dataType)
3861                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3862             FreeType(dest);
3863             dest = _class.dataType;
3864             dest.refCount++;
3865          }
3866
3867          // Accept lower precision types for units, since we want to keep the unit type
3868          if(dest.kind == doubleType &&
3869             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3870              source.kind == charType || source.kind == _BoolType))
3871          {
3872             specs = MkListOne(MkSpecifier(DOUBLE));
3873          }
3874          else if(dest.kind == floatType &&
3875             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3876             source.kind == _BoolType || source.kind == doubleType))
3877          {
3878             specs = MkListOne(MkSpecifier(FLOAT));
3879          }
3880          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3881             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3882          {
3883             specs = MkList();
3884             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3885             ListAdd(specs, MkSpecifier(INT64));
3886          }
3887          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3888             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3889          {
3890             specs = MkList();
3891             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3892             ListAdd(specs, MkSpecifier(INT));
3893          }
3894          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3895             source.kind == floatType || source.kind == doubleType))
3896          {
3897             specs = MkList();
3898             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3899             ListAdd(specs, MkSpecifier(SHORT));
3900          }
3901          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3902             source.kind == floatType || source.kind == doubleType))
3903          {
3904             specs = MkList();
3905             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3906             ListAdd(specs, MkSpecifier(CHAR));
3907          }
3908          else
3909          {
3910             FreeType(source);
3911             FreeType(dest);
3912             if(backupSourceExpType)
3913             {
3914                // Failed to convert: revert previous exp type
3915                if(sourceExp.expType) FreeType(sourceExp.expType);
3916                sourceExp.expType = backupSourceExpType;
3917             }
3918             return false;
3919          }
3920       }
3921       else if(dest.kind == doubleType &&
3922          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3923           source.kind == _BoolType || source.kind == charType))
3924       {
3925          specs = MkListOne(MkSpecifier(DOUBLE));
3926       }
3927       else if(dest.kind == floatType &&
3928          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3929       {
3930          specs = MkListOne(MkSpecifier(FLOAT));
3931       }
3932       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3933          (value == 1 || value == 0))
3934       {
3935          specs = MkList();
3936          ListAdd(specs, MkSpecifier(BOOL));
3937       }
3938       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3939          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3940       {
3941          specs = MkList();
3942          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3943          ListAdd(specs, MkSpecifier(CHAR));
3944       }
3945       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3946          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3947       {
3948          specs = MkList();
3949          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3950          ListAdd(specs, MkSpecifier(SHORT));
3951       }
3952       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3953       {
3954          specs = MkList();
3955          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3956          ListAdd(specs, MkSpecifier(INT));
3957       }
3958       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3959       {
3960          specs = MkList();
3961          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3962          ListAdd(specs, MkSpecifier(INT64));
3963       }
3964       else if(dest.kind == enumType &&
3965          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3966       {
3967          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3968       }
3969       else
3970       {
3971          FreeType(source);
3972          FreeType(dest);
3973          if(backupSourceExpType)
3974          {
3975             // Failed to convert: revert previous exp type
3976             if(sourceExp.expType) FreeType(sourceExp.expType);
3977             sourceExp.expType = backupSourceExpType;
3978          }
3979          return false;
3980       }
3981
3982       if(!flag && !sourceExp.opDestType)
3983       {
3984          Expression newExp { };
3985          *newExp = *sourceExp;
3986          newExp.prev = null;
3987          newExp.next = null;
3988          if(sourceExp.destType) sourceExp.destType.refCount++;
3989          if(sourceExp.expType)  sourceExp.expType.refCount++;
3990
3991          sourceExp.type = castExp;
3992          if(realDest.kind == classType)
3993          {
3994             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3995             FreeList(specs, FreeSpecifier);
3996          }
3997          else
3998             sourceExp.cast.typeName = MkTypeName(specs, null);
3999          if(newExp.type == opExp)
4000          {
4001             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4002          }
4003          else
4004             sourceExp.cast.exp = newExp;
4005
4006          FreeType(sourceExp.expType);
4007          sourceExp.expType = null;
4008          ProcessExpressionType(sourceExp);
4009       }
4010       else
4011          FreeList(specs, FreeSpecifier);
4012
4013       FreeType(dest);
4014       FreeType(source);
4015       if(backupSourceExpType) FreeType(backupSourceExpType);
4016
4017       return true;
4018    }
4019    else
4020    {
4021       if(computedExp != sourceExp)
4022       {
4023          FreeExpression(computedExp);
4024          computedExp = sourceExp;
4025       }
4026
4027       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4028       if(sourceExp.type == identifierExp)
4029       {
4030          Identifier id = sourceExp.identifier;
4031          if(dest.kind == classType)
4032          {
4033             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4034             {
4035                Class _class = dest._class.registered;
4036                Class enumClass = eSystem_FindClass(privateModule, "enum");
4037                if(enumClass)
4038                {
4039                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4040                   {
4041                      NamedLink value;
4042                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4043                      for(value = e.values.first; value; value = value.next)
4044                      {
4045                         if(!strcmp(value.name, id.string))
4046                            break;
4047                      }
4048                      if(value)
4049                      {
4050                         FreeExpContents(sourceExp);
4051                         FreeType(sourceExp.expType);
4052
4053                         sourceExp.isConstant = true;
4054                         sourceExp.expType = MkClassType(_class.fullName);
4055                         //if(inCompiler)
4056                         {
4057                            char constant[256];
4058                            sourceExp.type = constantExp;
4059                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
4060                               sprintf(constant, "%d", (int) value.data);
4061                            else
4062                               sprintf(constant, "0x%X", (int) value.data);
4063                            sourceExp.constant = CopyString(constant);
4064                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4065                         }
4066                         FreeType(dest);
4067                         return true;
4068                      }
4069                   }
4070                }
4071             }
4072          }
4073
4074          // Loop through all enum classes
4075          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4076          {
4077             FreeType(dest);
4078             return true;
4079          }
4080       }
4081       FreeType(dest);
4082    }
4083    return false;
4084 }
4085
4086 #define TERTIARY(o, name, m, t, p) \
4087    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4088    {                                                              \
4089       exp.type = constantExp;                                    \
4090       exp.string = p(op1.m ? op2.m : op3.m);                     \
4091       if(!exp.expType) \
4092          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4093       return true;                                                \
4094    }
4095
4096 #define BINARY(o, name, m, t, p) \
4097    static bool name(Expression exp, Operand op1, Operand op2)   \
4098    {                                                              \
4099       t value2 = op2.m;                                           \
4100       exp.type = constantExp;                                    \
4101       exp.string = p((t)(op1.m o value2));                     \
4102       if(!exp.expType) \
4103          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4104       return true;                                                \
4105    }
4106
4107 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4108    static bool name(Expression exp, Operand op1, Operand op2)   \
4109    {                                                              \
4110       t value2 = op2.m;                                           \
4111       exp.type = constantExp;                                    \
4112       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4113       if(!exp.expType) \
4114          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4115       return true;                                                \
4116    }
4117
4118 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4119    static bool name(Expression exp, Operand op1, Operand op2)   \
4120    {                                                              \
4121       t value2 = op2.m;                                           \
4122       exp.type = constantExp;                                    \
4123       exp.string = p(op1.m o value2);             \
4124       if(!exp.expType) \
4125          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4126       return true;                                                \
4127    }
4128
4129 #define UNARY(o, name, m, t, p) \
4130    static bool name(Expression exp, Operand op1)                \
4131    {                                                              \
4132       exp.type = constantExp;                                    \
4133       exp.string = p((t)(o op1.m));                                   \
4134       if(!exp.expType) \
4135          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4136       return true;                                                \
4137    }
4138
4139 #define OPERATOR_ALL(macro, o, name) \
4140    macro(o, Int##name, i, int, PrintInt) \
4141    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4142    macro(o, Int64##name, i64, int64, PrintInt64) \
4143    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4144    macro(o, Short##name, s, short, PrintShort) \
4145    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4146    macro(o, Char##name, c, char, PrintChar) \
4147    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4148    macro(o, Float##name, f, float, PrintFloat) \
4149    macro(o, Double##name, d, double, PrintDouble)
4150
4151 #define OPERATOR_INTTYPES(macro, o, name) \
4152    macro(o, Int##name, i, int, PrintInt) \
4153    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4154    macro(o, Int64##name, i64, int64, PrintInt64) \
4155    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4156    macro(o, Short##name, s, short, PrintShort) \
4157    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4158    macro(o, Char##name, c, char, PrintChar) \
4159    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4160
4161 #define OPERATOR_REALTYPES(macro, o, name) \
4162    macro(o, Float##name, f, float, PrintFloat) \
4163    macro(o, Double##name, d, double, PrintDouble)
4164
4165 // binary arithmetic
4166 OPERATOR_ALL(BINARY, +, Add)
4167 OPERATOR_ALL(BINARY, -, Sub)
4168 OPERATOR_ALL(BINARY, *, Mul)
4169 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4170 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4171 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4172
4173 // unary arithmetic
4174 OPERATOR_ALL(UNARY, -, Neg)
4175
4176 // unary arithmetic increment and decrement
4177 OPERATOR_ALL(UNARY, ++, Inc)
4178 OPERATOR_ALL(UNARY, --, Dec)
4179
4180 // binary arithmetic assignment
4181 OPERATOR_ALL(BINARY, =, Asign)
4182 OPERATOR_ALL(BINARY, +=, AddAsign)
4183 OPERATOR_ALL(BINARY, -=, SubAsign)
4184 OPERATOR_ALL(BINARY, *=, MulAsign)
4185 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4186 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4187 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4188
4189 // binary bitwise
4190 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4191 OPERATOR_INTTYPES(BINARY, |, BitOr)
4192 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4193 OPERATOR_INTTYPES(BINARY, <<, LShift)
4194 OPERATOR_INTTYPES(BINARY, >>, RShift)
4195
4196 // unary bitwise
4197 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4198
4199 // binary bitwise assignment
4200 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4201 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4202 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4203 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4204 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4205
4206 // unary logical negation
4207 OPERATOR_INTTYPES(UNARY, !, Not)
4208
4209 // binary logical equality
4210 OPERATOR_ALL(BINARY, ==, Equ)
4211 OPERATOR_ALL(BINARY, !=, Nqu)
4212
4213 // binary logical
4214 OPERATOR_ALL(BINARY, &&, And)
4215 OPERATOR_ALL(BINARY, ||, Or)
4216
4217 // binary logical relational
4218 OPERATOR_ALL(BINARY, >, Grt)
4219 OPERATOR_ALL(BINARY, <, Sma)
4220 OPERATOR_ALL(BINARY, >=, GrtEqu)
4221 OPERATOR_ALL(BINARY, <=, SmaEqu)
4222
4223 // tertiary condition operator
4224 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4225
4226 //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
4227 #define OPERATOR_TABLE_ALL(name, type) \
4228     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4229                           type##Neg, \
4230                           type##Inc, type##Dec, \
4231                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4232                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4233                           type##BitNot, \
4234                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4235                           type##Not, \
4236                           type##Equ, type##Nqu, \
4237                           type##And, type##Or, \
4238                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4239                         }; \
4240
4241 #define OPERATOR_TABLE_INTTYPES(name, type) \
4242     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4243                           type##Neg, \
4244                           type##Inc, type##Dec, \
4245                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4246                           null, null, null, null, null, \
4247                           null, \
4248                           null, null, null, null, null, \
4249                           null, \
4250                           type##Equ, type##Nqu, \
4251                           type##And, type##Or, \
4252                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4253                         }; \
4254
4255 OPERATOR_TABLE_ALL(int, Int)
4256 OPERATOR_TABLE_ALL(uint, UInt)
4257 OPERATOR_TABLE_ALL(int64, Int64)
4258 OPERATOR_TABLE_ALL(uint64, UInt64)
4259 OPERATOR_TABLE_ALL(short, Short)
4260 OPERATOR_TABLE_ALL(ushort, UShort)
4261 OPERATOR_TABLE_INTTYPES(float, Float)
4262 OPERATOR_TABLE_INTTYPES(double, Double)
4263 OPERATOR_TABLE_ALL(char, Char)
4264 OPERATOR_TABLE_ALL(uchar, UChar)
4265
4266 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4267 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4268 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4269 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4270 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4271 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4272 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4273 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4274
4275 public void ReadString(char * output,  char * string)
4276 {
4277    int len = strlen(string);
4278    int c,d = 0;
4279    bool quoted = false, escaped = false;
4280    for(c = 0; c<len; c++)
4281    {
4282       char ch = string[c];
4283       if(escaped)
4284       {
4285          switch(ch)
4286          {
4287             case 'n': output[d] = '\n'; break;
4288             case 't': output[d] = '\t'; break;
4289             case 'a': output[d] = '\a'; break;
4290             case 'b': output[d] = '\b'; break;
4291             case 'f': output[d] = '\f'; break;
4292             case 'r': output[d] = '\r'; break;
4293             case 'v': output[d] = '\v'; break;
4294             case '\\': output[d] = '\\'; break;
4295             case '\"': output[d] = '\"'; break;
4296             case '\'': output[d] = '\''; break;
4297             default: output[d] = ch;
4298          }
4299          d++;
4300          escaped = false;
4301       }
4302       else
4303       {
4304          if(ch == '\"')
4305             quoted ^= true;
4306          else if(quoted)
4307          {
4308             if(ch == '\\')
4309                escaped = true;
4310             else
4311                output[d++] = ch;
4312          }
4313       }
4314    }
4315    output[d] = '\0';
4316 }
4317
4318 // String Unescape Copy
4319
4320 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4321 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4322 public int UnescapeString(char * d, char * s, int len)
4323 {
4324    int j = 0, k = 0;
4325    char ch;
4326    while(j < len && (ch = s[j]))
4327    {
4328       switch(ch)
4329       {
4330          case '\\':
4331             switch((ch = s[++j]))
4332             {
4333                case 'n': d[k] = '\n'; break;
4334                case 't': d[k] = '\t'; break;
4335                case 'a': d[k] = '\a'; break;
4336                case 'b': d[k] = '\b'; break;
4337                case 'f': d[k] = '\f'; break;
4338                case 'r': d[k] = '\r'; break;
4339                case 'v': d[k] = '\v'; break;
4340                case '\\': d[k] = '\\'; break;
4341                case '\"': d[k] = '\"'; break;
4342                case '\'': d[k] = '\''; break;
4343                default: d[k] = '\\'; d[k] = ch;
4344             }
4345             break;
4346          default:
4347             d[k] = ch;
4348       }
4349       j++, k++;
4350    }
4351    d[k] = '\0';
4352    return k;
4353 }
4354
4355 public char * OffsetEscapedString(char * s, int len, int offset)
4356 {
4357    char ch;
4358    int j = 0, k = 0;
4359    while(j < len && k < offset && (ch = s[j]))
4360    {
4361       if(ch == '\\') ++j;
4362       j++, k++;
4363    }
4364    return (k == offset) ? s + j : null;
4365 }
4366
4367 public Operand GetOperand(Expression exp)
4368 {
4369    Operand op { };
4370    Type type = exp.expType;
4371    if(type)
4372    {
4373       while(type.kind == classType &&
4374          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4375       {
4376          if(!type._class.registered.dataType)
4377             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4378          type = type._class.registered.dataType;
4379
4380       }
4381       if(exp.type == stringExp && op.kind == pointerType)
4382       {
4383          op.ui64 = (uint64)exp.string;
4384          op.kind = pointerType;
4385          op.ops = uint64Ops;
4386       }
4387       else if(exp.isConstant && exp.type == constantExp)
4388       {
4389          op.kind = type.kind;
4390          op.type = exp.expType;
4391
4392          switch(op.kind)
4393          {
4394             case _BoolType:
4395             case charType:
4396             {
4397                if(exp.constant[0] == '\'')
4398                {
4399                   op.c = exp.constant[1];
4400                   op.ops = charOps;
4401                }
4402                else if(type.isSigned)
4403                {
4404                   op.c = (char)strtol(exp.constant, null, 0);
4405                   op.ops = charOps;
4406                }
4407                else
4408                {
4409                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4410                   op.ops = ucharOps;
4411                }
4412                break;
4413             }
4414             case shortType:
4415                if(type.isSigned)
4416                {
4417                   op.s = (short)strtol(exp.constant, null, 0);
4418                   op.ops = shortOps;
4419                }
4420                else
4421                {
4422                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4423                   op.ops = ushortOps;
4424                }
4425                break;
4426             case intType:
4427             case longType:
4428                if(type.isSigned)
4429                {
4430                   op.i = (int)strtol(exp.constant, null, 0);
4431                   op.ops = intOps;
4432                }
4433                else
4434                {
4435                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4436                   op.ops = uintOps;
4437                }
4438                op.kind = intType;
4439                break;
4440             case int64Type:
4441                if(type.isSigned)
4442                {
4443                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4444                   op.ops = int64Ops;
4445                }
4446                else
4447                {
4448                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4449                   op.ops = uint64Ops;
4450                }
4451                op.kind = int64Type;
4452                break;
4453             case intPtrType:
4454                if(type.isSigned)
4455                {
4456                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4457                   op.ops = int64Ops;
4458                }
4459                else
4460                {
4461                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4462                   op.ops = uint64Ops;
4463                }
4464                op.kind = int64Type;
4465                break;
4466             case intSizeType:
4467                if(type.isSigned)
4468                {
4469                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4470                   op.ops = int64Ops;
4471                }
4472                else
4473                {
4474                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4475                   op.ops = uint64Ops;
4476                }
4477                op.kind = int64Type;
4478                break;
4479             case floatType:
4480                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4481                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4482                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4483                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4484                else
4485                   op.f = (float)strtod(exp.constant, null);
4486                op.ops = floatOps;
4487                break;
4488             case doubleType:
4489                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4490                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4491                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4492                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4493                else
4494                   op.d = (double)strtod(exp.constant, null);
4495                op.ops = doubleOps;
4496                break;
4497             //case classType:    For when we have operator overloading...
4498             // Pointer additions
4499             //case functionType:
4500             case arrayType:
4501             case pointerType:
4502             case classType:
4503                op.ui64 = _strtoui64(exp.constant, null, 0);
4504                op.kind = pointerType;
4505                op.ops = uint64Ops;
4506                // op.ptrSize =
4507                break;
4508          }
4509       }
4510    }
4511    return op;
4512 }
4513
4514 static void UnusedFunction()
4515 {
4516    int a;
4517    a.OnGetString(0,0,0);
4518 }
4519 default:
4520 extern int __ecereVMethodID_class_OnGetString;
4521 public:
4522
4523 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4524 {
4525    DataMember dataMember;
4526    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4527    {
4528       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4529          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4530       else
4531       {
4532          Expression exp { };
4533          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4534          Type type;
4535          void * ptr = inst.data + dataMember.offset + offset;
4536          char * result = null;
4537          exp.loc = member.loc = inst.loc;
4538          ((Identifier)member.identifiers->first).loc = inst.loc;
4539
4540          if(!dataMember.dataType)
4541             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4542          type = dataMember.dataType;
4543          if(type.kind == classType)
4544          {
4545             Class _class = type._class.registered;
4546             if(_class.type == enumClass)
4547             {
4548                Class enumClass = eSystem_FindClass(privateModule, "enum");
4549                if(enumClass)
4550                {
4551                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4552                   NamedLink item;
4553                   for(item = e.values.first; item; item = item.next)
4554                   {
4555                      if((int)item.data == *(int *)ptr)
4556                      {
4557                         result = item.name;
4558                         break;
4559                      }
4560                   }
4561                   if(result)
4562                   {
4563                      exp.identifier = MkIdentifier(result);
4564                      exp.type = identifierExp;
4565                      exp.destType = MkClassType(_class.fullName);
4566                      ProcessExpressionType(exp);
4567                   }
4568                }
4569             }
4570             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4571             {
4572                if(!_class.dataType)
4573                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4574                type = _class.dataType;
4575             }
4576          }
4577          if(!result)
4578          {
4579             switch(type.kind)
4580             {
4581                case floatType:
4582                {
4583                   FreeExpContents(exp);
4584
4585                   exp.constant = PrintFloat(*(float*)ptr);
4586                   exp.type = constantExp;
4587                   break;
4588                }
4589                case doubleType:
4590                {
4591                   FreeExpContents(exp);
4592
4593                   exp.constant = PrintDouble(*(double*)ptr);
4594                   exp.type = constantExp;
4595                   break;
4596                }
4597                case intType:
4598                {
4599                   FreeExpContents(exp);
4600
4601                   exp.constant = PrintInt(*(int*)ptr);
4602                   exp.type = constantExp;
4603                   break;
4604                }
4605                case int64Type:
4606                {
4607                   FreeExpContents(exp);
4608
4609                   exp.constant = PrintInt64(*(int64*)ptr);
4610                   exp.type = constantExp;
4611                   break;
4612                }
4613                case intPtrType:
4614                {
4615                   FreeExpContents(exp);
4616                   // TODO: This should probably use proper type
4617                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4618                   exp.type = constantExp;
4619                   break;
4620                }
4621                case intSizeType:
4622                {
4623                   FreeExpContents(exp);
4624                   // TODO: This should probably use proper type
4625                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4626                   exp.type = constantExp;
4627                   break;
4628                }
4629                default:
4630                   Compiler_Error($"Unhandled type populating instance\n");
4631             }
4632          }
4633          ListAdd(memberList, member);
4634       }
4635
4636       if(parentDataMember.type == unionMember)
4637          break;
4638    }
4639 }
4640
4641 void PopulateInstance(Instantiation inst)
4642 {
4643    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4644    Class _class = classSym.registered;
4645    DataMember dataMember;
4646    OldList * memberList = MkList();
4647    // Added this check and ->Add to prevent memory leaks on bad code
4648    if(!inst.members)
4649       inst.members = MkListOne(MkMembersInitList(memberList));
4650    else
4651       inst.members->Add(MkMembersInitList(memberList));
4652    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4653    {
4654       if(!dataMember.isProperty)
4655       {
4656          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4657             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4658          else
4659          {
4660             Expression exp { };
4661             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4662             Type type;
4663             void * ptr = inst.data + dataMember.offset;
4664             char * result = null;
4665
4666             exp.loc = member.loc = inst.loc;
4667             ((Identifier)member.identifiers->first).loc = inst.loc;
4668
4669             if(!dataMember.dataType)
4670                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4671             type = dataMember.dataType;
4672             if(type.kind == classType)
4673             {
4674                Class _class = type._class.registered;
4675                if(_class.type == enumClass)
4676                {
4677                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4678                   if(enumClass)
4679                   {
4680                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4681                      NamedLink item;
4682                      for(item = e.values.first; item; item = item.next)
4683                      {
4684                         if((int)item.data == *(int *)ptr)
4685                         {
4686                            result = item.name;
4687                            break;
4688                         }
4689                      }
4690                   }
4691                   if(result)
4692                   {
4693                      exp.identifier = MkIdentifier(result);
4694                      exp.type = identifierExp;
4695                      exp.destType = MkClassType(_class.fullName);
4696                      ProcessExpressionType(exp);
4697                   }
4698                }
4699                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4700                {
4701                   if(!_class.dataType)
4702                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4703                   type = _class.dataType;
4704                }
4705             }
4706             if(!result)
4707             {
4708                switch(type.kind)
4709                {
4710                   case floatType:
4711                   {
4712                      exp.constant = PrintFloat(*(float*)ptr);
4713                      exp.type = constantExp;
4714                      break;
4715                   }
4716                   case doubleType:
4717                   {
4718                      exp.constant = PrintDouble(*(double*)ptr);
4719                      exp.type = constantExp;
4720                      break;
4721                   }
4722                   case intType:
4723                   {
4724                      exp.constant = PrintInt(*(int*)ptr);
4725                      exp.type = constantExp;
4726                      break;
4727                   }
4728                   case int64Type:
4729                   {
4730                      exp.constant = PrintInt64(*(int64*)ptr);
4731                      exp.type = constantExp;
4732                      break;
4733                   }
4734                   case intPtrType:
4735                   {
4736                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4737                      exp.type = constantExp;
4738                      break;
4739                   }
4740                   default:
4741                      Compiler_Error($"Unhandled type populating instance\n");
4742                }
4743             }
4744             ListAdd(memberList, member);
4745          }
4746       }
4747    }
4748 }
4749
4750 void ComputeInstantiation(Expression exp)
4751 {
4752    Instantiation inst = exp.instance;
4753    MembersInit members;
4754    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4755    Class _class = classSym ? classSym.registered : null;
4756    DataMember curMember = null;
4757    Class curClass = null;
4758    DataMember subMemberStack[256];
4759    int subMemberStackPos = 0;
4760    uint64 bits = 0;
4761
4762    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4763    {
4764       // Don't recompute the instantiation...
4765       // Non Simple classes will have become constants by now
4766       if(inst.data)
4767          return;
4768
4769       if(_class.type == normalClass || _class.type == noHeadClass)
4770       {
4771          inst.data = (byte *)eInstance_New(_class);
4772          if(_class.type == normalClass)
4773             ((Instance)inst.data)._refCount++;
4774       }
4775       else
4776          inst.data = new0 byte[_class.structSize];
4777    }
4778
4779    if(inst.members)
4780    {
4781       for(members = inst.members->first; members; members = members.next)
4782       {
4783          switch(members.type)
4784          {
4785             case dataMembersInit:
4786             {
4787                if(members.dataMembers)
4788                {
4789                   MemberInit member;
4790                   for(member = members.dataMembers->first; member; member = member.next)
4791                   {
4792                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4793                      bool found = false;
4794
4795                      Property prop = null;
4796                      DataMember dataMember = null;
4797                      Method method = null;
4798                      uint dataMemberOffset;
4799
4800                      if(!ident)
4801                      {
4802                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4803                         if(curMember)
4804                         {
4805                            if(curMember.isProperty)
4806                               prop = (Property)curMember;
4807                            else
4808                            {
4809                               dataMember = curMember;
4810
4811                               // CHANGED THIS HERE
4812                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4813
4814                               // 2013/17/29 -- It seems that this was missing here!
4815                               if(_class.type == normalClass)
4816                                  dataMemberOffset += _class.base.structSize;
4817                               // dataMemberOffset = dataMember.offset;
4818                            }
4819                            found = true;
4820                         }
4821                      }
4822                      else
4823                      {
4824                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4825                         if(prop)
4826                         {
4827                            found = true;
4828                            if(prop.memberAccess == publicAccess)
4829                            {
4830                               curMember = (DataMember)prop;
4831                               curClass = prop._class;
4832                            }
4833                         }
4834                         else
4835                         {
4836                            DataMember _subMemberStack[256];
4837                            int _subMemberStackPos = 0;
4838
4839                            // FILL MEMBER STACK
4840                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4841
4842                            if(dataMember)
4843                            {
4844                               found = true;
4845                               if(dataMember.memberAccess == publicAccess)
4846                               {
4847                                  curMember = dataMember;
4848                                  curClass = dataMember._class;
4849                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4850                                  subMemberStackPos = _subMemberStackPos;
4851                               }
4852                            }
4853                         }
4854                      }
4855
4856                      if(found && member.initializer && member.initializer.type == expInitializer)
4857                      {
4858                         Expression value = member.initializer.exp;
4859                         Type type = null;
4860                         bool deepMember = false;
4861                         if(prop)
4862                         {
4863                            type = prop.dataType;
4864                         }
4865                         else if(dataMember)
4866                         {
4867                            if(!dataMember.dataType)
4868                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4869
4870                            type = dataMember.dataType;
4871                         }
4872
4873                         if(ident && ident.next)
4874                         {
4875                            deepMember = true;
4876
4877                            // for(; ident && type; ident = ident.next)
4878                            for(ident = ident.next; ident && type; ident = ident.next)
4879                            {
4880                               if(type.kind == classType)
4881                               {
4882                                  prop = eClass_FindProperty(type._class.registered,
4883                                     ident.string, privateModule);
4884                                  if(prop)
4885                                     type = prop.dataType;
4886                                  else
4887                                  {
4888                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4889                                        ident.string, &dataMemberOffset, privateModule, null, null);
4890                                     if(dataMember)
4891                                        type = dataMember.dataType;
4892                                  }
4893                               }
4894                               else if(type.kind == structType || type.kind == unionType)
4895                               {
4896                                  Type memberType;
4897                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4898                                  {
4899                                     if(!strcmp(memberType.name, ident.string))
4900                                     {
4901                                        type = memberType;
4902                                        break;
4903                                     }
4904                                  }
4905                               }
4906                            }
4907                         }
4908                         if(value)
4909                         {
4910                            FreeType(value.destType);
4911                            value.destType = type;
4912                            if(type) type.refCount++;
4913                            ComputeExpression(value);
4914                         }
4915                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4916                         {
4917                            if(type.kind == classType)
4918                            {
4919                               Class _class = type._class.registered;
4920                               if(_class.type == bitClass || _class.type == unitClass ||
4921                                  _class.type == enumClass)
4922                               {
4923                                  if(!_class.dataType)
4924                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4925                                  type = _class.dataType;
4926                               }
4927                            }
4928
4929                            if(dataMember)
4930                            {
4931                               void * ptr = inst.data + dataMemberOffset;
4932
4933                               if(value.type == constantExp)
4934                               {
4935                                  switch(type.kind)
4936                                  {
4937                                     case intType:
4938                                     {
4939                                        GetInt(value, (int*)ptr);
4940                                        break;
4941                                     }
4942                                     case int64Type:
4943                                     {
4944                                        GetInt64(value, (int64*)ptr);
4945                                        break;
4946                                     }
4947                                     case intPtrType:
4948                                     {
4949                                        GetIntPtr(value, (intptr*)ptr);
4950                                        break;
4951                                     }
4952                                     case intSizeType:
4953                                     {
4954                                        GetIntSize(value, (intsize*)ptr);
4955                                        break;
4956                                     }
4957                                     case floatType:
4958                                     {
4959                                        GetFloat(value, (float*)ptr);
4960                                        break;
4961                                     }
4962                                     case doubleType:
4963                                     {
4964                                        GetDouble(value, (double *)ptr);
4965                                        break;
4966                                     }
4967                                  }
4968                               }
4969                               else if(value.type == instanceExp)
4970                               {
4971                                  if(type.kind == classType)
4972                                  {
4973                                     Class _class = type._class.registered;
4974                                     if(_class.type == structClass)
4975                                     {
4976                                        ComputeTypeSize(type);
4977                                        if(value.instance.data)
4978                                           memcpy(ptr, value.instance.data, type.size);
4979                                     }
4980                                  }
4981                               }
4982                            }
4983                            else if(prop)
4984                            {
4985                               if(value.type == instanceExp && value.instance.data)
4986                               {
4987                                  if(type.kind == classType)
4988                                  {
4989                                     Class _class = type._class.registered;
4990                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4991                                     {
4992                                        void (*Set)(void *, void *) = (void *)prop.Set;
4993                                        Set(inst.data, value.instance.data);
4994                                        PopulateInstance(inst);
4995                                     }
4996                                  }
4997                               }
4998                               else if(value.type == constantExp)
4999                               {
5000                                  switch(type.kind)
5001                                  {
5002                                     case doubleType:
5003                                     {
5004                                        void (*Set)(void *, double) = (void *)prop.Set;
5005                                        Set(inst.data, strtod(value.constant, null) );
5006                                        break;
5007                                     }
5008                                     case floatType:
5009                                     {
5010                                        void (*Set)(void *, float) = (void *)prop.Set;
5011                                        Set(inst.data, (float)(strtod(value.constant, null)));
5012                                        break;
5013                                     }
5014                                     case intType:
5015                                     {
5016                                        void (*Set)(void *, int) = (void *)prop.Set;
5017                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5018                                        break;
5019                                     }
5020                                     case int64Type:
5021                                     {
5022                                        void (*Set)(void *, int64) = (void *)prop.Set;
5023                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5024                                        break;
5025                                     }
5026                                     case intPtrType:
5027                                     {
5028                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5029                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5030                                        break;
5031                                     }
5032                                     case intSizeType:
5033                                     {
5034                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5035                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5036                                        break;
5037                                     }
5038                                  }
5039                               }
5040                               else if(value.type == stringExp)
5041                               {
5042                                  char temp[1024];
5043                                  ReadString(temp, value.string);
5044                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5045                               }
5046                            }
5047                         }
5048                         else if(!deepMember && type && _class.type == unitClass)
5049                         {
5050                            if(prop)
5051                            {
5052                               // Only support converting units to units for now...
5053                               if(value.type == constantExp)
5054                               {
5055                                  if(type.kind == classType)
5056                                  {
5057                                     Class _class = type._class.registered;
5058                                     if(_class.type == unitClass)
5059                                     {
5060                                        if(!_class.dataType)
5061                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5062                                        type = _class.dataType;
5063                                     }
5064                                  }
5065                                  // TODO: Assuming same base type for units...
5066                                  switch(type.kind)
5067                                  {
5068                                     case floatType:
5069                                     {
5070                                        float fValue;
5071                                        float (*Set)(float) = (void *)prop.Set;
5072                                        GetFloat(member.initializer.exp, &fValue);
5073                                        exp.constant = PrintFloat(Set(fValue));
5074                                        exp.type = constantExp;
5075                                        break;
5076                                     }
5077                                     case doubleType:
5078                                     {
5079                                        double dValue;
5080                                        double (*Set)(double) = (void *)prop.Set;
5081                                        GetDouble(member.initializer.exp, &dValue);
5082                                        exp.constant = PrintDouble(Set(dValue));
5083                                        exp.type = constantExp;
5084                                        break;
5085                                     }
5086                                  }
5087                               }
5088                            }
5089                         }
5090                         else if(!deepMember && type && _class.type == bitClass)
5091                         {
5092                            if(prop)
5093                            {
5094                               if(value.type == instanceExp && value.instance.data)
5095                               {
5096                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5097                                  bits = Set(value.instance.data);
5098                               }
5099                               else if(value.type == constantExp)
5100                               {
5101                               }
5102                            }
5103                            else if(dataMember)
5104                            {
5105                               BitMember bitMember = (BitMember) dataMember;
5106                               Type type;
5107                               uint64 part;
5108                               bits = (bits & ~bitMember.mask);
5109                               if(!bitMember.dataType)
5110                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5111                               type = bitMember.dataType;
5112                               if(type.kind == classType && type._class && type._class.registered)
5113                               {
5114                                  if(!type._class.registered.dataType)
5115                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5116                                  type = type._class.registered.dataType;
5117                               }
5118                               switch(type.kind)
5119                               {
5120                                  case _BoolType:
5121                                  case charType:       { byte v; type.isSigned ? GetChar(value, &v) : GetUChar(value, &v); part = (uint64)v; break; }
5122                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, &v) : GetUShort(value, &v); part = (uint64)v; break; }
5123                                  case intType:
5124                                  case longType:       { uint v; type.isSigned ? GetInt(value, &v) : GetUInt(value, &v); part = (uint64)v; break; }
5125                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, &v) : GetUInt64(value, &v); part = (uint64)v; break; }
5126                                  case intPtrType:     { intptr v; type.isSigned ? GetIntPtr(value, &v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5127                                  case intSizeType:    { intsize v; type.isSigned ? GetIntSize(value, &v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5128                               }
5129                               bits |= part << bitMember.pos;
5130                            }
5131                         }
5132                      }
5133                      else
5134                      {
5135                         if(_class && _class.type == unitClass)
5136                         {
5137                            ComputeExpression(member.initializer.exp);
5138                            exp.constant = member.initializer.exp.constant;
5139                            exp.type = constantExp;
5140
5141                            member.initializer.exp.constant = null;
5142                         }
5143                      }
5144                   }
5145                }
5146                break;
5147             }
5148          }
5149       }
5150    }
5151    if(_class && _class.type == bitClass)
5152    {
5153       exp.constant = PrintHexUInt(bits);
5154       exp.type = constantExp;
5155    }
5156    if(exp.type != instanceExp)
5157    {
5158       FreeInstance(inst);
5159    }
5160 }
5161
5162 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5163 {
5164    bool result = false;
5165    switch(kind)
5166    {
5167       case shortType:
5168          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5169             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5170          break;
5171       case intType:
5172       case longType:
5173          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5174             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5175          break;
5176       case int64Type:
5177          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5178             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5179             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5180          break;
5181       case floatType:
5182          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5183             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5184             result = GetOpFloat(op, &op.f);
5185          break;
5186       case doubleType:
5187          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5188             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5189             result = GetOpDouble(op, &op.d);
5190          break;
5191       case pointerType:
5192          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5193             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5194             result = GetOpUIntPtr(op, &op.ui64);
5195          break;
5196       case enumType:
5197          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5198             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5199             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5200          break;
5201       case intPtrType:
5202          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5203             result = isSigned ? GetOpIntPtr(op, &op.i64) : GetOpUIntPtr(op, &op.i64);
5204          break;
5205       case intSizeType:
5206          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5207             result = isSigned ? GetOpIntSize(op, &op.ui64) : GetOpUIntSize(op, &op.ui64);
5208          break;
5209    }
5210    return result;
5211 }
5212
5213 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5214 {
5215    if(exp.op.op == SIZEOF)
5216    {
5217       FreeExpContents(exp);
5218       exp.type = constantExp;
5219       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5220    }
5221    else
5222    {
5223       if(!exp.op.exp1)
5224       {
5225          switch(exp.op.op)
5226          {
5227             // unary arithmetic
5228             case '+':
5229             {
5230                // Provide default unary +
5231                Expression exp2 = exp.op.exp2;
5232                exp.op.exp2 = null;
5233                FreeExpContents(exp);
5234                FreeType(exp.expType);
5235                FreeType(exp.destType);
5236                *exp = *exp2;
5237                delete exp2;
5238                break;
5239             }
5240             case '-':
5241                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5242                break;
5243             // unary arithmetic increment and decrement
5244                   //OPERATOR_ALL(UNARY, ++, Inc)
5245                   //OPERATOR_ALL(UNARY, --, Dec)
5246             // unary bitwise
5247             case '~':
5248                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5249                break;
5250             // unary logical negation
5251             case '!':
5252                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5253                break;
5254          }
5255       }
5256       else
5257       {
5258          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5259          {
5260             if(Promote(op2, op1.kind, op1.type.isSigned))
5261                op2.kind = op1.kind, op2.ops = op1.ops;
5262             else if(Promote(op1, op2.kind, op2.type.isSigned))
5263                op1.kind = op2.kind, op1.ops = op2.ops;
5264          }
5265          switch(exp.op.op)
5266          {
5267             // binary arithmetic
5268             case '+':
5269                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5270                break;
5271             case '-':
5272                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5273                break;
5274             case '*':
5275                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5276                break;
5277             case '/':
5278                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5279                break;
5280             case '%':
5281                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5282                break;
5283             // binary arithmetic assignment
5284                   //OPERATOR_ALL(BINARY, =, Asign)
5285                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5286                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5287                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5288                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5289                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5290             // binary bitwise
5291             case '&':
5292                if(exp.op.exp2)
5293                {
5294                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5295                }
5296                break;
5297             case '|':
5298                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5299                break;
5300             case '^':
5301                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5302                break;
5303             case LEFT_OP:
5304                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5305                break;
5306             case RIGHT_OP:
5307                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5308                break;
5309             // binary bitwise assignment
5310                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5311                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5312                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5313                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5314                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5315             // binary logical equality
5316             case EQ_OP:
5317                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5318                break;
5319             case NE_OP:
5320                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5321                break;
5322             // binary logical
5323             case AND_OP:
5324                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5325                break;
5326             case OR_OP:
5327                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5328                break;
5329             // binary logical relational
5330             case '>':
5331                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5332                break;
5333             case '<':
5334                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5335                break;
5336             case GE_OP:
5337                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5338                break;
5339             case LE_OP:
5340                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5341                break;
5342          }
5343       }
5344    }
5345 }
5346
5347 void ComputeExpression(Expression exp)
5348 {
5349    char expString[10240];
5350    expString[0] = '\0';
5351 #ifdef _DEBUG
5352    PrintExpression(exp, expString);
5353 #endif
5354
5355    switch(exp.type)
5356    {
5357       case instanceExp:
5358       {
5359          ComputeInstantiation(exp);
5360          break;
5361       }
5362       /*
5363       case constantExp:
5364          break;
5365       */
5366       case opExp:
5367       {
5368          Expression exp1, exp2 = null;
5369          Operand op1 { };
5370          Operand op2 { };
5371
5372          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5373          if(exp.op.exp2)
5374          {
5375             Expression e = exp.op.exp2;
5376
5377             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5378             {
5379                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5380                {
5381                   if(e.type == extensionCompoundExp)
5382                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5383                   else
5384                      e = e.list->last;
5385                }
5386             }
5387             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5388             {
5389                if(e.type == stringExp && e.string)
5390                {
5391                   char * string = e.string;
5392                   int len = strlen(string);
5393                   char * tmp = new char[len-2+1];
5394                   len = UnescapeString(tmp, string + 1, len - 2);
5395                   delete tmp;
5396                   FreeExpContents(exp);
5397                   exp.type = constantExp;
5398                   exp.constant = PrintUInt(len + 1);
5399                }
5400                else
5401                {
5402                   Type type = e.expType;
5403                   type.refCount++;
5404                   FreeExpContents(exp);
5405                   exp.type = constantExp;
5406                   exp.constant = PrintUInt(ComputeTypeSize(type));
5407                   FreeType(type);
5408                }
5409                break;
5410             }
5411             else
5412                ComputeExpression(exp.op.exp2);
5413          }
5414          if(exp.op.exp1)
5415          {
5416             ComputeExpression(exp.op.exp1);
5417             exp1 = exp.op.exp1;
5418             exp2 = exp.op.exp2;
5419             op1 = GetOperand(exp1);
5420             if(op1.type) op1.type.refCount++;
5421             if(exp2)
5422             {
5423                op2 = GetOperand(exp2);
5424                if(op2.type) op2.type.refCount++;
5425             }
5426          }
5427          else
5428          {
5429             exp1 = exp.op.exp2;
5430             op1 = GetOperand(exp1);
5431             if(op1.type) op1.type.refCount++;
5432          }
5433
5434          CallOperator(exp, exp1, exp2, op1, op2);
5435          /*
5436          switch(exp.op.op)
5437          {
5438             // Unary operators
5439             case '&':
5440                // Also binary
5441                if(exp.op.exp1 && exp.op.exp2)
5442                {
5443                   // Binary And
5444                   if(op1.ops.BitAnd)
5445                   {
5446                      FreeExpContents(exp);
5447                      op1.ops.BitAnd(exp, op1, op2);
5448                   }
5449                }
5450                break;
5451             case '*':
5452                if(exp.op.exp1)
5453                {
5454                   if(op1.ops.Mul)
5455                   {
5456                      FreeExpContents(exp);
5457                      op1.ops.Mul(exp, op1, op2);
5458                   }
5459                }
5460                break;
5461             case '+':
5462                if(exp.op.exp1)
5463                {
5464                   if(op1.ops.Add)
5465                   {
5466                      FreeExpContents(exp);
5467                      op1.ops.Add(exp, op1, op2);
5468                   }
5469                }
5470                else
5471                {
5472                   // Provide default unary +
5473                   Expression exp2 = exp.op.exp2;
5474                   exp.op.exp2 = null;
5475                   FreeExpContents(exp);
5476                   FreeType(exp.expType);
5477                   FreeType(exp.destType);
5478
5479                   *exp = *exp2;
5480                   delete exp2;
5481                }
5482                break;
5483             case '-':
5484                if(exp.op.exp1)
5485                {
5486                   if(op1.ops.Sub)
5487                   {
5488                      FreeExpContents(exp);
5489                      op1.ops.Sub(exp, op1, op2);
5490                   }
5491                }
5492                else
5493                {
5494                   if(op1.ops.Neg)
5495                   {
5496                      FreeExpContents(exp);
5497                      op1.ops.Neg(exp, op1);
5498                   }
5499                }
5500                break;
5501             case '~':
5502                if(op1.ops.BitNot)
5503                {
5504                   FreeExpContents(exp);
5505                   op1.ops.BitNot(exp, op1);
5506                }
5507                break;
5508             case '!':
5509                if(op1.ops.Not)
5510                {
5511                   FreeExpContents(exp);
5512                   op1.ops.Not(exp, op1);
5513                }
5514                break;
5515             // Binary only operators
5516             case '/':
5517                if(op1.ops.Div)
5518                {
5519                   FreeExpContents(exp);
5520                   op1.ops.Div(exp, op1, op2);
5521                }
5522                break;
5523             case '%':
5524                if(op1.ops.Mod)
5525                {
5526                   FreeExpContents(exp);
5527                   op1.ops.Mod(exp, op1, op2);
5528                }
5529                break;
5530             case LEFT_OP:
5531                break;
5532             case RIGHT_OP:
5533                break;
5534             case '<':
5535                if(exp.op.exp1)
5536                {
5537                   if(op1.ops.Sma)
5538                   {
5539                      FreeExpContents(exp);
5540                      op1.ops.Sma(exp, op1, op2);
5541                   }
5542                }
5543                break;
5544             case '>':
5545                if(exp.op.exp1)
5546                {
5547                   if(op1.ops.Grt)
5548                   {
5549                      FreeExpContents(exp);
5550                      op1.ops.Grt(exp, op1, op2);
5551                   }
5552                }
5553                break;
5554             case LE_OP:
5555                if(exp.op.exp1)
5556                {
5557                   if(op1.ops.SmaEqu)
5558                   {
5559                      FreeExpContents(exp);
5560                      op1.ops.SmaEqu(exp, op1, op2);
5561                   }
5562                }
5563                break;
5564             case GE_OP:
5565                if(exp.op.exp1)
5566                {
5567                   if(op1.ops.GrtEqu)
5568                   {
5569                      FreeExpContents(exp);
5570                      op1.ops.GrtEqu(exp, op1, op2);
5571                   }
5572                }
5573                break;
5574             case EQ_OP:
5575                if(exp.op.exp1)
5576                {
5577                   if(op1.ops.Equ)
5578                   {
5579                      FreeExpContents(exp);
5580                      op1.ops.Equ(exp, op1, op2);
5581                   }
5582                }
5583                break;
5584             case NE_OP:
5585                if(exp.op.exp1)
5586                {
5587                   if(op1.ops.Nqu)
5588                   {
5589                      FreeExpContents(exp);
5590                      op1.ops.Nqu(exp, op1, op2);
5591                   }
5592                }
5593                break;
5594             case '|':
5595                if(op1.ops.BitOr)
5596                {
5597                   FreeExpContents(exp);
5598                   op1.ops.BitOr(exp, op1, op2);
5599                }
5600                break;
5601             case '^':
5602                if(op1.ops.BitXor)
5603                {
5604                   FreeExpContents(exp);
5605                   op1.ops.BitXor(exp, op1, op2);
5606                }
5607                break;
5608             case AND_OP:
5609                break;
5610             case OR_OP:
5611                break;
5612             case SIZEOF:
5613                FreeExpContents(exp);
5614                exp.type = constantExp;
5615                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5616                break;
5617          }
5618          */
5619          if(op1.type) FreeType(op1.type);
5620          if(op2.type) FreeType(op2.type);
5621          break;
5622       }
5623       case bracketsExp:
5624       case extensionExpressionExp:
5625       {
5626          Expression e, n;
5627          for(e = exp.list->first; e; e = n)
5628          {
5629             n = e.next;
5630             if(!n)
5631             {
5632                OldList * list = exp.list;
5633                Expression prev = exp.prev;
5634                Expression next = exp.next;
5635                ComputeExpression(e);
5636                //FreeExpContents(exp);
5637                FreeType(exp.expType);
5638                FreeType(exp.destType);
5639                *exp = *e;
5640                exp.prev = prev;
5641                exp.next = next;
5642                delete e;
5643                delete list;
5644             }
5645             else
5646             {
5647                FreeExpression(e);
5648             }
5649          }
5650          break;
5651       }
5652       /*
5653
5654       case ExpIndex:
5655       {
5656          Expression e;
5657          exp.isConstant = true;
5658
5659          ComputeExpression(exp.index.exp);
5660          if(!exp.index.exp.isConstant)
5661             exp.isConstant = false;
5662
5663          for(e = exp.index.index->first; e; e = e.next)
5664          {
5665             ComputeExpression(e);
5666             if(!e.next)
5667             {
5668                // Check if this type is int
5669             }
5670             if(!e.isConstant)
5671                exp.isConstant = false;
5672          }
5673          exp.expType = Dereference(exp.index.exp.expType);
5674          break;
5675       }
5676       */
5677       case memberExp:
5678       {
5679          Expression memberExp = exp.member.exp;
5680          Identifier memberID = exp.member.member;
5681
5682          Type type;
5683          ComputeExpression(exp.member.exp);
5684          type = exp.member.exp.expType;
5685          if(type)
5686          {
5687             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);
5688             Property prop = null;
5689             DataMember member = null;
5690             Class convertTo = null;
5691             if(type.kind == subClassType && exp.member.exp.type == classExp)
5692                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5693
5694             if(!_class)
5695             {
5696                char string[256];
5697                Symbol classSym;
5698                string[0] = '\0';
5699                PrintTypeNoConst(type, string, false, true);
5700                classSym = FindClass(string);
5701                _class = classSym ? classSym.registered : null;
5702             }
5703
5704             if(exp.member.member)
5705             {
5706                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5707                if(!prop)
5708                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5709             }
5710             if(!prop && !member && _class && exp.member.member)
5711             {
5712                Symbol classSym = FindClass(exp.member.member.string);
5713                convertTo = _class;
5714                _class = classSym ? classSym.registered : null;
5715                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5716             }
5717
5718             if(prop)
5719             {
5720                if(prop.compiled)
5721                {
5722                   Type type = prop.dataType;
5723                   // TODO: Assuming same base type for units...
5724                   if(_class.type == unitClass)
5725                   {
5726                      if(type.kind == classType)
5727                      {
5728                         Class _class = type._class.registered;
5729                         if(_class.type == unitClass)
5730                         {
5731                            if(!_class.dataType)
5732                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5733                            type = _class.dataType;
5734                         }
5735                      }
5736                      switch(type.kind)
5737                      {
5738                         case floatType:
5739                         {
5740                            float value;
5741                            float (*Get)(float) = (void *)prop.Get;
5742                            GetFloat(exp.member.exp, &value);
5743                            exp.constant = PrintFloat(Get ? Get(value) : value);
5744                            exp.type = constantExp;
5745                            break;
5746                         }
5747                         case doubleType:
5748                         {
5749                            double value;
5750                            double (*Get)(double);
5751                            GetDouble(exp.member.exp, &value);
5752
5753                            if(convertTo)
5754                               Get = (void *)prop.Set;
5755                            else
5756                               Get = (void *)prop.Get;
5757                            exp.constant = PrintDouble(Get ? Get(value) : value);
5758                            exp.type = constantExp;
5759                            break;
5760                         }
5761                      }
5762                   }
5763                   else
5764                   {
5765                      if(convertTo)
5766                      {
5767                         Expression value = exp.member.exp;
5768                         Type type;
5769                         if(!prop.dataType)
5770                            ProcessPropertyType(prop);
5771
5772                         type = prop.dataType;
5773                         if(!type)
5774                         {
5775                             // printf("Investigate this\n");
5776                         }
5777                         else if(_class.type == structClass)
5778                         {
5779                            switch(type.kind)
5780                            {
5781                               case classType:
5782                               {
5783                                  Class propertyClass = type._class.registered;
5784                                  if(propertyClass.type == structClass && value.type == instanceExp)
5785                                  {
5786                                     void (*Set)(void *, void *) = (void *)prop.Set;
5787                                     exp.instance = Instantiation { };
5788                                     exp.instance.data = new0 byte[_class.structSize];
5789                                     exp.instance._class = MkSpecifierName(_class.fullName);
5790                                     exp.instance.loc = exp.loc;
5791                                     exp.type = instanceExp;
5792                                     Set(exp.instance.data, value.instance.data);
5793                                     PopulateInstance(exp.instance);
5794                                  }
5795                                  break;
5796                               }
5797                               case intType:
5798                               {
5799                                  int intValue;
5800                                  void (*Set)(void *, int) = (void *)prop.Set;
5801
5802                                  exp.instance = Instantiation { };
5803                                  exp.instance.data = new0 byte[_class.structSize];
5804                                  exp.instance._class = MkSpecifierName(_class.fullName);
5805                                  exp.instance.loc = exp.loc;
5806                                  exp.type = instanceExp;
5807
5808                                  GetInt(value, &intValue);
5809
5810                                  Set(exp.instance.data, intValue);
5811                                  PopulateInstance(exp.instance);
5812                                  break;
5813                               }
5814                               case int64Type:
5815                               {
5816                                  int64 intValue;
5817                                  void (*Set)(void *, int64) = (void *)prop.Set;
5818
5819                                  exp.instance = Instantiation { };
5820                                  exp.instance.data = new0 byte[_class.structSize];
5821                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5822                                  exp.instance.loc = exp.loc;
5823                                  exp.type = instanceExp;
5824
5825                                  GetInt64(value, &intValue);
5826
5827                                  Set(exp.instance.data, intValue);
5828                                  PopulateInstance(exp.instance);
5829                                  break;
5830                               }
5831                               case intPtrType:
5832                               {
5833                                  // TOFIX:
5834                                  intptr intValue;
5835                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5836
5837                                  exp.instance = Instantiation { };
5838                                  exp.instance.data = new0 byte[_class.structSize];
5839                                  exp.instance._class = MkSpecifierName(_class.fullName);
5840                                  exp.instance.loc = exp.loc;
5841                                  exp.type = instanceExp;
5842
5843                                  GetIntPtr(value, &intValue);
5844
5845                                  Set(exp.instance.data, intValue);
5846                                  PopulateInstance(exp.instance);
5847                                  break;
5848                               }
5849                               case intSizeType:
5850                               {
5851                                  // TOFIX:
5852                                  intsize intValue;
5853                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5854
5855                                  exp.instance = Instantiation { };
5856                                  exp.instance.data = new0 byte[_class.structSize];
5857                                  exp.instance._class = MkSpecifierName(_class.fullName);
5858                                  exp.instance.loc = exp.loc;
5859                                  exp.type = instanceExp;
5860
5861                                  GetIntSize(value, &intValue);
5862
5863                                  Set(exp.instance.data, intValue);
5864                                  PopulateInstance(exp.instance);
5865                                  break;
5866                               }
5867                               case floatType:
5868                               {
5869                                  float floatValue;
5870                                  void (*Set)(void *, float) = (void *)prop.Set;
5871
5872                                  exp.instance = Instantiation { };
5873                                  exp.instance.data = new0 byte[_class.structSize];
5874                                  exp.instance._class = MkSpecifierName(_class.fullName);
5875                                  exp.instance.loc = exp.loc;
5876                                  exp.type = instanceExp;
5877
5878                                  GetFloat(value, &floatValue);
5879
5880                                  Set(exp.instance.data, floatValue);
5881                                  PopulateInstance(exp.instance);
5882                                  break;
5883                               }
5884                               case doubleType:
5885                               {
5886                                  double doubleValue;
5887                                  void (*Set)(void *, double) = (void *)prop.Set;
5888
5889                                  exp.instance = Instantiation { };
5890                                  exp.instance.data = new0 byte[_class.structSize];
5891                                  exp.instance._class = MkSpecifierName(_class.fullName);
5892                                  exp.instance.loc = exp.loc;
5893                                  exp.type = instanceExp;
5894
5895                                  GetDouble(value, &doubleValue);
5896
5897                                  Set(exp.instance.data, doubleValue);
5898                                  PopulateInstance(exp.instance);
5899                                  break;
5900                               }
5901                            }
5902                         }
5903                         else if(_class.type == bitClass)
5904                         {
5905                            switch(type.kind)
5906                            {
5907                               case classType:
5908                               {
5909                                  Class propertyClass = type._class.registered;
5910                                  if(propertyClass.type == structClass && value.instance.data)
5911                                  {
5912                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5913                                     unsigned int bits = Set(value.instance.data);
5914                                     exp.constant = PrintHexUInt(bits);
5915                                     exp.type = constantExp;
5916                                     break;
5917                                  }
5918                                  else if(_class.type == bitClass)
5919                                  {
5920                                     unsigned int value;
5921                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5922                                     unsigned int bits;
5923
5924                                     GetUInt(exp.member.exp, &value);
5925                                     bits = Set(value);
5926                                     exp.constant = PrintHexUInt(bits);
5927                                     exp.type = constantExp;
5928                                  }
5929                               }
5930                            }
5931                         }
5932                      }
5933                      else
5934                      {
5935                         if(_class.type == bitClass)
5936                         {
5937                            unsigned int value;
5938                            GetUInt(exp.member.exp, &value);
5939
5940                            switch(type.kind)
5941                            {
5942                               case classType:
5943                               {
5944                                  Class _class = type._class.registered;
5945                                  if(_class.type == structClass)
5946                                  {
5947                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5948
5949                                     exp.instance = Instantiation { };
5950                                     exp.instance.data = new0 byte[_class.structSize];
5951                                     exp.instance._class = MkSpecifierName(_class.fullName);
5952                                     exp.instance.loc = exp.loc;
5953                                     //exp.instance.fullSet = true;
5954                                     exp.type = instanceExp;
5955                                     Get(value, exp.instance.data);
5956                                     PopulateInstance(exp.instance);
5957                                  }
5958                                  else if(_class.type == bitClass)
5959                                  {
5960                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5961                                     uint64 bits = Get(value);
5962                                     exp.constant = PrintHexUInt64(bits);
5963                                     exp.type = constantExp;
5964                                  }
5965                                  break;
5966                               }
5967                            }
5968                         }
5969                         else if(_class.type == structClass)
5970                         {
5971                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5972                            switch(type.kind)
5973                            {
5974                               case classType:
5975                               {
5976                                  Class _class = type._class.registered;
5977                                  if(_class.type == structClass && value)
5978                                  {
5979                                     void (*Get)(void *, void *) = (void *)prop.Get;
5980
5981                                     exp.instance = Instantiation { };
5982                                     exp.instance.data = new0 byte[_class.structSize];
5983                                     exp.instance._class = MkSpecifierName(_class.fullName);
5984                                     exp.instance.loc = exp.loc;
5985                                     //exp.instance.fullSet = true;
5986                                     exp.type = instanceExp;
5987                                     Get(value, exp.instance.data);
5988                                     PopulateInstance(exp.instance);
5989                                  }
5990                                  break;
5991                               }
5992                            }
5993                         }
5994                         /*else
5995                         {
5996                            char * value = exp.member.exp.instance.data;
5997                            switch(type.kind)
5998                            {
5999                               case classType:
6000                               {
6001                                  Class _class = type._class.registered;
6002                                  if(_class.type == normalClass)
6003                                  {
6004                                     void *(*Get)(void *) = (void *)prop.Get;
6005
6006                                     exp.instance = Instantiation { };
6007                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6008                                     exp.type = instanceExp;
6009                                     exp.instance.data = Get(value, exp.instance.data);
6010                                  }
6011                                  break;
6012                               }
6013                            }
6014                         }
6015                         */
6016                      }
6017                   }
6018                }
6019                else
6020                {
6021                   exp.isConstant = false;
6022                }
6023             }
6024             else if(member)
6025             {
6026             }
6027          }
6028
6029          if(exp.type != ExpressionType::memberExp)
6030          {
6031             FreeExpression(memberExp);
6032             FreeIdentifier(memberID);
6033          }
6034          break;
6035       }
6036       case typeSizeExp:
6037       {
6038          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6039          FreeExpContents(exp);
6040          exp.constant = PrintUInt(ComputeTypeSize(type));
6041          exp.type = constantExp;
6042          FreeType(type);
6043          break;
6044       }
6045       case classSizeExp:
6046       {
6047          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6048          if(classSym && classSym.registered)
6049          {
6050             if(classSym.registered.fixed)
6051             {
6052                FreeSpecifier(exp._class);
6053                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6054                exp.type = constantExp;
6055             }
6056             else
6057             {
6058                char className[1024];
6059                strcpy(className, "__ecereClass_");
6060                FullClassNameCat(className, classSym.string, true);
6061                MangleClassName(className);
6062
6063                DeclareClass(classSym, className);
6064
6065                FreeExpContents(exp);
6066                exp.type = pointerExp;
6067                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6068                exp.member.member = MkIdentifier("structSize");
6069             }
6070          }
6071          break;
6072       }
6073       case castExp:
6074       //case constantExp:
6075       {
6076          Type type;
6077          Expression e = exp;
6078          if(exp.type == castExp)
6079          {
6080             if(exp.cast.exp)
6081                ComputeExpression(exp.cast.exp);
6082             e = exp.cast.exp;
6083          }
6084          if(e && exp.expType)
6085          {
6086             /*if(exp.destType)
6087                type = exp.destType;
6088             else*/
6089                type = exp.expType;
6090             if(type.kind == classType)
6091             {
6092                Class _class = type._class.registered;
6093                if(_class && (_class.type == unitClass || _class.type == bitClass))
6094                {
6095                   if(!_class.dataType)
6096                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6097                   type = _class.dataType;
6098                }
6099             }
6100
6101             switch(type.kind)
6102             {
6103                case _BoolType:
6104                case charType:
6105                   if(type.isSigned)
6106                   {
6107                      char value = 0;
6108                      if(GetChar(e, &value))
6109                      {
6110                         FreeExpContents(exp);
6111                         exp.constant = PrintChar(value);
6112                         exp.type = constantExp;
6113                      }
6114                   }
6115                   else
6116                   {
6117                      unsigned char value = 0;
6118                      if(GetUChar(e, &value))
6119                      {
6120                         FreeExpContents(exp);
6121                         exp.constant = PrintUChar(value);
6122                         exp.type = constantExp;
6123                      }
6124                   }
6125                   break;
6126                case shortType:
6127                   if(type.isSigned)
6128                   {
6129                      short value = 0;
6130                      if(GetShort(e, &value))
6131                      {
6132                         FreeExpContents(exp);
6133                         exp.constant = PrintShort(value);
6134                         exp.type = constantExp;
6135                      }
6136                   }
6137                   else
6138                   {
6139                      unsigned short value = 0;
6140                      if(GetUShort(e, &value))
6141                      {
6142                         FreeExpContents(exp);
6143                         exp.constant = PrintUShort(value);
6144                         exp.type = constantExp;
6145                      }
6146                   }
6147                   break;
6148                case intType:
6149                   if(type.isSigned)
6150                   {
6151                      int value = 0;
6152                      if(GetInt(e, &value))
6153                      {
6154                         FreeExpContents(exp);
6155                         exp.constant = PrintInt(value);
6156                         exp.type = constantExp;
6157                      }
6158                   }
6159                   else
6160                   {
6161                      unsigned int value = 0;
6162                      if(GetUInt(e, &value))
6163                      {
6164                         FreeExpContents(exp);
6165                         exp.constant = PrintUInt(value);
6166                         exp.type = constantExp;
6167                      }
6168                   }
6169                   break;
6170                case int64Type:
6171                   if(type.isSigned)
6172                   {
6173                      int64 value = 0;
6174                      if(GetInt64(e, &value))
6175                      {
6176                         FreeExpContents(exp);
6177                         exp.constant = PrintInt64(value);
6178                         exp.type = constantExp;
6179                      }
6180                   }
6181                   else
6182                   {
6183                      uint64 value = 0;
6184                      if(GetUInt64(e, &value))
6185                      {
6186                         FreeExpContents(exp);
6187                         exp.constant = PrintUInt64(value);
6188                         exp.type = constantExp;
6189                      }
6190                   }
6191                   break;
6192                case intPtrType:
6193                   if(type.isSigned)
6194                   {
6195                      intptr value = 0;
6196                      if(GetIntPtr(e, &value))
6197                      {
6198                         FreeExpContents(exp);
6199                         exp.constant = PrintInt64((int64)value);
6200                         exp.type = constantExp;
6201                      }
6202                   }
6203                   else
6204                   {
6205                      uintptr value = 0;
6206                      if(GetUIntPtr(e, &value))
6207                      {
6208                         FreeExpContents(exp);
6209                         exp.constant = PrintUInt64((uint64)value);
6210                         exp.type = constantExp;
6211                      }
6212                   }
6213                   break;
6214                case intSizeType:
6215                   if(type.isSigned)
6216                   {
6217                      intsize value = 0;
6218                      if(GetIntSize(e, &value))
6219                      {
6220                         FreeExpContents(exp);
6221                         exp.constant = PrintInt64((int64)value);
6222                         exp.type = constantExp;
6223                      }
6224                   }
6225                   else
6226                   {
6227                      uintsize value = 0;
6228                      if(GetUIntSize(e, &value))
6229                      {
6230                         FreeExpContents(exp);
6231                         exp.constant = PrintUInt64((uint64)value);
6232                         exp.type = constantExp;
6233                      }
6234                   }
6235                   break;
6236                case floatType:
6237                {
6238                   float value = 0;
6239                   if(GetFloat(e, &value))
6240                   {
6241                      FreeExpContents(exp);
6242                      exp.constant = PrintFloat(value);
6243                      exp.type = constantExp;
6244                   }
6245                   break;
6246                }
6247                case doubleType:
6248                {
6249                   double value = 0;
6250                   if(GetDouble(e, &value))
6251                   {
6252                      FreeExpContents(exp);
6253                      exp.constant = PrintDouble(value);
6254                      exp.type = constantExp;
6255                   }
6256                   break;
6257                }
6258             }
6259          }
6260          break;
6261       }
6262       case conditionExp:
6263       {
6264          Operand op1 { };
6265          Operand op2 { };
6266          Operand op3 { };
6267
6268          if(exp.cond.exp)
6269             // Caring only about last expression for now...
6270             ComputeExpression(exp.cond.exp->last);
6271          if(exp.cond.elseExp)
6272             ComputeExpression(exp.cond.elseExp);
6273          if(exp.cond.cond)
6274             ComputeExpression(exp.cond.cond);
6275
6276          op1 = GetOperand(exp.cond.cond);
6277          if(op1.type) op1.type.refCount++;
6278          op2 = GetOperand(exp.cond.exp->last);
6279          if(op2.type) op2.type.refCount++;
6280          op3 = GetOperand(exp.cond.elseExp);
6281          if(op3.type) op3.type.refCount++;
6282
6283          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6284          if(op1.type) FreeType(op1.type);
6285          if(op2.type) FreeType(op2.type);
6286          if(op3.type) FreeType(op3.type);
6287          break;
6288       }
6289    }
6290 }
6291
6292 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
6293 {
6294    bool result = true;
6295    if(destType)
6296    {
6297       OldList converts { };
6298       Conversion convert;
6299
6300       if(destType.kind == voidType)
6301          return false;
6302
6303       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
6304          result = false;
6305       if(converts.count)
6306       {
6307          // for(convert = converts.last; convert; convert = convert.prev)
6308          for(convert = converts.first; convert; convert = convert.next)
6309          {
6310             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6311             if(!empty)
6312             {
6313                Expression newExp { };
6314                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6315
6316                // TODO: Check this...
6317                *newExp = *exp;
6318                newExp.prev = null;
6319                newExp.next = null;
6320                newExp.destType = null;
6321
6322                if(convert.isGet)
6323                {
6324                   // [exp].ColorRGB
6325                   exp.type = memberExp;
6326                   exp.addedThis = true;
6327                   exp.member.exp = newExp;
6328                   FreeType(exp.member.exp.expType);
6329
6330                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6331                   exp.member.exp.expType.classObjectType = objectType;
6332                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6333                   exp.member.memberType = propertyMember;
6334                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6335                   // TESTING THIS... for (int)degrees
6336                   exp.needCast = true;
6337                   if(exp.expType) exp.expType.refCount++;
6338                   ApplyAnyObjectLogic(exp.member.exp);
6339                }
6340                else
6341                {
6342
6343                   /*if(exp.isConstant)
6344                   {
6345                      // Color { ColorRGB = [exp] };
6346                      exp.type = instanceExp;
6347                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6348                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6349                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6350                   }
6351                   else*/
6352                   {
6353                      // If not constant, don't turn it yet into an instantiation
6354                      // (Go through the deep members system first)
6355                      exp.type = memberExp;
6356                      exp.addedThis = true;
6357                      exp.member.exp = newExp;
6358
6359                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6360                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6361                         newExp.expType._class.registered.type == noHeadClass)
6362                      {
6363                         newExp.byReference = true;
6364                      }
6365
6366                      FreeType(exp.member.exp.expType);
6367                      /*exp.member.exp.expType = convert.convert.dataType;
6368                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6369                      exp.member.exp.expType = null;
6370                      if(convert.convert.dataType)
6371                      {
6372                         exp.member.exp.expType = { };
6373                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6374                         exp.member.exp.expType.refCount = 1;
6375                         exp.member.exp.expType.classObjectType = objectType;
6376                         ApplyAnyObjectLogic(exp.member.exp);
6377                      }
6378
6379                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6380                      exp.member.memberType = reverseConversionMember;
6381                      exp.expType = convert.resultType ? convert.resultType :
6382                         MkClassType(convert.convert._class.fullName);
6383                      exp.needCast = true;
6384                      if(convert.resultType) convert.resultType.refCount++;
6385                   }
6386                }
6387             }
6388             else
6389             {
6390                FreeType(exp.expType);
6391                if(convert.isGet)
6392                {
6393                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6394                   exp.needCast = true;
6395                   if(exp.expType) exp.expType.refCount++;
6396                }
6397                else
6398                {
6399                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6400                   exp.needCast = true;
6401                   if(convert.resultType)
6402                      convert.resultType.refCount++;
6403                }
6404             }
6405          }
6406          if(exp.isConstant && inCompiler)
6407             ComputeExpression(exp);
6408
6409          converts.Free(FreeConvert);
6410       }
6411
6412       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6413       {
6414          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6415       }
6416       if(!result && exp.expType && exp.destType)
6417       {
6418          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6419              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6420             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6421             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6422             result = true;
6423       }
6424    }
6425    // if(result) CheckTemplateTypes(exp);
6426    return result;
6427 }
6428
6429 void CheckTemplateTypes(Expression exp)
6430 {
6431    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6432    {
6433       Expression newExp { };
6434       Statement compound;
6435       Context context;
6436       *newExp = *exp;
6437       if(exp.destType) exp.destType.refCount++;
6438       if(exp.expType)  exp.expType.refCount++;
6439       newExp.prev = null;
6440       newExp.next = null;
6441
6442       switch(exp.expType.kind)
6443       {
6444          case doubleType:
6445             if(exp.destType.classObjectType)
6446             {
6447                // We need to pass the address, just pass it along (Undo what was done above)
6448                if(exp.destType) exp.destType.refCount--;
6449                if(exp.expType)  exp.expType.refCount--;
6450                delete newExp;
6451             }
6452             else
6453             {
6454                // If we're looking for value:
6455                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6456                OldList * specs;
6457                OldList * unionDefs = MkList();
6458                OldList * statements = MkList();
6459                context = PushContext();
6460                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6461                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6462                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6463                exp.type = extensionCompoundExp;
6464                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6465                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6466                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6467                exp.compound.compound.context = context;
6468                PopContext(context);
6469             }
6470             break;
6471          default:
6472             exp.type = castExp;
6473             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6474             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6475             break;
6476       }
6477    }
6478    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6479    {
6480       Expression newExp { };
6481       Statement compound;
6482       Context context;
6483       *newExp = *exp;
6484       if(exp.destType) exp.destType.refCount++;
6485       if(exp.expType)  exp.expType.refCount++;
6486       newExp.prev = null;
6487       newExp.next = null;
6488
6489       switch(exp.expType.kind)
6490       {
6491          case doubleType:
6492             if(exp.destType.classObjectType)
6493             {
6494                // We need to pass the address, just pass it along (Undo what was done above)
6495                if(exp.destType) exp.destType.refCount--;
6496                if(exp.expType)  exp.expType.refCount--;
6497                delete newExp;
6498             }
6499             else
6500             {
6501                // If we're looking for value:
6502                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6503                OldList * specs;
6504                OldList * unionDefs = MkList();
6505                OldList * statements = MkList();
6506                context = PushContext();
6507                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6508                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6509                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6510                exp.type = extensionCompoundExp;
6511                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6512                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6513                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6514                exp.compound.compound.context = context;
6515                PopContext(context);
6516             }
6517             break;
6518          case classType:
6519          {
6520             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6521             {
6522                exp.type = bracketsExp;
6523                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6524                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6525                ProcessExpressionType(exp.list->first);
6526                break;
6527             }
6528             else
6529             {
6530                exp.type = bracketsExp;
6531                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6532                newExp.needCast = true;
6533                ProcessExpressionType(exp.list->first);
6534                break;
6535             }
6536          }
6537          default:
6538          {
6539             if(exp.expType.kind == templateType)
6540             {
6541                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6542                if(type)
6543                {
6544                   FreeType(exp.destType);
6545                   FreeType(exp.expType);
6546                   delete newExp;
6547                   break;
6548                }
6549             }
6550             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6551             {
6552                exp.type = opExp;
6553                exp.op.op = '*';
6554                exp.op.exp1 = null;
6555                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6556                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6557             }
6558             else
6559             {
6560                char typeString[1024];
6561                Declarator decl;
6562                OldList * specs = MkList();
6563                typeString[0] = '\0';
6564                PrintType(exp.expType, typeString, false, false);
6565                decl = SpecDeclFromString(typeString, specs, null);
6566
6567                exp.type = castExp;
6568                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6569                exp.cast.typeName = MkTypeName(specs, decl);
6570                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6571                exp.cast.exp.needCast = true;
6572             }
6573             break;
6574          }
6575       }
6576    }
6577 }
6578 // TODO: The Symbol tree should be reorganized by namespaces
6579 // Name Space:
6580 //    - Tree of all symbols within (stored without namespace)
6581 //    - Tree of sub-namespaces
6582
6583 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6584 {
6585    int nsLen = strlen(nameSpace);
6586    Symbol symbol;
6587    // Start at the name space prefix
6588    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6589    {
6590       char * s = symbol.string;
6591       if(!strncmp(s, nameSpace, nsLen))
6592       {
6593          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6594          int c;
6595          char * namePart;
6596          for(c = strlen(s)-1; c >= 0; c--)
6597             if(s[c] == ':')
6598                break;
6599
6600          namePart = s+c+1;
6601          if(!strcmp(namePart, name))
6602          {
6603             // TODO: Error on ambiguity
6604             return symbol;
6605          }
6606       }
6607       else
6608          break;
6609    }
6610    return null;
6611 }
6612
6613 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6614 {
6615    int c;
6616    char nameSpace[1024];
6617    char * namePart;
6618    bool gotColon = false;
6619
6620    nameSpace[0] = '\0';
6621    for(c = strlen(name)-1; c >= 0; c--)
6622       if(name[c] == ':')
6623       {
6624          gotColon = true;
6625          break;
6626       }
6627
6628    namePart = name+c+1;
6629    while(c >= 0 && name[c] == ':') c--;
6630    if(c >= 0)
6631    {
6632       // Try an exact match first
6633       Symbol symbol = (Symbol)tree.FindString(name);
6634       if(symbol)
6635          return symbol;
6636
6637       // Namespace specified
6638       memcpy(nameSpace, name, c + 1);
6639       nameSpace[c+1] = 0;
6640
6641       return ScanWithNameSpace(tree, nameSpace, namePart);
6642    }
6643    else if(gotColon)
6644    {
6645       // Looking for a global symbol, e.g. ::Sleep()
6646       Symbol symbol = (Symbol)tree.FindString(namePart);
6647       return symbol;
6648    }
6649    else
6650    {
6651       // Name only (no namespace specified)
6652       Symbol symbol = (Symbol)tree.FindString(namePart);
6653       if(symbol)
6654          return symbol;
6655       return ScanWithNameSpace(tree, "", namePart);
6656    }
6657    return null;
6658 }
6659
6660 static void ProcessDeclaration(Declaration decl);
6661
6662 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6663 {
6664 #ifdef _DEBUG
6665    //Time startTime = GetTime();
6666 #endif
6667    // Optimize this later? Do this before/less?
6668    Context ctx;
6669    Symbol symbol = null;
6670    // First, check if the identifier is declared inside the function
6671    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6672
6673    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6674    {
6675       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6676       {
6677          symbol = null;
6678          if(thisNameSpace)
6679          {
6680             char curName[1024];
6681             strcpy(curName, thisNameSpace);
6682             strcat(curName, "::");
6683             strcat(curName, name);
6684             // Try to resolve in current namespace first
6685             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6686          }
6687          if(!symbol)
6688             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6689       }
6690       else
6691          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6692
6693       if(symbol || ctx == endContext) break;
6694    }
6695    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6696    {
6697       if(symbol.pointerExternal.type == functionExternal)
6698       {
6699          FunctionDefinition function = symbol.pointerExternal.function;
6700
6701          // Modified this recently...
6702          Context tmpContext = curContext;
6703          curContext = null;
6704          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6705          curContext = tmpContext;
6706
6707          symbol.pointerExternal.symbol = symbol;
6708
6709          // TESTING THIS:
6710          DeclareType(symbol.type, true, true);
6711
6712          ast->Insert(curExternal.prev, symbol.pointerExternal);
6713
6714          symbol.id = curExternal.symbol.idCode;
6715
6716       }
6717       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6718       {
6719          ast->Move(symbol.pointerExternal, curExternal.prev);
6720          symbol.id = curExternal.symbol.idCode;
6721       }
6722    }
6723 #ifdef _DEBUG
6724    //findSymbolTotalTime += GetTime() - startTime;
6725 #endif
6726    return symbol;
6727 }
6728
6729 static void GetTypeSpecs(Type type, OldList * specs)
6730 {
6731    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6732    switch(type.kind)
6733    {
6734       case classType:
6735       {
6736          if(type._class.registered)
6737          {
6738             if(!type._class.registered.dataType)
6739                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6740             GetTypeSpecs(type._class.registered.dataType, specs);
6741          }
6742          break;
6743       }
6744       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6745       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6746       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6747       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6748       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6749       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6750       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6751       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6752       case intType:
6753       default:
6754          ListAdd(specs, MkSpecifier(INT)); break;
6755    }
6756 }
6757
6758 static void PrintArraySize(Type arrayType, char * string)
6759 {
6760    char size[256];
6761    size[0] = '\0';
6762    strcat(size, "[");
6763    if(arrayType.enumClass)
6764       strcat(size, arrayType.enumClass.string);
6765    else if(arrayType.arraySizeExp)
6766       PrintExpression(arrayType.arraySizeExp, size);
6767    strcat(size, "]");
6768    strcat(string, size);
6769 }
6770
6771 // WARNING : This function expects a null terminated string since it recursively concatenate...
6772 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6773 {
6774    if(type)
6775    {
6776       if(printConst && type.constant)
6777          strcat(string, "const ");
6778       switch(type.kind)
6779       {
6780          case classType:
6781          {
6782             Symbol c = type._class;
6783             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6784             //       look into merging with thisclass ?
6785             if(type.classObjectType == typedObject)
6786                strcat(string, "typed_object");
6787             else if(type.classObjectType == anyObject)
6788                strcat(string, "any_object");
6789             else
6790             {
6791                if(c && c.string)
6792                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6793             }
6794             if(type.byReference)
6795                strcat(string, " &");
6796             break;
6797          }
6798          case voidType: strcat(string, "void"); break;
6799          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6800          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6801          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6802          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6803          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6804          case _BoolType: strcat(string, "_Bool"); break;
6805          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6806          case floatType: strcat(string, "float"); break;
6807          case doubleType: strcat(string, "double"); break;
6808          case structType:
6809             if(type.enumName)
6810             {
6811                strcat(string, "struct ");
6812                strcat(string, type.enumName);
6813             }
6814             else if(type.typeName)
6815                strcat(string, type.typeName);
6816             else
6817             {
6818                Type member;
6819                strcat(string, "struct { ");
6820                for(member = type.members.first; member; member = member.next)
6821                {
6822                   PrintType(member, string, true, fullName);
6823                   strcat(string,"; ");
6824                }
6825                strcat(string,"}");
6826             }
6827             break;
6828          case unionType:
6829             if(type.enumName)
6830             {
6831                strcat(string, "union ");
6832                strcat(string, type.enumName);
6833             }
6834             else if(type.typeName)
6835                strcat(string, type.typeName);
6836             else
6837             {
6838                strcat(string, "union ");
6839                strcat(string,"(unnamed)");
6840             }
6841             break;
6842          case enumType:
6843             if(type.enumName)
6844             {
6845                strcat(string, "enum ");
6846                strcat(string, type.enumName);
6847             }
6848             else if(type.typeName)
6849                strcat(string, type.typeName);
6850             else
6851                strcat(string, "int"); // "enum");
6852             break;
6853          case ellipsisType:
6854             strcat(string, "...");
6855             break;
6856          case subClassType:
6857             strcat(string, "subclass(");
6858             strcat(string, type._class ? type._class.string : "int");
6859             strcat(string, ")");
6860             break;
6861          case templateType:
6862             strcat(string, type.templateParameter.identifier.string);
6863             break;
6864          case thisClassType:
6865             strcat(string, "thisclass");
6866             break;
6867          case vaListType:
6868             strcat(string, "__builtin_va_list");
6869             break;
6870       }
6871    }
6872 }
6873
6874 static void PrintName(Type type, char * string, bool fullName)
6875 {
6876    if(type.name && type.name[0])
6877    {
6878       if(fullName)
6879          strcat(string, type.name);
6880       else
6881       {
6882          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6883          if(name) name += 2; else name = type.name;
6884          strcat(string, name);
6885       }
6886    }
6887 }
6888
6889 static void PrintAttribs(Type type, char * string)
6890 {
6891    if(type)
6892    {
6893       if(type.dllExport)   strcat(string, "dllexport ");
6894       if(type.attrStdcall) strcat(string, "stdcall ");
6895    }
6896 }
6897
6898 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6899 {
6900    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6901    {
6902       Type attrType = null;
6903       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6904          PrintAttribs(type, string);
6905       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6906          strcat(string, " const");
6907       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6908       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6909          strcat(string, " (");
6910       if(type.kind == pointerType)
6911       {
6912          if(type.type.kind == functionType || type.type.kind == methodType)
6913             PrintAttribs(type.type, string);
6914       }
6915       if(type.kind == pointerType)
6916       {
6917          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6918             strcat(string, "*");
6919          else
6920             strcat(string, " *");
6921       }
6922       if(printConst && type.constant && type.kind == pointerType)
6923          strcat(string, " const");
6924    }
6925    else
6926       PrintTypeSpecs(type, string, fullName, printConst);
6927 }
6928
6929 static void PostPrintType(Type type, char * string, bool fullName)
6930 {
6931    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6932       strcat(string, ")");
6933    if(type.kind == arrayType)
6934       PrintArraySize(type, string);
6935    else if(type.kind == functionType)
6936    {
6937       Type param;
6938       strcat(string, "(");
6939       for(param = type.params.first; param; param = param.next)
6940       {
6941          PrintType(param, string, true, fullName);
6942          if(param.next) strcat(string, ", ");
6943       }
6944       strcat(string, ")");
6945    }
6946    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6947       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6948 }
6949
6950 // *****
6951 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6952 // *****
6953 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6954 {
6955    PrePrintType(type, string, fullName, null, printConst);
6956
6957    if(type.thisClass || (printName && type.name && type.name[0]))
6958       strcat(string, " ");
6959    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6960    {
6961       Symbol _class = type.thisClass;
6962       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6963       {
6964          if(type.classObjectType == classPointer)
6965             strcat(string, "class");
6966          else
6967             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6968       }
6969       else if(_class && _class.string)
6970       {
6971          String s = _class.string;
6972          if(fullName)
6973             strcat(string, s);
6974          else
6975          {
6976             char * name = RSearchString(s, "::", strlen(s), true, false);
6977             if(name) name += 2; else name = s;
6978             strcat(string, name);
6979          }
6980       }
6981       strcat(string, "::");
6982    }
6983
6984    if(printName && type.name)
6985       PrintName(type, string, fullName);
6986    PostPrintType(type, string, fullName);
6987    if(type.bitFieldCount)
6988    {
6989       char count[100];
6990       sprintf(count, ":%d", type.bitFieldCount);
6991       strcat(string, count);
6992    }
6993 }
6994
6995 void PrintType(Type type, char * string, bool printName, bool fullName)
6996 {
6997    _PrintType(type, string, printName, fullName, true);
6998 }
6999
7000 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7001 {
7002    _PrintType(type, string, printName, fullName, false);
7003 }
7004
7005 static Type FindMember(Type type, char * string)
7006 {
7007    Type memberType;
7008    for(memberType = type.members.first; memberType; memberType = memberType.next)
7009    {
7010       if(!memberType.name)
7011       {
7012          Type subType = FindMember(memberType, string);
7013          if(subType)
7014             return subType;
7015       }
7016       else if(!strcmp(memberType.name, string))
7017          return memberType;
7018    }
7019    return null;
7020 }
7021
7022 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7023 {
7024    Type memberType;
7025    for(memberType = type.members.first; memberType; memberType = memberType.next)
7026    {
7027       if(!memberType.name)
7028       {
7029          Type subType = FindMember(memberType, string);
7030          if(subType)
7031          {
7032             *offset += memberType.offset;
7033             return subType;
7034          }
7035       }
7036       else if(!strcmp(memberType.name, string))
7037       {
7038          *offset += memberType.offset;
7039          return memberType;
7040       }
7041    }
7042    return null;
7043 }
7044
7045 public bool GetParseError() { return parseError; }
7046
7047 Expression ParseExpressionString(char * expression)
7048 {
7049    parseError = false;
7050
7051    fileInput = TempFile { };
7052    fileInput.Write(expression, 1, strlen(expression));
7053    fileInput.Seek(0, start);
7054
7055    echoOn = false;
7056    parsedExpression = null;
7057    resetScanner();
7058    expression_yyparse();
7059    delete fileInput;
7060
7061    return parsedExpression;
7062 }
7063
7064 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7065 {
7066    Identifier id = exp.identifier;
7067    Method method = null;
7068    Property prop = null;
7069    DataMember member = null;
7070    ClassProperty classProp = null;
7071
7072    if(_class && _class.type == enumClass)
7073    {
7074       NamedLink value = null;
7075       Class enumClass = eSystem_FindClass(privateModule, "enum");
7076       if(enumClass)
7077       {
7078          Class baseClass;
7079          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7080          {
7081             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7082             for(value = e.values.first; value; value = value.next)
7083             {
7084                if(!strcmp(value.name, id.string))
7085                   break;
7086             }
7087             if(value)
7088             {
7089                char constant[256];
7090
7091                FreeExpContents(exp);
7092
7093                exp.type = constantExp;
7094                exp.isConstant = true;
7095                if(!strcmp(baseClass.dataTypeString, "int"))
7096                   sprintf(constant, "%d",(int)value.data);
7097                else
7098                   sprintf(constant, "0x%X",(int)value.data);
7099                exp.constant = CopyString(constant);
7100                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7101                exp.expType = MkClassType(baseClass.fullName);
7102                break;
7103             }
7104          }
7105       }
7106       if(value)
7107          return true;
7108    }
7109    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7110    {
7111       ProcessMethodType(method);
7112       exp.expType = Type
7113       {
7114          refCount = 1;
7115          kind = methodType;
7116          method = method;
7117          // Crash here?
7118          // TOCHECK: Put it back to what it was...
7119          // methodClass = _class;
7120          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7121       };
7122       //id._class = null;
7123       return true;
7124    }
7125    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7126    {
7127       if(!prop.dataType)
7128          ProcessPropertyType(prop);
7129       exp.expType = prop.dataType;
7130       if(prop.dataType) prop.dataType.refCount++;
7131       return true;
7132    }
7133    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7134    {
7135       if(!member.dataType)
7136          member.dataType = ProcessTypeString(member.dataTypeString, false);
7137       exp.expType = member.dataType;
7138       if(member.dataType) member.dataType.refCount++;
7139       return true;
7140    }
7141    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7142    {
7143       if(!classProp.dataType)
7144          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7145
7146       if(classProp.constant)
7147       {
7148          FreeExpContents(exp);
7149
7150          exp.isConstant = true;
7151          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7152          {
7153             //char constant[256];
7154             exp.type = stringExp;
7155             exp.constant = QMkString((char *)classProp.Get(_class));
7156          }
7157          else
7158          {
7159             char constant[256];
7160             exp.type = constantExp;
7161             sprintf(constant, "%d", (int)classProp.Get(_class));
7162             exp.constant = CopyString(constant);
7163          }
7164       }
7165       else
7166       {
7167          // TO IMPLEMENT...
7168       }
7169
7170       exp.expType = classProp.dataType;
7171       if(classProp.dataType) classProp.dataType.refCount++;
7172       return true;
7173    }
7174    return false;
7175 }
7176
7177 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7178 {
7179    BinaryTree * tree = &nameSpace.functions;
7180    GlobalData data = (GlobalData)tree->FindString(name);
7181    NameSpace * child;
7182    if(!data)
7183    {
7184       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7185       {
7186          data = ScanGlobalData(child, name);
7187          if(data)
7188             break;
7189       }
7190    }
7191    return data;
7192 }
7193
7194 static GlobalData FindGlobalData(char * name)
7195 {
7196    int start = 0, c;
7197    NameSpace * nameSpace;
7198    nameSpace = globalData;
7199    for(c = 0; name[c]; c++)
7200    {
7201       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7202       {
7203          NameSpace * newSpace;
7204          char * spaceName = new char[c - start + 1];
7205          strncpy(spaceName, name + start, c - start);
7206          spaceName[c-start] = '\0';
7207          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7208          delete spaceName;
7209          if(!newSpace)
7210             return null;
7211          nameSpace = newSpace;
7212          if(name[c] == ':') c++;
7213          start = c+1;
7214       }
7215    }
7216    if(c - start)
7217    {
7218       return ScanGlobalData(nameSpace, name + start);
7219    }
7220    return null;
7221 }
7222
7223 static int definedExpStackPos;
7224 static void * definedExpStack[512];
7225
7226 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7227 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7228 {
7229    Expression prev = checkedExp.prev, next = checkedExp.next;
7230
7231    FreeExpContents(checkedExp);
7232    FreeType(checkedExp.expType);
7233    FreeType(checkedExp.destType);
7234
7235    *checkedExp = *newExp;
7236
7237    delete newExp;
7238
7239    checkedExp.prev = prev;
7240    checkedExp.next = next;
7241 }
7242
7243 void ApplyAnyObjectLogic(Expression e)
7244 {
7245    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7246 #ifdef _DEBUG
7247    char debugExpString[4096];
7248    debugExpString[0] = '\0';
7249    PrintExpression(e, debugExpString);
7250 #endif
7251
7252    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7253    {
7254       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7255       //ellipsisDestType = destType;
7256       if(e && e.expType)
7257       {
7258          Type type = e.expType;
7259          Class _class = null;
7260          //Type destType = e.destType;
7261
7262          if(type.kind == classType && type._class && type._class.registered)
7263          {
7264             _class = type._class.registered;
7265          }
7266          else if(type.kind == subClassType)
7267          {
7268             _class = FindClass("ecere::com::Class").registered;
7269          }
7270          else
7271          {
7272             char string[1024] = "";
7273             Symbol classSym;
7274
7275             PrintTypeNoConst(type, string, false, true);
7276             classSym = FindClass(string);
7277             if(classSym) _class = classSym.registered;
7278          }
7279
7280          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...
7281             (!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))) ||
7282             destType.byReference)))
7283          {
7284             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7285             {
7286                Expression checkedExp = e, newExp;
7287
7288                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7289                {
7290                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7291                   {
7292                      if(checkedExp.type == extensionCompoundExp)
7293                      {
7294                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7295                      }
7296                      else
7297                         checkedExp = checkedExp.list->last;
7298                   }
7299                   else if(checkedExp.type == castExp)
7300                      checkedExp = checkedExp.cast.exp;
7301                }
7302
7303                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7304                {
7305                   newExp = checkedExp.op.exp2;
7306                   checkedExp.op.exp2 = null;
7307                   FreeExpContents(checkedExp);
7308
7309                   if(e.expType && e.expType.passAsTemplate)
7310                   {
7311                      char size[100];
7312                      ComputeTypeSize(e.expType);
7313                      sprintf(size, "%d", e.expType.size);
7314                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7315                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7316                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7317                   }
7318
7319                   ReplaceExpContents(checkedExp, newExp);
7320                   e.byReference = true;
7321                }
7322                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7323                {
7324                   Expression checkedExp, newExp;
7325
7326                   {
7327                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7328                      bool hasAddress =
7329                         e.type == identifierExp ||
7330                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7331                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7332                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7333                         e.type == indexExp;
7334
7335                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7336                      {
7337                         Context context = PushContext();
7338                         Declarator decl;
7339                         OldList * specs = MkList();
7340                         char typeString[1024];
7341                         Expression newExp { };
7342
7343                         typeString[0] = '\0';
7344                         *newExp = *e;
7345
7346                         //if(e.destType) e.destType.refCount++;
7347                         // if(exp.expType) exp.expType.refCount++;
7348                         newExp.prev = null;
7349                         newExp.next = null;
7350                         newExp.expType = null;
7351
7352                         PrintTypeNoConst(e.expType, typeString, false, true);
7353                         decl = SpecDeclFromString(typeString, specs, null);
7354                         newExp.destType = ProcessType(specs, decl);
7355
7356                         curContext = context;
7357
7358                         // We need a current compound for this
7359                         if(curCompound)
7360                         {
7361                            char name[100];
7362                            OldList * stmts = MkList();
7363                            e.type = extensionCompoundExp;
7364                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7365                            if(!curCompound.compound.declarations)
7366                               curCompound.compound.declarations = MkList();
7367                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7368                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7369                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7370                            e.compound = MkCompoundStmt(null, stmts);
7371                         }
7372                         else
7373                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7374
7375                         /*
7376                         e.compound = MkCompoundStmt(
7377                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7378                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7379
7380                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7381                         */
7382
7383                         {
7384                            Type type = e.destType;
7385                            e.destType = { };
7386                            CopyTypeInto(e.destType, type);
7387                            e.destType.refCount = 1;
7388                            e.destType.classObjectType = none;
7389                            FreeType(type);
7390                         }
7391
7392                         e.compound.compound.context = context;
7393                         PopContext(context);
7394                         curContext = context.parent;
7395                      }
7396                   }
7397
7398                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7399                   checkedExp = e;
7400                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7401                   {
7402                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7403                      {
7404                         if(checkedExp.type == extensionCompoundExp)
7405                         {
7406                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7407                         }
7408                         else
7409                            checkedExp = checkedExp.list->last;
7410                      }
7411                      else if(checkedExp.type == castExp)
7412                         checkedExp = checkedExp.cast.exp;
7413                   }
7414                   {
7415                      Expression operand { };
7416                      operand = *checkedExp;
7417                      checkedExp.destType = null;
7418                      checkedExp.expType = null;
7419                      checkedExp.Clear();
7420                      checkedExp.type = opExp;
7421                      checkedExp.op.op = '&';
7422                      checkedExp.op.exp1 = null;
7423                      checkedExp.op.exp2 = operand;
7424
7425                      //newExp = MkExpOp(null, '&', checkedExp);
7426                   }
7427                   //ReplaceExpContents(checkedExp, newExp);
7428                }
7429             }
7430          }
7431       }
7432    }
7433    {
7434       // If expression type is a simple class, make it an address
7435       // FixReference(e, true);
7436    }
7437 //#if 0
7438    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7439       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7440          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7441    {
7442       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"))
7443       {
7444          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7445       }
7446       else
7447       {
7448          Expression thisExp { };
7449
7450          *thisExp = *e;
7451          thisExp.prev = null;
7452          thisExp.next = null;
7453          e.Clear();
7454
7455          e.type = bracketsExp;
7456          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7457          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7458             ((Expression)e.list->first).byReference = true;
7459
7460          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7461          {
7462             e.expType = thisExp.expType;
7463             e.expType.refCount++;
7464          }
7465          else*/
7466          {
7467             e.expType = { };
7468             CopyTypeInto(e.expType, thisExp.expType);
7469             e.expType.byReference = false;
7470             e.expType.refCount = 1;
7471
7472             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7473                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7474             {
7475                e.expType.classObjectType = none;
7476             }
7477          }
7478       }
7479    }
7480 // TOFIX: Try this for a nice IDE crash!
7481 //#endif
7482    // The other way around
7483    else
7484 //#endif
7485    if(destType && e.expType &&
7486          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7487          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7488          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7489    {
7490       if(destType.kind == ellipsisType)
7491       {
7492          Compiler_Error($"Unspecified type\n");
7493       }
7494       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7495       {
7496          bool byReference = e.expType.byReference;
7497          Expression thisExp { };
7498          Declarator decl;
7499          OldList * specs = MkList();
7500          char typeString[1024]; // Watch buffer overruns
7501          Type type;
7502          ClassObjectType backupClassObjectType;
7503          bool backupByReference;
7504
7505          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7506             type = e.expType;
7507          else
7508             type = destType;
7509
7510          backupClassObjectType = type.classObjectType;
7511          backupByReference = type.byReference;
7512
7513          type.classObjectType = none;
7514          type.byReference = false;
7515
7516          typeString[0] = '\0';
7517          PrintType(type, typeString, false, true);
7518          decl = SpecDeclFromString(typeString, specs, null);
7519
7520          type.classObjectType = backupClassObjectType;
7521          type.byReference = backupByReference;
7522
7523          *thisExp = *e;
7524          thisExp.prev = null;
7525          thisExp.next = null;
7526          e.Clear();
7527
7528          if( ( type.kind == classType && type._class && type._class.registered &&
7529                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7530                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7531              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7532              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7533          {
7534             e.type = opExp;
7535             e.op.op = '*';
7536             e.op.exp1 = null;
7537             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7538
7539             e.expType = { };
7540             CopyTypeInto(e.expType, type);
7541             e.expType.byReference = false;
7542             e.expType.refCount = 1;
7543          }
7544          else
7545          {
7546             e.type = castExp;
7547             e.cast.typeName = MkTypeName(specs, decl);
7548             e.cast.exp = thisExp;
7549             e.byReference = true;
7550             e.expType = type;
7551             type.refCount++;
7552          }
7553          e.destType = destType;
7554          destType.refCount++;
7555       }
7556    }
7557 }
7558
7559 void ProcessExpressionType(Expression exp)
7560 {
7561    bool unresolved = false;
7562    Location oldyylloc = yylloc;
7563    bool notByReference = false;
7564 #ifdef _DEBUG
7565    char debugExpString[4096];
7566    debugExpString[0] = '\0';
7567    PrintExpression(exp, debugExpString);
7568 #endif
7569    if(!exp || exp.expType)
7570       return;
7571
7572    //eSystem_Logf("%s\n", expString);
7573
7574    // Testing this here
7575    yylloc = exp.loc;
7576    switch(exp.type)
7577    {
7578       case identifierExp:
7579       {
7580          Identifier id = exp.identifier;
7581          if(!id || !topContext) return;
7582
7583          // DOING THIS LATER NOW...
7584          if(id._class && id._class.name)
7585          {
7586             id.classSym = id._class.symbol; // FindClass(id._class.name);
7587             /* TODO: Name Space Fix ups
7588             if(!id.classSym)
7589                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7590             */
7591          }
7592
7593          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7594          {
7595             exp.expType = ProcessTypeString("Module", true);
7596             break;
7597          }
7598          else */if(strstr(id.string, "__ecereClass") == id.string)
7599          {
7600             exp.expType = ProcessTypeString("ecere::com::Class", true);
7601             break;
7602          }
7603          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7604          {
7605             // Added this here as well
7606             ReplaceClassMembers(exp, thisClass);
7607             if(exp.type != identifierExp)
7608             {
7609                ProcessExpressionType(exp);
7610                break;
7611             }
7612
7613             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7614                break;
7615          }
7616          else
7617          {
7618             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7619             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7620             if(!symbol/* && exp.destType*/)
7621             {
7622                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7623                   break;
7624                else
7625                {
7626                   if(thisClass)
7627                   {
7628                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7629                      if(exp.type != identifierExp)
7630                      {
7631                         ProcessExpressionType(exp);
7632                         break;
7633                      }
7634                   }
7635                   // Static methods called from inside the _class
7636                   else if(currentClass && !id._class)
7637                   {
7638                      if(ResolveIdWithClass(exp, currentClass, true))
7639                         break;
7640                   }
7641                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7642                }
7643             }
7644
7645             // If we manage to resolve this symbol
7646             if(symbol)
7647             {
7648                Type type = symbol.type;
7649                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7650
7651                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7652                {
7653                   Context context = SetupTemplatesContext(_class);
7654                   type = ReplaceThisClassType(_class);
7655                   FinishTemplatesContext(context);
7656                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7657                }
7658
7659                FreeSpecifier(id._class);
7660                id._class = null;
7661                delete id.string;
7662                id.string = CopyString(symbol.string);
7663
7664                id.classSym = null;
7665                exp.expType = type;
7666                if(type)
7667                   type.refCount++;
7668
7669                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7670                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7671                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7672                   // Add missing cases here... enum Classes...
7673                   exp.isConstant = true;
7674
7675                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7676                if(symbol.isParam || !strcmp(id.string, "this"))
7677                {
7678                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7679                      exp.byReference = true;
7680
7681                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7682                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7683                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7684                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7685                   {
7686                      Identifier id = exp.identifier;
7687                      exp.type = bracketsExp;
7688                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7689                   }*/
7690                }
7691
7692                if(symbol.isIterator)
7693                {
7694                   if(symbol.isIterator == 3)
7695                   {
7696                      exp.type = bracketsExp;
7697                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7698                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7699                      exp.expType = null;
7700                      ProcessExpressionType(exp);
7701                   }
7702                   else if(symbol.isIterator != 4)
7703                   {
7704                      exp.type = memberExp;
7705                      exp.member.exp = MkExpIdentifier(exp.identifier);
7706                      exp.member.exp.expType = exp.expType;
7707                      /*if(symbol.isIterator == 6)
7708                         exp.member.member = MkIdentifier("key");
7709                      else*/
7710                         exp.member.member = MkIdentifier("data");
7711                      exp.expType = null;
7712                      ProcessExpressionType(exp);
7713                   }
7714                }
7715                break;
7716             }
7717             else
7718             {
7719                DefinedExpression definedExp = null;
7720                if(thisNameSpace && !(id._class && !id._class.name))
7721                {
7722                   char name[1024];
7723                   strcpy(name, thisNameSpace);
7724                   strcat(name, "::");
7725                   strcat(name, id.string);
7726                   definedExp = eSystem_FindDefine(privateModule, name);
7727                }
7728                if(!definedExp)
7729                   definedExp = eSystem_FindDefine(privateModule, id.string);
7730                if(definedExp)
7731                {
7732                   int c;
7733                   for(c = 0; c<definedExpStackPos; c++)
7734                      if(definedExpStack[c] == definedExp)
7735                         break;
7736                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7737                   {
7738                      Location backupYylloc = yylloc;
7739                      File backInput = fileInput;
7740                      definedExpStack[definedExpStackPos++] = definedExp;
7741
7742                      fileInput = TempFile { };
7743                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7744                      fileInput.Seek(0, start);
7745
7746                      echoOn = false;
7747                      parsedExpression = null;
7748                      resetScanner();
7749                      expression_yyparse();
7750                      delete fileInput;
7751                      if(backInput)
7752                         fileInput = backInput;
7753
7754                      yylloc = backupYylloc;
7755
7756                      if(parsedExpression)
7757                      {
7758                         FreeIdentifier(id);
7759                         exp.type = bracketsExp;
7760                         exp.list = MkListOne(parsedExpression);
7761                         parsedExpression.loc = yylloc;
7762                         ProcessExpressionType(exp);
7763                         definedExpStackPos--;
7764                         return;
7765                      }
7766                      definedExpStackPos--;
7767                   }
7768                   else
7769                   {
7770                      if(inCompiler)
7771                      {
7772                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7773                      }
7774                   }
7775                }
7776                else
7777                {
7778                   GlobalData data = null;
7779                   if(thisNameSpace && !(id._class && !id._class.name))
7780                   {
7781                      char name[1024];
7782                      strcpy(name, thisNameSpace);
7783                      strcat(name, "::");
7784                      strcat(name, id.string);
7785                      data = FindGlobalData(name);
7786                   }
7787                   if(!data)
7788                      data = FindGlobalData(id.string);
7789                   if(data)
7790                   {
7791                      DeclareGlobalData(data);
7792                      exp.expType = data.dataType;
7793                      if(data.dataType) data.dataType.refCount++;
7794
7795                      delete id.string;
7796                      id.string = CopyString(data.fullName);
7797                      FreeSpecifier(id._class);
7798                      id._class = null;
7799
7800                      break;
7801                   }
7802                   else
7803                   {
7804                      GlobalFunction function = null;
7805                      if(thisNameSpace && !(id._class && !id._class.name))
7806                      {
7807                         char name[1024];
7808                         strcpy(name, thisNameSpace);
7809                         strcat(name, "::");
7810                         strcat(name, id.string);
7811                         function = eSystem_FindFunction(privateModule, name);
7812                      }
7813                      if(!function)
7814                         function = eSystem_FindFunction(privateModule, id.string);
7815                      if(function)
7816                      {
7817                         char name[1024];
7818                         delete id.string;
7819                         id.string = CopyString(function.name);
7820                         name[0] = 0;
7821
7822                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7823                            strcpy(name, "__ecereFunction_");
7824                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7825                         if(DeclareFunction(function, name))
7826                         {
7827                            delete id.string;
7828                            id.string = CopyString(name);
7829                         }
7830                         exp.expType = function.dataType;
7831                         if(function.dataType) function.dataType.refCount++;
7832
7833                         FreeSpecifier(id._class);
7834                         id._class = null;
7835
7836                         break;
7837                      }
7838                   }
7839                }
7840             }
7841          }
7842          unresolved = true;
7843          break;
7844       }
7845       case instanceExp:
7846       {
7847          Class _class;
7848          // Symbol classSym;
7849
7850          if(!exp.instance._class)
7851          {
7852             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7853             {
7854                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7855             }
7856          }
7857
7858          //classSym = FindClass(exp.instance._class.fullName);
7859          //_class = classSym ? classSym.registered : null;
7860
7861          ProcessInstantiationType(exp.instance);
7862          exp.isConstant = exp.instance.isConstant;
7863
7864          /*
7865          if(_class.type == unitClass && _class.base.type != systemClass)
7866          {
7867             {
7868                Type destType = exp.destType;
7869
7870                exp.destType = MkClassType(_class.base.fullName);
7871                exp.expType = MkClassType(_class.fullName);
7872                CheckExpressionType(exp, exp.destType, true);
7873
7874                exp.destType = destType;
7875             }
7876             exp.expType = MkClassType(_class.fullName);
7877          }
7878          else*/
7879          if(exp.instance._class)
7880          {
7881             exp.expType = MkClassType(exp.instance._class.name);
7882             /*if(exp.expType._class && exp.expType._class.registered &&
7883                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7884                exp.expType.byReference = true;*/
7885          }
7886          break;
7887       }
7888       case constantExp:
7889       {
7890          if(!exp.expType)
7891          {
7892             char * constant = exp.constant;
7893             Type type
7894             {
7895                refCount = 1;
7896                constant = true;
7897             };
7898             exp.expType = type;
7899
7900             if(constant[0] == '\'')
7901             {
7902                if((int)((byte *)constant)[1] > 127)
7903                {
7904                   int nb;
7905                   unichar ch = UTF8GetChar(constant + 1, &nb);
7906                   if(nb < 2) ch = constant[1];
7907                   delete constant;
7908                   exp.constant = PrintUInt(ch);
7909                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7910                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7911                   type._class = FindClass("unichar");
7912
7913                   type.isSigned = false;
7914                }
7915                else
7916                {
7917                   type.kind = charType;
7918                   type.isSigned = true;
7919                }
7920             }
7921             else
7922             {
7923                char * dot = strchr(constant, '.');
7924                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7925                char * exponent;
7926                if(isHex)
7927                {
7928                   exponent = strchr(constant, 'p');
7929                   if(!exponent) exponent = strchr(constant, 'P');
7930                }
7931                else
7932                {
7933                   exponent = strchr(constant, 'e');
7934                   if(!exponent) exponent = strchr(constant, 'E');
7935                }
7936
7937                if(dot || exponent)
7938                {
7939                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7940                      type.kind = floatType;
7941                   else
7942                      type.kind = doubleType;
7943                   type.isSigned = true;
7944                }
7945                else
7946                {
7947                   bool isSigned = constant[0] == '-';
7948                   char * endP = null;
7949                   int64 i64 = strtoll(constant, &endP, 0);
7950                   uint64 ui64 = strtoull(constant, &endP, 0);
7951                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
7952                   if(isSigned)
7953                   {
7954                      if(i64 < MININT)
7955                         is64Bit = true;
7956                   }
7957                   else
7958                   {
7959                      if(ui64 > MAXINT)
7960                      {
7961                         if(ui64 > MAXDWORD)
7962                         {
7963                            is64Bit = true;
7964                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7965                               isSigned = true;
7966                         }
7967                      }
7968                      else if(constant[0] != '0' || !constant[1])
7969                         isSigned = true;
7970                   }
7971                   type.kind = is64Bit ? int64Type : intType;
7972                   type.isSigned = isSigned;
7973                }
7974             }
7975             exp.isConstant = true;
7976             if(exp.destType && exp.destType.kind == doubleType)
7977                type.kind = doubleType;
7978             else if(exp.destType && exp.destType.kind == floatType)
7979                type.kind = floatType;
7980             else if(exp.destType && exp.destType.kind == int64Type)
7981                type.kind = int64Type;
7982          }
7983          break;
7984       }
7985       case stringExp:
7986       {
7987          exp.isConstant = true;      // Why wasn't this constant?
7988          exp.expType = Type
7989          {
7990             refCount = 1;
7991             kind = pointerType;
7992             type = Type
7993             {
7994                refCount = 1;
7995                kind = charType;
7996                constant = true;
7997                isSigned = true;
7998             }
7999          };
8000          break;
8001       }
8002       case newExp:
8003       case new0Exp:
8004          ProcessExpressionType(exp._new.size);
8005          exp.expType = Type
8006          {
8007             refCount = 1;
8008             kind = pointerType;
8009             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8010          };
8011          DeclareType(exp.expType.type, false, false);
8012          break;
8013       case renewExp:
8014       case renew0Exp:
8015          ProcessExpressionType(exp._renew.size);
8016          ProcessExpressionType(exp._renew.exp);
8017          exp.expType = Type
8018          {
8019             refCount = 1;
8020             kind = pointerType;
8021             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8022          };
8023          DeclareType(exp.expType.type, false, false);
8024          break;
8025       case opExp:
8026       {
8027          bool assign = false, boolResult = false, boolOps = false;
8028          Type type1 = null, type2 = null;
8029          bool useDestType = false, useSideType = false;
8030          Location oldyylloc = yylloc;
8031          bool useSideUnit = false;
8032          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8033
8034          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8035          Type dummy
8036          {
8037             count = 1;
8038             refCount = 1;
8039          };
8040
8041          switch(exp.op.op)
8042          {
8043             // Assignment Operators
8044             case '=':
8045             case MUL_ASSIGN:
8046             case DIV_ASSIGN:
8047             case MOD_ASSIGN:
8048             case ADD_ASSIGN:
8049             case SUB_ASSIGN:
8050             case LEFT_ASSIGN:
8051             case RIGHT_ASSIGN:
8052             case AND_ASSIGN:
8053             case XOR_ASSIGN:
8054             case OR_ASSIGN:
8055                assign = true;
8056                break;
8057             // boolean Operators
8058             case '!':
8059                // Expect boolean operators
8060                //boolOps = true;
8061                //boolResult = true;
8062                break;
8063             case AND_OP:
8064             case OR_OP:
8065                // Expect boolean operands
8066                boolOps = true;
8067                boolResult = true;
8068                break;
8069             // Comparisons
8070             case EQ_OP:
8071             case '<':
8072             case '>':
8073             case LE_OP:
8074             case GE_OP:
8075             case NE_OP:
8076                // Gives boolean result
8077                boolResult = true;
8078                useSideType = true;
8079                break;
8080             case '+':
8081             case '-':
8082                useSideUnit = true;
8083                useSideType = true;
8084                useDestType = true;
8085                break;
8086
8087             case LEFT_OP:
8088             case RIGHT_OP:
8089                useSideType = true;
8090                useDestType = true;
8091                break;
8092
8093             case '|':
8094             case '^':
8095                useSideType = true;
8096                useDestType = true;
8097                break;
8098
8099             case '/':
8100             case '%':
8101                useSideType = true;
8102                useDestType = true;
8103                break;
8104             case '&':
8105             case '*':
8106                if(exp.op.exp1)
8107                {
8108                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8109                   useSideType = true;
8110                   useDestType = true;
8111                }
8112                break;
8113
8114             /*// Implement speed etc.
8115             case '*':
8116             case '/':
8117                break;
8118             */
8119          }
8120          if(exp.op.op == '&')
8121          {
8122             // Added this here earlier for Iterator address as key
8123             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8124             {
8125                Identifier id = exp.op.exp2.identifier;
8126                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8127                if(symbol && symbol.isIterator == 2)
8128                {
8129                   exp.type = memberExp;
8130                   exp.member.exp = exp.op.exp2;
8131                   exp.member.member = MkIdentifier("key");
8132                   exp.expType = null;
8133                   exp.op.exp2.expType = symbol.type;
8134                   symbol.type.refCount++;
8135                   ProcessExpressionType(exp);
8136                   FreeType(dummy);
8137                   break;
8138                }
8139                // exp.op.exp2.usage.usageRef = true;
8140             }
8141          }
8142
8143          //dummy.kind = TypeDummy;
8144          if(exp.op.exp1)
8145          {
8146             // Added this check here to use the dest type only for units derived from the base unit
8147             // So that untyped units will use the side unit as opposed to the untyped destination unit
8148             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8149             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8150                useDestType = false;
8151
8152             if(destClass && useDestType &&
8153               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8154
8155               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8156             {
8157                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8158                exp.op.exp1.destType = exp.destType;
8159                exp.op.exp1.opDestType = true;
8160                if(exp.destType)
8161                   exp.destType.refCount++;
8162             }
8163             else if(!assign)
8164             {
8165                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8166                exp.op.exp1.destType = dummy;
8167                dummy.refCount++;
8168             }
8169
8170             // TESTING THIS HERE...
8171             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8172             ProcessExpressionType(exp.op.exp1);
8173             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8174
8175             exp.op.exp1.opDestType = false;
8176
8177             // Fix for unit and ++ / --
8178             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8179                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8180             {
8181                exp.op.exp2 = MkExpConstant("1");
8182                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8183                assign = true;
8184             }
8185
8186             if(exp.op.exp1.destType == dummy)
8187             {
8188                FreeType(dummy);
8189                exp.op.exp1.destType = null;
8190             }
8191             type1 = exp.op.exp1.expType;
8192          }
8193
8194          if(exp.op.exp2)
8195          {
8196             char expString[10240];
8197             expString[0] = '\0';
8198             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8199             {
8200                if(exp.op.exp1)
8201                {
8202                   exp.op.exp2.destType = exp.op.exp1.expType;
8203                   if(exp.op.exp1.expType)
8204                      exp.op.exp1.expType.refCount++;
8205                }
8206                else
8207                {
8208                   exp.op.exp2.destType = exp.destType;
8209                   if(!exp.op.exp1 || exp.op.op != '&')
8210                      exp.op.exp2.opDestType = true;
8211                   if(exp.destType)
8212                      exp.destType.refCount++;
8213                }
8214
8215                if(type1) type1.refCount++;
8216                exp.expType = type1;
8217             }
8218             else if(assign)
8219             {
8220                if(inCompiler)
8221                   PrintExpression(exp.op.exp2, expString);
8222
8223                if(type1 && type1.kind == pointerType)
8224                {
8225                   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 ||
8226                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8227                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8228                   else if(exp.op.op == '=')
8229                   {
8230                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8231                      exp.op.exp2.destType = type1;
8232                      if(type1)
8233                         type1.refCount++;
8234                   }
8235                }
8236                else
8237                {
8238                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8239                   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/* ||
8240                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8241                   else
8242                   {
8243                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8244                      exp.op.exp2.destType = type1;
8245                      if(type1)
8246                         type1.refCount++;
8247                   }
8248                }
8249                if(type1) type1.refCount++;
8250                exp.expType = type1;
8251             }
8252             else if(destClass &&
8253                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8254                   (destClass.type == enumClass && useDestType)))
8255             {
8256                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8257                exp.op.exp2.destType = exp.destType;
8258                if(exp.op.op != '&')
8259                   exp.op.exp2.opDestType = true;
8260                if(exp.destType)
8261                   exp.destType.refCount++;
8262             }
8263             else
8264             {
8265                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8266                exp.op.exp2.destType = dummy;
8267                dummy.refCount++;
8268             }
8269
8270             // TESTING THIS HERE... (DANGEROUS)
8271             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8272                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8273             {
8274                FreeType(exp.op.exp2.destType);
8275                exp.op.exp2.destType = type1;
8276                type1.refCount++;
8277             }
8278             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8279             // Cannot lose the cast on a sizeof
8280             if(exp.op.op == SIZEOF)
8281             {
8282                Expression e = exp.op.exp2;
8283                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8284                {
8285                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8286                   {
8287                      if(e.type == extensionCompoundExp)
8288                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8289                      else
8290                         e = e.list->last;
8291                   }
8292                }
8293                if(e.type == castExp && e.cast.exp)
8294                   e.cast.exp.needCast = true;
8295             }
8296             ProcessExpressionType(exp.op.exp2);
8297             exp.op.exp2.opDestType = false;
8298             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8299
8300             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8301             {
8302                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)
8303                {
8304                   if(exp.op.op != '=' && type1.type.kind == voidType)
8305                      Compiler_Error($"void *: unknown size\n");
8306                }
8307                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||
8308                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8309                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8310                               exp.op.exp2.expType._class.registered.type == structClass ||
8311                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8312                {
8313                   if(exp.op.op == ADD_ASSIGN)
8314                      Compiler_Error($"cannot add two pointers\n");
8315                }
8316                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8317                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8318                {
8319                   if(exp.op.op == ADD_ASSIGN)
8320                      Compiler_Error($"cannot add two pointers\n");
8321                }
8322                else if(inCompiler)
8323                {
8324                   char type1String[1024];
8325                   char type2String[1024];
8326                   type1String[0] = '\0';
8327                   type2String[0] = '\0';
8328
8329                   PrintType(exp.op.exp2.expType, type1String, false, true);
8330                   PrintType(type1, type2String, false, true);
8331                   ChangeCh(expString, '\n', ' ');
8332                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8333                }
8334             }
8335
8336             if(exp.op.exp2.destType == dummy)
8337             {
8338                FreeType(dummy);
8339                exp.op.exp2.destType = null;
8340             }
8341
8342             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8343             {
8344                type2 = { };
8345                type2.refCount = 1;
8346                CopyTypeInto(type2, exp.op.exp2.expType);
8347                type2.isSigned = true;
8348             }
8349             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8350             {
8351                type2 = { kind = intType };
8352                type2.refCount = 1;
8353                type2.isSigned = true;
8354             }
8355             else
8356             {
8357                type2 = exp.op.exp2.expType;
8358                if(type2) type2.refCount++;
8359             }
8360          }
8361
8362          dummy.kind = voidType;
8363
8364          if(exp.op.op == SIZEOF)
8365          {
8366             exp.expType = Type
8367             {
8368                refCount = 1;
8369                kind = intSizeType;
8370             };
8371             exp.isConstant = true;
8372          }
8373          // Get type of dereferenced pointer
8374          else if(exp.op.op == '*' && !exp.op.exp1)
8375          {
8376             exp.expType = Dereference(type2);
8377             if(type2 && type2.kind == classType)
8378                notByReference = true;
8379          }
8380          else if(exp.op.op == '&' && !exp.op.exp1)
8381             exp.expType = Reference(type2);
8382          else if(!assign)
8383          {
8384             if(boolOps)
8385             {
8386                if(exp.op.exp1)
8387                {
8388                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8389                   exp.op.exp1.destType = MkClassType("bool");
8390                   exp.op.exp1.destType.truth = true;
8391                   if(!exp.op.exp1.expType)
8392                      ProcessExpressionType(exp.op.exp1);
8393                   else
8394                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8395                   FreeType(exp.op.exp1.expType);
8396                   exp.op.exp1.expType = MkClassType("bool");
8397                   exp.op.exp1.expType.truth = true;
8398                }
8399                if(exp.op.exp2)
8400                {
8401                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8402                   exp.op.exp2.destType = MkClassType("bool");
8403                   exp.op.exp2.destType.truth = true;
8404                   if(!exp.op.exp2.expType)
8405                      ProcessExpressionType(exp.op.exp2);
8406                   else
8407                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8408                   FreeType(exp.op.exp2.expType);
8409                   exp.op.exp2.expType = MkClassType("bool");
8410                   exp.op.exp2.expType.truth = true;
8411                }
8412             }
8413             else if(exp.op.exp1 && exp.op.exp2 &&
8414                ((useSideType /*&&
8415                      (useSideUnit ||
8416                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8417                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8418                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8419                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8420             {
8421                if(type1 && type2 &&
8422                   // If either both are class or both are not class
8423                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8424                {
8425                   // Added this check for enum subtraction to result in an int type:
8426                   if(exp.op.op == '-' &&
8427                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8428                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8429                   {
8430                      Type intType;
8431                      if(!type1._class.registered.dataType)
8432                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8433                      if(!type2._class.registered.dataType)
8434                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8435
8436                      intType = ProcessTypeString(
8437                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8438
8439                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8440                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8441                      exp.op.exp1.destType = intType;
8442                      exp.op.exp2.destType = intType;
8443                      intType.refCount++;
8444                   }
8445                   else
8446                   {
8447                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8448                      exp.op.exp2.destType = type1;
8449                      type1.refCount++;
8450                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8451                      exp.op.exp1.destType = type2;
8452                      type2.refCount++;
8453                   }
8454
8455                   // Warning here for adding Radians + Degrees with no destination type
8456                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8457                      type1._class.registered && type1._class.registered.type == unitClass &&
8458                      type2._class.registered && type2._class.registered.type == unitClass &&
8459                      type1._class.registered != type2._class.registered)
8460                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8461                         type1._class.string, type2._class.string, type1._class.string);
8462
8463                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8464                   {
8465                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8466                      if(argExp)
8467                      {
8468                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8469
8470                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8471                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8472                            exp.op.exp1)));
8473
8474                         ProcessExpressionType(exp.op.exp1);
8475
8476                         if(type2.kind != pointerType)
8477                         {
8478                            ProcessExpressionType(classExp);
8479
8480                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8481                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8482                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8483                                  // noHeadClass
8484                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8485                                     OR_OP,
8486                                  // normalClass
8487                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8488                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8489                                        MkPointer(null, null), null)))),
8490                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8491
8492                            if(!exp.op.exp2.expType)
8493                            {
8494                               if(type2)
8495                                  FreeType(type2);
8496                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8497                               type2.refCount++;
8498                            }
8499
8500                            ProcessExpressionType(exp.op.exp2);
8501                         }
8502                      }
8503                   }
8504
8505                   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)))
8506                   {
8507                      if(type1.kind != classType && type1.type.kind == voidType)
8508                         Compiler_Error($"void *: unknown size\n");
8509                      exp.expType = type1;
8510                      if(type1) type1.refCount++;
8511                   }
8512                   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)))
8513                   {
8514                      if(type2.kind != classType && type2.type.kind == voidType)
8515                         Compiler_Error($"void *: unknown size\n");
8516                      exp.expType = type2;
8517                      if(type2) type2.refCount++;
8518                   }
8519                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8520                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8521                   {
8522                      Compiler_Warning($"different levels of indirection\n");
8523                   }
8524                   else
8525                   {
8526                      bool success = false;
8527                      if(type1.kind == pointerType && type2.kind == pointerType)
8528                      {
8529                         if(exp.op.op == '+')
8530                            Compiler_Error($"cannot add two pointers\n");
8531                         else if(exp.op.op == '-')
8532                         {
8533                            // Pointer Subtraction gives integer
8534                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8535                            {
8536                               exp.expType = Type
8537                               {
8538                                  kind = intType;
8539                                  refCount = 1;
8540                               };
8541                               success = true;
8542
8543                               if(type1.type.kind == templateType)
8544                               {
8545                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8546                                  if(argExp)
8547                                  {
8548                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8549
8550                                     ProcessExpressionType(classExp);
8551
8552                                     exp.type = bracketsExp;
8553                                     exp.list = MkListOne(MkExpOp(
8554                                        MkExpBrackets(MkListOne(MkExpOp(
8555                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8556                                              , exp.op.op,
8557                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8558
8559                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8560
8561                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8562                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8563                                                 // noHeadClass
8564                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8565                                                    OR_OP,
8566                                                 // normalClass
8567                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8568                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8569                                                       MkPointer(null, null), null)))),
8570                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8571
8572
8573                                              ));
8574
8575                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8576                                     FreeType(dummy);
8577                                     return;
8578                                  }
8579                               }
8580                            }
8581                         }
8582                      }
8583
8584                      if(!success && exp.op.exp1.type == constantExp)
8585                      {
8586                         // If first expression is constant, try to match that first
8587                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8588                         {
8589                            if(exp.expType) FreeType(exp.expType);
8590                            exp.expType = exp.op.exp1.destType;
8591                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8592                            success = true;
8593                         }
8594                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8595                         {
8596                            if(exp.expType) FreeType(exp.expType);
8597                            exp.expType = exp.op.exp2.destType;
8598                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8599                            success = true;
8600                         }
8601                      }
8602                      else if(!success)
8603                      {
8604                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8605                         {
8606                            if(exp.expType) FreeType(exp.expType);
8607                            exp.expType = exp.op.exp2.destType;
8608                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8609                            success = true;
8610                         }
8611                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8612                         {
8613                            if(exp.expType) FreeType(exp.expType);
8614                            exp.expType = exp.op.exp1.destType;
8615                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8616                            success = true;
8617                         }
8618                      }
8619                      if(!success)
8620                      {
8621                         char expString1[10240];
8622                         char expString2[10240];
8623                         char type1[1024];
8624                         char type2[1024];
8625                         expString1[0] = '\0';
8626                         expString2[0] = '\0';
8627                         type1[0] = '\0';
8628                         type2[0] = '\0';
8629                         if(inCompiler)
8630                         {
8631                            PrintExpression(exp.op.exp1, expString1);
8632                            ChangeCh(expString1, '\n', ' ');
8633                            PrintExpression(exp.op.exp2, expString2);
8634                            ChangeCh(expString2, '\n', ' ');
8635                            PrintType(exp.op.exp1.expType, type1, false, true);
8636                            PrintType(exp.op.exp2.expType, type2, false, true);
8637                         }
8638
8639                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8640                      }
8641                   }
8642                }
8643                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8644                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8645                {
8646                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8647                   // Convert e.g. / 4 into / 4.0
8648                   exp.op.exp1.destType = type2._class.registered.dataType;
8649                   if(type2._class.registered.dataType)
8650                      type2._class.registered.dataType.refCount++;
8651                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8652                   exp.expType = type2;
8653                   if(type2) type2.refCount++;
8654                }
8655                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8656                {
8657                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8658                   // Convert e.g. / 4 into / 4.0
8659                   exp.op.exp2.destType = type1._class.registered.dataType;
8660                   if(type1._class.registered.dataType)
8661                      type1._class.registered.dataType.refCount++;
8662                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8663                   exp.expType = type1;
8664                   if(type1) type1.refCount++;
8665                }
8666                else if(type1)
8667                {
8668                   bool valid = false;
8669
8670                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8671                   {
8672                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8673
8674                      if(!type1._class.registered.dataType)
8675                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8676                      exp.op.exp2.destType = type1._class.registered.dataType;
8677                      exp.op.exp2.destType.refCount++;
8678
8679                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8680                      if(type2)
8681                         FreeType(type2);
8682                      type2 = exp.op.exp2.destType;
8683                      if(type2) type2.refCount++;
8684
8685                      exp.expType = type2;
8686                      type2.refCount++;
8687                   }
8688
8689                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8690                   {
8691                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8692
8693                      if(!type2._class.registered.dataType)
8694                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8695                      exp.op.exp1.destType = type2._class.registered.dataType;
8696                      exp.op.exp1.destType.refCount++;
8697
8698                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8699                      type1 = exp.op.exp1.destType;
8700                      exp.expType = type1;
8701                      type1.refCount++;
8702                   }
8703
8704                   // TESTING THIS NEW CODE
8705                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8706                   {
8707                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8708                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8709                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8710                      {
8711                         // Convert the enum to an int instead for these operators
8712                         if(op1IsEnum && exp.op.exp2.expType)
8713                         {
8714                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8715                            {
8716                               if(exp.expType) FreeType(exp.expType);
8717                               exp.expType = exp.op.exp2.expType;
8718                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8719                               valid = true;
8720                            }
8721                         }
8722                         else if(op2IsEnum && exp.op.exp1.expType)
8723                         {
8724                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8725                            {
8726                               if(exp.expType) FreeType(exp.expType);
8727                               exp.expType = exp.op.exp1.expType;
8728                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8729                               valid = true;
8730                            }
8731                         }
8732                      }
8733                      else
8734                      {
8735                         if(op1IsEnum && exp.op.exp2.expType)
8736                         {
8737                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8738                            {
8739                               if(exp.expType) FreeType(exp.expType);
8740                               exp.expType = exp.op.exp1.expType;
8741                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8742                               valid = true;
8743                            }
8744                         }
8745                         else if(op2IsEnum && exp.op.exp1.expType)
8746                         {
8747                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8748                            {
8749                               if(exp.expType) FreeType(exp.expType);
8750                               exp.expType = exp.op.exp2.expType;
8751                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8752                               valid = true;
8753                            }
8754                         }
8755                      }
8756                   }
8757
8758                   if(!valid)
8759                   {
8760                      // 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
8761                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
8762                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
8763                      {
8764                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8765                         exp.op.exp1.destType = type2;
8766                         type2.refCount++;
8767
8768                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8769                         {
8770                            if(exp.expType) FreeType(exp.expType);
8771                            exp.expType = exp.op.exp1.destType;
8772                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8773                         }
8774                      }
8775                      else
8776                      {
8777                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8778                         exp.op.exp2.destType = type1;
8779                         type1.refCount++;
8780
8781                      /*
8782                      // Maybe this was meant to be an enum...
8783                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8784                      {
8785                         Type oldType = exp.op.exp2.expType;
8786                         exp.op.exp2.expType = null;
8787                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8788                            FreeType(oldType);
8789                         else
8790                            exp.op.exp2.expType = oldType;
8791                      }
8792                      */
8793
8794                      /*
8795                      // TESTING THIS HERE... LATEST ADDITION
8796                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8797                      {
8798                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8799                         exp.op.exp2.destType = type2._class.registered.dataType;
8800                         if(type2._class.registered.dataType)
8801                            type2._class.registered.dataType.refCount++;
8802                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8803
8804                         //exp.expType = type2._class.registered.dataType; //type2;
8805                         //if(type2) type2.refCount++;
8806                      }
8807
8808                      // TESTING THIS HERE... LATEST ADDITION
8809                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8810                      {
8811                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8812                         exp.op.exp1.destType = type1._class.registered.dataType;
8813                         if(type1._class.registered.dataType)
8814                            type1._class.registered.dataType.refCount++;
8815                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8816                         exp.expType = type1._class.registered.dataType; //type1;
8817                         if(type1) type1.refCount++;
8818                      }
8819                      */
8820
8821                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8822                         {
8823                            if(exp.expType) FreeType(exp.expType);
8824                            exp.expType = exp.op.exp2.destType;
8825                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8826                         }
8827                         else if(type1 && type2)
8828                         {
8829                            char expString1[10240];
8830                            char expString2[10240];
8831                            char type1String[1024];
8832                            char type2String[1024];
8833                            expString1[0] = '\0';
8834                            expString2[0] = '\0';
8835                            type1String[0] = '\0';
8836                            type2String[0] = '\0';
8837                            if(inCompiler)
8838                            {
8839                               PrintExpression(exp.op.exp1, expString1);
8840                               ChangeCh(expString1, '\n', ' ');
8841                               PrintExpression(exp.op.exp2, expString2);
8842                               ChangeCh(expString2, '\n', ' ');
8843                               PrintType(exp.op.exp1.expType, type1String, false, true);
8844                               PrintType(exp.op.exp2.expType, type2String, false, true);
8845                            }
8846
8847                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8848
8849                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8850                            {
8851                               exp.expType = exp.op.exp1.expType;
8852                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8853                            }
8854                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8855                            {
8856                               exp.expType = exp.op.exp2.expType;
8857                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8858                            }
8859                         }
8860                      }
8861                   }
8862                }
8863                else if(type2)
8864                {
8865                   // Maybe this was meant to be an enum...
8866                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8867                   {
8868                      Type oldType = exp.op.exp1.expType;
8869                      exp.op.exp1.expType = null;
8870                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8871                         FreeType(oldType);
8872                      else
8873                         exp.op.exp1.expType = oldType;
8874                   }
8875
8876                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8877                   exp.op.exp1.destType = type2;
8878                   type2.refCount++;
8879                   /*
8880                   // TESTING THIS HERE... LATEST ADDITION
8881                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8882                   {
8883                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8884                      exp.op.exp1.destType = type1._class.registered.dataType;
8885                      if(type1._class.registered.dataType)
8886                         type1._class.registered.dataType.refCount++;
8887                   }
8888
8889                   // TESTING THIS HERE... LATEST ADDITION
8890                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8891                   {
8892                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8893                      exp.op.exp2.destType = type2._class.registered.dataType;
8894                      if(type2._class.registered.dataType)
8895                         type2._class.registered.dataType.refCount++;
8896                   }
8897                   */
8898
8899                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8900                   {
8901                      if(exp.expType) FreeType(exp.expType);
8902                      exp.expType = exp.op.exp1.destType;
8903                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8904                   }
8905                }
8906             }
8907             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8908             {
8909                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8910                {
8911                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8912                   // Convert e.g. / 4 into / 4.0
8913                   exp.op.exp1.destType = type2._class.registered.dataType;
8914                   if(type2._class.registered.dataType)
8915                      type2._class.registered.dataType.refCount++;
8916                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8917                }
8918                if(exp.op.op == '!')
8919                {
8920                   exp.expType = MkClassType("bool");
8921                   exp.expType.truth = true;
8922                }
8923                else
8924                {
8925                   exp.expType = type2;
8926                   if(type2) type2.refCount++;
8927                }
8928             }
8929             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8930             {
8931                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8932                {
8933                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8934                   // Convert e.g. / 4 into / 4.0
8935                   exp.op.exp2.destType = type1._class.registered.dataType;
8936                   if(type1._class.registered.dataType)
8937                      type1._class.registered.dataType.refCount++;
8938                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8939                }
8940                exp.expType = type1;
8941                if(type1) type1.refCount++;
8942             }
8943          }
8944
8945          yylloc = exp.loc;
8946          if(exp.op.exp1 && !exp.op.exp1.expType)
8947          {
8948             char expString[10000];
8949             expString[0] = '\0';
8950             if(inCompiler)
8951             {
8952                PrintExpression(exp.op.exp1, expString);
8953                ChangeCh(expString, '\n', ' ');
8954             }
8955             if(expString[0])
8956                Compiler_Error($"couldn't determine type of %s\n", expString);
8957          }
8958          if(exp.op.exp2 && !exp.op.exp2.expType)
8959          {
8960             char expString[10240];
8961             expString[0] = '\0';
8962             if(inCompiler)
8963             {
8964                PrintExpression(exp.op.exp2, expString);
8965                ChangeCh(expString, '\n', ' ');
8966             }
8967             if(expString[0])
8968                Compiler_Error($"couldn't determine type of %s\n", expString);
8969          }
8970
8971          if(boolResult)
8972          {
8973             FreeType(exp.expType);
8974             exp.expType = MkClassType("bool");
8975             exp.expType.truth = true;
8976          }
8977
8978          if(exp.op.op != SIZEOF)
8979             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8980                (!exp.op.exp2 || exp.op.exp2.isConstant);
8981
8982          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8983          {
8984             DeclareType(exp.op.exp2.expType, false, false);
8985          }
8986
8987          yylloc = oldyylloc;
8988
8989          FreeType(dummy);
8990          if(type2)
8991             FreeType(type2);
8992          break;
8993       }
8994       case bracketsExp:
8995       case extensionExpressionExp:
8996       {
8997          Expression e;
8998          exp.isConstant = true;
8999          for(e = exp.list->first; e; e = e.next)
9000          {
9001             bool inced = false;
9002             if(!e.next)
9003             {
9004                FreeType(e.destType);
9005                e.opDestType = exp.opDestType;
9006                e.destType = exp.destType;
9007                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
9008             }
9009             ProcessExpressionType(e);
9010             if(inced)
9011                exp.destType.count--;
9012             if(!exp.expType && !e.next)
9013             {
9014                exp.expType = e.expType;
9015                if(e.expType) e.expType.refCount++;
9016             }
9017             if(!e.isConstant)
9018                exp.isConstant = false;
9019          }
9020
9021          // In case a cast became a member...
9022          e = exp.list->first;
9023          if(!e.next && e.type == memberExp)
9024          {
9025             // Preserve prev, next
9026             Expression next = exp.next, prev = exp.prev;
9027
9028
9029             FreeType(exp.expType);
9030             FreeType(exp.destType);
9031             delete exp.list;
9032
9033             *exp = *e;
9034
9035             exp.prev = prev;
9036             exp.next = next;
9037
9038             delete e;
9039
9040             ProcessExpressionType(exp);
9041          }
9042          break;
9043       }
9044       case indexExp:
9045       {
9046          Expression e;
9047          exp.isConstant = true;
9048
9049          ProcessExpressionType(exp.index.exp);
9050          if(!exp.index.exp.isConstant)
9051             exp.isConstant = false;
9052
9053          if(exp.index.exp.expType)
9054          {
9055             Type source = exp.index.exp.expType;
9056             if(source.kind == classType && source._class && source._class.registered)
9057             {
9058                Class _class = source._class.registered;
9059                Class c = _class.templateClass ? _class.templateClass : _class;
9060                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9061                {
9062                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9063
9064                   if(exp.index.index && exp.index.index->last)
9065                   {
9066                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9067                   }
9068                }
9069             }
9070          }
9071
9072          for(e = exp.index.index->first; e; e = e.next)
9073          {
9074             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9075             {
9076                if(e.destType) FreeType(e.destType);
9077                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9078             }
9079             ProcessExpressionType(e);
9080             if(!e.next)
9081             {
9082                // Check if this type is int
9083             }
9084             if(!e.isConstant)
9085                exp.isConstant = false;
9086          }
9087
9088          if(!exp.expType)
9089             exp.expType = Dereference(exp.index.exp.expType);
9090          if(exp.expType)
9091             DeclareType(exp.expType, false, false);
9092          break;
9093       }
9094       case callExp:
9095       {
9096          Expression e;
9097          Type functionType;
9098          Type methodType = null;
9099          char name[1024];
9100          name[0] = '\0';
9101
9102          if(inCompiler)
9103          {
9104             PrintExpression(exp.call.exp,  name);
9105             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9106             {
9107                //exp.call.exp.expType = null;
9108                PrintExpression(exp.call.exp,  name);
9109             }
9110          }
9111          if(exp.call.exp.type == identifierExp)
9112          {
9113             Expression idExp = exp.call.exp;
9114             Identifier id = idExp.identifier;
9115             if(!strcmp(id.string, "__builtin_frame_address"))
9116             {
9117                exp.expType = ProcessTypeString("void *", true);
9118                if(exp.call.arguments && exp.call.arguments->first)
9119                   ProcessExpressionType(exp.call.arguments->first);
9120                break;
9121             }
9122             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9123             {
9124                exp.expType = ProcessTypeString("int", true);
9125                if(exp.call.arguments && exp.call.arguments->first)
9126                   ProcessExpressionType(exp.call.arguments->first);
9127                break;
9128             }
9129             else if(!strcmp(id.string, "Max") ||
9130                !strcmp(id.string, "Min") ||
9131                !strcmp(id.string, "Sgn") ||
9132                !strcmp(id.string, "Abs"))
9133             {
9134                Expression a = null;
9135                Expression b = null;
9136                Expression tempExp1 = null, tempExp2 = null;
9137                if((!strcmp(id.string, "Max") ||
9138                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9139                {
9140                   a = exp.call.arguments->first;
9141                   b = exp.call.arguments->last;
9142                   tempExp1 = a;
9143                   tempExp2 = b;
9144                }
9145                else if(exp.call.arguments->count == 1)
9146                {
9147                   a = exp.call.arguments->first;
9148                   tempExp1 = a;
9149                }
9150
9151                if(a)
9152                {
9153                   exp.call.arguments->Clear();
9154                   idExp.identifier = null;
9155
9156                   FreeExpContents(exp);
9157
9158                   ProcessExpressionType(a);
9159                   if(b)
9160                      ProcessExpressionType(b);
9161
9162                   exp.type = bracketsExp;
9163                   exp.list = MkList();
9164
9165                   if(a.expType && (!b || b.expType))
9166                   {
9167                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9168                      {
9169                         // Use the simpleStruct name/ids for now...
9170                         if(inCompiler)
9171                         {
9172                            OldList * specs = MkList();
9173                            OldList * decls = MkList();
9174                            Declaration decl;
9175                            char temp1[1024], temp2[1024];
9176
9177                            GetTypeSpecs(a.expType, specs);
9178
9179                            if(a && !a.isConstant && a.type != identifierExp)
9180                            {
9181                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9182                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9183                               tempExp1 = QMkExpId(temp1);
9184                               tempExp1.expType = a.expType;
9185                               if(a.expType)
9186                                  a.expType.refCount++;
9187                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9188                            }
9189                            if(b && !b.isConstant && b.type != identifierExp)
9190                            {
9191                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9192                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9193                               tempExp2 = QMkExpId(temp2);
9194                               tempExp2.expType = b.expType;
9195                               if(b.expType)
9196                                  b.expType.refCount++;
9197                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9198                            }
9199
9200                            decl = MkDeclaration(specs, decls);
9201                            if(!curCompound.compound.declarations)
9202                               curCompound.compound.declarations = MkList();
9203                            curCompound.compound.declarations->Insert(null, decl);
9204                         }
9205                      }
9206                   }
9207
9208                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9209                   {
9210                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9211                      ListAdd(exp.list,
9212                         MkExpCondition(MkExpBrackets(MkListOne(
9213                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9214                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9215                      exp.expType = a.expType;
9216                      if(a.expType)
9217                         a.expType.refCount++;
9218                   }
9219                   else if(!strcmp(id.string, "Abs"))
9220                   {
9221                      ListAdd(exp.list,
9222                         MkExpCondition(MkExpBrackets(MkListOne(
9223                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9224                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9225                      exp.expType = a.expType;
9226                      if(a.expType)
9227                         a.expType.refCount++;
9228                   }
9229                   else if(!strcmp(id.string, "Sgn"))
9230                   {
9231                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9232                      ListAdd(exp.list,
9233                         MkExpCondition(MkExpBrackets(MkListOne(
9234                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9235                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9236                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9237                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9238                      exp.expType = ProcessTypeString("int", false);
9239                   }
9240
9241                   FreeExpression(tempExp1);
9242                   if(tempExp2) FreeExpression(tempExp2);
9243
9244                   FreeIdentifier(id);
9245                   break;
9246                }
9247             }
9248          }
9249
9250          {
9251             Type dummy
9252             {
9253                count = 1;
9254                refCount = 1;
9255             };
9256             if(!exp.call.exp.destType)
9257             {
9258                exp.call.exp.destType = dummy;
9259                dummy.refCount++;
9260             }
9261             ProcessExpressionType(exp.call.exp);
9262             if(exp.call.exp.destType == dummy)
9263             {
9264                FreeType(dummy);
9265                exp.call.exp.destType = null;
9266             }
9267             FreeType(dummy);
9268          }
9269
9270          // Check argument types against parameter types
9271          functionType = exp.call.exp.expType;
9272
9273          if(functionType && functionType.kind == TypeKind::methodType)
9274          {
9275             methodType = functionType;
9276             functionType = methodType.method.dataType;
9277
9278             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9279             // TOCHECK: Instead of doing this here could this be done per param?
9280             if(exp.call.exp.expType.usedClass)
9281             {
9282                char typeString[1024];
9283                typeString[0] = '\0';
9284                {
9285                   Symbol back = functionType.thisClass;
9286                   // Do not output class specifier here (thisclass was added to this)
9287                   functionType.thisClass = null;
9288                   PrintType(functionType, typeString, true, true);
9289                   functionType.thisClass = back;
9290                }
9291                if(strstr(typeString, "thisclass"))
9292                {
9293                   OldList * specs = MkList();
9294                   Declarator decl;
9295                   {
9296                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9297
9298                      decl = SpecDeclFromString(typeString, specs, null);
9299
9300                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9301                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9302                         exp.call.exp.expType.usedClass))
9303                         thisClassParams = false;
9304
9305                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9306                      {
9307                         Class backupThisClass = thisClass;
9308                         thisClass = exp.call.exp.expType.usedClass;
9309                         ProcessDeclarator(decl);
9310                         thisClass = backupThisClass;
9311                      }
9312
9313                      thisClassParams = true;
9314
9315                      functionType = ProcessType(specs, decl);
9316                      functionType.refCount = 0;
9317                      FinishTemplatesContext(context);
9318                   }
9319
9320                   FreeList(specs, FreeSpecifier);
9321                   FreeDeclarator(decl);
9322                 }
9323             }
9324          }
9325          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9326          {
9327             Type type = functionType.type;
9328             if(!functionType.refCount)
9329             {
9330                functionType.type = null;
9331                FreeType(functionType);
9332             }
9333             //methodType = functionType;
9334             functionType = type;
9335          }
9336          if(functionType && functionType.kind != TypeKind::functionType)
9337          {
9338             Compiler_Error($"called object %s is not a function\n", name);
9339          }
9340          else if(functionType)
9341          {
9342             bool emptyParams = false, noParams = false;
9343             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9344             Type type = functionType.params.first;
9345             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9346             int extra = 0;
9347             Location oldyylloc = yylloc;
9348
9349             if(!type) emptyParams = true;
9350
9351             // WORKING ON THIS:
9352             if(functionType.extraParam && e && functionType.thisClass)
9353             {
9354                e.destType = MkClassType(functionType.thisClass.string);
9355                e = e.next;
9356             }
9357
9358             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9359             // Fixed #141 by adding '&& !functionType.extraParam'
9360             if(!functionType.staticMethod && !functionType.extraParam)
9361             {
9362                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9363                   memberExp.member.exp.expType._class)
9364                {
9365                   type = MkClassType(memberExp.member.exp.expType._class.string);
9366                   if(e)
9367                   {
9368                      e.destType = type;
9369                      e = e.next;
9370                      type = functionType.params.first;
9371                   }
9372                   else
9373                      type.refCount = 0;
9374                }
9375                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9376                {
9377                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9378                   type.byReference = functionType.byReference;
9379                   type.typedByReference = functionType.typedByReference;
9380                   if(e)
9381                   {
9382                      // Allow manually passing a class for typed object
9383                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9384                         e = e.next;
9385                      e.destType = type;
9386                      e = e.next;
9387                      type = functionType.params.first;
9388                   }
9389                   else
9390                      type.refCount = 0;
9391                   //extra = 1;
9392                }
9393             }
9394
9395             if(type && type.kind == voidType)
9396             {
9397                noParams = true;
9398                if(!type.refCount) FreeType(type);
9399                type = null;
9400             }
9401
9402             for( ; e; e = e.next)
9403             {
9404                if(!type && !emptyParams)
9405                {
9406                   yylloc = e.loc;
9407                   if(methodType && methodType.methodClass)
9408                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9409                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9410                         noParams ? 0 : functionType.params.count);
9411                   else
9412                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9413                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9414                         noParams ? 0 : functionType.params.count);
9415                   break;
9416                }
9417
9418                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9419                {
9420                   Type templatedType = null;
9421                   Class _class = methodType.usedClass;
9422                   ClassTemplateParameter curParam = null;
9423                   int id = 0;
9424                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9425                   {
9426                      Class sClass;
9427                      for(sClass = _class; sClass; sClass = sClass.base)
9428                      {
9429                         if(sClass.templateClass) sClass = sClass.templateClass;
9430                         id = 0;
9431                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9432                         {
9433                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9434                            {
9435                               Class nextClass;
9436                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9437                               {
9438                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9439                                  id += nextClass.templateParams.count;
9440                               }
9441                               break;
9442                            }
9443                            id++;
9444                         }
9445                         if(curParam) break;
9446                      }
9447                   }
9448                   if(curParam && _class.templateArgs[id].dataTypeString)
9449                   {
9450                      ClassTemplateArgument arg = _class.templateArgs[id];
9451                      {
9452                         Context context = SetupTemplatesContext(_class);
9453
9454                         /*if(!arg.dataType)
9455                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9456                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9457                         FinishTemplatesContext(context);
9458                      }
9459                      e.destType = templatedType;
9460                      if(templatedType)
9461                      {
9462                         templatedType.passAsTemplate = true;
9463                         // templatedType.refCount++;
9464                      }
9465                   }
9466                   else
9467                   {
9468                      e.destType = type;
9469                      if(type) type.refCount++;
9470                   }
9471                }
9472                else
9473                {
9474                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9475                   {
9476                      e.destType = type.prev;
9477                      e.destType.refCount++;
9478                   }
9479                   else
9480                   {
9481                      e.destType = type;
9482                      if(type) type.refCount++;
9483                   }
9484                }
9485                // Don't reach the end for the ellipsis
9486                if(type && type.kind != ellipsisType)
9487                {
9488                   Type next = type.next;
9489                   if(!type.refCount) FreeType(type);
9490                   type = next;
9491                }
9492             }
9493
9494             if(type && type.kind != ellipsisType)
9495             {
9496                if(methodType && methodType.methodClass)
9497                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9498                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9499                      functionType.params.count + extra);
9500                else
9501                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9502                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9503                      functionType.params.count + extra);
9504             }
9505             yylloc = oldyylloc;
9506             if(type && !type.refCount) FreeType(type);
9507          }
9508          else
9509          {
9510             functionType = Type
9511             {
9512                refCount = 0;
9513                kind = TypeKind::functionType;
9514             };
9515
9516             if(exp.call.exp.type == identifierExp)
9517             {
9518                char * string = exp.call.exp.identifier.string;
9519                if(inCompiler)
9520                {
9521                   Symbol symbol;
9522                   Location oldyylloc = yylloc;
9523
9524                   yylloc = exp.call.exp.identifier.loc;
9525                   if(strstr(string, "__builtin_") == string)
9526                   {
9527                      if(exp.destType)
9528                      {
9529                         functionType.returnType = exp.destType;
9530                         exp.destType.refCount++;
9531                      }
9532                   }
9533                   else
9534                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9535                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9536                   globalContext.symbols.Add((BTNode)symbol);
9537                   if(strstr(symbol.string, "::"))
9538                      globalContext.hasNameSpace = true;
9539
9540                   yylloc = oldyylloc;
9541                }
9542             }
9543             else if(exp.call.exp.type == memberExp)
9544             {
9545                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9546                   exp.call.exp.member.member.string);*/
9547             }
9548             else
9549                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9550
9551             if(!functionType.returnType)
9552             {
9553                functionType.returnType = Type
9554                {
9555                   refCount = 1;
9556                   kind = intType;
9557                };
9558             }
9559          }
9560          if(functionType && functionType.kind == TypeKind::functionType)
9561          {
9562             exp.expType = functionType.returnType;
9563
9564             if(functionType.returnType)
9565                functionType.returnType.refCount++;
9566
9567             if(!functionType.refCount)
9568                FreeType(functionType);
9569          }
9570
9571          if(exp.call.arguments)
9572          {
9573             for(e = exp.call.arguments->first; e; e = e.next)
9574             {
9575                Type destType = e.destType;
9576                ProcessExpressionType(e);
9577             }
9578          }
9579          break;
9580       }
9581       case memberExp:
9582       {
9583          Type type;
9584          Location oldyylloc = yylloc;
9585          bool thisPtr;
9586          Expression checkExp = exp.member.exp;
9587          while(checkExp)
9588          {
9589             if(checkExp.type == castExp)
9590                checkExp = checkExp.cast.exp;
9591             else if(checkExp.type == bracketsExp)
9592                checkExp = checkExp.list ? checkExp.list->first : null;
9593             else
9594                break;
9595          }
9596
9597          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9598          exp.thisPtr = thisPtr;
9599
9600          // DOING THIS LATER NOW...
9601          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9602          {
9603             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9604             /* TODO: Name Space Fix ups
9605             if(!exp.member.member.classSym)
9606                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9607             */
9608          }
9609
9610          ProcessExpressionType(exp.member.exp);
9611          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9612             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9613          {
9614             exp.isConstant = false;
9615          }
9616          else
9617             exp.isConstant = exp.member.exp.isConstant;
9618          type = exp.member.exp.expType;
9619
9620          yylloc = exp.loc;
9621
9622          if(type && (type.kind == templateType))
9623          {
9624             Class _class = thisClass ? thisClass : currentClass;
9625             ClassTemplateParameter param = null;
9626             if(_class)
9627             {
9628                for(param = _class.templateParams.first; param; param = param.next)
9629                {
9630                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9631                      break;
9632                }
9633             }
9634             if(param && param.defaultArg.member)
9635             {
9636                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9637                if(argExp)
9638                {
9639                   Expression expMember = exp.member.exp;
9640                   Declarator decl;
9641                   OldList * specs = MkList();
9642                   char thisClassTypeString[1024];
9643
9644                   FreeIdentifier(exp.member.member);
9645
9646                   ProcessExpressionType(argExp);
9647
9648                   {
9649                      char * colon = strstr(param.defaultArg.memberString, "::");
9650                      if(colon)
9651                      {
9652                         char className[1024];
9653                         Class sClass;
9654
9655                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9656                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9657                      }
9658                      else
9659                         strcpy(thisClassTypeString, _class.fullName);
9660                   }
9661
9662                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9663
9664                   exp.expType = ProcessType(specs, decl);
9665                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9666                   {
9667                      Class expClass = exp.expType._class.registered;
9668                      Class cClass = null;
9669                      int c;
9670                      int paramCount = 0;
9671                      int lastParam = -1;
9672
9673                      char templateString[1024];
9674                      ClassTemplateParameter param;
9675                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9676                      for(cClass = expClass; cClass; cClass = cClass.base)
9677                      {
9678                         int p = 0;
9679                         for(param = cClass.templateParams.first; param; param = param.next)
9680                         {
9681                            int id = p;
9682                            Class sClass;
9683                            ClassTemplateArgument arg;
9684                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9685                            arg = expClass.templateArgs[id];
9686
9687                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9688                            {
9689                               ClassTemplateParameter cParam;
9690                               //int p = numParams - sClass.templateParams.count;
9691                               int p = 0;
9692                               Class nextClass;
9693                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9694
9695                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9696                               {
9697                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9698                                  {
9699                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9700                                     {
9701                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9702                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9703                                        break;
9704                                     }
9705                                  }
9706                               }
9707                            }
9708
9709                            {
9710                               char argument[256];
9711                               argument[0] = '\0';
9712                               /*if(arg.name)
9713                               {
9714                                  strcat(argument, arg.name.string);
9715                                  strcat(argument, " = ");
9716                               }*/
9717                               switch(param.type)
9718                               {
9719                                  case expression:
9720                                  {
9721                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9722                                     char expString[1024];
9723                                     OldList * specs = MkList();
9724                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9725                                     Expression exp;
9726                                     char * string = PrintHexUInt64(arg.expression.ui64);
9727                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9728                                     delete string;
9729
9730                                     ProcessExpressionType(exp);
9731                                     ComputeExpression(exp);
9732                                     expString[0] = '\0';
9733                                     PrintExpression(exp, expString);
9734                                     strcat(argument, expString);
9735                                     // delete exp;
9736                                     FreeExpression(exp);
9737                                     break;
9738                                  }
9739                                  case identifier:
9740                                  {
9741                                     strcat(argument, arg.member.name);
9742                                     break;
9743                                  }
9744                                  case TemplateParameterType::type:
9745                                  {
9746                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9747                                     {
9748                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9749                                           strcat(argument, thisClassTypeString);
9750                                        else
9751                                           strcat(argument, arg.dataTypeString);
9752                                     }
9753                                     break;
9754                                  }
9755                               }
9756                               if(argument[0])
9757                               {
9758                                  if(paramCount) strcat(templateString, ", ");
9759                                  if(lastParam != p - 1)
9760                                  {
9761                                     strcat(templateString, param.name);
9762                                     strcat(templateString, " = ");
9763                                  }
9764                                  strcat(templateString, argument);
9765                                  paramCount++;
9766                                  lastParam = p;
9767                               }
9768                               p++;
9769                            }
9770                         }
9771                      }
9772                      {
9773                         int len = strlen(templateString);
9774                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9775                         templateString[len++] = '>';
9776                         templateString[len++] = '\0';
9777                      }
9778                      {
9779                         Context context = SetupTemplatesContext(_class);
9780                         FreeType(exp.expType);
9781                         exp.expType = ProcessTypeString(templateString, false);
9782                         FinishTemplatesContext(context);
9783                      }
9784                   }
9785
9786                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9787                   exp.type = bracketsExp;
9788                   exp.list = MkListOne(MkExpOp(null, '*',
9789                   /*opExp;
9790                   exp.op.op = '*';
9791                   exp.op.exp1 = null;
9792                   exp.op.exp2 = */
9793                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9794                      MkExpBrackets(MkListOne(
9795                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9796                            '+',
9797                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9798                            '+',
9799                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9800
9801                            ));
9802                }
9803             }
9804             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9805                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9806             {
9807                type = ProcessTemplateParameterType(type.templateParameter);
9808             }
9809          }
9810          // TODO: *** This seems to be where we should add method support for all basic types ***
9811          if(type && (type.kind == templateType));
9812          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9813                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9814                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9815                           (type.kind == pointerType && type.type.kind == charType)))
9816          {
9817             Identifier id = exp.member.member;
9818             TypeKind typeKind = type.kind;
9819             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9820             if(typeKind == subClassType && exp.member.exp.type == classExp)
9821             {
9822                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9823                typeKind = classType;
9824             }
9825
9826             if(id)
9827             {
9828                if(typeKind == intType || typeKind == enumType)
9829                   _class = eSystem_FindClass(privateModule, "int");
9830                else if(!_class)
9831                {
9832                   if(type.kind == classType && type._class && type._class.registered)
9833                   {
9834                      _class = type._class.registered;
9835                   }
9836                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9837                   {
9838                      _class = FindClass("char *").registered;
9839                   }
9840                   else if(type.kind == pointerType)
9841                   {
9842                      _class = eSystem_FindClass(privateModule, "uintptr");
9843                      FreeType(exp.expType);
9844                      exp.expType = ProcessTypeString("uintptr", false);
9845                      exp.byReference = true;
9846                   }
9847                   else
9848                   {
9849                      char string[1024] = "";
9850                      Symbol classSym;
9851                      PrintTypeNoConst(type, string, false, true);
9852                      classSym = FindClass(string);
9853                      if(classSym) _class = classSym.registered;
9854                   }
9855                }
9856             }
9857
9858             if(_class && id)
9859             {
9860                /*bool thisPtr =
9861                   (exp.member.exp.type == identifierExp &&
9862                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9863                Property prop = null;
9864                Method method = null;
9865                DataMember member = null;
9866                Property revConvert = null;
9867                ClassProperty classProp = null;
9868
9869                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9870                   exp.member.memberType = propertyMember;
9871
9872                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9873                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9874
9875                if(typeKind != subClassType)
9876                {
9877                   // Prioritize data members over properties for "this"
9878                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9879                   {
9880                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9881                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9882                      {
9883                         prop = eClass_FindProperty(_class, id.string, privateModule);
9884                         if(prop)
9885                            member = null;
9886                      }
9887                      if(!member && !prop)
9888                         prop = eClass_FindProperty(_class, id.string, privateModule);
9889                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9890                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9891                         exp.member.thisPtr = true;
9892                   }
9893                   // Prioritize properties over data members otherwise
9894                   else
9895                   {
9896                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9897                      if(!id.classSym)
9898                      {
9899                         prop = eClass_FindProperty(_class, id.string, null);
9900                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9901                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9902                      }
9903
9904                      if(!prop && !member)
9905                      {
9906                         method = eClass_FindMethod(_class, id.string, null);
9907                         if(!method)
9908                         {
9909                            prop = eClass_FindProperty(_class, id.string, privateModule);
9910                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9911                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9912                         }
9913                      }
9914
9915                      if(member && prop)
9916                      {
9917                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9918                            prop = null;
9919                         else
9920                            member = null;
9921                      }
9922                   }
9923                }
9924                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9925                   method = eClass_FindMethod(_class, id.string, privateModule);
9926                if(!prop && !member && !method)
9927                {
9928                   if(typeKind == subClassType)
9929                   {
9930                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9931                      if(classProp)
9932                      {
9933                         exp.member.memberType = classPropertyMember;
9934                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9935                      }
9936                      else
9937                      {
9938                         // Assume this is a class_data member
9939                         char structName[1024];
9940                         Identifier id = exp.member.member;
9941                         Expression classExp = exp.member.exp;
9942                         type.refCount++;
9943
9944                         FreeType(classExp.expType);
9945                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9946
9947                         strcpy(structName, "__ecereClassData_");
9948                         FullClassNameCat(structName, type._class.string, false);
9949                         exp.type = pointerExp;
9950                         exp.member.member = id;
9951
9952                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9953                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9954                               MkExpBrackets(MkListOne(MkExpOp(
9955                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9956                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9957                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9958                                  )));
9959
9960                         FreeType(type);
9961
9962                         ProcessExpressionType(exp);
9963                         return;
9964                      }
9965                   }
9966                   else
9967                   {
9968                      // Check for reverse conversion
9969                      // (Convert in an instantiation later, so that we can use
9970                      //  deep properties system)
9971                      Symbol classSym = FindClass(id.string);
9972                      if(classSym)
9973                      {
9974                         Class convertClass = classSym.registered;
9975                         if(convertClass)
9976                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9977                      }
9978                   }
9979                }
9980
9981                if(prop)
9982                {
9983                   exp.member.memberType = propertyMember;
9984                   if(!prop.dataType)
9985                      ProcessPropertyType(prop);
9986                   exp.expType = prop.dataType;
9987                   if(prop.dataType) prop.dataType.refCount++;
9988                }
9989                else if(member)
9990                {
9991                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9992                   {
9993                      FreeExpContents(exp);
9994                      exp.type = identifierExp;
9995                      exp.identifier = MkIdentifier("class");
9996                      ProcessExpressionType(exp);
9997                      return;
9998                   }
9999
10000                   exp.member.memberType = dataMember;
10001                   DeclareStruct(_class.fullName, false);
10002                   if(!member.dataType)
10003                   {
10004                      Context context = SetupTemplatesContext(_class);
10005                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10006                      FinishTemplatesContext(context);
10007                   }
10008                   exp.expType = member.dataType;
10009                   if(member.dataType) member.dataType.refCount++;
10010                }
10011                else if(revConvert)
10012                {
10013                   exp.member.memberType = reverseConversionMember;
10014                   exp.expType = MkClassType(revConvert._class.fullName);
10015                }
10016                else if(method)
10017                {
10018                   //if(inCompiler)
10019                   {
10020                      /*if(id._class)
10021                      {
10022                         exp.type = identifierExp;
10023                         exp.identifier = exp.member.member;
10024                      }
10025                      else*/
10026                         exp.member.memberType = methodMember;
10027                   }
10028                   if(!method.dataType)
10029                      ProcessMethodType(method);
10030                   exp.expType = Type
10031                   {
10032                      refCount = 1;
10033                      kind = methodType;
10034                      method = method;
10035                   };
10036
10037                   // Tricky spot here... To use instance versus class virtual table
10038                   // Put it back to what it was... What did we break?
10039
10040                   // Had to put it back for overriding Main of Thread global instance
10041
10042                   //exp.expType.methodClass = _class;
10043                   exp.expType.methodClass = (id && id._class) ? _class : null;
10044
10045                   // Need the actual class used for templated classes
10046                   exp.expType.usedClass = _class;
10047                }
10048                else if(!classProp)
10049                {
10050                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10051                   {
10052                      FreeExpContents(exp);
10053                      exp.type = identifierExp;
10054                      exp.identifier = MkIdentifier("class");
10055                      FreeType(exp.expType);
10056                      exp.expType = MkClassType("ecere::com::Class");
10057                      return;
10058                   }
10059                   yylloc = exp.member.member.loc;
10060                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10061                   if(inCompiler)
10062                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10063                }
10064
10065                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10066                {
10067                   Class tClass;
10068
10069                   tClass = _class;
10070                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10071
10072                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10073                   {
10074                      int id = 0;
10075                      ClassTemplateParameter curParam = null;
10076                      Class sClass;
10077
10078                      for(sClass = tClass; sClass; sClass = sClass.base)
10079                      {
10080                         id = 0;
10081                         if(sClass.templateClass) sClass = sClass.templateClass;
10082                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10083                         {
10084                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10085                            {
10086                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10087                                  id += sClass.templateParams.count;
10088                               break;
10089                            }
10090                            id++;
10091                         }
10092                         if(curParam) break;
10093                      }
10094
10095                      if(curParam && tClass.templateArgs[id].dataTypeString)
10096                      {
10097                         ClassTemplateArgument arg = tClass.templateArgs[id];
10098                         Context context = SetupTemplatesContext(tClass);
10099                         /*if(!arg.dataType)
10100                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10101                         FreeType(exp.expType);
10102                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10103                         if(exp.expType)
10104                         {
10105                            if(exp.expType.kind == thisClassType)
10106                            {
10107                               FreeType(exp.expType);
10108                               exp.expType = ReplaceThisClassType(_class);
10109                            }
10110
10111                            if(tClass.templateClass)
10112                               exp.expType.passAsTemplate = true;
10113                            //exp.expType.refCount++;
10114                            if(!exp.destType)
10115                            {
10116                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10117                               //exp.destType.refCount++;
10118
10119                               if(exp.destType.kind == thisClassType)
10120                               {
10121                                  FreeType(exp.destType);
10122                                  exp.destType = ReplaceThisClassType(_class);
10123                               }
10124                            }
10125                         }
10126                         FinishTemplatesContext(context);
10127                      }
10128                   }
10129                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10130                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10131                   {
10132                      int id = 0;
10133                      ClassTemplateParameter curParam = null;
10134                      Class sClass;
10135
10136                      for(sClass = tClass; sClass; sClass = sClass.base)
10137                      {
10138                         id = 0;
10139                         if(sClass.templateClass) sClass = sClass.templateClass;
10140                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10141                         {
10142                            if(curParam.type == TemplateParameterType::type &&
10143                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10144                            {
10145                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10146                                  id += sClass.templateParams.count;
10147                               break;
10148                            }
10149                            id++;
10150                         }
10151                         if(curParam) break;
10152                      }
10153
10154                      if(curParam)
10155                      {
10156                         ClassTemplateArgument arg = tClass.templateArgs[id];
10157                         Context context = SetupTemplatesContext(tClass);
10158                         Type basicType;
10159                         /*if(!arg.dataType)
10160                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10161
10162                         basicType = ProcessTypeString(arg.dataTypeString, false);
10163                         if(basicType)
10164                         {
10165                            if(basicType.kind == thisClassType)
10166                            {
10167                               FreeType(basicType);
10168                               basicType = ReplaceThisClassType(_class);
10169                            }
10170
10171                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10172                            if(tClass.templateClass)
10173                               basicType.passAsTemplate = true;
10174                            */
10175
10176                            FreeType(exp.expType);
10177
10178                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10179                            //exp.expType.refCount++;
10180                            if(!exp.destType)
10181                            {
10182                               exp.destType = exp.expType;
10183                               exp.destType.refCount++;
10184                            }
10185
10186                            {
10187                               Expression newExp { };
10188                               OldList * specs = MkList();
10189                               Declarator decl;
10190                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10191                               *newExp = *exp;
10192                               if(exp.destType) exp.destType.refCount++;
10193                               if(exp.expType)  exp.expType.refCount++;
10194                               exp.type = castExp;
10195                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10196                               exp.cast.exp = newExp;
10197                               //FreeType(exp.expType);
10198                               //exp.expType = null;
10199                               //ProcessExpressionType(sourceExp);
10200                            }
10201                         }
10202                         FinishTemplatesContext(context);
10203                      }
10204                   }
10205                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10206                   {
10207                      Class expClass = exp.expType._class.registered;
10208                      if(expClass)
10209                      {
10210                         Class cClass = null;
10211                         int c;
10212                         int p = 0;
10213                         int paramCount = 0;
10214                         int lastParam = -1;
10215                         char templateString[1024];
10216                         ClassTemplateParameter param;
10217                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10218                         while(cClass != expClass)
10219                         {
10220                            Class sClass;
10221                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10222                            cClass = sClass;
10223
10224                            for(param = cClass.templateParams.first; param; param = param.next)
10225                            {
10226                               Class cClassCur = null;
10227                               int c;
10228                               int cp = 0;
10229                               ClassTemplateParameter paramCur = null;
10230                               ClassTemplateArgument arg;
10231                               while(cClassCur != tClass && !paramCur)
10232                               {
10233                                  Class sClassCur;
10234                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10235                                  cClassCur = sClassCur;
10236
10237                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10238                                  {
10239                                     if(!strcmp(paramCur.name, param.name))
10240                                     {
10241
10242                                        break;
10243                                     }
10244                                     cp++;
10245                                  }
10246                               }
10247                               if(paramCur && paramCur.type == TemplateParameterType::type)
10248                                  arg = tClass.templateArgs[cp];
10249                               else
10250                                  arg = expClass.templateArgs[p];
10251
10252                               {
10253                                  char argument[256];
10254                                  argument[0] = '\0';
10255                                  /*if(arg.name)
10256                                  {
10257                                     strcat(argument, arg.name.string);
10258                                     strcat(argument, " = ");
10259                                  }*/
10260                                  switch(param.type)
10261                                  {
10262                                     case expression:
10263                                     {
10264                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10265                                        char expString[1024];
10266                                        OldList * specs = MkList();
10267                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10268                                        Expression exp;
10269                                        char * string = PrintHexUInt64(arg.expression.ui64);
10270                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10271                                        delete string;
10272
10273                                        ProcessExpressionType(exp);
10274                                        ComputeExpression(exp);
10275                                        expString[0] = '\0';
10276                                        PrintExpression(exp, expString);
10277                                        strcat(argument, expString);
10278                                        // delete exp;
10279                                        FreeExpression(exp);
10280                                        break;
10281                                     }
10282                                     case identifier:
10283                                     {
10284                                        strcat(argument, arg.member.name);
10285                                        break;
10286                                     }
10287                                     case TemplateParameterType::type:
10288                                     {
10289                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10290                                           strcat(argument, arg.dataTypeString);
10291                                        break;
10292                                     }
10293                                  }
10294                                  if(argument[0])
10295                                  {
10296                                     if(paramCount) strcat(templateString, ", ");
10297                                     if(lastParam != p - 1)
10298                                     {
10299                                        strcat(templateString, param.name);
10300                                        strcat(templateString, " = ");
10301                                     }
10302                                     strcat(templateString, argument);
10303                                     paramCount++;
10304                                     lastParam = p;
10305                                  }
10306                               }
10307                               p++;
10308                            }
10309                         }
10310                         {
10311                            int len = strlen(templateString);
10312                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10313                            templateString[len++] = '>';
10314                            templateString[len++] = '\0';
10315                         }
10316
10317                         FreeType(exp.expType);
10318                         {
10319                            Context context = SetupTemplatesContext(tClass);
10320                            exp.expType = ProcessTypeString(templateString, false);
10321                            FinishTemplatesContext(context);
10322                         }
10323                      }
10324                   }
10325                }
10326             }
10327             else
10328                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10329          }
10330          else if(type && (type.kind == structType || type.kind == unionType))
10331          {
10332             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10333             if(memberType)
10334             {
10335                exp.expType = memberType;
10336                if(memberType)
10337                   memberType.refCount++;
10338             }
10339          }
10340          else
10341          {
10342             char expString[10240];
10343             expString[0] = '\0';
10344             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10345             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10346          }
10347
10348          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10349          {
10350             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10351             {
10352                Identifier id = exp.member.member;
10353                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10354                if(_class)
10355                {
10356                   FreeType(exp.expType);
10357                   exp.expType = ReplaceThisClassType(_class);
10358                }
10359             }
10360          }
10361          yylloc = oldyylloc;
10362          break;
10363       }
10364       // Convert x->y into (*x).y
10365       case pointerExp:
10366       {
10367          Type destType = exp.destType;
10368
10369          // DOING THIS LATER NOW...
10370          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10371          {
10372             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10373             /* TODO: Name Space Fix ups
10374             if(!exp.member.member.classSym)
10375                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10376             */
10377          }
10378
10379          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10380          exp.type = memberExp;
10381          if(destType)
10382             destType.count++;
10383          ProcessExpressionType(exp);
10384          if(destType)
10385             destType.count--;
10386          break;
10387       }
10388       case classSizeExp:
10389       {
10390          //ComputeExpression(exp);
10391
10392          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10393          if(classSym && classSym.registered)
10394          {
10395             if(classSym.registered.type == noHeadClass)
10396             {
10397                char name[1024];
10398                name[0] = '\0';
10399                DeclareStruct(classSym.string, false);
10400                FreeSpecifier(exp._class);
10401                exp.type = typeSizeExp;
10402                FullClassNameCat(name, classSym.string, false);
10403                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10404             }
10405             else
10406             {
10407                if(classSym.registered.fixed)
10408                {
10409                   FreeSpecifier(exp._class);
10410                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10411                   exp.type = constantExp;
10412                }
10413                else
10414                {
10415                   char className[1024];
10416                   strcpy(className, "__ecereClass_");
10417                   FullClassNameCat(className, classSym.string, true);
10418                   MangleClassName(className);
10419
10420                   DeclareClass(classSym, className);
10421
10422                   FreeExpContents(exp);
10423                   exp.type = pointerExp;
10424                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10425                   exp.member.member = MkIdentifier("structSize");
10426                }
10427             }
10428          }
10429
10430          exp.expType = Type
10431          {
10432             refCount = 1;
10433             kind = intSizeType;
10434          };
10435          // exp.isConstant = true;
10436          break;
10437       }
10438       case typeSizeExp:
10439       {
10440          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10441
10442          exp.expType = Type
10443          {
10444             refCount = 1;
10445             kind = intSizeType;
10446          };
10447          exp.isConstant = true;
10448
10449          DeclareType(type, false, false);
10450          FreeType(type);
10451          break;
10452       }
10453       case castExp:
10454       {
10455          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10456          type.count = 1;
10457          FreeType(exp.cast.exp.destType);
10458          exp.cast.exp.destType = type;
10459          type.refCount++;
10460          ProcessExpressionType(exp.cast.exp);
10461          type.count = 0;
10462          exp.expType = type;
10463          //type.refCount++;
10464
10465          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10466          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10467          {
10468             void * prev = exp.prev, * next = exp.next;
10469             Type expType = exp.cast.exp.destType;
10470             Expression castExp = exp.cast.exp;
10471             Type destType = exp.destType;
10472
10473             if(expType) expType.refCount++;
10474
10475             //FreeType(exp.destType);
10476             FreeType(exp.expType);
10477             FreeTypeName(exp.cast.typeName);
10478
10479             *exp = *castExp;
10480             FreeType(exp.expType);
10481             FreeType(exp.destType);
10482
10483             exp.expType = expType;
10484             exp.destType = destType;
10485
10486             delete castExp;
10487
10488             exp.prev = prev;
10489             exp.next = next;
10490
10491          }
10492          else
10493          {
10494             exp.isConstant = exp.cast.exp.isConstant;
10495          }
10496          //FreeType(type);
10497          break;
10498       }
10499       case extensionInitializerExp:
10500       {
10501          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10502          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10503          // ProcessInitializer(exp.initializer.initializer, type);
10504          exp.expType = type;
10505          break;
10506       }
10507       case vaArgExp:
10508       {
10509          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10510          ProcessExpressionType(exp.vaArg.exp);
10511          exp.expType = type;
10512          break;
10513       }
10514       case conditionExp:
10515       {
10516          Expression e;
10517          exp.isConstant = true;
10518
10519          FreeType(exp.cond.cond.destType);
10520          exp.cond.cond.destType = MkClassType("bool");
10521          exp.cond.cond.destType.truth = true;
10522          ProcessExpressionType(exp.cond.cond);
10523          if(!exp.cond.cond.isConstant)
10524             exp.isConstant = false;
10525          for(e = exp.cond.exp->first; e; e = e.next)
10526          {
10527             if(!e.next)
10528             {
10529                FreeType(e.destType);
10530                e.destType = exp.destType;
10531                if(e.destType) e.destType.refCount++;
10532             }
10533             ProcessExpressionType(e);
10534             if(!e.next)
10535             {
10536                exp.expType = e.expType;
10537                if(e.expType) e.expType.refCount++;
10538             }
10539             if(!e.isConstant)
10540                exp.isConstant = false;
10541          }
10542
10543          FreeType(exp.cond.elseExp.destType);
10544          // Added this check if we failed to find an expType
10545          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10546
10547          // Reversed it...
10548          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10549
10550          if(exp.cond.elseExp.destType)
10551             exp.cond.elseExp.destType.refCount++;
10552          ProcessExpressionType(exp.cond.elseExp);
10553
10554          // FIXED THIS: Was done before calling process on elseExp
10555          if(!exp.cond.elseExp.isConstant)
10556             exp.isConstant = false;
10557          break;
10558       }
10559       case extensionCompoundExp:
10560       {
10561          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10562          {
10563             Statement last = exp.compound.compound.statements->last;
10564             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10565             {
10566                ((Expression)last.expressions->last).destType = exp.destType;
10567                if(exp.destType)
10568                   exp.destType.refCount++;
10569             }
10570             ProcessStatement(exp.compound);
10571             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10572             if(exp.expType)
10573                exp.expType.refCount++;
10574          }
10575          break;
10576       }
10577       case classExp:
10578       {
10579          Specifier spec = exp._classExp.specifiers->first;
10580          if(spec && spec.type == nameSpecifier)
10581          {
10582             exp.expType = MkClassType(spec.name);
10583             exp.expType.kind = subClassType;
10584             exp.byReference = true;
10585          }
10586          else
10587          {
10588             exp.expType = MkClassType("ecere::com::Class");
10589             exp.byReference = true;
10590          }
10591          break;
10592       }
10593       case classDataExp:
10594       {
10595          Class _class = thisClass ? thisClass : currentClass;
10596          if(_class)
10597          {
10598             Identifier id = exp.classData.id;
10599             char structName[1024];
10600             Expression classExp;
10601             strcpy(structName, "__ecereClassData_");
10602             FullClassNameCat(structName, _class.fullName, false);
10603             exp.type = pointerExp;
10604             exp.member.member = id;
10605             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10606                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10607             else
10608                classExp = MkExpIdentifier(MkIdentifier("class"));
10609
10610             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10611                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10612                   MkExpBrackets(MkListOne(MkExpOp(
10613                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10614                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10615                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10616                      )));
10617
10618             ProcessExpressionType(exp);
10619             return;
10620          }
10621          break;
10622       }
10623       case arrayExp:
10624       {
10625          Type type = null;
10626          char * typeString = null;
10627          char typeStringBuf[1024];
10628          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10629             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10630          {
10631             Class templateClass = exp.destType._class.registered;
10632             typeString = templateClass.templateArgs[2].dataTypeString;
10633          }
10634          else if(exp.list)
10635          {
10636             // Guess type from expressions in the array
10637             Expression e;
10638             for(e = exp.list->first; e; e = e.next)
10639             {
10640                ProcessExpressionType(e);
10641                if(e.expType)
10642                {
10643                   if(!type) { type = e.expType; type.refCount++; }
10644                   else
10645                   {
10646                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10647                      if(!MatchTypeExpression(e, type, null, false))
10648                      {
10649                         FreeType(type);
10650                         type = e.expType;
10651                         e.expType = null;
10652
10653                         e = exp.list->first;
10654                         ProcessExpressionType(e);
10655                         if(e.expType)
10656                         {
10657                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10658                            if(!MatchTypeExpression(e, type, null, false))
10659                            {
10660                               FreeType(e.expType);
10661                               e.expType = null;
10662                               FreeType(type);
10663                               type = null;
10664                               break;
10665                            }
10666                         }
10667                      }
10668                   }
10669                   if(e.expType)
10670                   {
10671                      FreeType(e.expType);
10672                      e.expType = null;
10673                   }
10674                }
10675             }
10676             if(type)
10677             {
10678                typeStringBuf[0] = '\0';
10679                PrintTypeNoConst(type, typeStringBuf, false, true);
10680                typeString = typeStringBuf;
10681                FreeType(type);
10682                type = null;
10683             }
10684          }
10685          if(typeString)
10686          {
10687             /*
10688             (Container)& (struct BuiltInContainer)
10689             {
10690                ._vTbl = class(BuiltInContainer)._vTbl,
10691                ._class = class(BuiltInContainer),
10692                .refCount = 0,
10693                .data = (int[]){ 1, 7, 3, 4, 5 },
10694                .count = 5,
10695                .type = class(int),
10696             }
10697             */
10698             char templateString[1024];
10699             OldList * initializers = MkList();
10700             OldList * structInitializers = MkList();
10701             OldList * specs = MkList();
10702             Expression expExt;
10703             Declarator decl = SpecDeclFromString(typeString, specs, null);
10704             sprintf(templateString, "Container<%s>", typeString);
10705
10706             if(exp.list)
10707             {
10708                Expression e;
10709                type = ProcessTypeString(typeString, false);
10710                while(e = exp.list->first)
10711                {
10712                   exp.list->Remove(e);
10713                   e.destType = type;
10714                   type.refCount++;
10715                   ProcessExpressionType(e);
10716                   ListAdd(initializers, MkInitializerAssignment(e));
10717                }
10718                FreeType(type);
10719                delete exp.list;
10720             }
10721
10722             DeclareStruct("ecere::com::BuiltInContainer", false);
10723
10724             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10725                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10726             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10727                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10728             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10729                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10730             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10731                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10732                MkInitializerList(initializers))));
10733                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10734             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10735                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10736             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10737                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10738             exp.expType = ProcessTypeString(templateString, false);
10739             exp.type = bracketsExp;
10740             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10741                MkExpOp(null, '&',
10742                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10743                   MkInitializerList(structInitializers)))));
10744             ProcessExpressionType(expExt);
10745          }
10746          else
10747          {
10748             exp.expType = ProcessTypeString("Container", false);
10749             Compiler_Error($"Couldn't determine type of array elements\n");
10750          }
10751          break;
10752       }
10753    }
10754
10755    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10756    {
10757       FreeType(exp.expType);
10758       exp.expType = ReplaceThisClassType(thisClass);
10759    }
10760
10761    // Resolve structures here
10762    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10763    {
10764       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10765       // TODO: Fix members reference...
10766       if(symbol)
10767       {
10768          if(exp.expType.kind != enumType)
10769          {
10770             Type member;
10771             String enumName = CopyString(exp.expType.enumName);
10772
10773             // Fixed a memory leak on self-referencing C structs typedefs
10774             // by instantiating a new type rather than simply copying members
10775             // into exp.expType
10776             FreeType(exp.expType);
10777             exp.expType = Type { };
10778             exp.expType.kind = symbol.type.kind;
10779             exp.expType.refCount++;
10780             exp.expType.enumName = enumName;
10781
10782             exp.expType.members = symbol.type.members;
10783             for(member = symbol.type.members.first; member; member = member.next)
10784                member.refCount++;
10785          }
10786          else
10787          {
10788             NamedLink member;
10789             for(member = symbol.type.members.first; member; member = member.next)
10790             {
10791                NamedLink value { name = CopyString(member.name) };
10792                exp.expType.members.Add(value);
10793             }
10794          }
10795       }
10796    }
10797
10798    yylloc = exp.loc;
10799    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10800    else if(exp.destType && !exp.destType.keepCast)
10801    {
10802       if(!CheckExpressionType(exp, exp.destType, false))
10803       {
10804          if(!exp.destType.count || unresolved)
10805          {
10806             if(!exp.expType)
10807             {
10808                yylloc = exp.loc;
10809                if(exp.destType.kind != ellipsisType)
10810                {
10811                   char type2[1024];
10812                   type2[0] = '\0';
10813                   if(inCompiler)
10814                   {
10815                      char expString[10240];
10816                      expString[0] = '\0';
10817
10818                      PrintType(exp.destType, type2, false, true);
10819
10820                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10821                      if(unresolved)
10822                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10823                      else if(exp.type != dummyExp)
10824                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10825                   }
10826                }
10827                else
10828                {
10829                   char expString[10240] ;
10830                   expString[0] = '\0';
10831                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10832
10833                   if(unresolved)
10834                      Compiler_Error($"unresolved identifier %s\n", expString);
10835                   else if(exp.type != dummyExp)
10836                      Compiler_Error($"couldn't determine type of %s\n", expString);
10837                }
10838             }
10839             else
10840             {
10841                char type1[1024];
10842                char type2[1024];
10843                type1[0] = '\0';
10844                type2[0] = '\0';
10845                if(inCompiler)
10846                {
10847                   PrintType(exp.expType, type1, false, true);
10848                   PrintType(exp.destType, type2, false, true);
10849                }
10850
10851                //CheckExpressionType(exp, exp.destType, false);
10852
10853                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10854                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10855                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10856                else
10857                {
10858                   char expString[10240];
10859                   expString[0] = '\0';
10860                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10861
10862 #ifdef _DEBUG
10863                   CheckExpressionType(exp, exp.destType, false);
10864 #endif
10865                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10866                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10867                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10868
10869                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10870                   FreeType(exp.expType);
10871                   exp.destType.refCount++;
10872                   exp.expType = exp.destType;
10873                }
10874             }
10875          }
10876       }
10877       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10878       {
10879          Expression newExp { };
10880          char typeString[1024];
10881          OldList * specs = MkList();
10882          Declarator decl;
10883
10884          typeString[0] = '\0';
10885
10886          *newExp = *exp;
10887
10888          if(exp.expType)  exp.expType.refCount++;
10889          if(exp.expType)  exp.expType.refCount++;
10890          exp.type = castExp;
10891          newExp.destType = exp.expType;
10892
10893          PrintType(exp.expType, typeString, false, false);
10894          decl = SpecDeclFromString(typeString, specs, null);
10895
10896          exp.cast.typeName = MkTypeName(specs, decl);
10897          exp.cast.exp = newExp;
10898       }
10899    }
10900    else if(unresolved)
10901    {
10902       if(exp.identifier._class && exp.identifier._class.name)
10903          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10904       else if(exp.identifier.string && exp.identifier.string[0])
10905          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10906    }
10907    else if(!exp.expType && exp.type != dummyExp)
10908    {
10909       char expString[10240];
10910       expString[0] = '\0';
10911       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10912       Compiler_Error($"couldn't determine type of %s\n", expString);
10913    }
10914
10915    // Let's try to support any_object & typed_object here:
10916    if(inCompiler)
10917       ApplyAnyObjectLogic(exp);
10918
10919    // Mark nohead classes as by reference, unless we're casting them to an integral type
10920    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10921       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10922          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10923           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10924    {
10925       exp.byReference = true;
10926    }
10927    yylloc = oldyylloc;
10928 }
10929
10930 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10931 {
10932    // THIS CODE WILL FIND NEXT MEMBER...
10933    if(*curMember)
10934    {
10935       *curMember = (*curMember).next;
10936
10937       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10938       {
10939          *curMember = subMemberStack[--(*subMemberStackPos)];
10940          *curMember = (*curMember).next;
10941       }
10942
10943       // SKIP ALL PROPERTIES HERE...
10944       while((*curMember) && (*curMember).isProperty)
10945          *curMember = (*curMember).next;
10946
10947       if(subMemberStackPos)
10948       {
10949          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10950          {
10951             subMemberStack[(*subMemberStackPos)++] = *curMember;
10952
10953             *curMember = (*curMember).members.first;
10954             while(*curMember && (*curMember).isProperty)
10955                *curMember = (*curMember).next;
10956          }
10957       }
10958    }
10959    while(!*curMember)
10960    {
10961       if(!*curMember)
10962       {
10963          if(subMemberStackPos && *subMemberStackPos)
10964          {
10965             *curMember = subMemberStack[--(*subMemberStackPos)];
10966             *curMember = (*curMember).next;
10967          }
10968          else
10969          {
10970             Class lastCurClass = *curClass;
10971
10972             if(*curClass == _class) break;     // REACHED THE END
10973
10974             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10975             *curMember = (*curClass).membersAndProperties.first;
10976          }
10977
10978          while((*curMember) && (*curMember).isProperty)
10979             *curMember = (*curMember).next;
10980          if(subMemberStackPos)
10981          {
10982             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10983             {
10984                subMemberStack[(*subMemberStackPos)++] = *curMember;
10985
10986                *curMember = (*curMember).members.first;
10987                while(*curMember && (*curMember).isProperty)
10988                   *curMember = (*curMember).next;
10989             }
10990          }
10991       }
10992    }
10993 }
10994
10995
10996 static void ProcessInitializer(Initializer init, Type type)
10997 {
10998    switch(init.type)
10999    {
11000       case expInitializer:
11001          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11002          {
11003             // TESTING THIS FOR SHUTTING = 0 WARNING
11004             if(init.exp && !init.exp.destType)
11005             {
11006                FreeType(init.exp.destType);
11007                init.exp.destType = type;
11008                if(type) type.refCount++;
11009             }
11010             if(init.exp)
11011             {
11012                ProcessExpressionType(init.exp);
11013                init.isConstant = init.exp.isConstant;
11014             }
11015             break;
11016          }
11017          else
11018          {
11019             Expression exp = init.exp;
11020             Instantiation inst = exp.instance;
11021             MembersInit members;
11022
11023             init.type = listInitializer;
11024             init.list = MkList();
11025
11026             if(inst.members)
11027             {
11028                for(members = inst.members->first; members; members = members.next)
11029                {
11030                   if(members.type == dataMembersInit)
11031                   {
11032                      MemberInit member;
11033                      for(member = members.dataMembers->first; member; member = member.next)
11034                      {
11035                         ListAdd(init.list, member.initializer);
11036                         member.initializer = null;
11037                      }
11038                   }
11039                   // Discard all MembersInitMethod
11040                }
11041             }
11042             FreeExpression(exp);
11043          }
11044       case listInitializer:
11045       {
11046          Initializer i;
11047          Type initializerType = null;
11048          Class curClass = null;
11049          DataMember curMember = null;
11050          DataMember subMemberStack[256];
11051          int subMemberStackPos = 0;
11052
11053          if(type && type.kind == arrayType)
11054             initializerType = Dereference(type);
11055          else if(type && (type.kind == structType || type.kind == unionType))
11056             initializerType = type.members.first;
11057
11058          for(i = init.list->first; i; i = i.next)
11059          {
11060             if(type && type.kind == classType && type._class && type._class.registered)
11061             {
11062                // 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)
11063                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11064                // TODO: Generate error on initializing a private data member this way from another module...
11065                if(curMember)
11066                {
11067                   if(!curMember.dataType)
11068                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11069                   initializerType = curMember.dataType;
11070                }
11071             }
11072             ProcessInitializer(i, initializerType);
11073             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11074                initializerType = initializerType.next;
11075             if(!i.isConstant)
11076                init.isConstant = false;
11077          }
11078
11079          if(type && type.kind == arrayType)
11080             FreeType(initializerType);
11081
11082          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11083          {
11084             Compiler_Error($"Assigning list initializer to non list\n");
11085          }
11086          break;
11087       }
11088    }
11089 }
11090
11091 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11092 {
11093    switch(spec.type)
11094    {
11095       case baseSpecifier:
11096       {
11097          if(spec.specifier == THISCLASS)
11098          {
11099             if(thisClass)
11100             {
11101                spec.type = nameSpecifier;
11102                spec.name = ReplaceThisClass(thisClass);
11103                spec.symbol = FindClass(spec.name);
11104                ProcessSpecifier(spec, declareStruct);
11105             }
11106          }
11107          break;
11108       }
11109       case nameSpecifier:
11110       {
11111          Symbol symbol = FindType(curContext, spec.name);
11112          if(symbol)
11113             DeclareType(symbol.type, true, true);
11114          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11115             DeclareStruct(spec.name, false);
11116          break;
11117       }
11118       case enumSpecifier:
11119       {
11120          Enumerator e;
11121          if(spec.list)
11122          {
11123             for(e = spec.list->first; e; e = e.next)
11124             {
11125                if(e.exp)
11126                   ProcessExpressionType(e.exp);
11127             }
11128          }
11129          break;
11130       }
11131       case structSpecifier:
11132       case unionSpecifier:
11133       {
11134          if(spec.definitions)
11135          {
11136             ClassDef def;
11137             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11138             //if(symbol)
11139                ProcessClass(spec.definitions, symbol);
11140             /*else
11141             {
11142                for(def = spec.definitions->first; def; def = def.next)
11143                {
11144                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11145                      ProcessDeclaration(def.decl);
11146                }
11147             }*/
11148          }
11149          break;
11150       }
11151       /*
11152       case classSpecifier:
11153       {
11154          Symbol classSym = FindClass(spec.name);
11155          if(classSym && classSym.registered && classSym.registered.type == structClass)
11156             DeclareStruct(spec.name, false);
11157          break;
11158       }
11159       */
11160    }
11161 }
11162
11163
11164 static void ProcessDeclarator(Declarator decl)
11165 {
11166    switch(decl.type)
11167    {
11168       case identifierDeclarator:
11169          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11170          {
11171             FreeSpecifier(decl.identifier._class);
11172             decl.identifier._class = null;
11173          }
11174          break;
11175       case arrayDeclarator:
11176          if(decl.array.exp)
11177             ProcessExpressionType(decl.array.exp);
11178       case structDeclarator:
11179       case bracketsDeclarator:
11180       case functionDeclarator:
11181       case pointerDeclarator:
11182       case extendedDeclarator:
11183       case extendedDeclaratorEnd:
11184          if(decl.declarator)
11185             ProcessDeclarator(decl.declarator);
11186          if(decl.type == functionDeclarator)
11187          {
11188             Identifier id = GetDeclId(decl);
11189             if(id && id._class)
11190             {
11191                TypeName param
11192                {
11193                   qualifiers = MkListOne(id._class);
11194                   declarator = null;
11195                };
11196                if(!decl.function.parameters)
11197                   decl.function.parameters = MkList();
11198                decl.function.parameters->Insert(null, param);
11199                id._class = null;
11200             }
11201             if(decl.function.parameters)
11202             {
11203                TypeName param;
11204
11205                for(param = decl.function.parameters->first; param; param = param.next)
11206                {
11207                   if(param.qualifiers && param.qualifiers->first)
11208                   {
11209                      Specifier spec = param.qualifiers->first;
11210                      if(spec && spec.specifier == TYPED_OBJECT)
11211                      {
11212                         Declarator d = param.declarator;
11213                         TypeName newParam
11214                         {
11215                            qualifiers = MkListOne(MkSpecifier(VOID));
11216                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11217                         };
11218
11219                         FreeList(param.qualifiers, FreeSpecifier);
11220
11221                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11222                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11223
11224                         decl.function.parameters->Insert(param, newParam);
11225                         param = newParam;
11226                      }
11227                      else if(spec && spec.specifier == ANY_OBJECT)
11228                      {
11229                         Declarator d = param.declarator;
11230
11231                         FreeList(param.qualifiers, FreeSpecifier);
11232
11233                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11234                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11235                      }
11236                      else if(spec.specifier == THISCLASS)
11237                      {
11238                         if(thisClass)
11239                         {
11240                            spec.type = nameSpecifier;
11241                            spec.name = ReplaceThisClass(thisClass);
11242                            spec.symbol = FindClass(spec.name);
11243                            ProcessSpecifier(spec, false);
11244                         }
11245                      }
11246                   }
11247
11248                   if(param.declarator)
11249                      ProcessDeclarator(param.declarator);
11250                }
11251             }
11252          }
11253          break;
11254    }
11255 }
11256
11257 static void ProcessDeclaration(Declaration decl)
11258 {
11259    yylloc = decl.loc;
11260    switch(decl.type)
11261    {
11262       case initDeclaration:
11263       {
11264          bool declareStruct = false;
11265          /*
11266          lineNum = decl.pos.line;
11267          column = decl.pos.col;
11268          */
11269
11270          if(decl.declarators)
11271          {
11272             InitDeclarator d;
11273
11274             for(d = decl.declarators->first; d; d = d.next)
11275             {
11276                Type type, subType;
11277                ProcessDeclarator(d.declarator);
11278
11279                type = ProcessType(decl.specifiers, d.declarator);
11280
11281                if(d.initializer)
11282                {
11283                   ProcessInitializer(d.initializer, type);
11284
11285                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11286
11287                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11288                      d.initializer.exp.type == instanceExp)
11289                   {
11290                      if(type.kind == classType && type._class ==
11291                         d.initializer.exp.expType._class)
11292                      {
11293                         Instantiation inst = d.initializer.exp.instance;
11294                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11295
11296                         d.initializer.exp.instance = null;
11297                         if(decl.specifiers)
11298                            FreeList(decl.specifiers, FreeSpecifier);
11299                         FreeList(decl.declarators, FreeInitDeclarator);
11300
11301                         d = null;
11302
11303                         decl.type = instDeclaration;
11304                         decl.inst = inst;
11305                      }
11306                   }
11307                }
11308                for(subType = type; subType;)
11309                {
11310                   if(subType.kind == classType)
11311                   {
11312                      declareStruct = true;
11313                      break;
11314                   }
11315                   else if(subType.kind == pointerType)
11316                      break;
11317                   else if(subType.kind == arrayType)
11318                      subType = subType.arrayType;
11319                   else
11320                      break;
11321                }
11322
11323                FreeType(type);
11324                if(!d) break;
11325             }
11326          }
11327
11328          if(decl.specifiers)
11329          {
11330             Specifier s;
11331             for(s = decl.specifiers->first; s; s = s.next)
11332             {
11333                ProcessSpecifier(s, declareStruct);
11334             }
11335          }
11336          break;
11337       }
11338       case instDeclaration:
11339       {
11340          ProcessInstantiationType(decl.inst);
11341          break;
11342       }
11343       case structDeclaration:
11344       {
11345          Specifier spec;
11346          Declarator d;
11347          bool declareStruct = false;
11348
11349          if(decl.declarators)
11350          {
11351             for(d = decl.declarators->first; d; d = d.next)
11352             {
11353                Type type = ProcessType(decl.specifiers, d.declarator);
11354                Type subType;
11355                ProcessDeclarator(d);
11356                for(subType = type; subType;)
11357                {
11358                   if(subType.kind == classType)
11359                   {
11360                      declareStruct = true;
11361                      break;
11362                   }
11363                   else if(subType.kind == pointerType)
11364                      break;
11365                   else if(subType.kind == arrayType)
11366                      subType = subType.arrayType;
11367                   else
11368                      break;
11369                }
11370                FreeType(type);
11371             }
11372          }
11373          if(decl.specifiers)
11374          {
11375             for(spec = decl.specifiers->first; spec; spec = spec.next)
11376                ProcessSpecifier(spec, declareStruct);
11377          }
11378          break;
11379       }
11380    }
11381 }
11382
11383 static FunctionDefinition curFunction;
11384
11385 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11386 {
11387    char propName[1024], propNameM[1024];
11388    char getName[1024], setName[1024];
11389    OldList * args;
11390
11391    DeclareProperty(prop, setName, getName);
11392
11393    // eInstance_FireWatchers(object, prop);
11394    strcpy(propName, "__ecereProp_");
11395    FullClassNameCat(propName, prop._class.fullName, false);
11396    strcat(propName, "_");
11397    // strcat(propName, prop.name);
11398    FullClassNameCat(propName, prop.name, true);
11399    MangleClassName(propName);
11400
11401    strcpy(propNameM, "__ecerePropM_");
11402    FullClassNameCat(propNameM, prop._class.fullName, false);
11403    strcat(propNameM, "_");
11404    // strcat(propNameM, prop.name);
11405    FullClassNameCat(propNameM, prop.name, true);
11406    MangleClassName(propNameM);
11407
11408    if(prop.isWatchable)
11409    {
11410       args = MkList();
11411       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11412       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11413       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11414
11415       args = MkList();
11416       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11417       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11418       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11419    }
11420
11421
11422    {
11423       args = MkList();
11424       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11425       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11426       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11427
11428       args = MkList();
11429       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11430       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11431       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11432    }
11433
11434    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11435       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11436       curFunction.propSet.fireWatchersDone = true;
11437 }
11438
11439 static void ProcessStatement(Statement stmt)
11440 {
11441    yylloc = stmt.loc;
11442    /*
11443    lineNum = stmt.pos.line;
11444    column = stmt.pos.col;
11445    */
11446    switch(stmt.type)
11447    {
11448       case labeledStmt:
11449          ProcessStatement(stmt.labeled.stmt);
11450          break;
11451       case caseStmt:
11452          // This expression should be constant...
11453          if(stmt.caseStmt.exp)
11454          {
11455             FreeType(stmt.caseStmt.exp.destType);
11456             stmt.caseStmt.exp.destType = curSwitchType;
11457             if(curSwitchType) curSwitchType.refCount++;
11458             ProcessExpressionType(stmt.caseStmt.exp);
11459             ComputeExpression(stmt.caseStmt.exp);
11460          }
11461          if(stmt.caseStmt.stmt)
11462             ProcessStatement(stmt.caseStmt.stmt);
11463          break;
11464       case compoundStmt:
11465       {
11466          if(stmt.compound.context)
11467          {
11468             Declaration decl;
11469             Statement s;
11470
11471             Statement prevCompound = curCompound;
11472             Context prevContext = curContext;
11473
11474             if(!stmt.compound.isSwitch)
11475                curCompound = stmt;
11476             curContext = stmt.compound.context;
11477
11478             if(stmt.compound.declarations)
11479             {
11480                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11481                   ProcessDeclaration(decl);
11482             }
11483             if(stmt.compound.statements)
11484             {
11485                for(s = stmt.compound.statements->first; s; s = s.next)
11486                   ProcessStatement(s);
11487             }
11488
11489             curContext = prevContext;
11490             curCompound = prevCompound;
11491          }
11492          break;
11493       }
11494       case expressionStmt:
11495       {
11496          Expression exp;
11497          if(stmt.expressions)
11498          {
11499             for(exp = stmt.expressions->first; exp; exp = exp.next)
11500                ProcessExpressionType(exp);
11501          }
11502          break;
11503       }
11504       case ifStmt:
11505       {
11506          Expression exp;
11507
11508          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11509          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11510          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11511          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11512          {
11513             ProcessExpressionType(exp);
11514          }
11515          if(stmt.ifStmt.stmt)
11516             ProcessStatement(stmt.ifStmt.stmt);
11517          if(stmt.ifStmt.elseStmt)
11518             ProcessStatement(stmt.ifStmt.elseStmt);
11519          break;
11520       }
11521       case switchStmt:
11522       {
11523          Type oldSwitchType = curSwitchType;
11524          if(stmt.switchStmt.exp)
11525          {
11526             Expression exp;
11527             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11528             {
11529                if(!exp.next)
11530                {
11531                   /*
11532                   Type destType
11533                   {
11534                      kind = intType;
11535                      refCount = 1;
11536                   };
11537                   e.exp.destType = destType;
11538                   */
11539
11540                   ProcessExpressionType(exp);
11541                }
11542                if(!exp.next)
11543                   curSwitchType = exp.expType;
11544             }
11545          }
11546          ProcessStatement(stmt.switchStmt.stmt);
11547          curSwitchType = oldSwitchType;
11548          break;
11549       }
11550       case whileStmt:
11551       {
11552          if(stmt.whileStmt.exp)
11553          {
11554             Expression exp;
11555
11556             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11557             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11558             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11559             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11560             {
11561                ProcessExpressionType(exp);
11562             }
11563          }
11564          if(stmt.whileStmt.stmt)
11565             ProcessStatement(stmt.whileStmt.stmt);
11566          break;
11567       }
11568       case doWhileStmt:
11569       {
11570          if(stmt.doWhile.exp)
11571          {
11572             Expression exp;
11573
11574             if(stmt.doWhile.exp->last)
11575             {
11576                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11577                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11578                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11579             }
11580             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11581             {
11582                ProcessExpressionType(exp);
11583             }
11584          }
11585          if(stmt.doWhile.stmt)
11586             ProcessStatement(stmt.doWhile.stmt);
11587          break;
11588       }
11589       case forStmt:
11590       {
11591          Expression exp;
11592          if(stmt.forStmt.init)
11593             ProcessStatement(stmt.forStmt.init);
11594
11595          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11596          {
11597             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11598             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11599             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11600          }
11601
11602          if(stmt.forStmt.check)
11603             ProcessStatement(stmt.forStmt.check);
11604          if(stmt.forStmt.increment)
11605          {
11606             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11607                ProcessExpressionType(exp);
11608          }
11609
11610          if(stmt.forStmt.stmt)
11611             ProcessStatement(stmt.forStmt.stmt);
11612          break;
11613       }
11614       case forEachStmt:
11615       {
11616          Identifier id = stmt.forEachStmt.id;
11617          OldList * exp = stmt.forEachStmt.exp;
11618          OldList * filter = stmt.forEachStmt.filter;
11619          Statement block = stmt.forEachStmt.stmt;
11620          char iteratorType[1024];
11621          Type source;
11622          Expression e;
11623          bool isBuiltin = exp && exp->last &&
11624             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11625               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11626          Expression arrayExp;
11627          char * typeString = null;
11628          int builtinCount = 0;
11629
11630          for(e = exp ? exp->first : null; e; e = e.next)
11631          {
11632             if(!e.next)
11633             {
11634                FreeType(e.destType);
11635                e.destType = ProcessTypeString("Container", false);
11636             }
11637             if(!isBuiltin || e.next)
11638                ProcessExpressionType(e);
11639          }
11640
11641          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11642          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11643             eClass_IsDerived(source._class.registered, containerClass)))
11644          {
11645             Class _class = source ? source._class.registered : null;
11646             Symbol symbol;
11647             Expression expIt = null;
11648             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11649             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11650             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11651             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11652             stmt.type = compoundStmt;
11653
11654             stmt.compound.context = Context { };
11655             stmt.compound.context.parent = curContext;
11656             curContext = stmt.compound.context;
11657
11658             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11659             {
11660                Class mapClass = eSystem_FindClass(privateModule, "Map");
11661                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11662                isCustomAVLTree = true;
11663                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11664                   isAVLTree = true;
11665                else if(eClass_IsDerived(source._class.registered, mapClass))
11666                   isMap = true;
11667             }
11668             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11669             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11670             {
11671                Class listClass = eSystem_FindClass(privateModule, "List");
11672                isLinkList = true;
11673                isList = eClass_IsDerived(source._class.registered, listClass);
11674             }
11675
11676             if(isArray)
11677             {
11678                Declarator decl;
11679                OldList * specs = MkList();
11680                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11681                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11682                stmt.compound.declarations = MkListOne(
11683                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11684                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11685                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11686                      MkInitializerAssignment(MkExpBrackets(exp))))));
11687             }
11688             else if(isBuiltin)
11689             {
11690                Type type = null;
11691                char typeStringBuf[1024];
11692
11693                // TODO: Merge this code?
11694                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11695                if(((Expression)exp->last).type == castExp)
11696                {
11697                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11698                   if(typeName)
11699                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11700                }
11701
11702                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11703                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11704                   arrayExp.destType._class.registered.templateArgs)
11705                {
11706                   Class templateClass = arrayExp.destType._class.registered;
11707                   typeString = templateClass.templateArgs[2].dataTypeString;
11708                }
11709                else if(arrayExp.list)
11710                {
11711                   // Guess type from expressions in the array
11712                   Expression e;
11713                   for(e = arrayExp.list->first; e; e = e.next)
11714                   {
11715                      ProcessExpressionType(e);
11716                      if(e.expType)
11717                      {
11718                         if(!type) { type = e.expType; type.refCount++; }
11719                         else
11720                         {
11721                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11722                            if(!MatchTypeExpression(e, type, null, false))
11723                            {
11724                               FreeType(type);
11725                               type = e.expType;
11726                               e.expType = null;
11727
11728                               e = arrayExp.list->first;
11729                               ProcessExpressionType(e);
11730                               if(e.expType)
11731                               {
11732                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11733                                  if(!MatchTypeExpression(e, type, null, false))
11734                                  {
11735                                     FreeType(e.expType);
11736                                     e.expType = null;
11737                                     FreeType(type);
11738                                     type = null;
11739                                     break;
11740                                  }
11741                               }
11742                            }
11743                         }
11744                         if(e.expType)
11745                         {
11746                            FreeType(e.expType);
11747                            e.expType = null;
11748                         }
11749                      }
11750                   }
11751                   if(type)
11752                   {
11753                      typeStringBuf[0] = '\0';
11754                      PrintType(type, typeStringBuf, false, true);
11755                      typeString = typeStringBuf;
11756                      FreeType(type);
11757                   }
11758                }
11759                if(typeString)
11760                {
11761                   OldList * initializers = MkList();
11762                   Declarator decl;
11763                   OldList * specs = MkList();
11764                   if(arrayExp.list)
11765                   {
11766                      Expression e;
11767
11768                      builtinCount = arrayExp.list->count;
11769                      type = ProcessTypeString(typeString, false);
11770                      while(e = arrayExp.list->first)
11771                      {
11772                         arrayExp.list->Remove(e);
11773                         e.destType = type;
11774                         type.refCount++;
11775                         ProcessExpressionType(e);
11776                         ListAdd(initializers, MkInitializerAssignment(e));
11777                      }
11778                      FreeType(type);
11779                      delete arrayExp.list;
11780                   }
11781                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11782                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11783                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11784
11785                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11786                      PlugDeclarator(
11787                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11788                         ), MkInitializerList(initializers)))));
11789                   FreeList(exp, FreeExpression);
11790                }
11791                else
11792                {
11793                   arrayExp.expType = ProcessTypeString("Container", false);
11794                   Compiler_Error($"Couldn't determine type of array elements\n");
11795                }
11796
11797                /*
11798                Declarator decl;
11799                OldList * specs = MkList();
11800
11801                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11802                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11803                stmt.compound.declarations = MkListOne(
11804                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11805                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11806                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11807                      MkInitializerAssignment(MkExpBrackets(exp))))));
11808                */
11809             }
11810             else if(isLinkList && !isList)
11811             {
11812                Declarator decl;
11813                OldList * specs = MkList();
11814                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11815                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11816                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11817                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11818                      MkInitializerAssignment(MkExpBrackets(exp))))));
11819             }
11820             /*else if(isCustomAVLTree)
11821             {
11822                Declarator decl;
11823                OldList * specs = MkList();
11824                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11825                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11826                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11827                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11828                      MkInitializerAssignment(MkExpBrackets(exp))))));
11829             }*/
11830             else if(_class.templateArgs)
11831             {
11832                if(isMap)
11833                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11834                else
11835                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11836
11837                stmt.compound.declarations = MkListOne(
11838                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11839                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11840                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11841             }
11842             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11843
11844             if(block)
11845             {
11846                // Reparent sub-contexts in this statement
11847                switch(block.type)
11848                {
11849                   case compoundStmt:
11850                      if(block.compound.context)
11851                         block.compound.context.parent = stmt.compound.context;
11852                      break;
11853                   case ifStmt:
11854                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11855                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11856                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11857                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11858                      break;
11859                   case switchStmt:
11860                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11861                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11862                      break;
11863                   case whileStmt:
11864                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11865                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11866                      break;
11867                   case doWhileStmt:
11868                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11869                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11870                      break;
11871                   case forStmt:
11872                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11873                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11874                      break;
11875                   case forEachStmt:
11876                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11877                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11878                      break;
11879                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11880                   case labeledStmt:
11881                   case caseStmt
11882                   case expressionStmt:
11883                   case gotoStmt:
11884                   case continueStmt:
11885                   case breakStmt
11886                   case returnStmt:
11887                   case asmStmt:
11888                   case badDeclarationStmt:
11889                   case fireWatchersStmt:
11890                   case stopWatchingStmt:
11891                   case watchStmt:
11892                   */
11893                }
11894             }
11895             if(filter)
11896             {
11897                block = MkIfStmt(filter, block, null);
11898             }
11899             if(isArray)
11900             {
11901                stmt.compound.statements = MkListOne(MkForStmt(
11902                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11903                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11904                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11905                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11906                   block));
11907               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11908               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11909               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11910             }
11911             else if(isBuiltin)
11912             {
11913                char count[128];
11914                //OldList * specs = MkList();
11915                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11916
11917                sprintf(count, "%d", builtinCount);
11918
11919                stmt.compound.statements = MkListOne(MkForStmt(
11920                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11921                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11922                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11923                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11924                   block));
11925
11926                /*
11927                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11928                stmt.compound.statements = MkListOne(MkForStmt(
11929                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11930                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11931                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11932                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11933                   block));
11934               */
11935               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11936               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11937               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11938             }
11939             else if(isLinkList && !isList)
11940             {
11941                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11942                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11943                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11944                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11945                {
11946                   stmt.compound.statements = MkListOne(MkForStmt(
11947                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11948                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11949                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11950                      block));
11951                }
11952                else
11953                {
11954                   OldList * specs = MkList();
11955                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11956                   stmt.compound.statements = MkListOne(MkForStmt(
11957                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11958                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11959                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11960                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11961                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11962                      block));
11963                }
11964                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11965                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11966                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11967             }
11968             /*else if(isCustomAVLTree)
11969             {
11970                stmt.compound.statements = MkListOne(MkForStmt(
11971                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11972                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11973                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11974                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11975                   block));
11976
11977                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11978                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11979                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11980             }*/
11981             else
11982             {
11983                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11984                   MkIdentifier("Next")), null)), block));
11985             }
11986             ProcessExpressionType(expIt);
11987             if(stmt.compound.declarations->first)
11988                ProcessDeclaration(stmt.compound.declarations->first);
11989
11990             if(symbol)
11991                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11992
11993             ProcessStatement(stmt);
11994             curContext = stmt.compound.context.parent;
11995             break;
11996          }
11997          else
11998          {
11999             Compiler_Error($"Expression is not a container\n");
12000          }
12001          break;
12002       }
12003       case gotoStmt:
12004          break;
12005       case continueStmt:
12006          break;
12007       case breakStmt:
12008          break;
12009       case returnStmt:
12010       {
12011          Expression exp;
12012          if(stmt.expressions)
12013          {
12014             for(exp = stmt.expressions->first; exp; exp = exp.next)
12015             {
12016                if(!exp.next)
12017                {
12018                   if(curFunction && !curFunction.type)
12019                      curFunction.type = ProcessType(
12020                         curFunction.specifiers, curFunction.declarator);
12021                   FreeType(exp.destType);
12022                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12023                   if(exp.destType) exp.destType.refCount++;
12024                }
12025                ProcessExpressionType(exp);
12026             }
12027          }
12028          break;
12029       }
12030       case badDeclarationStmt:
12031       {
12032          ProcessDeclaration(stmt.decl);
12033          break;
12034       }
12035       case asmStmt:
12036       {
12037          AsmField field;
12038          if(stmt.asmStmt.inputFields)
12039          {
12040             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12041                if(field.expression)
12042                   ProcessExpressionType(field.expression);
12043          }
12044          if(stmt.asmStmt.outputFields)
12045          {
12046             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12047                if(field.expression)
12048                   ProcessExpressionType(field.expression);
12049          }
12050          if(stmt.asmStmt.clobberedFields)
12051          {
12052             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12053             {
12054                if(field.expression)
12055                   ProcessExpressionType(field.expression);
12056             }
12057          }
12058          break;
12059       }
12060       case watchStmt:
12061       {
12062          PropertyWatch propWatch;
12063          OldList * watches = stmt._watch.watches;
12064          Expression object = stmt._watch.object;
12065          Expression watcher = stmt._watch.watcher;
12066          if(watcher)
12067             ProcessExpressionType(watcher);
12068          if(object)
12069             ProcessExpressionType(object);
12070
12071          if(inCompiler)
12072          {
12073             if(watcher || thisClass)
12074             {
12075                External external = curExternal;
12076                Context context = curContext;
12077
12078                stmt.type = expressionStmt;
12079                stmt.expressions = MkList();
12080
12081                curExternal = external.prev;
12082
12083                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12084                {
12085                   ClassFunction func;
12086                   char watcherName[1024];
12087                   Class watcherClass = watcher ?
12088                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12089                   External createdExternal;
12090
12091                   // Create a declaration above
12092                   External externalDecl = MkExternalDeclaration(null);
12093                   ast->Insert(curExternal.prev, externalDecl);
12094
12095                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12096                   if(propWatch.deleteWatch)
12097                      strcat(watcherName, "_delete");
12098                   else
12099                   {
12100                      Identifier propID;
12101                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12102                      {
12103                         strcat(watcherName, "_");
12104                         strcat(watcherName, propID.string);
12105                      }
12106                   }
12107
12108                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12109                   {
12110                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12111                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12112                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12113                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12114                      ProcessClassFunctionBody(func, propWatch.compound);
12115                      propWatch.compound = null;
12116
12117                      //afterExternal = afterExternal ? afterExternal : curExternal;
12118
12119                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12120                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12121                      // TESTING THIS...
12122                      createdExternal.symbol.idCode = external.symbol.idCode;
12123
12124                      curExternal = createdExternal;
12125                      ProcessFunction(createdExternal.function);
12126
12127
12128                      // Create a declaration above
12129                      {
12130                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12131                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12132                         externalDecl.declaration = decl;
12133                         if(decl.symbol && !decl.symbol.pointerExternal)
12134                            decl.symbol.pointerExternal = externalDecl;
12135                      }
12136
12137                      if(propWatch.deleteWatch)
12138                      {
12139                         OldList * args = MkList();
12140                         ListAdd(args, CopyExpression(object));
12141                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12142                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12143                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12144                      }
12145                      else
12146                      {
12147                         Class _class = object.expType._class.registered;
12148                         Identifier propID;
12149
12150                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12151                         {
12152                            char propName[1024];
12153                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12154                            if(prop)
12155                            {
12156                               char getName[1024], setName[1024];
12157                               OldList * args = MkList();
12158
12159                               DeclareProperty(prop, setName, getName);
12160
12161                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12162                               strcpy(propName, "__ecereProp_");
12163                               FullClassNameCat(propName, prop._class.fullName, false);
12164                               strcat(propName, "_");
12165                               // strcat(propName, prop.name);
12166                               FullClassNameCat(propName, prop.name, true);
12167
12168                               ListAdd(args, CopyExpression(object));
12169                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12170                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12171                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12172
12173                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12174                            }
12175                            else
12176                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12177                         }
12178                      }
12179                   }
12180                   else
12181                      Compiler_Error($"Invalid watched object\n");
12182                }
12183
12184                curExternal = external;
12185                curContext = context;
12186
12187                if(watcher)
12188                   FreeExpression(watcher);
12189                if(object)
12190                   FreeExpression(object);
12191                FreeList(watches, FreePropertyWatch);
12192             }
12193             else
12194                Compiler_Error($"No observer specified and not inside a _class\n");
12195          }
12196          else
12197          {
12198             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12199             {
12200                ProcessStatement(propWatch.compound);
12201             }
12202
12203          }
12204          break;
12205       }
12206       case fireWatchersStmt:
12207       {
12208          OldList * watches = stmt._watch.watches;
12209          Expression object = stmt._watch.object;
12210          Class _class;
12211          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12212          // printf("%X\n", watches);
12213          // printf("%X\n", stmt._watch.watches);
12214          if(object)
12215             ProcessExpressionType(object);
12216
12217          if(inCompiler)
12218          {
12219             _class = object ?
12220                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12221
12222             if(_class)
12223             {
12224                Identifier propID;
12225
12226                stmt.type = expressionStmt;
12227                stmt.expressions = MkList();
12228
12229                // Check if we're inside a property set
12230                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12231                {
12232                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12233                }
12234                else if(!watches)
12235                {
12236                   //Compiler_Error($"No property specified and not inside a property set\n");
12237                }
12238                if(watches)
12239                {
12240                   for(propID = watches->first; propID; propID = propID.next)
12241                   {
12242                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12243                      if(prop)
12244                      {
12245                         CreateFireWatcher(prop, object, stmt);
12246                      }
12247                      else
12248                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12249                   }
12250                }
12251                else
12252                {
12253                   // Fire all properties!
12254                   Property prop;
12255                   Class base;
12256                   for(base = _class; base; base = base.base)
12257                   {
12258                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12259                      {
12260                         if(prop.isProperty && prop.isWatchable)
12261                         {
12262                            CreateFireWatcher(prop, object, stmt);
12263                         }
12264                      }
12265                   }
12266                }
12267
12268                if(object)
12269                   FreeExpression(object);
12270                FreeList(watches, FreeIdentifier);
12271             }
12272             else
12273                Compiler_Error($"Invalid object specified and not inside a class\n");
12274          }
12275          break;
12276       }
12277       case stopWatchingStmt:
12278       {
12279          OldList * watches = stmt._watch.watches;
12280          Expression object = stmt._watch.object;
12281          Expression watcher = stmt._watch.watcher;
12282          Class _class;
12283          if(object)
12284             ProcessExpressionType(object);
12285          if(watcher)
12286             ProcessExpressionType(watcher);
12287          if(inCompiler)
12288          {
12289             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12290
12291             if(watcher || thisClass)
12292             {
12293                if(_class)
12294                {
12295                   Identifier propID;
12296
12297                   stmt.type = expressionStmt;
12298                   stmt.expressions = MkList();
12299
12300                   if(!watches)
12301                   {
12302                      OldList * args;
12303                      // eInstance_StopWatching(object, null, watcher);
12304                      args = MkList();
12305                      ListAdd(args, CopyExpression(object));
12306                      ListAdd(args, MkExpConstant("0"));
12307                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12308                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12309                   }
12310                   else
12311                   {
12312                      for(propID = watches->first; propID; propID = propID.next)
12313                      {
12314                         char propName[1024];
12315                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12316                         if(prop)
12317                         {
12318                            char getName[1024], setName[1024];
12319                            OldList * args = MkList();
12320
12321                            DeclareProperty(prop, setName, getName);
12322
12323                            // eInstance_StopWatching(object, prop, watcher);
12324                            strcpy(propName, "__ecereProp_");
12325                            FullClassNameCat(propName, prop._class.fullName, false);
12326                            strcat(propName, "_");
12327                            // strcat(propName, prop.name);
12328                            FullClassNameCat(propName, prop.name, true);
12329                            MangleClassName(propName);
12330
12331                            ListAdd(args, CopyExpression(object));
12332                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12333                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12334                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12335                         }
12336                         else
12337                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12338                      }
12339                   }
12340
12341                   if(object)
12342                      FreeExpression(object);
12343                   if(watcher)
12344                      FreeExpression(watcher);
12345                   FreeList(watches, FreeIdentifier);
12346                }
12347                else
12348                   Compiler_Error($"Invalid object specified and not inside a class\n");
12349             }
12350             else
12351                Compiler_Error($"No observer specified and not inside a class\n");
12352          }
12353          break;
12354       }
12355    }
12356 }
12357
12358 static void ProcessFunction(FunctionDefinition function)
12359 {
12360    Identifier id = GetDeclId(function.declarator);
12361    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12362    Type type = symbol ? symbol.type : null;
12363    Class oldThisClass = thisClass;
12364    Context oldTopContext = topContext;
12365
12366    yylloc = function.loc;
12367    // Process thisClass
12368
12369    if(type && type.thisClass)
12370    {
12371       Symbol classSym = type.thisClass;
12372       Class _class = type.thisClass.registered;
12373       char className[1024];
12374       char structName[1024];
12375       Declarator funcDecl;
12376       Symbol thisSymbol;
12377
12378       bool typedObject = false;
12379
12380       if(_class && !_class.base)
12381       {
12382          _class = currentClass;
12383          if(_class && !_class.symbol)
12384             _class.symbol = FindClass(_class.fullName);
12385          classSym = _class ? _class.symbol : null;
12386          typedObject = true;
12387       }
12388
12389       thisClass = _class;
12390
12391       if(inCompiler && _class)
12392       {
12393          if(type.kind == functionType)
12394          {
12395             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12396             {
12397                //TypeName param = symbol.type.params.first;
12398                Type param = symbol.type.params.first;
12399                symbol.type.params.Remove(param);
12400                //FreeTypeName(param);
12401                FreeType(param);
12402             }
12403             if(type.classObjectType != classPointer)
12404             {
12405                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12406                symbol.type.staticMethod = true;
12407                symbol.type.thisClass = null;
12408
12409                // HIGH DANGER: VERIFYING THIS...
12410                symbol.type.extraParam = false;
12411             }
12412          }
12413
12414          strcpy(className, "__ecereClass_");
12415          FullClassNameCat(className, _class.fullName, true);
12416
12417          MangleClassName(className);
12418
12419          structName[0] = 0;
12420          FullClassNameCat(structName, _class.fullName, false);
12421
12422          // [class] this
12423
12424
12425          funcDecl = GetFuncDecl(function.declarator);
12426          if(funcDecl)
12427          {
12428             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12429             {
12430                TypeName param = funcDecl.function.parameters->first;
12431                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12432                {
12433                   funcDecl.function.parameters->Remove(param);
12434                   FreeTypeName(param);
12435                }
12436             }
12437
12438             // DANGER: Watch for this... Check if it's a Conversion?
12439             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12440
12441             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12442             if(!function.propertyNoThis)
12443             {
12444                TypeName thisParam;
12445
12446                if(type.classObjectType != classPointer)
12447                {
12448                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12449                   if(!funcDecl.function.parameters)
12450                      funcDecl.function.parameters = MkList();
12451                   funcDecl.function.parameters->Insert(null, thisParam);
12452                }
12453
12454                if(typedObject)
12455                {
12456                   if(type.classObjectType != classPointer)
12457                   {
12458                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12459                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12460                   }
12461
12462                   thisParam = TypeName
12463                   {
12464                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12465                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12466                   };
12467                   funcDecl.function.parameters->Insert(null, thisParam);
12468                }
12469             }
12470          }
12471
12472          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12473          {
12474             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12475             funcDecl = GetFuncDecl(initDecl.declarator);
12476             if(funcDecl)
12477             {
12478                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12479                {
12480                   TypeName param = funcDecl.function.parameters->first;
12481                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12482                   {
12483                      funcDecl.function.parameters->Remove(param);
12484                      FreeTypeName(param);
12485                   }
12486                }
12487
12488                if(type.classObjectType != classPointer)
12489                {
12490                   // DANGER: Watch for this... Check if it's a Conversion?
12491                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12492                   {
12493                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12494
12495                      if(!funcDecl.function.parameters)
12496                         funcDecl.function.parameters = MkList();
12497                      funcDecl.function.parameters->Insert(null, thisParam);
12498                   }
12499                }
12500             }
12501          }
12502       }
12503
12504       // Add this to the context
12505       if(function.body)
12506       {
12507          if(type.classObjectType != classPointer)
12508          {
12509             thisSymbol = Symbol
12510             {
12511                string = CopyString("this");
12512                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12513             };
12514             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12515
12516             if(typedObject && thisSymbol.type)
12517             {
12518                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12519                thisSymbol.type.byReference = type.byReference;
12520                thisSymbol.type.typedByReference = type.byReference;
12521                /*
12522                thisSymbol = Symbol { string = CopyString("class") };
12523                function.body.compound.context.symbols.Add(thisSymbol);
12524                */
12525             }
12526          }
12527       }
12528
12529       // Pointer to class data
12530
12531       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12532       {
12533          DataMember member = null;
12534          {
12535             Class base;
12536             for(base = _class; base && base.type != systemClass; base = base.next)
12537             {
12538                for(member = base.membersAndProperties.first; member; member = member.next)
12539                   if(!member.isProperty)
12540                      break;
12541                if(member)
12542                   break;
12543             }
12544          }
12545          for(member = _class.membersAndProperties.first; member; member = member.next)
12546             if(!member.isProperty)
12547                break;
12548          if(member)
12549          {
12550             char pointerName[1024];
12551
12552             Declaration decl;
12553             Initializer initializer;
12554             Expression exp, bytePtr;
12555
12556             strcpy(pointerName, "__ecerePointer_");
12557             FullClassNameCat(pointerName, _class.fullName, false);
12558             {
12559                char className[1024];
12560                strcpy(className, "__ecereClass_");
12561                FullClassNameCat(className, classSym.string, true);
12562                MangleClassName(className);
12563
12564                // Testing This
12565                DeclareClass(classSym, className);
12566             }
12567
12568             // ((byte *) this)
12569             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12570
12571             if(_class.fixed)
12572             {
12573                char string[256];
12574                sprintf(string, "%d", _class.offset);
12575                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12576             }
12577             else
12578             {
12579                // ([bytePtr] + [className]->offset)
12580                exp = QBrackets(MkExpOp(bytePtr, '+',
12581                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12582             }
12583
12584             // (this ? [exp] : 0)
12585             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12586             exp.expType = Type
12587             {
12588                refCount = 1;
12589                kind = pointerType;
12590                type = Type { refCount = 1, kind = voidType };
12591             };
12592
12593             if(function.body)
12594             {
12595                yylloc = function.body.loc;
12596                // ([structName] *) [exp]
12597                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12598                initializer = MkInitializerAssignment(
12599                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12600
12601                // [structName] * [pointerName] = [initializer];
12602                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12603
12604                {
12605                   Context prevContext = curContext;
12606                   curContext = function.body.compound.context;
12607
12608                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12609                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12610
12611                   curContext = prevContext;
12612                }
12613
12614                // WHY?
12615                decl.symbol = null;
12616
12617                if(!function.body.compound.declarations)
12618                   function.body.compound.declarations = MkList();
12619                function.body.compound.declarations->Insert(null, decl);
12620             }
12621          }
12622       }
12623
12624
12625       // Loop through the function and replace undeclared identifiers
12626       // which are a member of the class (methods, properties or data)
12627       // by "this.[member]"
12628    }
12629    else
12630       thisClass = null;
12631
12632    if(id)
12633    {
12634       FreeSpecifier(id._class);
12635       id._class = null;
12636
12637       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12638       {
12639          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12640          id = GetDeclId(initDecl.declarator);
12641
12642          FreeSpecifier(id._class);
12643          id._class = null;
12644       }
12645    }
12646    if(function.body)
12647       topContext = function.body.compound.context;
12648    {
12649       FunctionDefinition oldFunction = curFunction;
12650       curFunction = function;
12651       if(function.body)
12652          ProcessStatement(function.body);
12653
12654       // If this is a property set and no firewatchers has been done yet, add one here
12655       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12656       {
12657          Statement prevCompound = curCompound;
12658          Context prevContext = curContext;
12659
12660          Statement fireWatchers = MkFireWatchersStmt(null, null);
12661          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12662          ListAdd(function.body.compound.statements, fireWatchers);
12663
12664          curCompound = function.body;
12665          curContext = function.body.compound.context;
12666
12667          ProcessStatement(fireWatchers);
12668
12669          curContext = prevContext;
12670          curCompound = prevCompound;
12671
12672       }
12673
12674       curFunction = oldFunction;
12675    }
12676
12677    if(function.declarator)
12678    {
12679       ProcessDeclarator(function.declarator);
12680    }
12681
12682    topContext = oldTopContext;
12683    thisClass = oldThisClass;
12684 }
12685
12686 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12687 static void ProcessClass(OldList definitions, Symbol symbol)
12688 {
12689    ClassDef def;
12690    External external = curExternal;
12691    Class regClass = symbol ? symbol.registered : null;
12692
12693    // Process all functions
12694    for(def = definitions.first; def; def = def.next)
12695    {
12696       if(def.type == functionClassDef)
12697       {
12698          if(def.function.declarator)
12699             curExternal = def.function.declarator.symbol.pointerExternal;
12700          else
12701             curExternal = external;
12702
12703          ProcessFunction((FunctionDefinition)def.function);
12704       }
12705       else if(def.type == declarationClassDef)
12706       {
12707          if(def.decl.type == instDeclaration)
12708          {
12709             thisClass = regClass;
12710             ProcessInstantiationType(def.decl.inst);
12711             thisClass = null;
12712          }
12713          // Testing this
12714          else
12715          {
12716             Class backThisClass = thisClass;
12717             if(regClass) thisClass = regClass;
12718             ProcessDeclaration(def.decl);
12719             thisClass = backThisClass;
12720          }
12721       }
12722       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12723       {
12724          MemberInit defProperty;
12725
12726          // Add this to the context
12727          Symbol thisSymbol = Symbol
12728          {
12729             string = CopyString("this");
12730             type = regClass ? MkClassType(regClass.fullName) : null;
12731          };
12732          globalContext.symbols.Add((BTNode)thisSymbol);
12733
12734          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12735          {
12736             thisClass = regClass;
12737             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12738             thisClass = null;
12739          }
12740
12741          globalContext.symbols.Remove((BTNode)thisSymbol);
12742          FreeSymbol(thisSymbol);
12743       }
12744       else if(def.type == propertyClassDef && def.propertyDef)
12745       {
12746          PropertyDef prop = def.propertyDef;
12747
12748          // Add this to the context
12749          /*
12750          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12751          globalContext.symbols.Add(thisSymbol);
12752          */
12753
12754          thisClass = regClass;
12755          if(prop.setStmt)
12756          {
12757             if(regClass)
12758             {
12759                Symbol thisSymbol
12760                {
12761                   string = CopyString("this");
12762                   type = MkClassType(regClass.fullName);
12763                };
12764                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12765             }
12766
12767             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12768             ProcessStatement(prop.setStmt);
12769          }
12770          if(prop.getStmt)
12771          {
12772             if(regClass)
12773             {
12774                Symbol thisSymbol
12775                {
12776                   string = CopyString("this");
12777                   type = MkClassType(regClass.fullName);
12778                };
12779                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12780             }
12781
12782             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12783             ProcessStatement(prop.getStmt);
12784          }
12785          if(prop.issetStmt)
12786          {
12787             if(regClass)
12788             {
12789                Symbol thisSymbol
12790                {
12791                   string = CopyString("this");
12792                   type = MkClassType(regClass.fullName);
12793                };
12794                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12795             }
12796
12797             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12798             ProcessStatement(prop.issetStmt);
12799          }
12800
12801          thisClass = null;
12802
12803          /*
12804          globalContext.symbols.Remove(thisSymbol);
12805          FreeSymbol(thisSymbol);
12806          */
12807       }
12808       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12809       {
12810          PropertyWatch propertyWatch = def.propertyWatch;
12811
12812          thisClass = regClass;
12813          if(propertyWatch.compound)
12814          {
12815             Symbol thisSymbol
12816             {
12817                string = CopyString("this");
12818                type = regClass ? MkClassType(regClass.fullName) : null;
12819             };
12820
12821             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12822
12823             curExternal = null;
12824             ProcessStatement(propertyWatch.compound);
12825          }
12826          thisClass = null;
12827       }
12828    }
12829 }
12830
12831 void DeclareFunctionUtil(String s)
12832 {
12833    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12834    if(function)
12835    {
12836       char name[1024];
12837       name[0] = 0;
12838       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12839          strcpy(name, "__ecereFunction_");
12840       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12841       DeclareFunction(function, name);
12842    }
12843 }
12844
12845 void ComputeDataTypes()
12846 {
12847    External external;
12848    External temp { };
12849    External after = null;
12850
12851    currentClass = null;
12852
12853    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12854
12855    for(external = ast->first; external; external = external.next)
12856    {
12857       if(external.type == declarationExternal)
12858       {
12859          Declaration decl = external.declaration;
12860          if(decl)
12861          {
12862             OldList * decls = decl.declarators;
12863             if(decls)
12864             {
12865                InitDeclarator initDecl = decls->first;
12866                if(initDecl)
12867                {
12868                   Declarator declarator = initDecl.declarator;
12869                   if(declarator && declarator.type == identifierDeclarator)
12870                   {
12871                      Identifier id = declarator.identifier;
12872                      if(id && id.string)
12873                      {
12874                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12875                         {
12876                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12877                            after = external;
12878                         }
12879                      }
12880                   }
12881                }
12882             }
12883          }
12884        }
12885    }
12886
12887    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12888    ast->Insert(after, temp);
12889    curExternal = temp;
12890
12891    DeclareFunctionUtil("eSystem_New");
12892    DeclareFunctionUtil("eSystem_New0");
12893    DeclareFunctionUtil("eSystem_Renew");
12894    DeclareFunctionUtil("eSystem_Renew0");
12895    DeclareFunctionUtil("eSystem_Delete");
12896    DeclareFunctionUtil("eClass_GetProperty");
12897    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12898
12899    DeclareStruct("ecere::com::Class", false);
12900    DeclareStruct("ecere::com::Instance", false);
12901    DeclareStruct("ecere::com::Property", false);
12902    DeclareStruct("ecere::com::DataMember", false);
12903    DeclareStruct("ecere::com::Method", false);
12904    DeclareStruct("ecere::com::SerialBuffer", false);
12905    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12906
12907    ast->Remove(temp);
12908
12909    for(external = ast->first; external; external = external.next)
12910    {
12911       afterExternal = curExternal = external;
12912       if(external.type == functionExternal)
12913       {
12914          currentClass = external.function._class;
12915          ProcessFunction(external.function);
12916       }
12917       // There shouldn't be any _class member access here anyways...
12918       else if(external.type == declarationExternal)
12919       {
12920          currentClass = null;
12921          if(external.declaration)
12922             ProcessDeclaration(external.declaration);
12923       }
12924       else if(external.type == classExternal)
12925       {
12926          ClassDefinition _class = external._class;
12927          currentClass = external.symbol.registered;
12928          if(_class.definitions)
12929          {
12930             ProcessClass(_class.definitions, _class.symbol);
12931          }
12932          if(inCompiler)
12933          {
12934             // Free class data...
12935             ast->Remove(external);
12936             delete external;
12937          }
12938       }
12939       else if(external.type == nameSpaceExternal)
12940       {
12941          thisNameSpace = external.id.string;
12942       }
12943    }
12944    currentClass = null;
12945    thisNameSpace = null;
12946    curExternal = null;
12947
12948    delete temp.symbol;
12949    delete temp;
12950 }