1d9cfdd3914cccfaa2fafbae985f84dbac5849e8
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 // UNTIL IMPLEMENTED IN GRAMMAR
4 #define ACCESS_CLASSDATA(_class, baseClass) \
5    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
6
7 #define YYLTYPE Location
8 #include "grammar.h"
9
10 extern OldList * ast;
11 extern int returnCode;
12 extern Expression parsedExpression;
13 extern bool yydebug;
14 public void SetYydebug(bool b) { yydebug = b; }
15 extern bool echoOn;
16
17 void resetScanner();
18
19 // TODO: Reset this to 0 on reinitialization
20 int propWatcherID;
21
22 int expression_yyparse();
23 static Statement curCompound;
24 External curExternal, afterExternal;
25 static Type curSwitchType;
26 static Class currentClass;
27 Class thisClass;
28 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
29 static char * thisNameSpace;
30 /*static */Class containerClass;
31 bool thisClassParams = true;
32
33 uint internalValueCounter;
34
35 #ifdef _DEBUG
36 Time findSymbolTotalTime;
37 #endif
38
39 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
40 /*static */public void PrintExpression(Expression exp, char * string)
41 {
42    //if(inCompiler)
43    {
44       TempFile f { };
45       int count;
46       bool backOutputLineNumbers = outputLineNumbers;
47       outputLineNumbers = false;
48
49       if(exp)
50          OutputExpression(exp, f);
51       f.Seek(0, start);
52       count = strlen(string);
53       count += f.Read(string + count, 1, 1023);
54       string[count] = '\0';
55       delete f;
56
57       outputLineNumbers = backOutputLineNumbers;
58    }
59 }
60
61 Type ProcessTemplateParameterType(TemplateParameter param)
62 {
63    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
64    {
65       // TOFIX: Will need to free this Type
66       if(!param.baseType)
67       {
68          if(param.dataTypeString)
69             param.baseType = ProcessTypeString(param.dataTypeString, false);
70          else
71             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
72       }
73       return param.baseType;
74    }
75    return null;
76 }
77
78 bool NeedCast(Type type1, Type type2)
79 {
80    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
81
82    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
83    {
84       return false;
85    }
86
87    if(type1.kind == type2.kind && type1.isLong == type2.isLong)
88    {
89       switch(type1.kind)
90       {
91          case _BoolType:
92          case charType:
93          case shortType:
94          case intType:
95          case int64Type:
96          case intPtrType:
97          case intSizeType:
98             if(type1.passAsTemplate && !type2.passAsTemplate)
99                return true;
100             return type1.isSigned != type2.isSigned;
101          case classType:
102             return type1._class != type2._class;
103          case pointerType:
104             return (type1.type && type2.type && type1.type.constant != type2.type.constant) || NeedCast(type1.type, type2.type);
105          default:
106             return true; //false; ????
107       }
108    }
109    return true;
110 }
111
112 static void ReplaceClassMembers(Expression exp, Class _class)
113 {
114    if(exp.type == identifierExp && exp.identifier)
115    {
116       Identifier id = exp.identifier;
117       Context ctx;
118       Symbol symbol = null;
119       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
120       {
121          // First, check if the identifier is declared inside the function
122          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
123          {
124             if(!ctx) break;   // This happened opening old mapTileCache.ec from archives?
125             symbol = (Symbol)ctx.symbols.FindString(id.string);
126             if(symbol) break;
127          }
128       }
129
130       // If it is not, check if it is a member of the _class
131       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
132       {
133          Property prop = eClass_FindProperty(_class, id.string, privateModule);
134          Method method = null;
135          DataMember member = null;
136          ClassProperty classProp = null;
137          if(!prop)
138          {
139             method = eClass_FindMethod(_class, id.string, privateModule);
140          }
141          if(!prop && !method)
142             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
143          if(!prop && !method && !member)
144          {
145             classProp = eClass_FindClassProperty(_class, id.string);
146          }
147          if(prop || method || member || classProp)
148          {
149             // Replace by this.[member]
150             exp.type = memberExp;
151             exp.member.member = id;
152             exp.member.memberType = unresolvedMember;
153             exp.member.exp = QMkExpId("this");
154             //exp.member.exp.loc = exp.loc;
155             exp.addedThis = true;
156          }
157          else if(_class && _class.templateParams.first)
158          {
159             Class sClass;
160             for(sClass = _class; sClass; sClass = sClass.base)
161             {
162                if(sClass.templateParams.first)
163                {
164                   ClassTemplateParameter param;
165                   for(param = sClass.templateParams.first; param; param = param.next)
166                   {
167                      if(param.type == expression && !strcmp(param.name, id.string))
168                      {
169                         Expression argExp = GetTemplateArgExpByName(param.name, null, _class, TemplateParameterType::expression);
170
171                         if(argExp)
172                         {
173                            Declarator decl;
174                            OldList * specs = MkList();
175
176                            FreeIdentifier(exp.member.member);
177
178                            ProcessExpressionType(argExp);
179
180                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
181
182                            exp.expType = ProcessType(specs, decl);
183
184                            // *[expType] *[argExp]
185                            exp.type = bracketsExp;
186                            exp.list = MkListOne(MkExpOp(null, '*',
187                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
188                         }
189                      }
190                   }
191                }
192             }
193          }
194       }
195    }
196 }
197
198 ////////////////////////////////////////////////////////////////////////
199 // PRINTING ////////////////////////////////////////////////////////////
200 ////////////////////////////////////////////////////////////////////////
201
202 public char * PrintInt(int64 result)
203 {
204    char temp[100];
205    if(result > MAXINT)
206       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
207    else
208       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
209    if(result > MAXINT || result < MININT)
210       strcat(temp, "LL");
211    return CopyString(temp);
212 }
213
214 public char * PrintUInt(uint64 result)
215 {
216    char temp[100];
217    if(result > MAXDWORD)
218       sprintf(temp, FORMAT64HEXLL /*"0x%I64X"*/, result);
219    else if(result > MAXINT)
220       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
221    else
222       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
223    return CopyString(temp);
224 }
225
226 public char *  PrintInt64(int64 result)
227 {
228    char temp[100];
229    if(result > MAXINT || result < MININT)
230       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
231    else
232       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
233    return CopyString(temp);
234 }
235
236 public char * PrintUInt64(uint64 result)
237 {
238    char temp[100];
239    if(result > MAXDWORD)
240       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
241    else if(result > MAXINT)
242       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
243    else
244       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
245    return CopyString(temp);
246 }
247
248 public char * PrintHexUInt(uint64 result)
249 {
250    char temp[100];
251    if(result > MAXDWORD)
252       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
253    else
254       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
255    if(result > MAXDWORD)
256       strcat(temp, "LL");
257    return CopyString(temp);
258 }
259
260 public char * PrintHexUInt64(uint64 result)
261 {
262    char temp[100];
263    if(result > MAXDWORD)
264       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
265    else
266       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
267    return CopyString(temp);
268 }
269
270 public char * PrintShort(short result)
271 {
272    char temp[100];
273    sprintf(temp, "%d", (unsigned short)result);
274    return CopyString(temp);
275 }
276
277 public char * PrintUShort(unsigned short result)
278 {
279    char temp[100];
280    if(result > 32767)
281       sprintf(temp, "0x%X", (int)result);
282    else
283       sprintf(temp, "%d", (int)result);
284    return CopyString(temp);
285 }
286
287 public char * PrintChar(char result)
288 {
289    char temp[100];
290    if(result > 0 && isprint(result))
291       sprintf(temp, "'%c'", result);
292    else if(result < 0)
293       sprintf(temp, "%d", (int)result);
294    else
295       //sprintf(temp, "%#X", result);
296       sprintf(temp, "0x%X", (unsigned char)result);
297    return CopyString(temp);
298 }
299
300 public char * PrintUChar(unsigned char result)
301 {
302    char temp[100];
303    sprintf(temp, "0x%X", result);
304    return CopyString(temp);
305 }
306
307 public char * PrintFloat(float result)
308 {
309    char temp[350];
310    if(result.isInf)
311    {
312       if(result.signBit)
313          strcpy(temp, "-inf");
314       else
315          strcpy(temp, "inf");
316    }
317    else if(result.isNan)
318    {
319       if(result.signBit)
320          strcpy(temp, "-nan");
321       else
322          strcpy(temp, "nan");
323    }
324    else
325       sprintf(temp, "%.16ff", result);
326    return CopyString(temp);
327 }
328
329 public char * PrintDouble(double result)
330 {
331    char temp[350];
332    if(result.isInf)
333    {
334       if(result.signBit)
335          strcpy(temp, "-inf");
336       else
337          strcpy(temp, "inf");
338    }
339    else if(result.isNan)
340    {
341       if(result.signBit)
342          strcpy(temp, "-nan");
343       else
344          strcpy(temp, "nan");
345    }
346    else
347       sprintf(temp, "%.16f", result);
348    return CopyString(temp);
349 }
350
351 ////////////////////////////////////////////////////////////////////////
352 ////////////////////////////////////////////////////////////////////////
353
354 //public Operand GetOperand(Expression exp);
355
356 #define GETVALUE(name, t) \
357    public bool GetOp##name(Operand op2, t * value2) \
358    {                                                        \
359       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
360       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
361       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
362       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
363       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
364       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
365       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
366       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
367       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
368       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
369       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
370       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
371       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
372       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
373       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
374       else                                                                          \
375          return false;                                                              \
376       return true;                                                                  \
377    } \
378    public bool Get##name(Expression exp, t * value2) \
379    {                                                        \
380       Operand op2 = GetOperand(exp);                        \
381       return GetOp##name(op2, value2); \
382    }
383
384 // To help the debugger currently not preprocessing...
385 #define HELP(x) x
386
387 GETVALUE(Int, HELP(int));
388 GETVALUE(UInt, HELP(unsigned int));
389 GETVALUE(Int64, HELP(int64));
390 GETVALUE(UInt64, HELP(uint64));
391 GETVALUE(IntPtr, HELP(intptr));
392 GETVALUE(UIntPtr, HELP(uintptr));
393 GETVALUE(IntSize, HELP(intsize));
394 GETVALUE(UIntSize, HELP(uintsize));
395 GETVALUE(Short, HELP(short));
396 GETVALUE(UShort, HELP(unsigned short));
397 GETVALUE(Char, HELP(char));
398 GETVALUE(UChar, HELP(unsigned char));
399 GETVALUE(Float, HELP(float));
400 GETVALUE(Double, HELP(double));
401
402 void ComputeExpression(Expression exp);
403
404 void ComputeClassMembers(Class _class, bool isMember)
405 {
406    DataMember member = isMember ? (DataMember) _class : null;
407    Context context = isMember ? null : SetupTemplatesContext(_class);
408    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
409                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
410    {
411       int unionMemberOffset = 0;
412       int bitFields = 0;
413
414       /*
415       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
416          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
417       */
418
419       if(member)
420       {
421          member.memberOffset = 0;
422          if(targetBits < sizeof(void *) * 8)
423             member.structAlignment = 0;
424       }
425       else if(targetBits < sizeof(void *) * 8)
426          _class.structAlignment = 0;
427
428       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
429       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
430          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
431
432       if(!member && _class.destructionWatchOffset)
433          _class.memberOffset += sizeof(OldList);
434
435       // To avoid reentrancy...
436       //_class.structSize = -1;
437
438       {
439          DataMember dataMember;
440          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
441          {
442             if(!dataMember.isProperty)
443             {
444                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
445                {
446                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
447                   /*if(!dataMember.dataType)
448                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
449                      */
450                }
451             }
452          }
453       }
454
455       {
456          DataMember dataMember;
457          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
458          {
459             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
460             {
461                if(!isMember && _class.type == bitClass && dataMember.dataType)
462                {
463                   BitMember bitMember = (BitMember) dataMember;
464                   uint64 mask = 0;
465                   int d;
466
467                   ComputeTypeSize(dataMember.dataType);
468
469                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
470                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
471
472                   _class.memberOffset = bitMember.pos + bitMember.size;
473                   for(d = 0; d<bitMember.size; d++)
474                   {
475                      if(d)
476                         mask <<= 1;
477                      mask |= 1;
478                   }
479                   bitMember.mask = mask << bitMember.pos;
480                }
481                else if(dataMember.type == normalMember && dataMember.dataType)
482                {
483                   int size;
484                   int alignment = 0;
485
486                   // Prevent infinite recursion
487                   if(dataMember.dataType.kind != classType ||
488                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
489                      _class.type != structClass)))
490                      ComputeTypeSize(dataMember.dataType);
491
492                   if(dataMember.dataType.bitFieldCount)
493                   {
494                      bitFields += dataMember.dataType.bitFieldCount;
495                      size = 0;
496                   }
497                   else
498                   {
499                      if(bitFields)
500                      {
501                         int size = (bitFields + 7) / 8;
502
503                         if(isMember)
504                         {
505                            // TESTING THIS PADDING CODE
506                            if(alignment)
507                            {
508                               member.structAlignment = Max(member.structAlignment, alignment);
509
510                               if(member.memberOffset % alignment)
511                                  member.memberOffset += alignment - (member.memberOffset % alignment);
512                            }
513
514                            dataMember.offset = member.memberOffset;
515                            if(member.type == unionMember)
516                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
517                            else
518                            {
519                               member.memberOffset += size;
520                            }
521                         }
522                         else
523                         {
524                            // TESTING THIS PADDING CODE
525                            if(alignment)
526                            {
527                               _class.structAlignment = Max(_class.structAlignment, alignment);
528
529                               if(_class.memberOffset % alignment)
530                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
531                            }
532
533                            dataMember.offset = _class.memberOffset;
534                            _class.memberOffset += size;
535                         }
536                         bitFields = 0;
537                      }
538                      size = dataMember.dataType.size;
539                      alignment = dataMember.dataType.alignment;
540                   }
541
542                   if(isMember)
543                   {
544                      // TESTING THIS PADDING CODE
545                      if(alignment)
546                      {
547                         member.structAlignment = Max(member.structAlignment, alignment);
548
549                         if(member.memberOffset % alignment)
550                            member.memberOffset += alignment - (member.memberOffset % alignment);
551                      }
552
553                      dataMember.offset = member.memberOffset;
554                      if(member.type == unionMember)
555                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
556                      else
557                      {
558                         member.memberOffset += size;
559                      }
560                   }
561                   else
562                   {
563                      // TESTING THIS PADDING CODE
564                      if(alignment)
565                      {
566                         _class.structAlignment = Max(_class.structAlignment, alignment);
567
568                         if(_class.memberOffset % alignment)
569                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
570                      }
571
572                      dataMember.offset = _class.memberOffset;
573                      _class.memberOffset += size;
574                   }
575                }
576                else
577                {
578                   int alignment;
579
580                   ComputeClassMembers((Class)dataMember, true);
581                   alignment = dataMember.structAlignment;
582
583                   if(isMember)
584                   {
585                      if(alignment)
586                      {
587                         if(member.memberOffset % alignment)
588                            member.memberOffset += alignment - (member.memberOffset % alignment);
589
590                         member.structAlignment = Max(member.structAlignment, alignment);
591                      }
592                      dataMember.offset = member.memberOffset;
593                      if(member.type == unionMember)
594                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
595                      else
596                         member.memberOffset += dataMember.memberOffset;
597                   }
598                   else
599                   {
600                      if(alignment)
601                      {
602                         if(_class.memberOffset % alignment)
603                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
604                         _class.structAlignment = Max(_class.structAlignment, alignment);
605                      }
606                      dataMember.offset = _class.memberOffset;
607                      _class.memberOffset += dataMember.memberOffset;
608                   }
609                }
610             }
611          }
612          if(bitFields)
613          {
614             int alignment = 0;
615             int size = (bitFields + 7) / 8;
616
617             if(isMember)
618             {
619                // TESTING THIS PADDING CODE
620                if(alignment)
621                {
622                   member.structAlignment = Max(member.structAlignment, alignment);
623
624                   if(member.memberOffset % alignment)
625                      member.memberOffset += alignment - (member.memberOffset % alignment);
626                }
627
628                if(member.type == unionMember)
629                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
630                else
631                {
632                   member.memberOffset += size;
633                }
634             }
635             else
636             {
637                // TESTING THIS PADDING CODE
638                if(alignment)
639                {
640                   _class.structAlignment = Max(_class.structAlignment, alignment);
641
642                   if(_class.memberOffset % alignment)
643                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
644                }
645                _class.memberOffset += size;
646             }
647             bitFields = 0;
648          }
649       }
650       if(member && member.type == unionMember)
651       {
652          member.memberOffset = unionMemberOffset;
653       }
654
655       if(!isMember)
656       {
657          /*if(_class.type == structClass)
658             _class.size = _class.memberOffset;
659          else
660          */
661
662          if(_class.type != bitClass)
663          {
664             int extra = 0;
665             if(_class.structAlignment)
666             {
667                if(_class.memberOffset % _class.structAlignment)
668                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
669             }
670             _class.structSize = (_class.base ? (_class.base.templateClass ?
671                (_class.base.type == noHeadClass ? _class.base.templateClass.memberOffset : _class.base.templateClass.structSize) :
672                   (_class.base.type == noHeadClass ? _class.base.memberOffset : _class.base.structSize) ) : 0) + _class.memberOffset + extra;
673             if(!member)
674             {
675                Property prop;
676                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
677                {
678                   if(prop.isProperty && prop.isWatchable)
679                   {
680                      prop.watcherOffset = _class.structSize;
681                      _class.structSize += sizeof(OldList);
682                   }
683                }
684             }
685
686             // Fix Derivatives
687             {
688                OldLink derivative;
689                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
690                {
691                   Class deriv = derivative.data;
692
693                   if(deriv.computeSize)
694                   {
695                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
696                      deriv.offset = /*_class.offset + */(_class.type == noHeadClass ? _class.memberOffset : _class.structSize);
697                      deriv.memberOffset = 0;
698                      // ----------------------
699
700                      deriv.structSize = deriv.offset;
701
702                      ComputeClassMembers(deriv, false);
703                   }
704                }
705             }
706          }
707       }
708    }
709    if(context)
710       FinishTemplatesContext(context);
711 }
712
713 public void ComputeModuleClasses(Module module)
714 {
715    Class _class;
716    OldLink subModule;
717
718    for(subModule = module.modules.first; subModule; subModule = subModule.next)
719       ComputeModuleClasses(subModule.data);
720    for(_class = module.classes.first; _class; _class = _class.next)
721       ComputeClassMembers(_class, false);
722 }
723
724
725 public int ComputeTypeSize(Type type)
726 {
727    uint size = type ? type.size : 0;
728    if(!size && type && !type.computing)
729    {
730       type.computing = true;
731       switch(type.kind)
732       {
733          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
734          case charType: type.alignment = size = sizeof(char); break;
735          case intType: type.alignment = size = sizeof(int); break;
736          case int64Type: type.alignment = size = sizeof(int64); break;
737          case intPtrType: type.alignment = size = targetBits / 8; type.pointerAlignment = true; break;
738          case intSizeType: type.alignment = size = targetBits / 8; type.pointerAlignment = true; break;
739          case longType: type.alignment = size = sizeof(long); break;
740          case shortType: type.alignment = size = sizeof(short); break;
741          case floatType: type.alignment = size = sizeof(float); break;
742          case doubleType: type.alignment = size = sizeof(double); break;
743          case classType:
744          {
745             Class _class = type._class ? type._class.registered : null;
746
747             if(_class && _class.type == structClass)
748             {
749                // Ensure all members are properly registered
750                ComputeClassMembers(_class, false);
751                type.alignment = _class.structAlignment;
752                type.pointerAlignment = (bool)_class.pointerAlignment;
753                size = _class.structSize;
754                if(type.alignment && size % type.alignment)
755                   size += type.alignment - (size % type.alignment);
756
757             }
758             else if(_class && (_class.type == unitClass ||
759                    _class.type == enumClass ||
760                    _class.type == bitClass))
761             {
762                if(!_class.dataType)
763                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
764                size = type.alignment = ComputeTypeSize(_class.dataType);
765             }
766             else
767             {
768                size = type.alignment = targetBits / 8; // sizeof(Instance *);
769                type.pointerAlignment = true;
770             }
771             break;
772          }
773          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */ type.pointerAlignment = true; break;
774          case arrayType:
775             if(type.arraySizeExp)
776             {
777                ProcessExpressionType(type.arraySizeExp);
778                ComputeExpression(type.arraySizeExp);
779                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType &&
780                   type.arraySizeExp.expType.kind != shortType &&
781                   type.arraySizeExp.expType.kind != charType &&
782                   type.arraySizeExp.expType.kind != longType &&
783                   type.arraySizeExp.expType.kind != int64Type &&
784                   type.arraySizeExp.expType.kind != intSizeType &&
785                   type.arraySizeExp.expType.kind != intPtrType &&
786                   type.arraySizeExp.expType.kind != enumType &&
787                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
788                {
789                   Location oldLoc = yylloc;
790                   // bool isConstant = type.arraySizeExp.isConstant;
791                   char expression[10240];
792                   expression[0] = '\0';
793                   type.arraySizeExp.expType = null;
794                   yylloc = type.arraySizeExp.loc;
795                   if(inCompiler)
796                      PrintExpression(type.arraySizeExp, expression);
797                   Compiler_Error($"Array size not constant int (%s)\n", expression);
798                   yylloc = oldLoc;
799                }
800                GetInt(type.arraySizeExp, &type.arraySize);
801             }
802             else if(type.enumClass)
803             {
804                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
805                {
806                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
807                }
808                else
809                   type.arraySize = 0;
810             }
811             else
812             {
813                // Unimplemented auto size
814                type.arraySize = 0;
815             }
816
817             size = ComputeTypeSize(type.type) * type.arraySize;
818             if(type.type)
819             {
820                type.alignment = type.type.alignment;
821                type.pointerAlignment = type.type.pointerAlignment;
822             }
823
824             break;
825          case structType:
826          {
827             if(!type.members.first && type.enumName)
828             {
829                Symbol symbol = FindStruct(curContext, type.enumName);
830                if(symbol && symbol.type)
831                {
832                   ComputeTypeSize(symbol.type);
833                   size = symbol.type.size;
834                }
835             }
836             else
837             {
838                Type member;
839                for(member = type.members.first; member; member = member.next)
840                {
841                   uint addSize = ComputeTypeSize(member);
842
843                   member.offset = size;
844                   if(member.alignment && size % member.alignment)
845                      member.offset += member.alignment - (size % member.alignment);
846                   size = member.offset;
847
848                   if(member.pointerAlignment && type.size <= 4)
849                      type.pointerAlignment = true;
850                   else if(!member.pointerAlignment && member.alignment >= 8)
851                      type.pointerAlignment = false;
852
853                   type.alignment = Max(type.alignment, member.alignment);
854                   size += addSize;
855                }
856                if(type.alignment && size % type.alignment)
857                   size += type.alignment - (size % type.alignment);
858             }
859             break;
860          }
861          case unionType:
862          {
863             if(!type.members.first && type.enumName)
864             {
865                Symbol symbol = FindStruct(curContext, type.enumName);
866                if(symbol && symbol.type)
867                {
868                   ComputeTypeSize(symbol.type);
869                   size = symbol.type.size;
870                   type.alignment = symbol.type.alignment;
871                }
872             }
873             else
874             {
875                Type member;
876                for(member = type.members.first; member; member = member.next)
877                {
878                   uint addSize = ComputeTypeSize(member);
879
880                   member.offset = size;
881                   if(member.alignment && size % member.alignment)
882                      member.offset += member.alignment - (size % member.alignment);
883                   size = member.offset;
884
885                   if(member.pointerAlignment && type.size <= 4)
886                      type.pointerAlignment = true;
887                   else if(!member.pointerAlignment && member.alignment >= 8)
888                      type.pointerAlignment = false;
889
890                   type.alignment = Max(type.alignment, member.alignment);
891
892                   size = Max(size, addSize);
893                }
894                if(type.alignment && size % type.alignment)
895                   size += type.alignment - (size % type.alignment);
896             }
897             break;
898          }
899          case templateType:
900          {
901             TemplateParameter param = type.templateParameter;
902             Type baseType = ProcessTemplateParameterType(param);
903             if(baseType)
904             {
905                size = ComputeTypeSize(baseType);
906                type.alignment = baseType.alignment;
907                type.pointerAlignment = baseType.pointerAlignment;
908             }
909             else
910                type.alignment = size = sizeof(uint64);
911             break;
912          }
913          case enumType:
914          {
915             type.alignment = size = sizeof(enum { test });
916             break;
917          }
918          case thisClassType:
919          {
920             type.alignment = size = targetBits / 8; //sizeof(void *);
921             type.pointerAlignment = true;
922             break;
923          }
924       }
925       type.size = size;
926       type.computing = false;
927    }
928    return size;
929 }
930
931
932 /*static */int AddMembers(External neededBy, OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
933 {
934    // This function is in need of a major review when implementing private members etc.
935    DataMember topMember = isMember ? (DataMember) _class : null;
936    uint totalSize = 0;
937    uint maxSize = 0;
938    int alignment;
939    uint size;
940    DataMember member;
941    int anonID = 1;
942    Context context = isMember ? null : SetupTemplatesContext(_class);
943    if(addedPadding)
944       *addedPadding = false;
945
946    if(!isMember && _class.base)
947    {
948       maxSize = _class.structSize;
949       //if(_class.base.type != systemClass) // Commented out with new Instance _class
950       {
951          // DANGER: Testing this noHeadClass here...
952          if(_class.type == structClass || _class.type == noHeadClass)
953             /*totalSize = */AddMembers(neededBy, declarations, _class.base, false, &totalSize, topClass, null);
954          else
955          {
956             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
957             if(maxSize > baseSize)
958                maxSize -= baseSize;
959             else
960                maxSize = 0;
961          }
962       }
963    }
964
965    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
966    {
967       if(!member.isProperty)
968       {
969          switch(member.type)
970          {
971             case normalMember:
972             {
973                if(member.dataTypeString)
974                {
975                   OldList * specs = MkList(), * decls = MkList();
976                   Declarator decl;
977
978                   decl = SpecDeclFromString(member.dataTypeString, specs,
979                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
980                   ListAdd(decls, MkStructDeclarator(decl, null));
981                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
982
983                   if(!member.dataType)
984                      member.dataType = ProcessType(specs, decl);
985
986                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
987
988                   {
989                      Type type = ProcessType(specs, decl);
990                      DeclareType(neededBy, member.dataType, true, false);
991                      FreeType(type);
992                   }
993                   /*
994                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
995                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
996                      DeclareStruct(member.dataType._class.string, false);
997                   */
998
999                   ComputeTypeSize(member.dataType);
1000                   size = member.dataType.size;
1001                   alignment = member.dataType.alignment;
1002
1003                   if(alignment)
1004                   {
1005                      if(totalSize % alignment)
1006                         totalSize += alignment - (totalSize % alignment);
1007                   }
1008                   totalSize += size;
1009                }
1010                break;
1011             }
1012             case unionMember:
1013             case structMember:
1014             {
1015                OldList * specs = MkList(), * list = MkList();
1016                char id[100];
1017                sprintf(id, "__anon%d", anonID++);
1018
1019                size = 0;
1020                AddMembers(neededBy, list, (Class)member, true, &size, topClass, null);
1021                ListAdd(specs,
1022                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
1023                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, MkListOne(MkDeclaratorIdentifier(MkIdentifier(id))),null)));
1024                alignment = member.structAlignment;
1025
1026                if(alignment)
1027                {
1028                   if(totalSize % alignment)
1029                      totalSize += alignment - (totalSize % alignment);
1030                }
1031                totalSize += size;
1032                break;
1033             }
1034          }
1035       }
1036    }
1037    if(retSize)
1038    {
1039       if(topMember && topMember.type == unionMember)
1040          *retSize = Max(*retSize, totalSize);
1041       else
1042          *retSize += totalSize;
1043    }
1044    else if(totalSize < maxSize && _class.type != systemClass)
1045    {
1046       int autoPadding = 0;
1047       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
1048          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
1049       if(totalSize + autoPadding < maxSize)
1050       {
1051          char sizeString[50];
1052          sprintf(sizeString, "%d", maxSize - totalSize);
1053          ListAdd(declarations,
1054             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
1055             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
1056          if(addedPadding)
1057             *addedPadding = true;
1058       }
1059    }
1060    if(context)
1061       FinishTemplatesContext(context);
1062    return topMember ? topMember.memberID : _class.memberID;
1063 }
1064
1065 static int DeclareMembers(External neededBy, Class _class, bool isMember)
1066 {
1067    DataMember topMember = isMember ? (DataMember) _class : null;
1068    DataMember member;
1069    Context context = isMember ? null : SetupTemplatesContext(_class);
1070
1071    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1072       DeclareMembers(neededBy, _class.base, false);
1073
1074    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1075    {
1076       if(!member.isProperty)
1077       {
1078          switch(member.type)
1079          {
1080             case normalMember:
1081             {
1082                if(!member.dataType && member.dataTypeString)
1083                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1084                if(member.dataType)
1085                   DeclareType(neededBy, member.dataType, true, false);
1086                break;
1087             }
1088             case unionMember:
1089             case structMember:
1090             {
1091                DeclareMembers(neededBy, (Class)member, true);
1092                break;
1093             }
1094          }
1095       }
1096    }
1097    if(context)
1098       FinishTemplatesContext(context);
1099
1100    return topMember ? topMember.memberID : _class.memberID;
1101 }
1102
1103 static void IdentifyAnonStructs(OldList/*<ClassDef>*/ *  definitions)
1104 {
1105    ClassDef def;
1106    int anonID = 1;
1107    for(def = definitions->first; def; def = def.next)
1108    {
1109       if(def.type == declarationClassDef)
1110       {
1111          Declaration decl = def.decl;
1112          if(decl && decl.specifiers)
1113          {
1114             Specifier spec;
1115             bool isStruct = false;
1116             for(spec = decl.specifiers->first; spec; spec = spec.next)
1117             {
1118                if(spec.type == structSpecifier || spec.type == unionSpecifier)
1119                {
1120                   if(spec.definitions)
1121                      IdentifyAnonStructs(spec.definitions);
1122                   isStruct = true;
1123                }
1124             }
1125             if(isStruct)
1126             {
1127                Declarator d = null;
1128                if(decl.declarators)
1129                {
1130                   for(d = decl.declarators->first; d; d = d.next)
1131                   {
1132                      Identifier idDecl = GetDeclId(d);
1133                      if(idDecl)
1134                         break;
1135                   }
1136                }
1137                if(!d)
1138                {
1139                   char id[100];
1140                   sprintf(id, "__anon%d", anonID++);
1141                   if(!decl.declarators)
1142                      decl.declarators = MkList();
1143                   ListAdd(decl.declarators, MkDeclaratorIdentifier(MkIdentifier(id)));
1144                }
1145             }
1146          }
1147       }
1148    }
1149 }
1150
1151 External DeclareStruct(External neededBy, const char * name, bool skipNoHead, bool needDereference)
1152 {
1153    return _DeclareStruct(neededBy, name, skipNoHead, needDereference, false);
1154 }
1155
1156 External _DeclareStruct(External neededBy, const char * name, bool skipNoHead, bool needDereference, bool fwdDecl)
1157 {
1158    External external = null;
1159    Symbol classSym = FindClass(name);
1160    OldList * curDeclarations = null;
1161
1162    if(!inCompiler || !classSym) return null;
1163
1164    // We don't need any declaration for bit classes...
1165    if(classSym.registered &&
1166       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1167       return null;
1168
1169    if(!classSym.registered || (classSym.registered.type == normalClass && classSym.registered.structSize && classSym.registered.base && classSym.registered.base.base))
1170       _DeclareStruct(neededBy, "ecere::com::Instance", false, true, fwdDecl);
1171
1172    external = classSym.structExternal;
1173
1174    if(external && external.declaration)
1175    {
1176       Specifier spec;
1177       for(spec = external.declaration.specifiers ? external.declaration.specifiers->first : null; spec; spec = spec.next)
1178          if(spec.type == structSpecifier || spec.type == unionSpecifier)
1179          {
1180             curDeclarations = spec.definitions;
1181             break;
1182          }
1183    }
1184
1185    if(classSym.registered && !classSym.declaring && classSym.imported && (!classSym.declaredStructSym || (classSym.registered.type == noHeadClass && !skipNoHead && external && !curDeclarations)))
1186    {
1187       OldList * specifiers, * declarators;
1188       OldList * declarations = null;
1189       char structName[1024];
1190       bool addedPadding = false;
1191       Specifier curSpec = null;
1192
1193       classSym.declaring++;
1194
1195       if(strchr(classSym.string, '<'))
1196       {
1197          if(classSym.registered.templateClass)
1198             external = _DeclareStruct(neededBy, classSym.registered.templateClass.fullName, skipNoHead, needDereference, fwdDecl);
1199          classSym.declaring--;
1200          return external;
1201       }
1202
1203       structName[0] = 0;
1204       FullClassNameCat(structName, name, false);
1205
1206       classSym.declaredStructSym = true;
1207       if(!external || (classSym.registered.type == noHeadClass && !skipNoHead && !curDeclarations))
1208       {
1209          bool add = false;
1210          if(!external)
1211          {
1212             external = MkExternalDeclaration(null);
1213             classSym.structExternal = external;
1214             external.symbol = classSym;
1215
1216             add = true;
1217          }
1218
1219          if(!skipNoHead)
1220          {
1221             declarations = MkList();
1222             AddMembers(external, declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1223          }
1224
1225          if(external.declaration)
1226          {
1227             Specifier spec;
1228             for(spec = external.declaration.specifiers ? external.declaration.specifiers->first : null; spec; spec = spec.next)
1229                if(spec.type == structSpecifier || spec.type == unionSpecifier)
1230                {
1231                   curSpec = spec;
1232                   curDeclarations = spec.definitions;
1233                   break;
1234                }
1235          }
1236
1237          if(declarations && (!declarations->count || (declarations->count == 1 && addedPadding)))
1238          {
1239             FreeList(declarations, FreeClassDef);
1240             declarations = null;
1241          }
1242
1243          if(classSym.registered.type != noHeadClass && !declarations)
1244          {
1245             FreeExternal(external);
1246             external = null;
1247             classSym.structExternal = null;
1248          }
1249          else
1250          {
1251             if(curSpec)
1252                curSpec.definitions = declarations;
1253             else
1254             {
1255                specifiers = MkList();
1256                declarators = MkList();
1257                ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1258                external.declaration = MkDeclaration(specifiers, declarators);
1259             }
1260             if(add)
1261                ast->Add(external);
1262          }
1263       }
1264       classSym.declaring--;
1265    }
1266    else if(!classSym.declaredStructSym && classSym.structExternal)
1267    {
1268       classSym.declaredStructSym = true;
1269
1270       if(classSym.registered)
1271          DeclareMembers(classSym.structExternal, classSym.registered, false);
1272
1273       if(classSym.structExternal.declaration && classSym.structExternal.declaration.specifiers)
1274       {
1275          Specifier spec;
1276          for(spec = classSym.structExternal.declaration.specifiers->first; spec; spec = spec.next)
1277          {
1278             if(spec.definitions)
1279                IdentifyAnonStructs(spec.definitions);
1280          }
1281       }
1282    }
1283    if(inCompiler && neededBy && (external || !classSym.imported))
1284    {
1285       if(!external)
1286       {
1287          classSym.structExternal = external = MkExternalDeclaration(null);
1288          external.symbol = classSym;
1289          ast->Add(external);
1290       }
1291       if(reachedPass15 && !external.declaration && classSym.registered && classSym.registered.type == noHeadClass)
1292       {
1293          // Declare nohead classes without definitions here (e.g. IteratorPointer)
1294          char structName[1024];
1295          OldList * specifiers, * declarators;
1296          structName[0] = 0;
1297          FullClassNameCat(structName, name, false);
1298          specifiers = MkList();
1299          declarators = MkList();
1300          ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), null));
1301          external.declaration = MkDeclaration(specifiers, declarators);
1302       }
1303       if(fwdDecl)
1304       {
1305          External e = external.fwdDecl ? external.fwdDecl : external;
1306          if(e.incoming.count)
1307             neededBy.CreateUniqueEdge(e, !needDereference && !external.fwdDecl);
1308       }
1309       else
1310          neededBy.CreateUniqueEdge(external, !needDereference);
1311    }
1312    return external;
1313 }
1314
1315 void DeclareProperty(External neededBy, Property prop, char * setName, char * getName)
1316 {
1317    Symbol symbol = prop.symbol;
1318    bool imported = false;
1319    bool dllImport = false;
1320    External structExternal = null;
1321    External instExternal = null;
1322
1323    strcpy(setName, "__ecereProp_");
1324    FullClassNameCat(setName, prop._class.fullName, false);
1325    strcat(setName, "_Set_");
1326    FullClassNameCat(setName, prop.name, true);
1327
1328    strcpy(getName, "__ecereProp_");
1329    FullClassNameCat(getName, prop._class.fullName, false);
1330    strcat(getName, "_Get_");
1331    FullClassNameCat(getName, prop.name, true);
1332
1333    if(!symbol || symbol._import)
1334    {
1335       if(!symbol)
1336       {
1337          Symbol classSym;
1338
1339          if(!prop._class.symbol)
1340             prop._class.symbol = FindClass(prop._class.fullName);
1341          classSym = prop._class.symbol;
1342          if(classSym && !classSym._import)
1343          {
1344             ModuleImport module;
1345
1346             if(prop._class.module)
1347                module = FindModule(prop._class.module);
1348             else
1349                module = mainModule;
1350
1351             classSym._import = ClassImport
1352             {
1353                name = CopyString(prop._class.fullName);
1354                isRemote = prop._class.isRemote;
1355             };
1356             module.classes.Add(classSym._import);
1357          }
1358          symbol = prop.symbol = Symbol { };
1359          symbol._import = (ClassImport)PropertyImport
1360          {
1361             name = CopyString(prop.name);
1362             isVirtual = false; //prop.isVirtual;
1363             hasSet = prop.Set ? true : false;
1364             hasGet = prop.Get ? true : false;
1365          };
1366          if(classSym)
1367             classSym._import.properties.Add(symbol._import);
1368       }
1369       imported = true;
1370       // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1371       if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1372          prop._class.module.importType != staticImport)
1373          dllImport = true;
1374    }
1375
1376    if(!symbol.type)
1377    {
1378       Context context = SetupTemplatesContext(prop._class);
1379       symbol.type = ProcessTypeString(prop.dataTypeString, false);
1380       FinishTemplatesContext(context);
1381    }
1382
1383    if((prop.Get && !symbol.externalGet) || (prop.Set && !symbol.externalSet))
1384    {
1385       if(prop._class.type == normalClass && prop._class.structSize)
1386          instExternal = DeclareStruct(null, "ecere::com::Instance", false, true);
1387       structExternal = DeclareStruct(null, prop._class.fullName, prop._class.type != structClass /*true*/, false);
1388    }
1389
1390    // Get
1391    if(prop.Get && !symbol.externalGet)
1392    {
1393       Declaration decl;
1394       OldList * specifiers, * declarators;
1395       Declarator d;
1396       OldList * params;
1397       Specifier spec = null;
1398       External external;
1399       Declarator typeDecl;
1400       bool simple = false;
1401       bool needReference;
1402
1403       specifiers = MkList();
1404       declarators = MkList();
1405       params = MkList();
1406
1407       ListAdd(params, MkTypeName(MkListOne(MkSpecifierName(prop._class.fullName)),
1408          MkDeclaratorIdentifier(MkIdentifier("this"))));
1409
1410       d = MkDeclaratorIdentifier(MkIdentifier(getName));
1411       //if(imported)
1412       if(dllImport)
1413          d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1414
1415       {
1416          Context context = SetupTemplatesContext(prop._class);
1417          typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1418          FinishTemplatesContext(context);
1419       }
1420
1421       // Make sure the simple _class's type is declared
1422       needReference = !typeDecl || typeDecl.type == identifierDeclarator;
1423       for(spec = specifiers->first; spec; spec = spec.next)
1424       {
1425          if(spec.type == nameSpecifier)
1426          {
1427             Symbol classSym = spec.symbol;
1428             if(needReference)
1429             {
1430                symbol._class = classSym.registered;
1431                if(classSym.registered && classSym.registered.type == structClass)
1432                   simple = true;
1433             }
1434             break;
1435          }
1436       }
1437
1438       if(!simple)
1439          d = PlugDeclarator(typeDecl, d);
1440       else
1441       {
1442          ListAdd(params, MkTypeName(specifiers,
1443             PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1444          specifiers = MkList();
1445       }
1446
1447       d = MkDeclaratorFunction(d, params);
1448
1449       //if(imported)
1450       if(dllImport)
1451          specifiers->Insert(null, MkSpecifier(EXTERN));
1452       else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1453          specifiers->Insert(null, MkSpecifier(STATIC));
1454       if(simple)
1455          ListAdd(specifiers, MkSpecifier(VOID));
1456
1457       ListAdd(declarators, MkInitDeclarator(d, null));
1458
1459       decl = MkDeclaration(specifiers, declarators);
1460
1461       external = MkExternalDeclaration(decl);
1462
1463       if(structExternal)
1464          external.CreateEdge(structExternal, false);
1465       if(instExternal)
1466          external.CreateEdge(instExternal, false);
1467
1468       if(spec)
1469          DeclareStruct(external, spec.name, false, needReference);
1470
1471       ast->Add(external);
1472       external.symbol = symbol;
1473       symbol.externalGet = external;
1474
1475       ReplaceThisClassSpecifiers(specifiers, prop._class);
1476
1477       if(typeDecl)
1478          FreeDeclarator(typeDecl);
1479    }
1480
1481    // Set
1482    if(prop.Set && !symbol.externalSet)
1483    {
1484       Declaration decl;
1485       OldList * specifiers, * declarators;
1486       Declarator d;
1487       OldList * params;
1488       Specifier spec = null;
1489       External external;
1490       Declarator typeDecl;
1491       bool needReference;
1492
1493       declarators = MkList();
1494       params = MkList();
1495
1496       if(!prop.conversion || prop._class.type == structClass)
1497       {
1498          ListAdd(params, MkTypeName(MkListOne(MkSpecifierName(prop._class.fullName)),
1499             MkDeclaratorIdentifier(MkIdentifier("this"))));
1500       }
1501
1502       specifiers = MkList();
1503
1504       {
1505          Context context = SetupTemplatesContext(prop._class);
1506          typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1507             MkDeclaratorIdentifier(MkIdentifier("value")));
1508          FinishTemplatesContext(context);
1509       }
1510       if(!strcmp(prop._class.base.fullName, "eda::Row") || !strcmp(prop._class.base.fullName, "eda::Id"))
1511          specifiers->Insert(null, MkSpecifier(CONST));
1512
1513       ListAdd(params, MkTypeName(specifiers, d));
1514
1515       d = MkDeclaratorIdentifier(MkIdentifier(setName));
1516       if(dllImport)
1517          d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1518       d = MkDeclaratorFunction(d, params);
1519
1520       // Make sure the simple _class's type is declared
1521       needReference = !typeDecl || typeDecl.type == identifierDeclarator;
1522       for(spec = specifiers->first; spec; spec = spec.next)
1523       {
1524          if(spec.type == nameSpecifier)
1525          {
1526             Symbol classSym = spec.symbol;
1527             if(needReference)
1528                symbol._class = classSym.registered;
1529             break;
1530          }
1531       }
1532
1533       ListAdd(declarators, MkInitDeclarator(d, null));
1534
1535       specifiers = MkList();
1536       if(dllImport)
1537          specifiers->Insert(null, MkSpecifier(EXTERN));
1538       else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1539          specifiers->Insert(null, MkSpecifier(STATIC));
1540
1541       if(!prop.conversion || prop._class.type == structClass)
1542          ListAdd(specifiers, MkSpecifier(VOID));
1543       else
1544          ListAdd(specifiers, MkSpecifierName(prop._class.fullName));
1545
1546       decl = MkDeclaration(specifiers, declarators);
1547
1548       external = MkExternalDeclaration(decl);
1549
1550       if(structExternal)
1551          external.CreateEdge(structExternal, false);
1552       if(instExternal)
1553          external.CreateEdge(instExternal, false);
1554
1555       if(spec)
1556          DeclareStruct(external, spec.name, false, needReference);
1557
1558       ast->Add(external);
1559       external.symbol = symbol;
1560       symbol.externalSet = external;
1561
1562       ReplaceThisClassSpecifiers(specifiers, prop._class);
1563    }
1564
1565    // Property (for Watchers)
1566    if(!symbol.externalPtr)
1567    {
1568       Declaration decl;
1569       External external;
1570       OldList * specifiers = MkList();
1571       char propName[1024];
1572
1573       if(imported)
1574          specifiers->Insert(null, MkSpecifier(EXTERN));
1575       else
1576       {
1577          specifiers->Insert(null, MkSpecifier(STATIC));
1578          specifiers->Add(MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
1579       }
1580
1581       ListAdd(specifiers, MkSpecifierName("Property"));
1582
1583       strcpy(propName, "__ecereProp_");
1584       FullClassNameCat(propName, prop._class.fullName, false);
1585       strcat(propName, "_");
1586       FullClassNameCat(propName, prop.name, true);
1587
1588       {
1589          OldList * list = MkList();
1590          ListAdd(list, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(propName)), null));
1591
1592          if(!imported)
1593          {
1594             strcpy(propName, "__ecerePropM_");
1595             FullClassNameCat(propName, prop._class.fullName, false);
1596             strcat(propName, "_");
1597             FullClassNameCat(propName, prop.name, true);
1598
1599             ListAdd(list, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(propName)), null));
1600          }
1601          decl = MkDeclaration(specifiers, list);
1602       }
1603
1604       external = MkExternalDeclaration(decl);
1605       ast->Insert(curExternal.prev, external);
1606       external.symbol = symbol;
1607       symbol.externalPtr = external;
1608    }
1609
1610    if(inCompiler && neededBy)
1611    {
1612       // Could improve this to create edge on only what is needed...
1613       if(symbol.externalPtr)
1614          neededBy.CreateUniqueEdge(symbol.externalPtr, false);
1615
1616       if(symbol.externalGet)
1617          neededBy.CreateUniqueEdge(symbol.externalGet, symbol.externalGet.type == functionExternal);
1618
1619       if(symbol.externalSet)
1620          neededBy.CreateUniqueEdge(symbol.externalSet, symbol.externalSet.type == functionExternal);
1621
1622       // IsSet ?
1623    }
1624 }
1625
1626 // ***************** EXPRESSION PROCESSING ***************************
1627 public Type Dereference(Type source)
1628 {
1629    Type type = null;
1630    if(source)
1631    {
1632       if(source.isVector)
1633       {
1634          type = { refCount = 1 };
1635          CopyTypeInto(type, source);
1636          type.isVector = false;
1637       }
1638       else if(source.kind == pointerType || source.kind == arrayType)
1639       {
1640          type = source.type;
1641          source.type.refCount++;
1642       }
1643       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1644       {
1645          type = Type
1646          {
1647             kind = charType;
1648             refCount = 1;
1649          };
1650       }
1651       // Support dereferencing of no head classes for now...
1652       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1653       {
1654          type = source;
1655          source.refCount++;
1656       }
1657       else
1658          Compiler_Error($"cannot dereference type\n");
1659    }
1660    return type;
1661 }
1662
1663 static Type Reference(Type source)
1664 {
1665    Type type = null;
1666    if(source)
1667    {
1668       type = Type
1669       {
1670          kind = pointerType;
1671          type = source;
1672          refCount = 1;
1673       };
1674       source.refCount++;
1675    }
1676    return type;
1677 }
1678
1679 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1680 {
1681    Identifier ident = member.identifiers ? member.identifiers->first : null;
1682    bool found = false;
1683    DataMember dataMember = null;
1684    Method method = null;
1685    bool freeType = false;
1686
1687    yylloc = member.loc;
1688
1689    if(!ident)
1690    {
1691       if(curMember)
1692       {
1693          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1694          if(*curMember)
1695          {
1696             found = true;
1697             dataMember = *curMember;
1698          }
1699       }
1700    }
1701    else
1702    {
1703       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1704       DataMember _subMemberStack[256];
1705       int _subMemberStackPos = 0;
1706
1707       // FILL MEMBER STACK
1708       if(!thisMember)
1709          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1710       if(thisMember)
1711       {
1712          dataMember = thisMember;
1713          if(curMember && thisMember.memberAccess == publicAccess)
1714          {
1715             *curMember = thisMember;
1716             *curClass = thisMember._class;
1717             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1718             *subMemberStackPos = _subMemberStackPos;
1719          }
1720          found = true;
1721       }
1722       else
1723       {
1724          // Setting a method
1725          method = eClass_FindMethod(_class, ident.string, privateModule);
1726          if(method && method.type == virtualMethod)
1727             found = true;
1728          else
1729             method = null;
1730       }
1731    }
1732
1733    if(found)
1734    {
1735       Type type = null;
1736       if(dataMember)
1737       {
1738          if(!dataMember.dataType && dataMember.dataTypeString)
1739          {
1740             //Context context = SetupTemplatesContext(dataMember._class);
1741             Context context = SetupTemplatesContext(_class);
1742             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1743             FinishTemplatesContext(context);
1744          }
1745          type = dataMember.dataType;
1746       }
1747       else if(method)
1748       {
1749          // This is for destination type...
1750          if(!method.dataType)
1751             ProcessMethodType(method);
1752          //DeclareMethod(method);
1753          // method.dataType = ((Symbol)method.symbol)->type;
1754          type = method.dataType;
1755       }
1756
1757       if(ident && ident.next)
1758       {
1759          for(ident = ident.next; ident && type; ident = ident.next)
1760          {
1761             if(type.kind == classType)
1762             {
1763                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1764                if(!dataMember)
1765                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1766                if(dataMember)
1767                   type = dataMember.dataType;
1768             }
1769             else if(type.kind == structType || type.kind == unionType)
1770             {
1771                Type memberType;
1772                for(memberType = type.members.first; memberType; memberType = memberType.next)
1773                {
1774                   if(!strcmp(memberType.name, ident.string))
1775                   {
1776                      type = memberType;
1777                      break;
1778                   }
1779                }
1780             }
1781          }
1782       }
1783
1784       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1785       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1786       {
1787          int id = 0;
1788          ClassTemplateParameter curParam = null;
1789          Class sClass;
1790          for(sClass = _class; sClass; sClass = sClass.base)
1791          {
1792             id = 0;
1793             if(sClass.templateClass) sClass = sClass.templateClass;
1794             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1795             {
1796                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1797                {
1798                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1799                   {
1800                      if(sClass.templateClass) sClass = sClass.templateClass;
1801                      id += sClass.templateParams.count;
1802                   }
1803                   break;
1804                }
1805                id++;
1806             }
1807             if(curParam) break;
1808          }
1809
1810          if(curParam)
1811          {
1812             ClassTemplateArgument arg = _class.templateArgs[id];
1813             if(arg.dataTypeString)
1814             {
1815                bool constant = type.constant;
1816                // FreeType(type);
1817                type = ProcessTypeString(arg.dataTypeString, false);
1818                if(type.kind == classType && constant) type.constant = true;
1819                else if(type.kind == pointerType)
1820                {
1821                   Type t = type.type;
1822                   while(t.kind == pointerType) t = t.type;
1823                   if(constant) t.constant = constant;
1824                }
1825                freeType = true;
1826                if(type && _class.templateClass)
1827                   type.passAsTemplate = true;
1828                if(type)
1829                {
1830                   // type.refCount++;
1831                   /*if(!exp.destType)
1832                   {
1833                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1834                      exp.destType.refCount++;
1835                   }*/
1836                }
1837             }
1838          }
1839       }
1840       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1841       {
1842          Class expClass = type._class.registered;
1843          Class cClass = null;
1844          int paramCount = 0;
1845          int lastParam = -1;
1846
1847          char templateString[1024];
1848          ClassTemplateParameter param;
1849          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1850          for(cClass = expClass; cClass; cClass = cClass.base)
1851          {
1852             int p = 0;
1853             if(cClass.templateClass) cClass = cClass.templateClass;
1854             for(param = cClass.templateParams.first; param; param = param.next)
1855             {
1856                int id = p;
1857                Class sClass;
1858                ClassTemplateArgument arg;
1859                for(sClass = cClass.base; sClass; sClass = sClass.base)
1860                {
1861                   if(sClass.templateClass) sClass = sClass.templateClass;
1862                   id += sClass.templateParams.count;
1863                }
1864                arg = expClass.templateArgs[id];
1865
1866                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1867                {
1868                   ClassTemplateParameter cParam;
1869                   //int p = numParams - sClass.templateParams.count;
1870                   int p = 0;
1871                   Class nextClass;
1872                   if(sClass.templateClass) sClass = sClass.templateClass;
1873
1874                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1875                   {
1876                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1877                      p += nextClass.templateParams.count;
1878                   }
1879
1880                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1881                   {
1882                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1883                      {
1884                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1885                         {
1886                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1887                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1888                            break;
1889                         }
1890                      }
1891                   }
1892                }
1893
1894                {
1895                   char argument[256];
1896                   argument[0] = '\0';
1897                   /*if(arg.name)
1898                   {
1899                      strcat(argument, arg.name.string);
1900                      strcat(argument, " = ");
1901                   }*/
1902                   switch(param.type)
1903                   {
1904                      case expression:
1905                      {
1906                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1907                         char expString[1024];
1908                         OldList * specs = MkList();
1909                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1910                         Expression exp;
1911                         char * string = PrintHexUInt64(arg.expression.ui64);
1912                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1913                         delete string;
1914
1915                         ProcessExpressionType(exp);
1916                         ComputeExpression(exp);
1917                         expString[0] = '\0';
1918                         PrintExpression(exp, expString);
1919                         strcat(argument, expString);
1920                         //delete exp;
1921                         FreeExpression(exp);
1922                         break;
1923                      }
1924                      case identifier:
1925                      {
1926                         strcat(argument, arg.member.name);
1927                         break;
1928                      }
1929                      case TemplateParameterType::type:
1930                      {
1931                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1932                            strcat(argument, arg.dataTypeString);
1933                         break;
1934                      }
1935                   }
1936                   if(argument[0])
1937                   {
1938                      if(paramCount) strcat(templateString, ", ");
1939                      if(lastParam != p - 1)
1940                      {
1941                         strcat(templateString, param.name);
1942                         strcat(templateString, " = ");
1943                      }
1944                      strcat(templateString, argument);
1945                      paramCount++;
1946                      lastParam = p;
1947                   }
1948                   p++;
1949                }
1950             }
1951          }
1952          {
1953             int len = strlen(templateString);
1954             if(templateString[len-1] == '<')
1955                len--;
1956             else
1957             {
1958                if(templateString[len-1] == '>')
1959                   templateString[len++] = ' ';
1960                templateString[len++] = '>';
1961             }
1962             templateString[len++] = '\0';
1963          }
1964          {
1965             Context context = SetupTemplatesContext(_class);
1966             if(freeType) FreeType(type);
1967             type = ProcessTypeString(templateString, false);
1968             freeType = true;
1969             FinishTemplatesContext(context);
1970          }
1971       }
1972
1973       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1974       {
1975          ProcessExpressionType(member.initializer.exp);
1976          if(!member.initializer.exp.expType)
1977          {
1978             if(inCompiler)
1979             {
1980                char expString[10240];
1981                expString[0] = '\0';
1982                PrintExpression(member.initializer.exp, expString);
1983                ChangeCh(expString, '\n', ' ');
1984                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1985             }
1986          }
1987          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1988          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false, true))
1989          {
1990             Compiler_Error($"incompatible instance method %s\n", ident.string);
1991          }
1992       }
1993       else if(member.initializer)
1994       {
1995          /*
1996          FreeType(member.exp.destType);
1997          member.exp.destType = type;
1998          if(member.exp.destType)
1999             member.exp.destType.refCount++;
2000          ProcessExpressionType(member.exp);
2001          */
2002
2003          ProcessInitializer(member.initializer, type);
2004       }
2005       if(freeType) FreeType(type);
2006    }
2007    else
2008    {
2009       if(_class && _class.type == unitClass)
2010       {
2011          if(member.initializer)
2012          {
2013             /*
2014             FreeType(member.exp.destType);
2015             member.exp.destType = MkClassType(_class.fullName);
2016             ProcessExpressionType(member.initializer, type);
2017             */
2018             Type type = MkClassType(_class.fullName);
2019             ProcessInitializer(member.initializer, type);
2020             FreeType(type);
2021          }
2022       }
2023       else
2024       {
2025          if(member.initializer)
2026          {
2027             //ProcessExpressionType(member.exp);
2028             ProcessInitializer(member.initializer, null);
2029          }
2030          if(ident)
2031          {
2032             if(method)
2033             {
2034                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
2035             }
2036             else if(_class)
2037             {
2038                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
2039                if(inCompiler)
2040                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
2041             }
2042          }
2043          else if(_class)
2044             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
2045       }
2046    }
2047 }
2048
2049 void ProcessInstantiationType(Instantiation inst)
2050 {
2051    yylloc = inst.loc;
2052    if(inst._class)
2053    {
2054       MembersInit members;
2055       Symbol classSym;
2056       Class _class;
2057
2058       classSym = inst._class.symbol;
2059       _class = classSym ? classSym.registered : null;
2060
2061       if(!_class || _class.type != noHeadClass)
2062          DeclareStruct(curExternal, inst._class.name, false, true);
2063
2064       afterExternal = afterExternal ? afterExternal : curExternal;
2065
2066       if(inst.exp)
2067          ProcessExpressionType(inst.exp);
2068
2069       inst.isConstant = true;
2070       if(inst.members)
2071       {
2072          DataMember curMember = null;
2073          Class curClass = null;
2074          DataMember subMemberStack[256];
2075          int subMemberStackPos = 0;
2076
2077          for(members = inst.members->first; members; members = members.next)
2078          {
2079             switch(members.type)
2080             {
2081                case methodMembersInit:
2082                {
2083                   char name[1024];
2084                   static uint instMethodID = 0;
2085                   External external = curExternal;
2086                   Context context = curContext;
2087                   Declarator declarator = members.function.declarator;
2088                   Identifier nameID = GetDeclId(declarator);
2089                   char * unmangled = nameID ? nameID.string : null;
2090                   Expression exp;
2091                   External createdExternal = null;
2092
2093                   if(inCompiler)
2094                   {
2095                      char number[16];
2096                      strcpy(name, "__ecereInstMeth_");
2097                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2098                      strcat(name, "_");
2099                      strcat(name, nameID.string);
2100                      strcat(name, "_");
2101                      sprintf(number, "_%08d", instMethodID++);
2102                      strcat(name, number);
2103                      nameID.string = CopyString(name);
2104                   }
2105
2106                   // Do modifications here...
2107                   if(declarator)
2108                   {
2109                      Symbol symbol = declarator.symbol;
2110                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2111
2112                      if(method && method.type == virtualMethod)
2113                      {
2114                         symbol.method = method;
2115                         ProcessMethodType(method);
2116
2117                         if(!symbol.type.thisClass)
2118                         {
2119                            if(method.dataType.thisClass && currentClass &&
2120                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2121                            {
2122                               if(!currentClass.symbol)
2123                                  currentClass.symbol = FindClass(currentClass.fullName);
2124                               symbol.type.thisClass = currentClass.symbol;
2125                            }
2126                            else
2127                            {
2128                               if(!_class.symbol)
2129                                  _class.symbol = FindClass(_class.fullName);
2130                               symbol.type.thisClass = _class.symbol;
2131                            }
2132                         }
2133                         DeclareType(curExternal, symbol.type, true, true);
2134
2135                      }
2136                      else if(classSym)
2137                      {
2138                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2139                            unmangled, classSym.string);
2140                      }
2141                   }
2142
2143                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2144
2145                   if(nameID)
2146                   {
2147                      FreeSpecifier(nameID._class);
2148                      nameID._class = null;
2149                   }
2150
2151                   curExternal = createdExternal;
2152                   if(inCompiler)
2153                   {
2154                      if(createdExternal.function)
2155                         ProcessFunction(createdExternal.function);
2156                   }
2157                   else if(declarator)
2158                   {
2159                      curExternal = declarator.symbol.pointerExternal;
2160                      ProcessFunction((FunctionDefinition)members.function);
2161                   }
2162                   curExternal = external;
2163                   curContext = context;
2164
2165                   if(inCompiler)
2166                   {
2167                      FreeClassFunction(members.function);
2168
2169                      // In this pass, turn this into a MemberInitData
2170                      exp = QMkExpId(name);
2171                      members.type = dataMembersInit;
2172                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2173
2174                      delete unmangled;
2175                   }
2176                   break;
2177                }
2178                case dataMembersInit:
2179                {
2180                   if(members.dataMembers && classSym)
2181                   {
2182                      MemberInit member;
2183                      Location oldyyloc = yylloc;
2184                      for(member = members.dataMembers->first; member; member = member.next)
2185                      {
2186                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2187                         if(member.initializer && !member.initializer.isConstant)
2188                            inst.isConstant = false;
2189                      }
2190                      yylloc = oldyyloc;
2191                   }
2192                   break;
2193                }
2194             }
2195          }
2196       }
2197    }
2198 }
2199
2200 void DeclareType(External neededFor, Type type, bool needDereference, bool forFunctionDef)
2201 {
2202    _DeclareType(neededFor, type, needDereference, forFunctionDef, false);
2203 }
2204
2205 void DeclareTypeForwardDeclare(External neededFor, Type type, bool needDereference, bool forFunctionDef)
2206 {
2207    _DeclareType(neededFor, type, needDereference, forFunctionDef, true);
2208 }
2209
2210 static void _DeclareType(External neededFor, Type type, bool needDereference, bool forFunctionDef, bool fwdDecl)
2211 {
2212    if(inCompiler)
2213    {
2214       if(type.kind == functionType)
2215       {
2216          Type param;
2217          for(param = type.params.first; param; param = param.next)
2218             _DeclareType(neededFor, param, forFunctionDef, false, fwdDecl);
2219          _DeclareType(neededFor, type.returnType, forFunctionDef, false, fwdDecl);
2220       }
2221       else if(type.kind == pointerType)
2222          _DeclareType(neededFor, type.type, false, false, fwdDecl);
2223       else if(type.kind == classType)
2224       {
2225          Class c = type._class ? type._class.registered : null;
2226          _DeclareStruct(neededFor, c ? c.fullName : "ecere::com::Instance", c ? c.type == noHeadClass : false, needDereference && c && c.type == structClass, fwdDecl);
2227       }
2228       else if(type.kind == structType || type.kind == unionType)
2229       {
2230          Type member;
2231          for(member = type.members.first; member; member = member.next)
2232             _DeclareType(neededFor, member, needDereference, forFunctionDef, fwdDecl);
2233       }
2234       else if(type.kind == arrayType)
2235          _DeclareType(neededFor, type.arrayType, true, false, fwdDecl);
2236    }
2237 }
2238
2239 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2240 {
2241    ClassTemplateArgument * arg = null;
2242    int id = 0;
2243    ClassTemplateParameter curParam = null;
2244    Class sClass;
2245    for(sClass = _class; sClass; sClass = sClass.base)
2246    {
2247       id = 0;
2248       if(sClass.templateClass) sClass = sClass.templateClass;
2249       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2250       {
2251          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2252          {
2253             for(sClass = sClass.base; sClass; sClass = sClass.base)
2254             {
2255                if(sClass.templateClass) sClass = sClass.templateClass;
2256                id += sClass.templateParams.count;
2257             }
2258             break;
2259          }
2260          id++;
2261       }
2262       if(curParam) break;
2263    }
2264    if(curParam)
2265    {
2266       arg = &_class.templateArgs[id];
2267       if(arg && param.type == type)
2268          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2269    }
2270    return arg;
2271 }
2272
2273 public Context SetupTemplatesContext(Class _class)
2274 {
2275    Context context = PushContext();
2276    context.templateTypesOnly = true;
2277    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2278    {
2279       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2280       for(; param; param = param.next)
2281       {
2282          if(param.type == type && param.identifier)
2283          {
2284             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2285             curContext.templateTypes.Add((BTNode)type);
2286          }
2287       }
2288    }
2289    else if(_class)
2290    {
2291       Class sClass;
2292       for(sClass = _class; sClass; sClass = sClass.base)
2293       {
2294          ClassTemplateParameter p;
2295          for(p = sClass.templateParams.first; p; p = p.next)
2296          {
2297             //OldList * specs = MkList();
2298             //Declarator decl = null;
2299             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2300             if(p.type == type)
2301             {
2302                TemplateParameter param = p.param;
2303                TemplatedType type;
2304                if(!param)
2305                {
2306                   // ADD DATA TYPE HERE...
2307                   p.param = param = TemplateParameter
2308                   {
2309                      identifier = MkIdentifier(p.name), type = p.type,
2310                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2311                   };
2312                }
2313                type = TemplatedType { key = (uintptr)p.name, param = param };
2314                curContext.templateTypes.Add((BTNode)type);
2315             }
2316          }
2317       }
2318    }
2319    return context;
2320 }
2321
2322 public void FinishTemplatesContext(Context context)
2323 {
2324    PopContext(context);
2325    FreeContext(context);
2326    delete context;
2327 }
2328
2329 public void ProcessMethodType(Method method)
2330 {
2331    if(!method.dataType)
2332    {
2333       Context context = SetupTemplatesContext(method._class);
2334
2335       method.dataType = ProcessTypeString(method.dataTypeString, false);
2336
2337       FinishTemplatesContext(context);
2338
2339       if(method.type != virtualMethod && method.dataType)
2340       {
2341          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2342          {
2343             if(!method._class.symbol)
2344                method._class.symbol = FindClass(method._class.fullName);
2345             method.dataType.thisClass = method._class.symbol;
2346          }
2347       }
2348
2349       // Why was this commented out? Working fine without now...
2350
2351       /*
2352       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2353          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2354          */
2355    }
2356
2357    /*
2358    if(type)
2359    {
2360       char * par = strstr(type, "(");
2361       char * classOp = null;
2362       int classOpLen = 0;
2363       if(par)
2364       {
2365          int c;
2366          for(c = par-type-1; c >= 0; c++)
2367          {
2368             if(type[c] == ':' && type[c+1] == ':')
2369             {
2370                classOp = type + c - 1;
2371                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2372                {
2373                   classOp--;
2374                   classOpLen++;
2375                }
2376                break;
2377             }
2378             else if(!isspace(type[c]))
2379                break;
2380          }
2381       }
2382       if(classOp)
2383       {
2384          char temp[1024];
2385          int typeLen = strlen(type);
2386          memcpy(temp, classOp, classOpLen);
2387          temp[classOpLen] = '\0';
2388          if(temp[0])
2389             _class = eSystem_FindClass(module, temp);
2390          else
2391             _class = null;
2392          method.dataTypeString = new char[typeLen - classOpLen + 1];
2393          memcpy(method.dataTypeString, type, classOp - type);
2394          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2395       }
2396       else
2397          method.dataTypeString = type;
2398    }
2399    */
2400 }
2401
2402
2403 public void ProcessPropertyType(Property prop)
2404 {
2405    if(!prop.dataType)
2406    {
2407       Context context = SetupTemplatesContext(prop._class);
2408       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2409       FinishTemplatesContext(context);
2410    }
2411 }
2412
2413 public void DeclareMethod(External neededFor, Method method, const char * name)
2414 {
2415    Symbol symbol = method.symbol;
2416    if(!symbol || (!symbol.pointerExternal && (!symbol.methodCodeExternal || method.type == virtualMethod)))
2417    {
2418       bool dllImport = false;
2419
2420       if(!method.dataType)
2421          method.dataType = ProcessTypeString(method.dataTypeString, false);
2422
2423       //if(!symbol || symbol._import || method.type == virtualMethod)
2424       {
2425          if(!symbol || method.type == virtualMethod)
2426          {
2427             Symbol classSym;
2428             if(!method._class.symbol)
2429                method._class.symbol = FindClass(method._class.fullName);
2430             classSym = method._class.symbol;
2431             if(!classSym._import)
2432             {
2433                ModuleImport module;
2434
2435                if(method._class.module && method._class.module.name)
2436                   module = FindModule(method._class.module);
2437                else
2438                   module = mainModule;
2439                classSym._import = ClassImport
2440                {
2441                   name = CopyString(method._class.fullName);
2442                   isRemote = method._class.isRemote;
2443                };
2444                module.classes.Add(classSym._import);
2445             }
2446             if(!symbol)
2447             {
2448                symbol = method.symbol = Symbol { };
2449             }
2450             if(!symbol._import)
2451             {
2452                symbol._import = (ClassImport)MethodImport
2453                {
2454                   name = CopyString(method.name);
2455                   isVirtual = method.type == virtualMethod;
2456                };
2457                classSym._import.methods.Add(symbol._import);
2458             }
2459             if(!symbol)
2460             {
2461                symbol.type = method.dataType;
2462                if(symbol.type) symbol.type.refCount++;
2463             }
2464          }
2465          if(!method.dataType.dllExport)
2466          {
2467             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2468                dllImport = true;
2469          }
2470       }
2471
2472       if(inCompiler)
2473       {
2474          // We need a declaration here :)
2475          Declaration decl;
2476          OldList * specifiers, * declarators;
2477          Declarator d;
2478          Declarator funcDecl;
2479          External external;
2480
2481          specifiers = MkList();
2482          declarators = MkList();
2483
2484          if(dllImport)
2485             ListAdd(specifiers, MkSpecifier(EXTERN));
2486          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2487             ListAdd(specifiers, MkSpecifier(STATIC));
2488
2489          if(method.type == virtualMethod)
2490          {
2491             ListAdd(specifiers, MkSpecifier(INT));
2492             d = MkDeclaratorIdentifier(MkIdentifier(name));
2493          }
2494          else
2495          {
2496             d = MkDeclaratorIdentifier(MkIdentifier(name));
2497             if(dllImport)
2498                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2499             {
2500                Context context = SetupTemplatesContext(method._class);
2501                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2502                FinishTemplatesContext(context);
2503             }
2504             funcDecl = GetFuncDecl(d);
2505
2506             if(dllImport)
2507             {
2508                Specifier spec, next;
2509                for(spec = specifiers->first; spec; spec = next)
2510                {
2511                   next = spec.next;
2512                   if(spec.type == extendedSpecifier)
2513                   {
2514                      specifiers->Remove(spec);
2515                      FreeSpecifier(spec);
2516                   }
2517                }
2518             }
2519
2520             // Add this parameter if not a static method
2521             if(method.dataType && !method.dataType.staticMethod)
2522             {
2523                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2524                {
2525                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2526                   TypeName thisParam = MkTypeName(MkListOne(
2527                      MkSpecifierName(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2528                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2529                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2530                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2531
2532                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2533                   {
2534                      TypeName param = funcDecl.function.parameters->first;
2535                      funcDecl.function.parameters->Remove(param);
2536                      FreeTypeName(param);
2537                   }
2538
2539                   if(!funcDecl.function.parameters)
2540                      funcDecl.function.parameters = MkList();
2541                   funcDecl.function.parameters->Insert(null, thisParam);
2542                }
2543             }
2544          }
2545          ProcessDeclarator(d, true);
2546
2547          ListAdd(declarators, MkInitDeclarator(d, null));
2548
2549          decl = MkDeclaration(specifiers, declarators);
2550
2551          ReplaceThisClassSpecifiers(specifiers, method._class);
2552
2553          external = MkExternalDeclaration(decl);
2554          external.symbol = symbol;
2555          symbol.pointerExternal = external;
2556          ast->Add(external);
2557          DeclareStruct(external, method._class.fullName, true, true);
2558          if(method.dataType)
2559             DeclareType(external, method.dataType, true, true);
2560       }
2561    }
2562    if(inCompiler && neededFor)
2563    {
2564       External external = symbol.pointerExternal ? symbol.pointerExternal : symbol.methodCodeExternal;
2565       neededFor.CreateUniqueEdge(external, external.type == functionExternal);
2566    }
2567 }
2568
2569 char * ReplaceThisClass(Class _class)
2570 {
2571    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2572    {
2573       bool first = true;
2574       int p = 0;
2575       ClassTemplateParameter param;
2576       int lastParam = -1;
2577
2578       char className[1024];
2579       strcpy(className, _class.fullName);
2580       for(param = _class.templateParams.first; param; param = param.next)
2581       {
2582          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2583          {
2584             if(first) strcat(className, "<");
2585             if(!first) strcat(className, ", ");
2586             if(lastParam + 1 != p)
2587             {
2588                strcat(className, param.name);
2589                strcat(className, " = ");
2590             }
2591             strcat(className, param.name);
2592             first = false;
2593             lastParam = p;
2594          }
2595          p++;
2596       }
2597       if(!first)
2598       {
2599          int len = strlen(className);
2600          if(className[len-1] == '>') className[len++] = ' ';
2601          className[len++] = '>';
2602          className[len++] = '\0';
2603       }
2604       return CopyString(className);
2605    }
2606    else
2607       return CopyString(_class.fullName);
2608 }
2609
2610 Type ReplaceThisClassType(Class _class)
2611 {
2612    Type type;
2613    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2614    {
2615       bool first = true;
2616       int p = 0;
2617       ClassTemplateParameter param;
2618       int lastParam = -1;
2619       char className[1024];
2620       strcpy(className, _class.fullName);
2621
2622       for(param = _class.templateParams.first; param; param = param.next)
2623       {
2624          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2625          {
2626             if(first) strcat(className, "<");
2627             if(!first) strcat(className, ", ");
2628             if(lastParam + 1 != p)
2629             {
2630                strcat(className, param.name);
2631                strcat(className, " = ");
2632             }
2633             strcat(className, param.name);
2634             first = false;
2635             lastParam = p;
2636          }
2637          p++;
2638       }
2639       if(!first)
2640       {
2641          int len = strlen(className);
2642          if(className[len-1] == '>') className[len++] = ' ';
2643          className[len++] = '>';
2644          className[len++] = '\0';
2645       }
2646       type = MkClassType(className);
2647       //type = ProcessTypeString(className, false);
2648    }
2649    else
2650    {
2651       type = MkClassType(_class.fullName);
2652       //type = ProcessTypeString(_class.fullName, false);
2653    }
2654    //type.wasThisClass = true;
2655    return type;
2656 }
2657
2658 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2659 {
2660    if(specs != null && _class)
2661    {
2662       Specifier spec;
2663       for(spec = specs.first; spec; spec = spec.next)
2664       {
2665          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2666          {
2667             spec.type = nameSpecifier;
2668             spec.name = ReplaceThisClass(_class);
2669             spec.symbol = FindClass(spec.name); //_class.symbol;
2670          }
2671       }
2672    }
2673 }
2674
2675 // Returns imported or not
2676 bool DeclareFunction(External neededFor, GlobalFunction function, char * name)
2677 {
2678    Symbol symbol = function.symbol;
2679    // TOCHECK: Might get rid of the pointerExternal check in favor of marking the edge as breakable
2680    if(!symbol || !symbol.pointerExternal)
2681    {
2682       bool imported = false;
2683       bool dllImport = false;
2684
2685       if(!function.dataType)
2686       {
2687          function.dataType = ProcessTypeString(function.dataTypeString, false);
2688          if(!function.dataType.thisClass)
2689             function.dataType.staticMethod = true;
2690       }
2691
2692       if(inCompiler)
2693       {
2694          if(!symbol)
2695          {
2696             ModuleImport module = FindModule(function.module);
2697             // WARNING: This is not added anywhere...
2698             symbol = function.symbol = Symbol {  };
2699
2700             if(module.name)
2701             {
2702                if(!function.dataType.dllExport)
2703                {
2704                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2705                   module.functions.Add(symbol._import);
2706                }
2707             }
2708             // Set the symbol type
2709             {
2710                symbol.type = ProcessTypeString(function.dataTypeString, false);
2711                if(!symbol.type.thisClass)
2712                   symbol.type.staticMethod = true;
2713             }
2714          }
2715          imported = symbol._import ? true : false;
2716          if(imported && function.module != privateModule && function.module.importType != staticImport)
2717             dllImport = true;
2718       }
2719
2720       if(inCompiler)
2721       {
2722          // TOCHECK: What's with the functionExternal check here? Is it Edge breaking / forward declaration?
2723          //if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2724          {
2725             // We need a declaration here :)
2726             Declaration decl;
2727             OldList * specifiers, * declarators;
2728             Declarator d;
2729             Declarator funcDecl;
2730             External external;
2731
2732             specifiers = MkList();
2733             declarators = MkList();
2734
2735             ListAdd(specifiers, MkSpecifier(EXTERN));
2736
2737             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2738             if(dllImport)
2739                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2740
2741             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2742             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2743             if(function.module.importType == staticImport)
2744             {
2745                Specifier spec;
2746                for(spec = specifiers->first; spec; spec = spec.next)
2747                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2748                   {
2749                      specifiers->Remove(spec);
2750                      FreeSpecifier(spec);
2751                      break;
2752                   }
2753             }
2754
2755             funcDecl = GetFuncDecl(d);
2756
2757             // Make sure we don't have empty parameter declarations for static methods...
2758             if(funcDecl && !funcDecl.function.parameters)
2759             {
2760                funcDecl.function.parameters = MkList();
2761                funcDecl.function.parameters->Insert(null,
2762                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2763             }
2764
2765             ListAdd(declarators, MkInitDeclarator(d, null));
2766
2767             {
2768                Context oldCtx = curContext;
2769                curContext = globalContext;
2770                decl = MkDeclaration(specifiers, declarators);
2771                curContext = oldCtx;
2772             }
2773
2774             // Keep a different symbol for the function definition than the declaration...
2775             /* Note: This should be handled by the edge breaking...
2776             if(symbol.pointerExternal && symbol.pointerExternal.type == functionExternal)
2777             {
2778                Symbol functionSymbol { };
2779                // Copy symbol
2780                {
2781                   *functionSymbol = *symbol;
2782                   functionSymbol.string = CopyString(symbol.string);
2783                   if(functionSymbol.type)
2784                      functionSymbol.type.refCount++;
2785                }
2786
2787                excludedSymbols->Add(functionSymbol);
2788
2789                symbol.pointerExternal.symbol = functionSymbol;
2790             }
2791             */
2792             external = MkExternalDeclaration(decl);
2793             ast->Add(external);
2794             external.symbol = symbol;
2795             symbol.pointerExternal = external;
2796
2797             DeclareType(external, function.dataType, true, true);
2798          }
2799       }
2800    }
2801    if(inCompiler && neededFor && symbol && symbol.pointerExternal)
2802       neededFor.CreateUniqueEdge(symbol.pointerExternal, symbol.pointerExternal.type == functionExternal);
2803    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2804 }
2805
2806 void DeclareGlobalData(External neededFor, GlobalData data)
2807 {
2808    Symbol symbol = data.symbol;
2809    // TOCHECK: Might get rid of the pointerExternal check in favor of marking the edge as breakable
2810    if(!symbol || !symbol.pointerExternal)
2811    {
2812       if(inCompiler)
2813       {
2814          if(!symbol)
2815             symbol = data.symbol = Symbol { };
2816       }
2817       if(!data.dataType)
2818          data.dataType = ProcessTypeString(data.dataTypeString, false);
2819
2820       if(inCompiler)
2821       {
2822          // We need a declaration here :)
2823          Declaration decl;
2824          OldList * specifiers, * declarators;
2825          Declarator d;
2826          External external;
2827
2828          specifiers = MkList();
2829          declarators = MkList();
2830
2831          ListAdd(specifiers, MkSpecifier(EXTERN));
2832          d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2833          d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2834
2835          ListAdd(declarators, MkInitDeclarator(d, null));
2836
2837          decl = MkDeclaration(specifiers, declarators);
2838          external = MkExternalDeclaration(decl);
2839          if(curExternal)
2840             ast->Insert(curExternal.prev, external);
2841          external.symbol = symbol;
2842          symbol.pointerExternal = external;
2843
2844          DeclareType(external, data.dataType, true, true);
2845       }
2846    }
2847    if(inCompiler && neededFor && symbol && symbol.pointerExternal)
2848       neededFor.CreateUniqueEdge(symbol.pointerExternal, false);
2849 }
2850
2851 class Conversion : struct
2852 {
2853    Conversion prev, next;
2854    Property convert;
2855    bool isGet;
2856    Type resultType;
2857 };
2858
2859 static bool CheckConstCompatibility(Type source, Type dest, bool warn)
2860 {
2861    bool status = true;
2862    if(((source.kind == classType && source._class && source._class.registered) || source.kind == arrayType || source.kind == pointerType) &&
2863       ((dest.kind == classType && dest._class && dest._class.registered) || /*dest.kind == arrayType || */dest.kind == pointerType))
2864    {
2865       Class sourceClass = source.kind == classType ? source._class.registered : null;
2866       Class destClass = dest.kind == classType ? dest._class.registered : null;
2867       if((!sourceClass || (sourceClass && sourceClass.type == normalClass && !sourceClass.structSize)) &&
2868          (!destClass || (destClass && destClass.type == normalClass && !destClass.structSize)))
2869       {
2870          Type sourceType = source, destType = dest;
2871          while((sourceType.kind == pointerType || sourceType.kind == arrayType) && sourceType.type) sourceType = sourceType.type;
2872          while((destType.kind == pointerType || destType.kind == arrayType) && destType.type) destType = destType.type;
2873          if(!destType.constant && sourceType.constant)
2874          {
2875             status = false;
2876             if(warn)
2877                Compiler_Warning($"discarding const qualifier\n");
2878          }
2879       }
2880    }
2881    return status;
2882 }
2883
2884 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams,
2885                        bool isConversionExploration, bool warnConst)
2886 {
2887    if(source && dest)
2888    {
2889       if(warnConst)
2890          CheckConstCompatibility(source, dest, true);
2891       // Property convert;
2892
2893       if(source.kind == templateType && dest.kind != templateType)
2894       {
2895          Type type = ProcessTemplateParameterType(source.templateParameter);
2896          if(type) source = type;
2897       }
2898
2899       if(dest.kind == templateType && source.kind != templateType)
2900       {
2901          Type type = ProcessTemplateParameterType(dest.templateParameter);
2902          if(type) dest = type;
2903       }
2904
2905       if(dest.classObjectType == typedObject && dest.kind != functionType)
2906       {
2907          if(source.classObjectType != anyObject)
2908             return true;
2909          else
2910          {
2911             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2912             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2913             {
2914                return true;
2915             }
2916          }
2917       }
2918       else
2919       {
2920          if(source.kind != functionType && source.classObjectType == anyObject)
2921             return true;
2922          if(dest.kind != functionType && dest.classObjectType == anyObject && source.classObjectType != typedObject)
2923             return true;
2924       }
2925
2926       if((dest.kind == structType && source.kind == structType) ||
2927          (dest.kind == unionType && source.kind == unionType))
2928       {
2929          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2930              (source.members.first && source.members.first == dest.members.first))
2931             return true;
2932       }
2933
2934       if(dest.kind == ellipsisType && source.kind != voidType)
2935          return true;
2936
2937       if(dest.kind == pointerType && dest.type.kind == voidType &&
2938          ((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))
2939          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2940
2941          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2942
2943          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2944          return true;
2945       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2946          ((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))
2947          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2948          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2949
2950          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2951          return true;
2952
2953       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2954       {
2955          if(source._class.registered && source._class.registered.type == unitClass)
2956          {
2957             if(conversions != null)
2958             {
2959                if(source._class.registered == dest._class.registered)
2960                   return true;
2961             }
2962             else
2963             {
2964                Class sourceBase, destBase;
2965                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2966                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2967                if(sourceBase == destBase)
2968                   return true;
2969             }
2970          }
2971          // Don't match enum inheriting from other enum if resolving enumeration values
2972          // TESTING: !dest.classObjectType
2973          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2974             (enumBaseType ||
2975                (!source._class.registered || source._class.registered.type != enumClass) ||
2976                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2977             return true;
2978          else
2979          {
2980             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2981             if(dest._class && dest._class.registered && source._class && source._class.registered &&
2982                (dest.casted || (enumBaseType && dest._class.registered.type == enumClass &&
2983                   (source.kind == classType ||  // Added this here for a base enum to be acceptable for a derived enum (#139)
2984                    source._class.registered.type != enumClass)
2985                 ) ) )
2986             {
2987                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2988                {
2989                   return true;
2990                }
2991             }
2992          }
2993       }
2994
2995       // JUST ADDED THIS...
2996       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2997          return true;
2998
2999       if(doConversion)
3000       {
3001          // Just added this for Straight conversion of ColorAlpha => Color
3002          if(source.kind == classType)
3003          {
3004             Class _class;
3005             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3006             {
3007                Property convert;
3008                for(convert = _class.conversions.first; convert; convert = convert.next)
3009                {
3010                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3011                   {
3012                      Conversion after = (conversions != null) ? conversions.last : null;
3013
3014                      if(!convert.dataType)
3015                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3016                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3017                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3018                         MatchTypes(convert.dataType, dest, conversions, null, null,
3019                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3020                               convert.dataType.kind == classType, false, true, warnConst))
3021                      {
3022                         if(!conversions && !convert.Get)
3023                            return true;
3024                         else if(conversions != null)
3025                         {
3026                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3027                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3028                               (dest.kind != classType || dest._class.registered != _class.base))
3029                               return true;
3030                            else
3031                            {
3032                               Conversion conv { convert = convert, isGet = true };
3033                               // conversions.Add(conv);
3034                               conversions.Insert(after, conv);
3035
3036                               return true;
3037                            }
3038                         }
3039                      }
3040                   }
3041                }
3042             }
3043          }
3044
3045          // MOVING THIS??
3046
3047          if(dest.kind == classType)
3048          {
3049             Class _class;
3050             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3051             {
3052                Property convert;
3053                for(convert = _class.conversions.first; convert; convert = convert.next)
3054                {
3055                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3056                   {
3057                      Type constType = null;
3058                      bool success = false;
3059                      // Conversion after = (conversions != null) ? conversions.last : null;
3060
3061                      if(!convert.dataType)
3062                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3063
3064                      if(warnConst && convert.dataType.kind == pointerType && convert.dataType.type && dest.constant)
3065                      {
3066                         Type ptrType { };
3067                         constType = { kind = pointerType, refCount = 1, type = ptrType };
3068                         CopyTypeInto(ptrType, convert.dataType.type);
3069                         ptrType.constant = true;
3070                      }
3071
3072                      // Just added this equality check to prevent recursion.... Make it safer?
3073                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3074                      if((constType || convert.dataType != dest) && MatchTypes(source, constType ? constType : convert.dataType, conversions, null, null, true, false /*true*/, false, true, warnConst))
3075                      {
3076                         if(!conversions && !convert.Set)
3077                            success = true;
3078                         else if(conversions != null)
3079                         {
3080                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3081                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3082                               (source.kind != classType || source._class.registered != _class.base))
3083                               success = true;
3084                            else
3085                            {
3086                               // *** Testing this! ***
3087                               Conversion conv { convert = convert };
3088                               conversions.Add(conv);
3089                               //conversions.Insert(after, conv);
3090                               success = true;
3091                            }
3092                         }
3093                      }
3094                      if(constType)
3095                         FreeType(constType);
3096                      if(success)
3097                         return true;
3098                   }
3099                }
3100             }
3101             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3102             {
3103                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3104                   (source.kind != classType || source._class.registered.type != structClass))
3105                   return true;
3106             }*/
3107
3108             // TESTING THIS... IS THIS OK??
3109             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3110             {
3111                if(!dest._class.registered.dataType)
3112                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3113                // Only support this for classes...
3114                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3115                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3116                {
3117                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false, warnConst))
3118                   {
3119                      return true;
3120                   }
3121                }
3122             }
3123          }
3124
3125          // Moved this lower
3126          if(source.kind == classType)
3127          {
3128             Class _class;
3129             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3130             {
3131                Property convert;
3132                for(convert = _class.conversions.first; convert; convert = convert.next)
3133                {
3134                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3135                   {
3136                      Conversion after = (conversions != null) ? conversions.last : null;
3137
3138                      if(!convert.dataType)
3139                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3140                      if(convert.dataType != source &&
3141                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3142                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true, warnConst))
3143                      {
3144                         if(!conversions && !convert.Get)
3145                            return true;
3146                         else if(conversions != null)
3147                         {
3148                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3149                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3150                               (dest.kind != classType || dest._class.registered != _class.base))
3151                               return true;
3152                            else
3153                            {
3154                               Conversion conv { convert = convert, isGet = true };
3155
3156                               // conversions.Add(conv);
3157                               conversions.Insert(after, conv);
3158                               return true;
3159                            }
3160                         }
3161                      }
3162                   }
3163                }
3164             }
3165
3166             // TESTING THIS... IS THIS OK??
3167             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3168             {
3169                if(!source._class.registered.dataType)
3170                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3171                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3172                {
3173                   if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, source._class.registered.dataType.kind == classType, source._class.registered.dataType.kind == classType, false, false, warnConst))
3174                      return true;
3175                   // For bool to be accepted by byte, short, etc.
3176                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false, warnConst))
3177                      return true;
3178                }
3179             }
3180          }
3181       }
3182
3183       if(source.kind == classType || source.kind == subClassType)
3184          ;
3185       else if(dest.kind == source.kind &&
3186          (dest.kind != structType && dest.kind != unionType &&
3187           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3188           return true;
3189       // RECENTLY ADDED THESE
3190       else if(dest.kind == doubleType && source.kind == floatType)
3191          return true;
3192       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3193          return true;
3194       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3195          return true;
3196       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3197          return true;
3198       else if(dest.kind == int128Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == int64Type || source.kind == intSizeType))
3199          return true;
3200       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3201          return true;
3202       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3203          return true;
3204       else if(source.kind == enumType &&
3205          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3206           return true;
3207       else if(dest.kind == enumType && !isConversionExploration &&
3208          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3209           return true;
3210       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3211               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3212       {
3213          Type paramSource, paramDest;
3214
3215          if(dest.kind == methodType)
3216             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3217          if(source.kind == methodType)
3218             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3219
3220          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3221          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3222          if(dest.kind == methodType)
3223             dest = dest.method.dataType;
3224          if(source.kind == methodType)
3225             source = source.method.dataType;
3226
3227          paramSource = source.params.first;
3228          if(paramSource && paramSource.kind == voidType) paramSource = null;
3229          paramDest = dest.params.first;
3230          if(paramDest && paramDest.kind == voidType) paramDest = null;
3231
3232
3233          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3234             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3235          {
3236             // Source thisClass must be derived from destination thisClass
3237             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3238                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3239             {
3240                if(paramDest && paramDest.kind == classType)
3241                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3242                else
3243                   Compiler_Error($"method class should not take an object\n");
3244                return false;
3245             }
3246             paramDest = paramDest.next;
3247          }
3248          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3249          {
3250             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3251             {
3252                if(dest.thisClass)
3253                {
3254                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3255                   {
3256                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3257                      return false;
3258                   }
3259                }
3260                else
3261                {
3262                   // THIS WAS BACKWARDS:
3263                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3264                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3265                   {
3266                      if(owningClassDest)
3267                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3268                      else
3269                         Compiler_Error($"overriding class expected to be derived from method class\n");
3270                      return false;
3271                   }
3272                }
3273                paramSource = paramSource.next;
3274             }
3275             else
3276             {
3277                if(dest.thisClass)
3278                {
3279                   // Source thisClass must be derived from destination thisClass
3280                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3281                   {
3282                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3283                      return false;
3284                   }
3285                }
3286                else
3287                {
3288                   // THIS WAS BACKWARDS TOO??
3289                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3290                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3291                   {
3292                      //if(owningClass)
3293                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3294                      //else
3295                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3296                      return false;
3297                   }
3298                }
3299             }
3300          }
3301
3302
3303          // Source return type must be derived from destination return type
3304          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false, warnConst))
3305          {
3306             Compiler_Warning($"incompatible return type for function\n");
3307             return false;
3308          }
3309          // The const check is backwards from the MatchTypes above (for derivative classes checks)
3310          else
3311             CheckConstCompatibility(dest.returnType, source.returnType, true);
3312
3313          // Check parameters
3314
3315          for(; paramDest; paramDest = paramDest.next)
3316          {
3317             if(!paramSource)
3318             {
3319                //Compiler_Warning($"not enough parameters\n");
3320                Compiler_Error($"not enough parameters\n");
3321                return false;
3322             }
3323             {
3324                Type paramDestType = paramDest;
3325                Type paramSourceType = paramSource;
3326                Type type = paramDestType;
3327
3328                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3329                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3330                   paramSource.kind != templateType)
3331                {
3332                   int id = 0;
3333                   ClassTemplateParameter curParam = null;
3334                   Class sClass;
3335                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3336                   {
3337                      id = 0;
3338                      if(sClass.templateClass) sClass = sClass.templateClass;
3339                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3340                      {
3341                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3342                         {
3343                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3344                            {
3345                               if(sClass.templateClass) sClass = sClass.templateClass;
3346                               id += sClass.templateParams.count;
3347                            }
3348                            break;
3349                         }
3350                         id++;
3351                      }
3352                      if(curParam) break;
3353                   }
3354
3355                   if(curParam)
3356                   {
3357                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3358                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3359                   }
3360                }
3361
3362                // paramDest must be derived from paramSource
3363                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false, warnConst) &&
3364                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false, warnConst)))
3365                {
3366                   char type[1024];
3367                   type[0] = 0;
3368                   PrintType(paramDest, type, false, true);
3369                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3370
3371                   if(paramDestType != paramDest)
3372                      FreeType(paramDestType);
3373                   return false;
3374                }
3375                if(paramDestType != paramDest)
3376                   FreeType(paramDestType);
3377             }
3378
3379             paramSource = paramSource.next;
3380          }
3381          if(paramSource)
3382          {
3383             Compiler_Error($"too many parameters\n");
3384             return false;
3385          }
3386          return true;
3387       }
3388       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3389       {
3390          return true;
3391       }
3392       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3393          (source.kind == arrayType || source.kind == pointerType))
3394       {
3395          // Pointers to pointer is incompatible with non normal/nohead classes
3396          if(!(dest.type && dest.type.kind == pointerType && source.type.kind == classType && source.type._class &&
3397             source.type._class.registered && (source.type._class.registered.type != normalClass && source.type._class.registered.type != noHeadClass) && !source.type.byReference))
3398          {
3399             ComputeTypeSize(source.type);
3400             ComputeTypeSize(dest.type);
3401             if(source.type.size == dest.type.size && MatchTypes(source.type, dest.type, null, null, null, true, true, false, false, warnConst))
3402                return true;
3403          }
3404       }
3405    }
3406    return false;
3407 }
3408
3409 static void FreeConvert(Conversion convert)
3410 {
3411    if(convert.resultType)
3412       FreeType(convert.resultType);
3413 }
3414
3415 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3416                               char * string, OldList conversions)
3417 {
3418    BTNamedLink link;
3419
3420    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3421    {
3422       Class _class = link.data;
3423       if(_class.type == enumClass)
3424       {
3425          OldList converts { };
3426          Type type { };
3427          type.kind = classType;
3428
3429          if(!_class.symbol)
3430             _class.symbol = FindClass(_class.fullName);
3431          type._class = _class.symbol;
3432
3433          if(MatchTypes(type, dest, &converts, null, null, dest.kind != classType || !dest._class || strcmp(dest._class.string, "bool"),
3434                false, false, false, false))
3435          {
3436             NamedLink64 value;
3437             Class enumClass = eSystem_FindClass(privateModule, "enum");
3438             if(enumClass)
3439             {
3440                Class baseClass;
3441                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3442                {
3443                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3444                   for(value = e.values.first; value; value = value.next)
3445                   {
3446                      if(!strcmp(value.name, string))
3447                         break;
3448                   }
3449                   if(value)
3450                   {
3451                      FreeType(sourceExp.expType);
3452
3453                      sourceExp.isConstant = true;
3454                      sourceExp.expType = MkClassType(baseClass.fullName);
3455                      if(inCompiler || inPreCompiler || inDebugger)
3456                      {
3457                         char constant[256];
3458                         FreeExpContents(sourceExp);
3459
3460                         sourceExp.type = constantExp;
3461                         if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "short") || !strcmp(baseClass.dataTypeString, "char"))
3462                            sprintf(constant, FORMAT64D, value.data);
3463                         else
3464                            sprintf(constant, FORMAT64HEXLL, value.data);
3465                         sourceExp.constant = CopyString(constant);
3466                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3467                      }
3468
3469                      while(converts.first)
3470                      {
3471                         Conversion convert = converts.first;
3472                         converts.Remove(convert);
3473                         conversions.Add(convert);
3474                      }
3475                      delete type;
3476                      return true;
3477                   }
3478                }
3479             }
3480          }
3481          if(converts.first)
3482             converts.Free(FreeConvert);
3483          delete type;
3484       }
3485    }
3486    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3487       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3488          return true;
3489    return false;
3490 }
3491
3492 public bool ModuleVisibility(Module searchIn, Module searchFor)
3493 {
3494    SubModule subModule;
3495
3496    if(searchFor == searchIn)
3497       return true;
3498
3499    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3500    {
3501       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3502       {
3503          if(ModuleVisibility(subModule.module, searchFor))
3504             return true;
3505       }
3506    }
3507    return false;
3508 }
3509
3510 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3511 {
3512    Module module;
3513
3514    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3515       return true;
3516    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3517       return true;
3518    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3519       return true;
3520
3521    for(module = mainModule.application.allModules.first; module; module = module.next)
3522    {
3523       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3524          return true;
3525    }
3526    return false;
3527 }
3528
3529 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla, bool warnConst)
3530 {
3531    Type source;
3532    Type realDest = dest;
3533    Type backupSourceExpType = null;
3534    Expression nbExp = GetNonBracketsExp(sourceExp);
3535    Expression computedExp = nbExp;
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(nbExp);        // 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 != nbExp)
3550       {
3551          FreeExpression(computedExp);
3552          computedExp = nbExp;
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 != nbExp)
3573             {
3574                FreeExpression(computedExp);
3575                computedExp = nbExp;
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 && computedExp.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 != nbExp)
3606       {
3607          FreeExpression(computedExp);
3608          computedExp = nbExp;
3609       }
3610
3611       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3612          !strcmp(source._class.registered.fullName, "unichar" /*"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, warnConst);
3641
3642                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3643                backupSourceExpType = sourceExp.expType;
3644                if(dest.passAsTemplate)
3645                {
3646                   // Don't carry passAsTemplate
3647                   sourceExp.expType = { };
3648                   CopyTypeInto(sourceExp.expType, dest);
3649                   sourceExp.expType.passAsTemplate = false;
3650                }
3651                else
3652                {
3653                   sourceExp.expType = dest;
3654                   dest.refCount++;
3655                }
3656                //sourceExp.expType = MkClassType(_class.fullName);
3657                flag = true;
3658
3659                delete tempType;
3660             }
3661          }
3662
3663
3664          // Why wasn't there something like this?
3665          if(_class && _class.type == bitClass && source.kind != classType)
3666          {
3667             if(!dest._class.registered.dataType)
3668                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3669             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false, warnConst))
3670             {
3671                FreeType(source);
3672                FreeType(sourceExp.expType);
3673                source = sourceExp.expType = MkClassType(dest._class.string);
3674                source.refCount++;
3675
3676                //source.kind = classType;
3677                //source._class = dest._class;
3678             }
3679          }
3680
3681          // Adding two enumerations
3682          /*
3683          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3684          {
3685             if(!source._class.registered.dataType)
3686                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3687             if(!dest._class.registered.dataType)
3688                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3689
3690             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3691             {
3692                FreeType(source);
3693                source = sourceExp.expType = MkClassType(dest._class.string);
3694                source.refCount++;
3695
3696                //source.kind = classType;
3697                //source._class = dest._class;
3698             }
3699          }*/
3700
3701          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3702          {
3703             OldList * specs = MkList();
3704             Declarator decl;
3705             char string[1024];
3706
3707             ReadString(string, sourceExp.string);
3708             decl = SpecDeclFromString(string, specs, null);
3709
3710             FreeExpContents(sourceExp);
3711             FreeType(sourceExp.expType);
3712
3713             sourceExp.type = classExp;
3714             sourceExp._classExp.specifiers = specs;
3715             sourceExp._classExp.decl = decl;
3716             sourceExp.expType = dest;
3717             dest.refCount++;
3718
3719             FreeType(source);
3720             FreeType(dest);
3721             if(backupSourceExpType) FreeType(backupSourceExpType);
3722             return true;
3723          }
3724       }
3725       else if(source.kind == classType)
3726       {
3727          Class _class = source._class ? source._class.registered : null;
3728
3729          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || _class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3730          {
3731             /*
3732             if(dest.kind != classType)
3733             {
3734                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3735                if(!source._class.registered.dataType)
3736                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3737
3738                FreeType(dest);
3739                dest = MkClassType(source._class.string);
3740                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3741                //   dest = MkClassType(source._class.string);
3742             }
3743             */
3744
3745             if(dest.kind != classType)
3746             {
3747                Type tempType { };
3748                Type tempDest, tempSource;
3749
3750                if(!source._class.registered.dataType)
3751                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3752
3753                for(; _class.base.type != systemClass; _class = _class.base);
3754                tempDest = source;
3755                tempSource = tempType;
3756                tempType.kind = classType;
3757                tempType._class = FindClass(_class.fullName);
3758                tempType.truth = source.truth;
3759                tempType.classObjectType = source.classObjectType;
3760
3761                if(tempType._class)
3762                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3763
3764                // PUT THIS BACK TESTING UNITS?
3765                if(conversions && conversions.last)
3766                {
3767                   ((Conversion)(conversions.last)).resultType = dest;
3768                   dest.refCount++;
3769
3770                   // This fixes passing unit as template to a double
3771                   modifyPassAsTemplate(&((Conversion)(conversions.last)).resultType, false);
3772                }
3773
3774                FreeType(sourceExp.expType);
3775                sourceExp.expType = MkClassType(_class.fullName);
3776                sourceExp.expType.truth = source.truth;
3777                sourceExp.expType.classObjectType = source.classObjectType;
3778
3779                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3780
3781                if(!sourceExp.destType)
3782                {
3783                   FreeType(sourceExp.destType);
3784                   sourceExp.destType = sourceExp.expType;
3785                   if(sourceExp.expType)
3786                      sourceExp.expType.refCount++;
3787                }
3788                //flag = true;
3789                //source = _class.dataType;
3790
3791
3792                // TOCHECK: TESTING THIS NEW CODE
3793                if(!_class.dataType)
3794                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3795                FreeType(dest);
3796                dest = MkClassType(source._class.string);
3797                dest.truth = source.truth;
3798                dest.classObjectType = source.classObjectType;
3799
3800                FreeType(source);
3801                source = _class.dataType;
3802                source.refCount++;
3803
3804                delete tempType;
3805             }
3806          }
3807       }
3808
3809       if(!flag)
3810       {
3811          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false, warnConst))
3812          {
3813             FreeType(source);
3814             FreeType(dest);
3815             return true;
3816          }
3817       }
3818
3819       // Implicit Casts
3820       /*
3821       if(source.kind == classType)
3822       {
3823          Class _class = source._class.registered;
3824          if(_class.type == unitClass)
3825          {
3826             if(!_class.dataType)
3827                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3828             source = _class.dataType;
3829          }
3830       }*/
3831
3832       if(dest.kind == classType)
3833       {
3834          Class _class = dest._class ? dest._class.registered : null;
3835          bool fittingValue = false;
3836          if(_class && _class.type == enumClass)
3837          {
3838             Class enumClass = eSystem_FindClass(privateModule, "enum");
3839             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3840             if(c && value >= 0 && value <= c.largest)
3841                fittingValue = true;
3842          }
3843
3844          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3845             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3846          {
3847             if(_class.type == normalClass || _class.type == noHeadClass)
3848             {
3849                Expression newExp { };
3850                *newExp = *sourceExp;
3851                if(sourceExp.destType) sourceExp.destType.refCount++;
3852                if(sourceExp.expType)  sourceExp.expType.refCount++;
3853                sourceExp.type = castExp;
3854                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3855                sourceExp.cast.exp = newExp;
3856                FreeType(sourceExp.expType);
3857                sourceExp.expType = null;
3858                ProcessExpressionType(sourceExp);
3859
3860                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3861                if(!inCompiler)
3862                {
3863                   FreeType(sourceExp.expType);
3864                   sourceExp.expType = dest;
3865                }
3866
3867                FreeType(source);
3868                if(inCompiler) FreeType(dest);
3869
3870                if(backupSourceExpType) FreeType(backupSourceExpType);
3871                return true;
3872             }
3873
3874             if(!_class.dataType)
3875                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3876             FreeType(dest);
3877             dest = _class.dataType;
3878             dest.refCount++;
3879          }
3880
3881          // Accept lower precision types for units, since we want to keep the unit type
3882          if(dest.kind == doubleType &&
3883             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3884              source.kind == charType || source.kind == _BoolType))
3885          {
3886             specs = MkListOne(MkSpecifier(DOUBLE));
3887          }
3888          else if(dest.kind == floatType &&
3889             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3890             source.kind == _BoolType || source.kind == doubleType))
3891          {
3892             specs = MkListOne(MkSpecifier(FLOAT));
3893          }
3894          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3895             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3896          {
3897             specs = MkList();
3898             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3899             ListAdd(specs, MkSpecifier(INT64));
3900          }
3901          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3902             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3903          {
3904             specs = MkList();
3905             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3906             ListAdd(specs, MkSpecifier(INT));
3907          }
3908          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3909             source.kind == floatType || source.kind == doubleType))
3910          {
3911             specs = MkList();
3912             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3913             ListAdd(specs, MkSpecifier(SHORT));
3914          }
3915          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3916             source.kind == floatType || source.kind == doubleType))
3917          {
3918             specs = MkList();
3919             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3920             ListAdd(specs, MkSpecifier(CHAR));
3921          }
3922          else
3923          {
3924             FreeType(source);
3925             FreeType(dest);
3926             if(backupSourceExpType)
3927             {
3928                // Failed to convert: revert previous exp type
3929                if(sourceExp.expType) FreeType(sourceExp.expType);
3930                sourceExp.expType = backupSourceExpType;
3931             }
3932             return false;
3933          }
3934       }
3935       else if(dest.kind == doubleType &&
3936          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3937           source.kind == _BoolType || source.kind == charType))
3938       {
3939          specs = MkListOne(MkSpecifier(DOUBLE));
3940       }
3941       else if(dest.kind == floatType &&
3942          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3943       {
3944          specs = MkListOne(MkSpecifier(FLOAT));
3945       }
3946       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3947          (value == 1 || value == 0))
3948       {
3949          specs = MkList();
3950          ListAdd(specs, MkSpecifier(BOOL));
3951       }
3952       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3953          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3954       {
3955          if(source.kind == intType)
3956          {
3957             FreeType(dest);
3958             FreeType(source);
3959             if(backupSourceExpType) FreeType(backupSourceExpType);
3960             return true;
3961          }
3962          else
3963          {
3964             specs = MkList();
3965             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3966             ListAdd(specs, MkSpecifier(CHAR));
3967          }
3968       }
3969       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3970          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3971       {
3972          if(source.kind == intType)
3973          {
3974             FreeType(dest);
3975             FreeType(source);
3976             if(backupSourceExpType) FreeType(backupSourceExpType);
3977             return true;
3978          }
3979          else
3980          {
3981             specs = MkList();
3982             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3983             ListAdd(specs, MkSpecifier(SHORT));
3984          }
3985       }
3986       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3987       {
3988          specs = MkList();
3989          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3990          ListAdd(specs, MkSpecifier(INT));
3991       }
3992       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3993       {
3994          specs = MkList();
3995          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3996          ListAdd(specs, MkSpecifier(INT64));
3997       }
3998       else if(dest.kind == enumType &&
3999          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4000       {
4001          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
4002       }
4003       else
4004       {
4005          FreeType(source);
4006          FreeType(dest);
4007          if(backupSourceExpType)
4008          {
4009             // Failed to convert: revert previous exp type
4010             if(sourceExp.expType) FreeType(sourceExp.expType);
4011             sourceExp.expType = backupSourceExpType;
4012          }
4013          return false;
4014       }
4015
4016       if(!flag && !sourceExp.opDestType)
4017       {
4018          Expression newExp { };
4019          *newExp = *sourceExp;
4020          newExp.prev = null;
4021          newExp.next = null;
4022          if(sourceExp.destType) sourceExp.destType.refCount++;
4023          if(sourceExp.expType)  sourceExp.expType.refCount++;
4024
4025          sourceExp.type = castExp;
4026          if(realDest.kind == classType)
4027          {
4028             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
4029             FreeList(specs, FreeSpecifier);
4030          }
4031          else
4032             sourceExp.cast.typeName = MkTypeName(specs, null);
4033          if(newExp.type == opExp)
4034          {
4035             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4036          }
4037          else
4038             sourceExp.cast.exp = newExp;
4039
4040          FreeType(sourceExp.expType);
4041          sourceExp.expType = null;
4042          ProcessExpressionType(sourceExp);
4043       }
4044       else
4045          FreeList(specs, FreeSpecifier);
4046
4047       FreeType(dest);
4048       FreeType(source);
4049       if(backupSourceExpType) FreeType(backupSourceExpType);
4050
4051       return true;
4052    }
4053    else
4054    {
4055       if(computedExp != nbExp)
4056       {
4057          FreeExpression(computedExp);
4058          computedExp = nbExp;
4059       }
4060
4061       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4062       if(sourceExp.type == identifierExp)
4063       {
4064          Identifier id = sourceExp.identifier;
4065          if(dest.kind == classType)
4066          {
4067             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4068             {
4069                Class _class = dest._class.registered;
4070                Class enumClass = eSystem_FindClass(privateModule, "enum");
4071                if(enumClass)
4072                {
4073                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4074                   {
4075                      NamedLink64 value;
4076                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4077                      for(value = e.values.first; value; value = value.next)
4078                      {
4079                         if(!strcmp(value.name, id.string))
4080                            break;
4081                      }
4082                      if(value)
4083                      {
4084                         FreeType(sourceExp.expType);
4085
4086                         sourceExp.isConstant = true;
4087                         sourceExp.expType = MkClassType(_class.fullName);
4088                         if(inCompiler || inPreCompiler || inDebugger)
4089                         {
4090                            FreeExpContents(sourceExp);
4091
4092                            sourceExp.type = constantExp;
4093                            if(_class.dataTypeString && (!strcmp(_class.dataTypeString, "int") || !strcmp(_class.dataTypeString, "int64") || !strcmp(_class.dataTypeString, "short") || !strcmp(_class.dataTypeString, "char"))) // _class cannot be null here!
4094                               sourceExp.constant = PrintInt64(value.data);
4095                            else
4096                               sourceExp.constant = PrintUInt64(value.data);
4097                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4098                         }
4099                         FreeType(dest);
4100                         return true;
4101                      }
4102                   }
4103                }
4104             }
4105          }
4106
4107          // Loop through all enum classes
4108          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4109          {
4110             FreeType(dest);
4111             return true;
4112          }
4113       }
4114       FreeType(dest);
4115    }
4116    return false;
4117 }
4118
4119 #define TERTIARY(o, name, m, t, p) \
4120    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4121    {                                                              \
4122       exp.type = constantExp;                                    \
4123       exp.string = p(op1.m ? op2.m : op3.m);                     \
4124       if(!exp.expType) \
4125          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4126       return true;                                                \
4127    }
4128
4129 #define BINARY(o, name, m, t, p) \
4130    static bool name(Expression exp, Operand op1, Operand op2)   \
4131    {                                                              \
4132       t value2 = op2.m;                                           \
4133       exp.type = constantExp;                                    \
4134       exp.string = p((t)(op1.m o value2));                     \
4135       if(!exp.expType) \
4136          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4137       return true;                                                \
4138    }
4139
4140 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4141    static bool name(Expression exp, Operand op1, Operand op2)   \
4142    {                                                              \
4143       t value2 = op2.m;                                           \
4144       exp.type = constantExp;                                    \
4145       exp.string = p(value2 ? ((t)(op1.m o value2)) : 0);             \
4146       if(!exp.expType) \
4147          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4148       return true;                                                \
4149    }
4150
4151 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4152    static bool name(Expression exp, Operand op1, Operand op2)   \
4153    {                                                              \
4154       t value2 = op2.m;                                           \
4155       exp.type = constantExp;                                    \
4156       exp.string = p((t)(op1.m o value2));             \
4157       if(!exp.expType) \
4158          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4159       return true;                                                \
4160    }
4161
4162 #define UNARY(o, name, m, t, p) \
4163    static bool name(Expression exp, Operand op1)                \
4164    {                                                              \
4165       exp.type = constantExp;                                    \
4166       exp.string = p((t)(o op1.m));                                   \
4167       if(!exp.expType) \
4168          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4169       return true;                                                \
4170    }
4171
4172 #define OPERATOR_ALL(macro, o, name) \
4173    macro(o, Int##name, i, int, PrintInt) \
4174    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4175    macro(o, Int64##name, i64, int64, PrintInt64) \
4176    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4177    macro(o, Short##name, s, short, PrintShort) \
4178    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4179    macro(o, Char##name, c, char, PrintChar) \
4180    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4181    macro(o, Float##name, f, float, PrintFloat) \
4182    macro(o, Double##name, d, double, PrintDouble)
4183
4184 #define OPERATOR_INTTYPES(macro, o, name) \
4185    macro(o, Int##name, i, int, PrintInt) \
4186    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4187    macro(o, Int64##name, i64, int64, PrintInt64) \
4188    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4189    macro(o, Short##name, s, short, PrintShort) \
4190    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4191    macro(o, Char##name, c, char, PrintChar) \
4192    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4193
4194 #define OPERATOR_REALTYPES(macro, o, name) \
4195    macro(o, Float##name, f, float, PrintFloat) \
4196    macro(o, Double##name, d, double, PrintDouble)
4197
4198 // binary arithmetic
4199 OPERATOR_ALL(BINARY, +, Add)
4200 OPERATOR_ALL(BINARY, -, Sub)
4201 OPERATOR_ALL(BINARY, *, Mul)
4202 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4203 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4204 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4205
4206 // unary arithmetic
4207 OPERATOR_ALL(UNARY, -, Neg)
4208
4209 // unary arithmetic increment and decrement
4210 OPERATOR_ALL(UNARY, ++, Inc)
4211 OPERATOR_ALL(UNARY, --, Dec)
4212
4213 // binary arithmetic assignment
4214 OPERATOR_ALL(BINARY, =, Asign)
4215 OPERATOR_ALL(BINARY, +=, AddAsign)
4216 OPERATOR_ALL(BINARY, -=, SubAsign)
4217 OPERATOR_ALL(BINARY, *=, MulAsign)
4218 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4219 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4220 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4221
4222 // binary bitwise
4223 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4224 OPERATOR_INTTYPES(BINARY, |, BitOr)
4225 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4226 OPERATOR_INTTYPES(BINARY, <<, LShift)
4227 OPERATOR_INTTYPES(BINARY, >>, RShift)
4228
4229 // unary bitwise
4230 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4231
4232 // binary bitwise assignment
4233 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4234 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4235 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4236 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4237 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4238
4239 // unary logical negation
4240 OPERATOR_INTTYPES(UNARY, !, Not)
4241
4242 // binary logical equality
4243 OPERATOR_ALL(BINARY, ==, Equ)
4244 OPERATOR_ALL(BINARY, !=, Nqu)
4245
4246 // binary logical
4247 OPERATOR_ALL(BINARY, &&, And)
4248 OPERATOR_ALL(BINARY, ||, Or)
4249
4250 // binary logical relational
4251 OPERATOR_ALL(BINARY, >, Grt)
4252 OPERATOR_ALL(BINARY, <, Sma)
4253 OPERATOR_ALL(BINARY, >=, GrtEqu)
4254 OPERATOR_ALL(BINARY, <=, SmaEqu)
4255
4256 // tertiary condition operator
4257 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4258
4259 //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
4260 #define OPERATOR_TABLE_ALL(name, type) \
4261     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4262                           type##Neg, \
4263                           type##Inc, type##Dec, \
4264                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4265                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4266                           type##BitNot, \
4267                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4268                           type##Not, \
4269                           type##Equ, type##Nqu, \
4270                           type##And, type##Or, \
4271                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4272                         }; \
4273
4274 #define OPERATOR_TABLE_INTTYPES(name, type) \
4275     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4276                           type##Neg, \
4277                           type##Inc, type##Dec, \
4278                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4279                           null, null, null, null, null, \
4280                           null, \
4281                           null, null, null, null, null, \
4282                           null, \
4283                           type##Equ, type##Nqu, \
4284                           type##And, type##Or, \
4285                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4286                         }; \
4287
4288 OPERATOR_TABLE_ALL(int, Int)
4289 OPERATOR_TABLE_ALL(uint, UInt)
4290 OPERATOR_TABLE_ALL(int64, Int64)
4291 OPERATOR_TABLE_ALL(uint64, UInt64)
4292 OPERATOR_TABLE_ALL(short, Short)
4293 OPERATOR_TABLE_ALL(ushort, UShort)
4294 OPERATOR_TABLE_INTTYPES(float, Float)
4295 OPERATOR_TABLE_INTTYPES(double, Double)
4296 OPERATOR_TABLE_ALL(char, Char)
4297 OPERATOR_TABLE_ALL(uchar, UChar)
4298
4299 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4300 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4301 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4302 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4303 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4304 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4305 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4306 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4307
4308 public void ReadString(char * output,  char * string)
4309 {
4310    int len = strlen(string);
4311    int c,d = 0;
4312    bool quoted = false, escaped = false;
4313    for(c = 0; c<len; c++)
4314    {
4315       char ch = string[c];
4316       if(escaped)
4317       {
4318          switch(ch)
4319          {
4320             case 'n': output[d] = '\n'; break;
4321             case 't': output[d] = '\t'; break;
4322             case 'a': output[d] = '\a'; break;
4323             case 'b': output[d] = '\b'; break;
4324             case 'f': output[d] = '\f'; break;
4325             case 'r': output[d] = '\r'; break;
4326             case 'v': output[d] = '\v'; break;
4327             case '\\': output[d] = '\\'; break;
4328             case '\"': output[d] = '\"'; break;
4329             case '\'': output[d] = '\''; break;
4330             default: output[d] = ch;
4331          }
4332          d++;
4333          escaped = false;
4334       }
4335       else
4336       {
4337          if(ch == '\"')
4338             quoted ^= true;
4339          else if(quoted)
4340          {
4341             if(ch == '\\')
4342                escaped = true;
4343             else
4344                output[d++] = ch;
4345          }
4346       }
4347    }
4348    output[d] = '\0';
4349 }
4350
4351 // String Unescape Copy
4352
4353 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4354 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4355 public int UnescapeString(char * d, char * s, int len)
4356 {
4357    int j = 0, k = 0;
4358    char ch;
4359    while(j < len && (ch = s[j]))
4360    {
4361       switch(ch)
4362       {
4363          case '\\':
4364             switch((ch = s[++j]))
4365             {
4366                case 'n': d[k] = '\n'; break;
4367                case 't': d[k] = '\t'; break;
4368                case 'a': d[k] = '\a'; break;
4369                case 'b': d[k] = '\b'; break;
4370                case 'f': d[k] = '\f'; break;
4371                case 'r': d[k] = '\r'; break;
4372                case 'v': d[k] = '\v'; break;
4373                case '\\': d[k] = '\\'; break;
4374                case '\"': d[k] = '\"'; break;
4375                case '\'': d[k] = '\''; break;
4376                default: d[k] = '\\'; d[k] = ch;
4377             }
4378             break;
4379          default:
4380             d[k] = ch;
4381       }
4382       j++, k++;
4383    }
4384    d[k] = '\0';
4385    return k;
4386 }
4387
4388 public char * OffsetEscapedString(char * s, int len, int offset)
4389 {
4390    char ch;
4391    int j = 0, k = 0;
4392    while(j < len && k < offset && (ch = s[j]))
4393    {
4394       if(ch == '\\') ++j;
4395       j++, k++;
4396    }
4397    return (k == offset) ? s + j : null;
4398 }
4399
4400 public Operand GetOperand(Expression exp)
4401 {
4402    Operand op { };
4403    Type type = exp.expType;
4404    if(type)
4405    {
4406       while(type.kind == classType && type._class &&
4407          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4408       {
4409          if(!type._class.registered.dataType)
4410             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4411          type = type._class.registered.dataType;
4412
4413       }
4414       if(exp.type == stringExp && op.kind == pointerType)
4415       {
4416          op.ui64 = (uint64)(uintptr)exp.string;
4417          op.kind = pointerType;
4418          op.ops = uint64Ops;
4419       }
4420       else if(exp.isConstant && exp.type == constantExp)
4421       {
4422          op.kind = type.kind;
4423          op.type = type;
4424
4425          switch(op.kind)
4426          {
4427             case _BoolType:
4428             case charType:
4429             {
4430                if(exp.constant[0] == '\'')
4431                {
4432                   op.c = exp.constant[1];
4433                   op.ops = charOps;
4434                }
4435                else if(type.isSigned)
4436                {
4437                   op.c = (char)strtol(exp.constant, null, 0);
4438                   op.ops = charOps;
4439                }
4440                else
4441                {
4442                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4443                   op.ops = ucharOps;
4444                }
4445                break;
4446             }
4447             case shortType:
4448                if(exp.constant[0] == '\'')
4449                {
4450                   op.s = exp.constant[1];
4451                   op.ops = shortOps;
4452                }
4453                else if(type.isSigned)
4454                {
4455                   op.s = (short)strtol(exp.constant, null, 0);
4456                   op.ops = shortOps;
4457                }
4458                else
4459                {
4460                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4461                   op.ops = ushortOps;
4462                }
4463                break;
4464             case intType:
4465             case longType:
4466                if(exp.constant[0] == '\'')
4467                {
4468                   op.i = exp.constant[1];
4469                   op.ops = intOps;
4470                }
4471                else if(type.isSigned)
4472                {
4473                   op.i = (int)strtol(exp.constant, null, 0);
4474                   op.ops = intOps;
4475                }
4476                else
4477                {
4478                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4479                   op.ops = uintOps;
4480                }
4481                op.kind = intType;
4482                break;
4483             case int64Type:
4484                if(type.isSigned)
4485                {
4486                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4487                   op.ops = int64Ops;
4488                }
4489                else
4490                {
4491                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4492                   op.ops = uint64Ops;
4493                }
4494                op.kind = int64Type;
4495                break;
4496             case intPtrType:
4497                if(type.isSigned)
4498                {
4499                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4500                   op.ops = int64Ops;
4501                }
4502                else
4503                {
4504                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4505                   op.ops = uint64Ops;
4506                }
4507                op.kind = int64Type;
4508                break;
4509             case intSizeType:
4510                if(type.isSigned)
4511                {
4512                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4513                   op.ops = int64Ops;
4514                }
4515                else
4516                {
4517                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4518                   op.ops = uint64Ops;
4519                }
4520                op.kind = int64Type;
4521                break;
4522             case floatType:
4523                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4524                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4525                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4526                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4527                else
4528                   op.f = (float)strtod(exp.constant, null);
4529                op.ops = floatOps;
4530                break;
4531             case doubleType:
4532                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4533                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4534                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4535                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4536                else
4537                   op.d = (double)strtod(exp.constant, null);
4538                op.ops = doubleOps;
4539                break;
4540             //case classType:    For when we have operator overloading...
4541             // Pointer additions
4542             //case functionType:
4543             case arrayType:
4544             case pointerType:
4545             case classType:
4546                op.ui64 = _strtoui64(exp.constant, null, 0);
4547                op.kind = pointerType;
4548                op.ops = uint64Ops;
4549                // op.ptrSize =
4550                break;
4551          }
4552       }
4553    }
4554    return op;
4555 }
4556
4557 static int64 GetEnumValue(Class _class, void * ptr)
4558 {
4559    int64 v = 0;
4560    switch(_class.typeSize)
4561    {
4562       case 8:
4563          if(!strcmp(_class.dataTypeString, "uint64"))
4564             v = (int64)*(uint64 *)ptr;
4565          else
4566             v = (int64)*(int64 *)ptr;
4567          break;
4568       case 4:
4569          if(!strcmp(_class.dataTypeString, "uint"))
4570             v = (int64)*(uint *)ptr;
4571          else
4572             v = (int64)*(int *)ptr;
4573          break;
4574       case 2:
4575          if(!strcmp(_class.dataTypeString, "uint16"))
4576             v = (int64)*(uint16 *)ptr;
4577          else
4578             v = (int64)*(short *)ptr;
4579          break;
4580       case 1:
4581          if(!strcmp(_class.dataTypeString, "byte"))
4582             v = (int64)*(byte *)ptr;
4583          else
4584             v = (int64)*(char *)ptr;
4585          break;
4586    }
4587    return v;
4588 }
4589
4590 static __attribute__((unused)) void UnusedFunction()
4591 {
4592    int a;
4593    a.OnGetString(0,0,0);
4594 }
4595 default:
4596 extern int __ecereVMethodID_class_OnGetString;
4597 public:
4598
4599 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4600 {
4601    DataMember dataMember;
4602    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4603    {
4604       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4605          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4606       else
4607       {
4608          Expression exp { };
4609          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4610          Type type;
4611          void * ptr = inst.data + dataMember.offset + offset;
4612          char * result = null;
4613          exp.loc = member.loc = inst.loc;
4614          ((Identifier)member.identifiers->first).loc = inst.loc;
4615
4616          if(!dataMember.dataType)
4617             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4618          type = dataMember.dataType;
4619          if(type.kind == classType)
4620          {
4621             Class _class = type._class.registered;
4622             if(_class.type == enumClass)
4623             {
4624                Class enumClass = eSystem_FindClass(privateModule, "enum");
4625                if(enumClass)
4626                {
4627                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4628                   NamedLink64 item;
4629                   for(item = e.values.first; item; item = item.next)
4630                   {
4631                      if(item.data == GetEnumValue(_class, ptr))
4632                      {
4633                         result = item.name;
4634                         break;
4635                      }
4636                   }
4637                   if(result)
4638                   {
4639                      exp.identifier = MkIdentifier(result);
4640                      exp.type = identifierExp;
4641                      exp.destType = MkClassType(_class.fullName);
4642                      ProcessExpressionType(exp);
4643                   }
4644                }
4645             }
4646             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4647             {
4648                if(!_class.dataType)
4649                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4650                type = _class.dataType;
4651             }
4652          }
4653          if(!result)
4654          {
4655             switch(type.kind)
4656             {
4657                case floatType:
4658                {
4659                   FreeExpContents(exp);
4660
4661                   exp.constant = PrintFloat(*(float*)ptr);
4662                   exp.type = constantExp;
4663                   break;
4664                }
4665                case doubleType:
4666                {
4667                   FreeExpContents(exp);
4668
4669                   exp.constant = PrintDouble(*(double*)ptr);
4670                   exp.type = constantExp;
4671                   break;
4672                }
4673                case intType:
4674                {
4675                   FreeExpContents(exp);
4676
4677                   exp.constant = PrintInt(*(int*)ptr);
4678                   exp.type = constantExp;
4679                   break;
4680                }
4681                case int64Type:
4682                {
4683                   FreeExpContents(exp);
4684
4685                   exp.constant = PrintInt64(*(int64*)ptr);
4686                   exp.type = constantExp;
4687                   break;
4688                }
4689                case intPtrType:
4690                {
4691                   FreeExpContents(exp);
4692                   // TODO: This should probably use proper type
4693                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4694                   exp.type = constantExp;
4695                   break;
4696                }
4697                case intSizeType:
4698                {
4699                   FreeExpContents(exp);
4700                   // TODO: This should probably use proper type
4701                   exp.constant = PrintInt64((int64)*(intsize*)ptr);
4702                   exp.type = constantExp;
4703                   break;
4704                }
4705                default:
4706                   Compiler_Error($"Unhandled type populating instance\n");
4707             }
4708          }
4709          ListAdd(memberList, member);
4710       }
4711
4712       if(parentDataMember.type == unionMember)
4713          break;
4714    }
4715 }
4716
4717 void PopulateInstance(Instantiation inst)
4718 {
4719    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4720    Class _class = classSym.registered;
4721    DataMember dataMember;
4722    OldList * memberList = MkList();
4723    // Added this check and ->Add to prevent memory leaks on bad code
4724    if(!inst.members)
4725       inst.members = MkListOne(MkMembersInitList(memberList));
4726    else
4727       inst.members->Add(MkMembersInitList(memberList));
4728    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4729    {
4730       if(!dataMember.isProperty)
4731       {
4732          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4733             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4734          else
4735          {
4736             Expression exp { };
4737             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4738             Type type;
4739             void * ptr = inst.data + dataMember.offset;
4740             char * result = null;
4741
4742             exp.loc = member.loc = inst.loc;
4743             ((Identifier)member.identifiers->first).loc = inst.loc;
4744
4745             if(!dataMember.dataType)
4746                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4747             type = dataMember.dataType;
4748             if(type.kind == classType)
4749             {
4750                Class _class = type._class.registered;
4751                if(_class.type == enumClass)
4752                {
4753                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4754                   if(enumClass)
4755                   {
4756                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4757                      NamedLink64 item;
4758                      for(item = e.values.first; item; item = item.next)
4759                      {
4760                         if(item.data == GetEnumValue(_class, ptr))
4761                         {
4762                            result = item.name;
4763                            break;
4764                         }
4765                      }
4766                   }
4767                   if(result)
4768                   {
4769                      exp.identifier = MkIdentifier(result);
4770                      exp.type = identifierExp;
4771                      exp.destType = MkClassType(_class.fullName);
4772                      ProcessExpressionType(exp);
4773                   }
4774                }
4775                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4776                {
4777                   if(!_class.dataType)
4778                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4779                   type = _class.dataType;
4780                }
4781             }
4782             if(!result)
4783             {
4784                switch(type.kind)
4785                {
4786                   case floatType:
4787                   {
4788                      exp.constant = PrintFloat(*(float*)ptr);
4789                      exp.type = constantExp;
4790                      break;
4791                   }
4792                   case doubleType:
4793                   {
4794                      exp.constant = PrintDouble(*(double*)ptr);
4795                      exp.type = constantExp;
4796                      break;
4797                   }
4798                   case intType:
4799                   {
4800                      exp.constant = PrintInt(*(int*)ptr);
4801                      exp.type = constantExp;
4802                      break;
4803                   }
4804                   case int64Type:
4805                   {
4806                      exp.constant = PrintInt64(*(int64*)ptr);
4807                      exp.type = constantExp;
4808                      break;
4809                   }
4810                   case intPtrType:
4811                   {
4812                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4813                      exp.type = constantExp;
4814                      break;
4815                   }
4816                   default:
4817                      Compiler_Error($"Unhandled type populating instance\n");
4818                }
4819             }
4820             ListAdd(memberList, member);
4821          }
4822       }
4823    }
4824 }
4825
4826 void ComputeInstantiation(Expression exp)
4827 {
4828    Instantiation inst = exp.instance;
4829    MembersInit members;
4830    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4831    Class _class = classSym ? classSym.registered : null;
4832    DataMember curMember = null;
4833    Class curClass = null;
4834    DataMember subMemberStack[256];
4835    int subMemberStackPos = 0;
4836    uint64 bits = 0;
4837
4838    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4839    {
4840       // Don't recompute the instantiation...
4841       // Non Simple classes will have become constants by now
4842       if(inst.data)
4843          return;
4844
4845       if(_class.type == normalClass || _class.type == noHeadClass)
4846       {
4847          inst.data = (byte *)eInstance_New(_class);
4848          if(_class.type == normalClass)
4849             ((Instance)inst.data)._refCount++;
4850       }
4851       else
4852          inst.data = new0 byte[_class.structSize];
4853    }
4854
4855    if(inst.members)
4856    {
4857       for(members = inst.members->first; members; members = members.next)
4858       {
4859          switch(members.type)
4860          {
4861             case dataMembersInit:
4862             {
4863                if(members.dataMembers)
4864                {
4865                   MemberInit member;
4866                   for(member = members.dataMembers->first; member; member = member.next)
4867                   {
4868                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4869                      bool found = false;
4870
4871                      Property prop = null;
4872                      DataMember dataMember = null;
4873                      uint dataMemberOffset;
4874
4875                      if(!ident)
4876                      {
4877                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4878                         if(curMember)
4879                         {
4880                            if(curMember.isProperty)
4881                               prop = (Property)curMember;
4882                            else
4883                            {
4884                               dataMember = curMember;
4885
4886                               // CHANGED THIS HERE
4887                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4888
4889                               // 2013/17/29 -- It seems that this was missing here!
4890                               if(_class.type == normalClass)
4891                                  dataMemberOffset += _class.base.structSize;
4892                               // dataMemberOffset = dataMember.offset;
4893                            }
4894                            found = true;
4895                         }
4896                      }
4897                      else
4898                      {
4899                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4900                         if(prop)
4901                         {
4902                            found = true;
4903                            if(prop.memberAccess == publicAccess)
4904                            {
4905                               curMember = (DataMember)prop;
4906                               curClass = prop._class;
4907                            }
4908                         }
4909                         else
4910                         {
4911                            DataMember _subMemberStack[256];
4912                            int _subMemberStackPos = 0;
4913
4914                            // FILL MEMBER STACK
4915                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4916
4917                            if(dataMember)
4918                            {
4919                               found = true;
4920                               if(dataMember.memberAccess == publicAccess)
4921                               {
4922                                  curMember = dataMember;
4923                                  curClass = dataMember._class;
4924                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4925                                  subMemberStackPos = _subMemberStackPos;
4926                               }
4927                            }
4928                         }
4929                      }
4930
4931                      if(found && member.initializer && member.initializer.type == expInitializer)
4932                      {
4933                         Expression value = member.initializer.exp;
4934                         Type type = null;
4935                         bool deepMember = false;
4936                         if(prop)
4937                         {
4938                            type = prop.dataType;
4939                         }
4940                         else if(dataMember)
4941                         {
4942                            if(!dataMember.dataType)
4943                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4944
4945                            type = dataMember.dataType;
4946                         }
4947
4948                         if(ident && ident.next)
4949                         {
4950                            deepMember = true;
4951
4952                            // for(; ident && type; ident = ident.next)
4953                            for(ident = ident.next; ident && type; ident = ident.next)
4954                            {
4955                               if(type.kind == classType)
4956                               {
4957                                  prop = eClass_FindProperty(type._class.registered,
4958                                     ident.string, privateModule);
4959                                  if(prop)
4960                                     type = prop.dataType;
4961                                  else
4962                                  {
4963                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4964                                        ident.string, &dataMemberOffset, privateModule, null, null);
4965                                     if(dataMember)
4966                                        type = dataMember.dataType;
4967                                  }
4968                               }
4969                               else if(type.kind == structType || type.kind == unionType)
4970                               {
4971                                  Type memberType;
4972                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4973                                  {
4974                                     if(!strcmp(memberType.name, ident.string))
4975                                     {
4976                                        type = memberType;
4977                                        break;
4978                                     }
4979                                  }
4980                               }
4981                            }
4982                         }
4983                         if(value)
4984                         {
4985                            FreeType(value.destType);
4986                            value.destType = type;
4987                            if(type) type.refCount++;
4988                            ComputeExpression(value);
4989                         }
4990                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4991                         {
4992                            if(type.kind == classType)
4993                            {
4994                               Class _class = type._class.registered;
4995                               if(_class && (_class.type == bitClass || _class.type == unitClass || _class.type == enumClass))
4996                               {
4997                                  if(!_class.dataType)
4998                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4999                                  type = _class.dataType;
5000                               }
5001                            }
5002
5003                            if(dataMember)
5004                            {
5005                               void * ptr = inst.data + dataMemberOffset;
5006
5007                               if(value.type == constantExp)
5008                               {
5009                                  switch(type.kind)
5010                                  {
5011                                     case intType:
5012                                     {
5013                                        GetInt(value, (int*)ptr);
5014                                        break;
5015                                     }
5016                                     case int64Type:
5017                                     {
5018                                        GetInt64(value, (int64*)ptr);
5019                                        break;
5020                                     }
5021                                     case intPtrType:
5022                                     {
5023                                        GetIntPtr(value, (intptr*)ptr);
5024                                        break;
5025                                     }
5026                                     case intSizeType:
5027                                     {
5028                                        GetIntSize(value, (intsize*)ptr);
5029                                        break;
5030                                     }
5031                                     case floatType:
5032                                     {
5033                                        GetFloat(value, (float*)ptr);
5034                                        break;
5035                                     }
5036                                     case doubleType:
5037                                     {
5038                                        GetDouble(value, (double *)ptr);
5039                                        break;
5040                                     }
5041                                  }
5042                               }
5043                               else if(value.type == instanceExp)
5044                               {
5045                                  if(type.kind == classType)
5046                                  {
5047                                     Class _class = type._class.registered;
5048                                     if(_class.type == structClass)
5049                                     {
5050                                        ComputeTypeSize(type);
5051                                        if(value.instance.data)
5052                                           memcpy(ptr, value.instance.data, type.size);
5053                                     }
5054                                  }
5055                               }
5056                            }
5057                            else if(prop && prop.Set != (void *)(intptr)1)
5058                            {
5059                               if(value.type == instanceExp && value.instance.data)
5060                               {
5061                                  if(type.kind == classType)
5062                                  {
5063                                     Class _class = type._class.registered;
5064                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
5065                                     {
5066                                        void (*Set)(void *, void *) = (void *)prop.Set;
5067                                        Set(inst.data, value.instance.data);
5068                                        PopulateInstance(inst);
5069                                     }
5070                                  }
5071                               }
5072                               else if(value.type == constantExp)
5073                               {
5074                                  switch(type.kind)
5075                                  {
5076                                     case doubleType:
5077                                     {
5078                                        void (*Set)(void *, double) = (void *)prop.Set;
5079                                        Set(inst.data, strtod(value.constant, null) );
5080                                        break;
5081                                     }
5082                                     case floatType:
5083                                     {
5084                                        void (*Set)(void *, float) = (void *)prop.Set;
5085                                        Set(inst.data, (float)(strtod(value.constant, null)));
5086                                        break;
5087                                     }
5088                                     case intType:
5089                                     {
5090                                        void (*Set)(void *, int) = (void *)prop.Set;
5091                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5092                                        break;
5093                                     }
5094                                     case int64Type:
5095                                     {
5096                                        void (*Set)(void *, int64) = (void *)prop.Set;
5097                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5098                                        break;
5099                                     }
5100                                     case intPtrType:
5101                                     {
5102                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5103                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5104                                        break;
5105                                     }
5106                                     case intSizeType:
5107                                     {
5108                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5109                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5110                                        break;
5111                                     }
5112                                  }
5113                               }
5114                               else if(value.type == stringExp)
5115                               {
5116                                  char temp[1024];
5117                                  ReadString(temp, value.string);
5118                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5119                               }
5120                            }
5121                         }
5122                         else if(!deepMember && type && _class.type == unitClass)
5123                         {
5124                            if(prop)
5125                            {
5126                               // Only support converting units to units for now...
5127                               if(value.type == constantExp)
5128                               {
5129                                  if(type.kind == classType)
5130                                  {
5131                                     Class _class = type._class.registered;
5132                                     if(_class.type == unitClass)
5133                                     {
5134                                        if(!_class.dataType)
5135                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5136                                        type = _class.dataType;
5137                                     }
5138                                  }
5139                                  // TODO: Assuming same base type for units...
5140                                  switch(type.kind)
5141                                  {
5142                                     case floatType:
5143                                     {
5144                                        float fValue;
5145                                        float (*Set)(float) = (void *)prop.Set;
5146                                        GetFloat(member.initializer.exp, &fValue);
5147                                        exp.constant = PrintFloat(Set(fValue));
5148                                        exp.type = constantExp;
5149                                        break;
5150                                     }
5151                                     case doubleType:
5152                                     {
5153                                        double dValue;
5154                                        double (*Set)(double) = (void *)prop.Set;
5155                                        GetDouble(member.initializer.exp, &dValue);
5156                                        exp.constant = PrintDouble(Set(dValue));
5157                                        exp.type = constantExp;
5158                                        break;
5159                                     }
5160                                  }
5161                               }
5162                            }
5163                         }
5164                         else if(!deepMember && type && _class.type == bitClass)
5165                         {
5166                            if(prop)
5167                            {
5168                               if(value.type == instanceExp && value.instance.data)
5169                               {
5170                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5171                                  bits = Set(value.instance.data);
5172                               }
5173                               else if(value.type == constantExp)
5174                               {
5175                               }
5176                            }
5177                            else if(dataMember)
5178                            {
5179                               BitMember bitMember = (BitMember) dataMember;
5180                               Type type;
5181                               uint64 part = 0;
5182                               bits = (bits & ~bitMember.mask);
5183                               if(!bitMember.dataType)
5184                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5185                               type = bitMember.dataType;
5186                               if(type.kind == classType && type._class && type._class.registered)
5187                               {
5188                                  if(!type._class.registered.dataType)
5189                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5190                                  type = type._class.registered.dataType;
5191                               }
5192                               switch(type.kind)
5193                               {
5194                                  case _BoolType:
5195                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5196                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (short *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5197                                  case intType:
5198                                  case longType:       { uint v; type.isSigned ? GetInt(value, (int *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5199                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (int64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5200                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (intptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5201                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (intsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5202                               }
5203                               bits |= part << bitMember.pos;
5204                            }
5205                         }
5206                      }
5207                      else
5208                      {
5209                         if(_class && _class.type == unitClass)
5210                         {
5211                            ComputeExpression(member.initializer.exp);
5212                            exp.constant = member.initializer.exp.constant;
5213                            exp.type = constantExp;
5214
5215                            member.initializer.exp.constant = null;
5216                         }
5217                      }
5218                   }
5219                }
5220                break;
5221             }
5222          }
5223       }
5224    }
5225    if(_class && _class.type == bitClass)
5226    {
5227       exp.constant = PrintHexUInt(bits);
5228       exp.type = constantExp;
5229    }
5230    if(exp.type != instanceExp)
5231    {
5232       FreeInstance(inst);
5233    }
5234 }
5235
5236 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5237 {
5238    bool result = false;
5239    switch(kind)
5240    {
5241       case shortType:
5242          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5243             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5244          break;
5245       case intType:
5246       case longType:
5247          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5248             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5249          break;
5250       case int64Type:
5251          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5252             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5253             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5254          break;
5255       case floatType:
5256          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5257             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5258             result = GetOpFloat(op, &op.f);
5259          break;
5260       case doubleType:
5261          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5262             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5263             result = GetOpDouble(op, &op.d);
5264          break;
5265       case pointerType:
5266          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5267             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5268             result = GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5269          break;
5270       case enumType:
5271          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5272             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5273             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5274          break;
5275       case intPtrType:
5276          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5277             result = isSigned ? GetOpInt64 /*GetOpIntPtr*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5278          break;
5279       case intSizeType:
5280          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5281             result = isSigned ? GetOpInt64 /*GetOpIntSize*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntSize*/(op, &op.ui64);
5282          break;
5283    }
5284    return result;
5285 }
5286
5287 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5288 {
5289    if(exp.op.op == SIZEOF)
5290    {
5291       FreeExpContents(exp);
5292       exp.type = constantExp;
5293       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5294    }
5295    else
5296    {
5297       if(!exp.op.exp1)
5298       {
5299          switch(exp.op.op)
5300          {
5301             // unary arithmetic
5302             case '+':
5303             {
5304                // Provide default unary +
5305                Expression exp2 = exp.op.exp2;
5306                exp.op.exp2 = null;
5307                FreeExpContents(exp);
5308                FreeType(exp.expType);
5309                FreeType(exp.destType);
5310                *exp = *exp2;
5311                delete exp2;
5312                break;
5313             }
5314             case '-':
5315                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5316                break;
5317             // unary arithmetic increment and decrement
5318                   //OPERATOR_ALL(UNARY, ++, Inc)
5319                   //OPERATOR_ALL(UNARY, --, Dec)
5320             // unary bitwise
5321             case '~':
5322                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5323                break;
5324             // unary logical negation
5325             case '!':
5326                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5327                break;
5328          }
5329       }
5330       else
5331       {
5332          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5333          {
5334             if(Promote(op2, op1.kind, op1.type.isSigned))
5335                op2.kind = op1.kind, op2.ops = op1.ops;
5336             else if(Promote(op1, op2.kind, op2.type.isSigned))
5337                op1.kind = op2.kind, op1.ops = op2.ops;
5338          }
5339          switch(exp.op.op)
5340          {
5341             // binary arithmetic
5342             case '+':
5343                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5344                break;
5345             case '-':
5346                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5347                break;
5348             case '*':
5349                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5350                break;
5351             case '/':
5352                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5353                break;
5354             case '%':
5355                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5356                break;
5357             // binary arithmetic assignment
5358                   //OPERATOR_ALL(BINARY, =, Asign)
5359                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5360                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5361                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5362                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5363                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5364             // binary bitwise
5365             case '&':
5366                if(exp.op.exp2)
5367                {
5368                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5369                }
5370                break;
5371             case '|':
5372                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5373                break;
5374             case '^':
5375                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5376                break;
5377             case LEFT_OP:
5378                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5379                break;
5380             case RIGHT_OP:
5381                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5382                break;
5383             // binary bitwise assignment
5384                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5385                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5386                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5387                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5388                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5389             // binary logical equality
5390             case EQ_OP:
5391                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5392                break;
5393             case NE_OP:
5394                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5395                break;
5396             // binary logical
5397             case AND_OP:
5398                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5399                break;
5400             case OR_OP:
5401                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5402                break;
5403             // binary logical relational
5404             case '>':
5405                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5406                break;
5407             case '<':
5408                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5409                break;
5410             case GE_OP:
5411                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5412                break;
5413             case LE_OP:
5414                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5415                break;
5416          }
5417       }
5418    }
5419 }
5420
5421 void ComputeExpression(Expression exp)
5422 {
5423 #ifdef _DEBUG
5424    char expString[10240];
5425    expString[0] = '\0';
5426    PrintExpression(exp, expString);
5427 #endif
5428
5429    switch(exp.type)
5430    {
5431       case identifierExp:
5432       {
5433          Identifier id = exp.identifier;
5434          if(id && exp.isConstant && !inCompiler && !inPreCompiler && !inDebugger)
5435          {
5436             Class c = (exp.expType && exp.expType.kind == classType && exp.expType._class) ? exp.expType._class.registered : null;
5437             if(c && c.type == enumClass)
5438             {
5439                Class enumClass = eSystem_FindClass(privateModule, "enum");
5440                if(enumClass)
5441                {
5442                   NamedLink64 value;
5443                   EnumClassData e = ACCESS_CLASSDATA(c, enumClass);
5444                   for(value = e.values.first; value; value = value.next)
5445                   {
5446                      if(!strcmp(value.name, id.string))
5447                         break;
5448                   }
5449                   if(value)
5450                   {
5451                      const String dts = c.dataTypeString;
5452                      FreeExpContents(exp);
5453                      exp.type = constantExp;
5454                      exp.constant = (dts && (!strcmp(dts, "int") || !strcmp(dts, "int64") || !strcmp(dts, "short") || !strcmp(dts, "char"))) ? PrintInt64(value.data) : PrintUInt64(value.data);
5455                   }
5456                }
5457             }
5458          }
5459          break;
5460       }
5461       case instanceExp:
5462       {
5463          ComputeInstantiation(exp);
5464          break;
5465       }
5466       /*
5467       case constantExp:
5468          break;
5469       */
5470       case opExp:
5471       {
5472          Expression exp1, exp2 = null;
5473          Operand op1 { };
5474          Operand op2 { };
5475
5476          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5477          if(exp.op.exp2)
5478          {
5479             Expression e = exp.op.exp2;
5480
5481             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5482             {
5483                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5484                {
5485                   if(e.type == extensionCompoundExp)
5486                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5487                   else
5488                      e = e.list->last;
5489                }
5490             }
5491             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5492             {
5493                if(e.type == stringExp && e.string)
5494                {
5495                   char * string = e.string;
5496                   int len = strlen(string);
5497                   char * tmp = new char[len-2+1];
5498                   len = UnescapeString(tmp, string + 1, len - 2);
5499                   delete tmp;
5500                   FreeExpContents(exp);
5501                   exp.type = constantExp;
5502                   exp.constant = PrintUInt(len + 1);
5503                }
5504                else
5505                {
5506                   Type type = e.expType;
5507                   type.refCount++;
5508                   FreeExpContents(exp);
5509                   exp.type = constantExp;
5510                   exp.constant = PrintUInt(ComputeTypeSize(type));
5511                   FreeType(type);
5512                }
5513                break;
5514             }
5515             else
5516                ComputeExpression(exp.op.exp2);
5517          }
5518          if(exp.op.exp1)
5519          {
5520             ComputeExpression(exp.op.exp1);
5521             exp1 = exp.op.exp1;
5522             exp2 = exp.op.exp2;
5523             op1 = GetOperand(exp1);
5524             if(op1.type) op1.type.refCount++;
5525             if(exp2)
5526             {
5527                op2 = GetOperand(exp2);
5528                if(op2.type) op2.type.refCount++;
5529             }
5530          }
5531          else
5532          {
5533             exp1 = exp.op.exp2;
5534             op1 = GetOperand(exp1);
5535             if(op1.type) op1.type.refCount++;
5536          }
5537
5538          CallOperator(exp, exp1, exp2, op1, op2);
5539          /*
5540          switch(exp.op.op)
5541          {
5542             // Unary operators
5543             case '&':
5544                // Also binary
5545                if(exp.op.exp1 && exp.op.exp2)
5546                {
5547                   // Binary And
5548                   if(op1.ops.BitAnd)
5549                   {
5550                      FreeExpContents(exp);
5551                      op1.ops.BitAnd(exp, op1, op2);
5552                   }
5553                }
5554                break;
5555             case '*':
5556                if(exp.op.exp1)
5557                {
5558                   if(op1.ops.Mul)
5559                   {
5560                      FreeExpContents(exp);
5561                      op1.ops.Mul(exp, op1, op2);
5562                   }
5563                }
5564                break;
5565             case '+':
5566                if(exp.op.exp1)
5567                {
5568                   if(op1.ops.Add)
5569                   {
5570                      FreeExpContents(exp);
5571                      op1.ops.Add(exp, op1, op2);
5572                   }
5573                }
5574                else
5575                {
5576                   // Provide default unary +
5577                   Expression exp2 = exp.op.exp2;
5578                   exp.op.exp2 = null;
5579                   FreeExpContents(exp);
5580                   FreeType(exp.expType);
5581                   FreeType(exp.destType);
5582
5583                   *exp = *exp2;
5584                   delete exp2;
5585                }
5586                break;
5587             case '-':
5588                if(exp.op.exp1)
5589                {
5590                   if(op1.ops.Sub)
5591                   {
5592                      FreeExpContents(exp);
5593                      op1.ops.Sub(exp, op1, op2);
5594                   }
5595                }
5596                else
5597                {
5598                   if(op1.ops.Neg)
5599                   {
5600                      FreeExpContents(exp);
5601                      op1.ops.Neg(exp, op1);
5602                   }
5603                }
5604                break;
5605             case '~':
5606                if(op1.ops.BitNot)
5607                {
5608                   FreeExpContents(exp);
5609                   op1.ops.BitNot(exp, op1);
5610                }
5611                break;
5612             case '!':
5613                if(op1.ops.Not)
5614                {
5615                   FreeExpContents(exp);
5616                   op1.ops.Not(exp, op1);
5617                }
5618                break;
5619             // Binary only operators
5620             case '/':
5621                if(op1.ops.Div)
5622                {
5623                   FreeExpContents(exp);
5624                   op1.ops.Div(exp, op1, op2);
5625                }
5626                break;
5627             case '%':
5628                if(op1.ops.Mod)
5629                {
5630                   FreeExpContents(exp);
5631                   op1.ops.Mod(exp, op1, op2);
5632                }
5633                break;
5634             case LEFT_OP:
5635                break;
5636             case RIGHT_OP:
5637                break;
5638             case '<':
5639                if(exp.op.exp1)
5640                {
5641                   if(op1.ops.Sma)
5642                   {
5643                      FreeExpContents(exp);
5644                      op1.ops.Sma(exp, op1, op2);
5645                   }
5646                }
5647                break;
5648             case '>':
5649                if(exp.op.exp1)
5650                {
5651                   if(op1.ops.Grt)
5652                   {
5653                      FreeExpContents(exp);
5654                      op1.ops.Grt(exp, op1, op2);
5655                   }
5656                }
5657                break;
5658             case LE_OP:
5659                if(exp.op.exp1)
5660                {
5661                   if(op1.ops.SmaEqu)
5662                   {
5663                      FreeExpContents(exp);
5664                      op1.ops.SmaEqu(exp, op1, op2);
5665                   }
5666                }
5667                break;
5668             case GE_OP:
5669                if(exp.op.exp1)
5670                {
5671                   if(op1.ops.GrtEqu)
5672                   {
5673                      FreeExpContents(exp);
5674                      op1.ops.GrtEqu(exp, op1, op2);
5675                   }
5676                }
5677                break;
5678             case EQ_OP:
5679                if(exp.op.exp1)
5680                {
5681                   if(op1.ops.Equ)
5682                   {
5683                      FreeExpContents(exp);
5684                      op1.ops.Equ(exp, op1, op2);
5685                   }
5686                }
5687                break;
5688             case NE_OP:
5689                if(exp.op.exp1)
5690                {
5691                   if(op1.ops.Nqu)
5692                   {
5693                      FreeExpContents(exp);
5694                      op1.ops.Nqu(exp, op1, op2);
5695                   }
5696                }
5697                break;
5698             case '|':
5699                if(op1.ops.BitOr)
5700                {
5701                   FreeExpContents(exp);
5702                   op1.ops.BitOr(exp, op1, op2);
5703                }
5704                break;
5705             case '^':
5706                if(op1.ops.BitXor)
5707                {
5708                   FreeExpContents(exp);
5709                   op1.ops.BitXor(exp, op1, op2);
5710                }
5711                break;
5712             case AND_OP:
5713                break;
5714             case OR_OP:
5715                break;
5716             case SIZEOF:
5717                FreeExpContents(exp);
5718                exp.type = constantExp;
5719                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5720                break;
5721          }
5722          */
5723          if(op1.type) FreeType(op1.type);
5724          if(op2.type) FreeType(op2.type);
5725          break;
5726       }
5727       case bracketsExp:
5728       case extensionExpressionExp:
5729       {
5730          Expression e, n;
5731          for(e = exp.list->first; e; e = n)
5732          {
5733             n = e.next;
5734             if(!n)
5735             {
5736                OldList * list = exp.list;
5737                Expression prev = exp.prev;
5738                Expression next = exp.next;
5739
5740                // For operations which set the exp type on brackets exp after the inner exp was processed...
5741                if(exp.expType && exp.expType.kind == classType && (!e.expType || e.expType.kind != classType))
5742                {
5743                   FreeType(e.expType);
5744                   e.expType = exp.expType;
5745                   e.expType.refCount++;
5746                }
5747                ComputeExpression(e);
5748                //FreeExpContents(exp);
5749                FreeType(exp.expType);
5750                FreeType(exp.destType);
5751                *exp = *e;
5752                exp.prev = prev;
5753                exp.next = next;
5754                delete e;
5755                delete list;
5756             }
5757             else
5758             {
5759                FreeExpression(e);
5760             }
5761          }
5762          break;
5763       }
5764       /*
5765
5766       case ExpIndex:
5767       {
5768          Expression e;
5769          exp.isConstant = true;
5770
5771          ComputeExpression(exp.index.exp);
5772          if(!exp.index.exp.isConstant)
5773             exp.isConstant = false;
5774
5775          for(e = exp.index.index->first; e; e = e.next)
5776          {
5777             ComputeExpression(e);
5778             if(!e.next)
5779             {
5780                // Check if this type is int
5781             }
5782             if(!e.isConstant)
5783                exp.isConstant = false;
5784          }
5785          exp.expType = Dereference(exp.index.exp.expType);
5786          break;
5787       }
5788       */
5789       case memberExp:
5790       {
5791          Expression memberExp = exp.member.exp;
5792          Identifier memberID = exp.member.member;
5793
5794          Type type;
5795          ComputeExpression(exp.member.exp);
5796          type = exp.member.exp.expType;
5797          if(type)
5798          {
5799             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);
5800             Property prop = null;
5801             DataMember member = null;
5802             Class convertTo = null;
5803             if(type.kind == subClassType && exp.member.exp.type == classExp)
5804                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5805
5806             if(!_class)
5807             {
5808                char string[256];
5809                Symbol classSym;
5810                string[0] = '\0';
5811                PrintTypeNoConst(type, string, false, true);
5812                classSym = FindClass(string);
5813                _class = classSym ? classSym.registered : null;
5814             }
5815
5816             if(exp.member.member)
5817             {
5818                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5819                if(!prop)
5820                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5821             }
5822             if(!prop && !member && _class && exp.member.member)
5823             {
5824                Symbol classSym = FindClass(exp.member.member.string);
5825                convertTo = _class;
5826                _class = classSym ? classSym.registered : null;
5827                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5828             }
5829
5830             if(prop)
5831             {
5832                if(prop.compiled)
5833                {
5834                   Type type = prop.dataType;
5835                   // TODO: Assuming same base type for units...
5836                   if(_class.type == unitClass)
5837                   {
5838                      if(type.kind == classType)
5839                      {
5840                         Class _class = type._class.registered;
5841                         if(_class.type == unitClass)
5842                         {
5843                            if(!_class.dataType)
5844                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5845                            type = _class.dataType;
5846                         }
5847                      }
5848                      switch(type.kind)
5849                      {
5850                         case floatType:
5851                         {
5852                            float value;
5853                            float (*Get)(float) = (void *)prop.Get;
5854                            GetFloat(exp.member.exp, &value);
5855                            exp.constant = PrintFloat(Get ? Get(value) : value);
5856                            exp.type = constantExp;
5857                            break;
5858                         }
5859                         case doubleType:
5860                         {
5861                            double value;
5862                            double (*Get)(double);
5863                            GetDouble(exp.member.exp, &value);
5864
5865                            if(convertTo)
5866                               Get = (void *)prop.Set;
5867                            else
5868                               Get = (void *)prop.Get;
5869                            exp.constant = PrintDouble(Get ? Get(value) : value);
5870                            exp.type = constantExp;
5871                            break;
5872                         }
5873                      }
5874                   }
5875                   else
5876                   {
5877                      if(convertTo)
5878                      {
5879                         Expression value = exp.member.exp;
5880                         Type type;
5881                         if(!prop.dataType)
5882                            ProcessPropertyType(prop);
5883
5884                         type = prop.dataType;
5885                         if(!type)
5886                         {
5887                             // printf("Investigate this\n");
5888                         }
5889                         else if(_class.type == structClass)
5890                         {
5891                            switch(type.kind)
5892                            {
5893                               case classType:
5894                               {
5895                                  Class propertyClass = type._class.registered;
5896                                  if(propertyClass.type == structClass && value.type == instanceExp)
5897                                  {
5898                                     void (*Set)(void *, void *) = (void *)prop.Set;
5899                                     exp.instance = Instantiation { };
5900                                     exp.instance.data = new0 byte[_class.structSize];
5901                                     exp.instance._class = MkSpecifierName(_class.fullName);
5902                                     exp.instance.loc = exp.loc;
5903                                     exp.type = instanceExp;
5904                                     Set(exp.instance.data, value.instance.data);
5905                                     PopulateInstance(exp.instance);
5906                                  }
5907                                  break;
5908                               }
5909                               case intType:
5910                               {
5911                                  int intValue;
5912                                  void (*Set)(void *, int) = (void *)prop.Set;
5913
5914                                  exp.instance = Instantiation { };
5915                                  exp.instance.data = new0 byte[_class.structSize];
5916                                  exp.instance._class = MkSpecifierName(_class.fullName);
5917                                  exp.instance.loc = exp.loc;
5918                                  exp.type = instanceExp;
5919
5920                                  GetInt(value, &intValue);
5921
5922                                  Set(exp.instance.data, intValue);
5923                                  PopulateInstance(exp.instance);
5924                                  break;
5925                               }
5926                               case int64Type:
5927                               {
5928                                  int64 intValue;
5929                                  void (*Set)(void *, int64) = (void *)prop.Set;
5930
5931                                  exp.instance = Instantiation { };
5932                                  exp.instance.data = new0 byte[_class.structSize];
5933                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5934                                  exp.instance.loc = exp.loc;
5935                                  exp.type = instanceExp;
5936
5937                                  GetInt64(value, &intValue);
5938
5939                                  Set(exp.instance.data, intValue);
5940                                  PopulateInstance(exp.instance);
5941                                  break;
5942                               }
5943                               case intPtrType:
5944                               {
5945                                  // TOFIX:
5946                                  intptr intValue;
5947                                  void (*Set)(void *, intptr) = (void *)prop.Set;
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.type = instanceExp;
5954
5955                                  GetIntPtr(value, &intValue);
5956
5957                                  Set(exp.instance.data, intValue);
5958                                  PopulateInstance(exp.instance);
5959                                  break;
5960                               }
5961                               case intSizeType:
5962                               {
5963                                  // TOFIX:
5964                                  intsize intValue;
5965                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5966
5967                                  exp.instance = Instantiation { };
5968                                  exp.instance.data = new0 byte[_class.structSize];
5969                                  exp.instance._class = MkSpecifierName(_class.fullName);
5970                                  exp.instance.loc = exp.loc;
5971                                  exp.type = instanceExp;
5972
5973                                  GetIntSize(value, &intValue);
5974
5975                                  Set(exp.instance.data, intValue);
5976                                  PopulateInstance(exp.instance);
5977                                  break;
5978                               }
5979                               case floatType:
5980                               {
5981                                  float floatValue;
5982                                  void (*Set)(void *, float) = (void *)prop.Set;
5983
5984                                  exp.instance = Instantiation { };
5985                                  exp.instance.data = new0 byte[_class.structSize];
5986                                  exp.instance._class = MkSpecifierName(_class.fullName);
5987                                  exp.instance.loc = exp.loc;
5988                                  exp.type = instanceExp;
5989
5990                                  GetFloat(value, &floatValue);
5991
5992                                  Set(exp.instance.data, floatValue);
5993                                  PopulateInstance(exp.instance);
5994                                  break;
5995                               }
5996                               case doubleType:
5997                               {
5998                                  double doubleValue;
5999                                  void (*Set)(void *, double) = (void *)prop.Set;
6000
6001                                  exp.instance = Instantiation { };
6002                                  exp.instance.data = new0 byte[_class.structSize];
6003                                  exp.instance._class = MkSpecifierName(_class.fullName);
6004                                  exp.instance.loc = exp.loc;
6005                                  exp.type = instanceExp;
6006
6007                                  GetDouble(value, &doubleValue);
6008
6009                                  Set(exp.instance.data, doubleValue);
6010                                  PopulateInstance(exp.instance);
6011                                  break;
6012                               }
6013                            }
6014                         }
6015                         else if(_class.type == bitClass)
6016                         {
6017                            switch(type.kind)
6018                            {
6019                               case classType:
6020                               {
6021                                  Class propertyClass = type._class.registered;
6022                                  if(propertyClass.type == structClass && value.instance.data)
6023                                  {
6024                                     unsigned int (*Set)(void *) = (void *)prop.Set;
6025                                     unsigned int bits = Set(value.instance.data);
6026                                     exp.constant = PrintHexUInt(bits);
6027                                     exp.type = constantExp;
6028                                     break;
6029                                  }
6030                                  else if(_class.type == bitClass)
6031                                  {
6032                                     unsigned int value;
6033                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
6034                                     unsigned int bits;
6035
6036                                     GetUInt(exp.member.exp, &value);
6037                                     bits = Set(value);
6038                                     exp.constant = PrintHexUInt(bits);
6039                                     exp.type = constantExp;
6040                                  }
6041                               }
6042                            }
6043                         }
6044                      }
6045                      else
6046                      {
6047                         if(_class.type == bitClass)
6048                         {
6049                            unsigned int value;
6050                            GetUInt(exp.member.exp, &value);
6051
6052                            switch(type.kind)
6053                            {
6054                               case classType:
6055                               {
6056                                  Class _class = type._class.registered;
6057                                  if(_class.type == structClass)
6058                                  {
6059                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
6060
6061                                     exp.instance = Instantiation { };
6062                                     exp.instance.data = new0 byte[_class.structSize];
6063                                     exp.instance._class = MkSpecifierName(_class.fullName);
6064                                     exp.instance.loc = exp.loc;
6065                                     //exp.instance.fullSet = true;
6066                                     exp.type = instanceExp;
6067                                     Get(value, exp.instance.data);
6068                                     PopulateInstance(exp.instance);
6069                                  }
6070                                  else if(_class.type == bitClass)
6071                                  {
6072                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
6073                                     uint64 bits = Get(value);
6074                                     exp.constant = PrintHexUInt64(bits);
6075                                     exp.type = constantExp;
6076                                  }
6077                                  break;
6078                               }
6079                            }
6080                         }
6081                         else if(_class.type == structClass)
6082                         {
6083                            byte * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
6084                            switch(type.kind)
6085                            {
6086                               case classType:
6087                               {
6088                                  Class _class = type._class.registered;
6089                                  if(_class.type == structClass && value)
6090                                  {
6091                                     void (*Get)(void *, void *) = (void *)prop.Get;
6092
6093                                     exp.instance = Instantiation { };
6094                                     exp.instance.data = new0 byte[_class.structSize];
6095                                     exp.instance._class = MkSpecifierName(_class.fullName);
6096                                     exp.instance.loc = exp.loc;
6097                                     //exp.instance.fullSet = true;
6098                                     exp.type = instanceExp;
6099                                     Get(value, exp.instance.data);
6100                                     PopulateInstance(exp.instance);
6101                                  }
6102                                  break;
6103                               }
6104                            }
6105                         }
6106                         /*else
6107                         {
6108                            char * value = exp.member.exp.instance.data;
6109                            switch(type.kind)
6110                            {
6111                               case classType:
6112                               {
6113                                  Class _class = type._class.registered;
6114                                  if(_class.type == normalClass)
6115                                  {
6116                                     void *(*Get)(void *) = (void *)prop.Get;
6117
6118                                     exp.instance = Instantiation { };
6119                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6120                                     exp.type = instanceExp;
6121                                     exp.instance.data = Get(value, exp.instance.data);
6122                                  }
6123                                  break;
6124                               }
6125                            }
6126                         }
6127                         */
6128                      }
6129                   }
6130                }
6131                else
6132                {
6133                   exp.isConstant = false;
6134                }
6135             }
6136             else if(member)
6137             {
6138             }
6139          }
6140
6141          if(exp.type != ExpressionType::memberExp)
6142          {
6143             FreeExpression(memberExp);
6144             FreeIdentifier(memberID);
6145          }
6146          break;
6147       }
6148       case typeSizeExp:
6149       {
6150          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6151          FreeExpContents(exp);
6152          exp.constant = PrintUInt(ComputeTypeSize(type));
6153          exp.type = constantExp;
6154          FreeType(type);
6155          break;
6156       }
6157       case classSizeExp:
6158       {
6159          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6160          if(classSym && classSym.registered)
6161          {
6162             if(classSym.registered.fixed)
6163             {
6164                FreeSpecifier(exp._class);
6165                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6166                exp.type = constantExp;
6167             }
6168             else
6169             {
6170                char className[1024];
6171                strcpy(className, "__ecereClass_");
6172                FullClassNameCat(className, classSym.string, true);
6173
6174                DeclareClass(curExternal, classSym, className);
6175
6176                FreeExpContents(exp);
6177                exp.type = pointerExp;
6178                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6179                exp.member.member = MkIdentifier("structSize");
6180             }
6181          }
6182          break;
6183       }
6184       case castExp:
6185       //case constantExp:
6186       {
6187          Type type;
6188          Expression e = exp;
6189          if(exp.type == castExp)
6190          {
6191             if(exp.cast.exp)
6192                ComputeExpression(exp.cast.exp);
6193             e = exp.cast.exp;
6194          }
6195          if(e && exp.expType)
6196          {
6197             /*if(exp.destType)
6198                type = exp.destType;
6199             else*/
6200                type = exp.expType;
6201             if(type.kind == classType)
6202             {
6203                Class _class = type._class.registered;
6204                if(_class && (_class.type == unitClass || _class.type == bitClass))
6205                {
6206                   if(!_class.dataType)
6207                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6208                   type = _class.dataType;
6209                }
6210             }
6211
6212             switch(type.kind)
6213             {
6214                case _BoolType:
6215                case charType:
6216                   if(type.isSigned)
6217                   {
6218                      char value = 0;
6219                      if(GetChar(e, &value))
6220                      {
6221                         FreeExpContents(exp);
6222                         exp.constant = PrintChar(value);
6223                         exp.type = constantExp;
6224                      }
6225                   }
6226                   else
6227                   {
6228                      unsigned char value = 0;
6229                      if(GetUChar(e, &value))
6230                      {
6231                         FreeExpContents(exp);
6232                         exp.constant = PrintUChar(value);
6233                         exp.type = constantExp;
6234                      }
6235                   }
6236                   break;
6237                case shortType:
6238                   if(type.isSigned)
6239                   {
6240                      short value = 0;
6241                      if(GetShort(e, &value))
6242                      {
6243                         FreeExpContents(exp);
6244                         exp.constant = PrintShort(value);
6245                         exp.type = constantExp;
6246                      }
6247                   }
6248                   else
6249                   {
6250                      unsigned short value = 0;
6251                      if(GetUShort(e, &value))
6252                      {
6253                         FreeExpContents(exp);
6254                         exp.constant = PrintUShort(value);
6255                         exp.type = constantExp;
6256                      }
6257                   }
6258                   break;
6259                case intType:
6260                   if(type.isSigned)
6261                   {
6262                      int value = 0;
6263                      if(GetInt(e, &value))
6264                      {
6265                         FreeExpContents(exp);
6266                         exp.constant = PrintInt(value);
6267                         exp.type = constantExp;
6268                      }
6269                   }
6270                   else
6271                   {
6272                      unsigned int value = 0;
6273                      if(GetUInt(e, &value))
6274                      {
6275                         FreeExpContents(exp);
6276                         exp.constant = PrintUInt(value);
6277                         exp.type = constantExp;
6278                      }
6279                   }
6280                   break;
6281                case int64Type:
6282                   if(type.isSigned)
6283                   {
6284                      int64 value = 0;
6285                      if(GetInt64(e, &value))
6286                      {
6287                         FreeExpContents(exp);
6288                         exp.constant = PrintInt64(value);
6289                         exp.type = constantExp;
6290                      }
6291                   }
6292                   else
6293                   {
6294                      uint64 value = 0;
6295                      if(GetUInt64(e, &value))
6296                      {
6297                         FreeExpContents(exp);
6298                         exp.constant = PrintUInt64(value);
6299                         exp.type = constantExp;
6300                      }
6301                   }
6302                   break;
6303                case intPtrType:
6304                   if(type.isSigned)
6305                   {
6306                      intptr value = 0;
6307                      if(GetIntPtr(e, &value))
6308                      {
6309                         FreeExpContents(exp);
6310                         exp.constant = PrintInt64((int64)value);
6311                         exp.type = constantExp;
6312                      }
6313                   }
6314                   else
6315                   {
6316                      uintptr value = 0;
6317                      if(GetUIntPtr(e, &value))
6318                      {
6319                         FreeExpContents(exp);
6320                         exp.constant = PrintUInt64((uint64)value);
6321                         exp.type = constantExp;
6322                      }
6323                   }
6324                   break;
6325                case intSizeType:
6326                   if(type.isSigned)
6327                   {
6328                      intsize value = 0;
6329                      if(GetIntSize(e, &value))
6330                      {
6331                         FreeExpContents(exp);
6332                         exp.constant = PrintInt64((int64)value);
6333                         exp.type = constantExp;
6334                      }
6335                   }
6336                   else
6337                   {
6338                      uintsize value = 0;
6339                      if(GetUIntSize(e, &value))
6340                      {
6341                         FreeExpContents(exp);
6342                         exp.constant = PrintUInt64((uint64)value);
6343                         exp.type = constantExp;
6344                      }
6345                   }
6346                   break;
6347                case floatType:
6348                {
6349                   float value = 0;
6350                   if(GetFloat(e, &value))
6351                   {
6352                      FreeExpContents(exp);
6353                      exp.constant = PrintFloat(value);
6354                      exp.type = constantExp;
6355                   }
6356                   break;
6357                }
6358                case doubleType:
6359                {
6360                   double value = 0;
6361                   if(GetDouble(e, &value))
6362                   {
6363                      FreeExpContents(exp);
6364                      exp.constant = PrintDouble(value);
6365                      exp.type = constantExp;
6366                   }
6367                   break;
6368                }
6369             }
6370          }
6371          break;
6372       }
6373       case conditionExp:
6374       {
6375          Operand op1 { };
6376          Operand op2 { };
6377          Operand op3 { };
6378
6379          if(exp.cond.exp)
6380             // Caring only about last expression for now...
6381             ComputeExpression(exp.cond.exp->last);
6382          if(exp.cond.elseExp)
6383             ComputeExpression(exp.cond.elseExp);
6384          if(exp.cond.cond)
6385             ComputeExpression(exp.cond.cond);
6386
6387          op1 = GetOperand(exp.cond.cond);
6388          if(op1.type) op1.type.refCount++;
6389          op2 = GetOperand(exp.cond.exp->last);
6390          if(op2.type) op2.type.refCount++;
6391          op3 = GetOperand(exp.cond.elseExp);
6392          if(op3.type) op3.type.refCount++;
6393
6394          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6395          if(op1.type) FreeType(op1.type);
6396          if(op2.type) FreeType(op2.type);
6397          if(op3.type) FreeType(op3.type);
6398          break;
6399       }
6400    }
6401 }
6402
6403 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla, bool warnConst)
6404 {
6405    bool result = true;
6406    if(destType)
6407    {
6408       OldList converts { };
6409       Conversion convert;
6410
6411       if(destType.kind == voidType)
6412          return false;
6413
6414       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla, warnConst))
6415          result = false;
6416       if(converts.count)
6417       {
6418          // for(convert = converts.last; convert; convert = convert.prev)
6419          for(convert = converts.first; convert; convert = convert.next)
6420          {
6421             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6422             if(!empty)
6423             {
6424                Expression newExp { };
6425                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6426
6427                // TODO: Check this...
6428                *newExp = *exp;
6429                newExp.prev = null;
6430                newExp.next = null;
6431                newExp.destType = null;
6432
6433                if(convert.isGet)
6434                {
6435                   // [exp].ColorRGB
6436                   exp.type = memberExp;
6437                   exp.addedThis = true;
6438                   exp.member.exp = newExp;
6439                   FreeType(exp.member.exp.expType);
6440
6441                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6442                   exp.member.exp.expType.classObjectType = objectType;
6443                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6444                   exp.member.memberType = propertyMember;
6445                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6446                   // TESTING THIS... for (int)degrees
6447                   exp.needCast = true;
6448                   if(exp.expType) exp.expType.refCount++;
6449                   ApplyAnyObjectLogic(exp.member.exp);
6450                }
6451                else
6452                {
6453
6454                   /*if(exp.isConstant)
6455                   {
6456                      // Color { ColorRGB = [exp] };
6457                      exp.type = instanceExp;
6458                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6459                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6460                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6461                   }
6462                   else*/
6463                   {
6464                      // If not constant, don't turn it yet into an instantiation
6465                      // (Go through the deep members system first)
6466                      exp.type = memberExp;
6467                      exp.addedThis = true;
6468                      exp.member.exp = newExp;
6469
6470                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6471                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6472                         newExp.expType._class.registered.type == noHeadClass)
6473                      {
6474                         newExp.byReference = true;
6475                      }
6476
6477                      FreeType(exp.member.exp.expType);
6478                      /*exp.member.exp.expType = convert.convert.dataType;
6479                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6480                      exp.member.exp.expType = null;
6481                      if(convert.convert.dataType)
6482                      {
6483                         exp.member.exp.expType = { };
6484                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6485                         exp.member.exp.expType.refCount = 1;
6486                         exp.member.exp.expType.classObjectType = objectType;
6487                         ApplyAnyObjectLogic(exp.member.exp);
6488                      }
6489
6490                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6491                      exp.member.memberType = reverseConversionMember;
6492                      exp.expType = convert.resultType ? convert.resultType :
6493                         MkClassType(convert.convert._class.fullName);
6494                      exp.needCast = true;
6495                      if(convert.resultType) convert.resultType.refCount++;
6496                   }
6497                }
6498             }
6499             else
6500             {
6501                FreeType(exp.expType);
6502                if(convert.isGet)
6503                {
6504                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6505                   if(exp.destType.casted)
6506                      exp.needCast = true;
6507                   if(exp.expType) exp.expType.refCount++;
6508                }
6509                else
6510                {
6511                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6512                   if(exp.destType.casted)
6513                      exp.needCast = true;
6514                   if(convert.resultType)
6515                      convert.resultType.refCount++;
6516                }
6517             }
6518          }
6519          if(exp.isConstant && inCompiler)
6520             ComputeExpression(exp);
6521
6522          converts.Free(FreeConvert);
6523       }
6524
6525       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6526       {
6527          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false, warnConst);
6528       }
6529       if(!result && exp.expType && exp.destType)
6530       {
6531          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6532              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6533             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6534             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6535             result = true;
6536       }
6537    }
6538    // if(result) CheckTemplateTypes(exp);
6539    return result;
6540 }
6541
6542 void modifyPassAsTemplate(Type * typePtr, bool value)
6543 {
6544    if(*typePtr && typePtr->passAsTemplate != value)
6545    {
6546       Type type { refCount = 1 };
6547       CopyTypeInto(type, *typePtr);
6548       type.passAsTemplate = value;
6549       FreeType(*typePtr);
6550       *typePtr = type;
6551    }
6552 }
6553
6554 void CheckTemplateTypes(Expression exp)
6555 {
6556    /*
6557    bool et = exp.expType ? exp.expType.passAsTemplate : false;
6558    bool dt = exp.destType ? exp.destType.passAsTemplate : false;
6559    */
6560    Expression nbExp = GetNonBracketsExp(exp);
6561    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate &&
6562       (nbExp == exp || nbExp.type != castExp))
6563    {
6564       Expression newExp { };
6565       Context context;
6566       TypeKind kind = exp.expType.kind;
6567       *newExp = *exp;
6568       if(exp.destType) exp.destType.refCount++;
6569       if(exp.expType)  exp.expType.refCount++;
6570       newExp.prev = null;
6571       newExp.next = null;
6572
6573       if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered)
6574       {
6575          Class c = exp.expType._class.registered;
6576          if(c.type == bitClass || c.type == enumClass || c.type == unitClass)
6577          {
6578             if(!c.dataType)
6579                c.dataType = ProcessTypeString(c.dataTypeString, false);
6580             kind = c.dataType.kind;
6581          }
6582       }
6583
6584       switch(kind)
6585       {
6586          case doubleType:
6587             if(exp.destType.classObjectType)
6588             {
6589                // We need to pass the address, just pass it along (Undo what was done above)
6590                if(exp.destType) exp.destType.refCount--;
6591                if(exp.expType)  exp.expType.refCount--;
6592                delete newExp;
6593             }
6594             else
6595             {
6596                // We want to pass as a template argument
6597                // ({ union { double d; uint64 i; } u; u.d = [newExp]; u.i; })
6598                OldList * specs;
6599                OldList * unionDefs = MkList();
6600                OldList * statements = MkList();
6601                context = PushContext();
6602                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6603                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6604                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6605                exp.type = extensionCompoundExp;
6606
6607                modifyPassAsTemplate(&exp.expType, true);
6608                modifyPassAsTemplate(&newExp.destType, false);
6609                modifyPassAsTemplate(&newExp.expType, false);
6610
6611                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6612                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6613                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6614                exp.compound.compound.context = context;
6615                PopContext(context);
6616             }
6617             break;
6618          case floatType:
6619             if(exp.destType.classObjectType)
6620             {
6621                // We need to pass the address, just pass it along (Undo what was done above)
6622                if(exp.destType) exp.destType.refCount--;
6623                if(exp.expType)  exp.expType.refCount--;
6624                delete newExp;
6625             }
6626             else
6627             {
6628                // We want to pass as a template argument
6629                // ({ union { float f; uint64 i; } u; u.f = [newExp]; u.i; })
6630                OldList * specs;
6631                OldList * unionDefs = MkList();
6632                OldList * statements = MkList();
6633                context = PushContext();
6634                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(FLOAT)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("f"))), null)));
6635                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6636                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6637                exp.type = extensionCompoundExp;
6638                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6639
6640                modifyPassAsTemplate(&exp.expType, true);
6641                modifyPassAsTemplate(&newExp.destType, false);
6642                modifyPassAsTemplate(&newExp.expType, false);
6643
6644                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("f")), '=', newExp))));
6645                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6646                exp.compound.compound.context = context;
6647                PopContext(context);
6648             }
6649             break;
6650          case voidType:
6651             // Generated code already processed...
6652             break;
6653          default:
6654             exp.type = castExp;
6655             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6656             if((exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass) || exp.expType.isPointerType)
6657                exp.cast.exp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), MkExpBrackets(MkListOne(newExp)));
6658             else
6659                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6660             exp.needCast = true;
6661             break;
6662       }
6663    }
6664    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6665    {
6666       Expression newExp { };
6667       Context context;
6668       TypeKind kind = exp.expType.kind;
6669       *newExp = *exp;
6670       if(exp.destType) exp.destType.refCount++;
6671       if(exp.expType)  exp.expType.refCount++;
6672       newExp.prev = null;
6673       newExp.next = null;
6674
6675       if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered)
6676       {
6677          Class c = exp.expType._class.registered;
6678          if(c.type == bitClass || c.type == enumClass || c.type == unitClass)
6679          {
6680             if(!c.dataType)
6681                c.dataType = ProcessTypeString(c.dataTypeString, false);
6682             kind = c.dataType.kind;
6683          }
6684       }
6685
6686       switch(kind)
6687       {
6688          case doubleType:
6689             if(exp.destType.classObjectType)
6690             {
6691                // We need to pass the address, just pass it along (Undo what was done above)
6692                if(exp.destType) exp.destType.refCount--;
6693                if(exp.expType)  exp.expType.refCount--;
6694                delete newExp;
6695             }
6696             else
6697             {
6698                // If we're looking for value:
6699                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6700                OldList * specs;
6701                OldList * unionDefs = MkList();
6702                OldList * statements = MkList();
6703                context = PushContext();
6704                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6705                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6706                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6707                exp.type = extensionCompoundExp;
6708                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6709
6710                modifyPassAsTemplate(&exp.expType, false);
6711                modifyPassAsTemplate(&newExp.destType, true);
6712                modifyPassAsTemplate(&newExp.expType, true);
6713
6714                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6715                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6716                exp.compound.compound.context = context;
6717                PopContext(context);
6718             }
6719             break;
6720          case floatType:
6721             if(exp.destType.classObjectType)
6722             {
6723                // We need to pass the address, just pass it along (Undo what was done above)
6724                if(exp.destType) exp.destType.refCount--;
6725                if(exp.expType)  exp.expType.refCount--;
6726                delete newExp;
6727             }
6728             else
6729             {
6730                // If we're looking for value:
6731                // ({ union { float f; uint64 i; } u; u.i = [newExp]; u.f; })
6732                OldList * specs;
6733                OldList * unionDefs = MkList();
6734                OldList * statements = MkList();
6735                context = PushContext();
6736                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(FLOAT)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("f"))), null)));
6737                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6738                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6739                exp.type = extensionCompoundExp;
6740                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6741
6742                modifyPassAsTemplate(&exp.expType, false);
6743                modifyPassAsTemplate(&newExp.destType, true);
6744                modifyPassAsTemplate(&newExp.expType, true);
6745
6746                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6747                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("f")))));
6748                exp.compound.compound.context = context;
6749                PopContext(context);
6750             }
6751             break;
6752          case classType:
6753          {
6754             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6755             {
6756                exp.type = bracketsExp;
6757
6758                newExp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), newExp);
6759                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6760                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6761                ProcessExpressionType(exp.list->first);
6762                break;
6763             }
6764             else
6765             {
6766                exp.type = bracketsExp;
6767                if(exp.expType.isPointerType)
6768                {
6769                   exp.needTemplateCast = 2;
6770                   newExp.needCast = true;
6771                   newExp.needTemplateCast = 2;
6772                   newExp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), newExp);
6773                }
6774
6775                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6776                exp.needTemplateCast = 2;
6777                newExp.needCast = true;
6778                newExp.needTemplateCast = 2;
6779                ProcessExpressionType(exp.list->first);
6780                break;
6781             }
6782          }
6783          default:
6784          {
6785             if(exp.expType.kind == templateType)
6786             {
6787                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6788                if(type)
6789                {
6790                   FreeType(exp.destType);
6791                   FreeType(exp.expType);
6792                   delete newExp;
6793                   break;
6794                }
6795             }
6796             /*if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6797             {
6798                // When was this required?    Removed to address using templated values to pass to printf()
6799                exp.type = opExp;
6800                exp.op.op = '*';
6801                exp.op.exp1 = null;
6802                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6803                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6804             }
6805             else*/
6806             {
6807                char typeString[1024];
6808                Declarator decl;
6809                OldList * specs = MkList();
6810                typeString[0] = '\0';
6811                PrintType(exp.expType, typeString, false, false);
6812                decl = SpecDeclFromString(typeString, specs, null);
6813
6814                exp.type = castExp;
6815                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6816                exp.cast.typeName = MkTypeName(specs, decl);
6817                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6818                exp.cast.exp.needCast = true;
6819                exp.needTemplateCast = 2;
6820                newExp.needTemplateCast = 2;
6821             }
6822             break;
6823          }
6824       }
6825    }
6826 }
6827 // TODO: The Symbol tree should be reorganized by namespaces
6828 // Name Space:
6829 //    - Tree of all symbols within (stored without namespace)
6830 //    - Tree of sub-namespaces
6831
6832 static Symbol ScanWithNameSpace(BinaryTree tree, const char * nameSpace, const char * name)
6833 {
6834    int nsLen = strlen(nameSpace);
6835    Symbol symbol;
6836    // Start at the name space prefix
6837    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6838    {
6839       char * s = symbol.string;
6840       if(!strncmp(s, nameSpace, nsLen))
6841       {
6842          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6843          int c;
6844          char * namePart;
6845          for(c = strlen(s)-1; c >= 0; c--)
6846             if(s[c] == ':')
6847                break;
6848
6849          namePart = s+c+1;
6850          if(!strcmp(namePart, name))
6851          {
6852             // TODO: Error on ambiguity
6853             return symbol;
6854          }
6855       }
6856       else
6857          break;
6858    }
6859    return null;
6860 }
6861
6862 static Symbol FindWithNameSpace(BinaryTree tree, const char * name)
6863 {
6864    int c;
6865    char nameSpace[1024];
6866    const char * namePart;
6867    bool gotColon = false;
6868
6869    nameSpace[0] = '\0';
6870    for(c = strlen(name)-1; c >= 0; c--)
6871       if(name[c] == ':')
6872       {
6873          gotColon = true;
6874          break;
6875       }
6876
6877    namePart = name+c+1;
6878    while(c >= 0 && name[c] == ':') c--;
6879    if(c >= 0)
6880    {
6881       // Try an exact match first
6882       Symbol symbol = (Symbol)tree.FindString(name);
6883       if(symbol)
6884          return symbol;
6885
6886       // Namespace specified
6887       memcpy(nameSpace, name, c + 1);
6888       nameSpace[c+1] = 0;
6889
6890       return ScanWithNameSpace(tree, nameSpace, namePart);
6891    }
6892    else if(gotColon)
6893    {
6894       // Looking for a global symbol, e.g. ::Sleep()
6895       Symbol symbol = (Symbol)tree.FindString(namePart);
6896       return symbol;
6897    }
6898    else
6899    {
6900       // Name only (no namespace specified)
6901       Symbol symbol = (Symbol)tree.FindString(namePart);
6902       if(symbol)
6903          return symbol;
6904       return ScanWithNameSpace(tree, "", namePart);
6905    }
6906    return null;
6907 }
6908
6909 /*static */Symbol FindSymbol(const char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6910 {
6911 #ifdef _DEBUG
6912    //Time startTime = GetTime();
6913 #endif
6914    // Optimize this later? Do this before/less?
6915    Context ctx;
6916    Symbol symbol = null;
6917
6918    // First, check if the identifier is declared inside the function
6919    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6920
6921    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6922    {
6923       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6924       {
6925          symbol = null;
6926          if(thisNameSpace)
6927          {
6928             char curName[1024];
6929             strcpy(curName, thisNameSpace);
6930             strcat(curName, "::");
6931             strcat(curName, name);
6932             // Try to resolve in current namespace first
6933             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6934          }
6935          if(!symbol)
6936             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6937       }
6938       else
6939          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6940
6941       if(symbol || ctx == endContext) break;
6942    }
6943    if(inCompiler && symbol && ctx == globalContext && symbol.pointerExternal && curExternal && symbol.pointerExternal != curExternal)
6944       curExternal.CreateUniqueEdge(symbol.pointerExternal, symbol.pointerExternal.type == functionExternal);
6945 #ifdef _DEBUG
6946    //findSymbolTotalTime += GetTime() - startTime;
6947 #endif
6948    return symbol;
6949 }
6950
6951 static void GetTypeSpecs(Type type, OldList * specs)
6952 {
6953    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6954    switch(type.kind)
6955    {
6956       case classType:
6957       {
6958          if(type._class.registered)
6959          {
6960             if(!type._class.registered.dataType)
6961                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6962             GetTypeSpecs(type._class.registered.dataType, specs);
6963          }
6964          break;
6965       }
6966       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6967       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6968       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6969       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6970       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6971       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6972       case int128Type: ListAdd(specs, MkSpecifier(INT128)); break;
6973       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6974       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6975       case intType:
6976       default:
6977          ListAdd(specs, MkSpecifier(INT)); break;
6978    }
6979 }
6980
6981 static void PrintArraySize(Type arrayType, char * string)
6982 {
6983    char size[256];
6984    size[0] = '\0';
6985    strcat(size, "[");
6986    if(arrayType.enumClass)
6987       strcat(size, arrayType.enumClass.string);
6988    else if(arrayType.arraySizeExp)
6989       PrintExpression(arrayType.arraySizeExp, size);
6990    strcat(size, "]");
6991    strcat(string, size);
6992 }
6993
6994 // WARNING : This function expects a null terminated string since it recursively concatenate...
6995 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6996 {
6997    if(type)
6998    {
6999       if(printConst && type.constant)
7000          strcat(string, "const ");
7001       switch(type.kind)
7002       {
7003          case classType:
7004          {
7005             Symbol c = type._class;
7006             bool isObjectBaseClass = !c || !c.string || !strcmp(c.string, "class");
7007             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
7008             //       look into merging with thisclass ?
7009             if(type.classObjectType == typedObject && isObjectBaseClass)
7010                strcat(string, "typed_object");
7011             else if(type.classObjectType == anyObject && isObjectBaseClass)
7012                strcat(string, "any_object");
7013             else
7014             {
7015                if(c && c.string)
7016                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
7017             }
7018             if(type.byReference)
7019                strcat(string, " &");
7020             break;
7021          }
7022          case voidType: strcat(string, "void"); break;
7023          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
7024          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
7025          case int128Type:  strcat(string, type.isSigned ? "__int128" : "unsigned __int128"); break;
7026          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
7027          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
7028          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
7029          case _BoolType: strcat(string, "_Bool"); break;
7030          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
7031          case floatType: strcat(string, "float"); break;
7032          case doubleType: strcat(string, "double"); break;
7033          case structType:
7034             if(type.enumName)
7035             {
7036                strcat(string, "struct ");
7037                strcat(string, type.enumName);
7038             }
7039             else if(type.typeName)
7040                strcat(string, type.typeName);
7041             else
7042             {
7043                Type member;
7044                strcat(string, "struct { ");
7045                for(member = type.members.first; member; member = member.next)
7046                {
7047                   PrintType(member, string, true, fullName);
7048                   strcat(string,"; ");
7049                }
7050                strcat(string,"}");
7051             }
7052             break;
7053          case unionType:
7054             if(type.enumName)
7055             {
7056                strcat(string, "union ");
7057                strcat(string, type.enumName);
7058             }
7059             else if(type.typeName)
7060                strcat(string, type.typeName);
7061             else
7062             {
7063                strcat(string, "union ");
7064                strcat(string,"(unnamed)");
7065             }
7066             break;
7067          case enumType:
7068             if(type.enumName)
7069             {
7070                strcat(string, "enum ");
7071                strcat(string, type.enumName);
7072             }
7073             else if(type.typeName)
7074                strcat(string, type.typeName);
7075             else
7076                strcat(string, "int"); // "enum");
7077             break;
7078          case ellipsisType:
7079             strcat(string, "...");
7080             break;
7081          case subClassType:
7082             strcat(string, "subclass(");
7083             strcat(string, type._class ? type._class.string : "int");
7084             strcat(string, ")");
7085             break;
7086          case templateType:
7087             strcat(string, type.templateParameter.identifier.string);
7088             break;
7089          case thisClassType:
7090             strcat(string, "thisclass");
7091             break;
7092          case vaListType:
7093             strcat(string, "__builtin_va_list");
7094             break;
7095       }
7096    }
7097 }
7098
7099 static void PrintName(Type type, char * string, bool fullName)
7100 {
7101    if(type.name && type.name[0])
7102    {
7103       if(fullName)
7104          strcat(string, type.name);
7105       else
7106       {
7107          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
7108          if(name) name += 2; else name = type.name;
7109          strcat(string, name);
7110       }
7111    }
7112 }
7113
7114 static void PrintAttribs(Type type, char * string)
7115 {
7116    if(type)
7117    {
7118       if(type.dllExport)   strcat(string, "dllexport ");
7119       if(type.attrStdcall) strcat(string, "stdcall ");
7120    }
7121 }
7122
7123 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
7124 {
7125    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7126    {
7127       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
7128          PrintAttribs(type, string);
7129       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
7130          strcat(string, " const");
7131       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
7132       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7133          strcat(string, " (");
7134       if(type.kind == pointerType)
7135       {
7136          if(type.type.kind == functionType || type.type.kind == methodType)
7137             PrintAttribs(type.type, string);
7138       }
7139       if(type.kind == pointerType)
7140       {
7141          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
7142             strcat(string, "*");
7143          else
7144             strcat(string, " *");
7145       }
7146       if(printConst && type.constant && type.kind == pointerType)
7147          strcat(string, " const");
7148    }
7149    else
7150       PrintTypeSpecs(type, string, fullName, printConst);
7151 }
7152
7153 static void PostPrintType(Type type, char * string, bool fullName)
7154 {
7155    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7156       strcat(string, ")");
7157    if(type.kind == arrayType)
7158       PrintArraySize(type, string);
7159    else if(type.kind == functionType)
7160    {
7161       Type param;
7162       strcat(string, "(");
7163       for(param = type.params.first; param; param = param.next)
7164       {
7165          PrintType(param, string, true, fullName);
7166          if(param.next) strcat(string, ", ");
7167       }
7168       strcat(string, ")");
7169    }
7170    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7171       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7172 }
7173
7174 // *****
7175 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7176 // *****
7177 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7178 {
7179    PrePrintType(type, string, fullName, null, printConst);
7180
7181    if(type.thisClass || (printName && type.name && type.name[0]))
7182       strcat(string, " ");
7183    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7184    {
7185       Symbol _class = type.thisClass;
7186       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7187       {
7188          if(type.classObjectType == classPointer)
7189             strcat(string, "class");
7190          else
7191             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7192       }
7193       else if(_class && _class.string)
7194       {
7195          String s = _class.string;
7196          if(fullName)
7197             strcat(string, s);
7198          else
7199          {
7200             char * name = RSearchString(s, "::", strlen(s), true, false);
7201             if(name) name += 2; else name = s;
7202             strcat(string, name);
7203          }
7204       }
7205       strcat(string, "::");
7206    }
7207
7208    if(printName && type.name)
7209       PrintName(type, string, fullName);
7210    PostPrintType(type, string, fullName);
7211    if(type.bitFieldCount)
7212    {
7213       char count[100];
7214       sprintf(count, ":%d", type.bitFieldCount);
7215       strcat(string, count);
7216    }
7217 }
7218
7219 void PrintType(Type type, char * string, bool printName, bool fullName)
7220 {
7221    _PrintType(type, string, printName, fullName, true);
7222 }
7223
7224 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7225 {
7226    _PrintType(type, string, printName, fullName, false);
7227 }
7228
7229 static Type FindMember(Type type, char * string)
7230 {
7231    Type memberType;
7232    for(memberType = type.members.first; memberType; memberType = memberType.next)
7233    {
7234       if(!memberType.name)
7235       {
7236          Type subType = FindMember(memberType, string);
7237          if(subType)
7238             return subType;
7239       }
7240       else if(!strcmp(memberType.name, string))
7241          return memberType;
7242    }
7243    return null;
7244 }
7245
7246 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7247 {
7248    Type memberType;
7249    for(memberType = type.members.first; memberType; memberType = memberType.next)
7250    {
7251       if(!memberType.name)
7252       {
7253          Type subType = FindMember(memberType, string);
7254          if(subType)
7255          {
7256             *offset += memberType.offset;
7257             return subType;
7258          }
7259       }
7260       else if(!strcmp(memberType.name, string))
7261       {
7262          *offset += memberType.offset;
7263          return memberType;
7264       }
7265    }
7266    return null;
7267 }
7268
7269 public bool GetParseError() { return parseError; }
7270
7271 Expression ParseExpressionString(char * expression)
7272 {
7273    parseError = false;
7274
7275    fileInput = TempFile { };
7276    fileInput.Write(expression, 1, strlen(expression));
7277    fileInput.Seek(0, start);
7278
7279    echoOn = false;
7280    parsedExpression = null;
7281    resetScanner();
7282    expression_yyparse();
7283    delete fileInput;
7284
7285    return parsedExpression;
7286 }
7287
7288 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7289 {
7290    Identifier id = exp.identifier;
7291    Method method = null;
7292    Property prop = null;
7293    DataMember member = null;
7294    ClassProperty classProp = null;
7295
7296    if(_class && _class.type == enumClass)
7297    {
7298       NamedLink64 value = null;
7299       Class enumClass = eSystem_FindClass(privateModule, "enum");
7300       if(enumClass)
7301       {
7302          Class baseClass;
7303          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7304          {
7305             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7306             for(value = e.values.first; value; value = value.next)
7307             {
7308                if(!strcmp(value.name, id.string))
7309                   break;
7310             }
7311             if(value)
7312             {
7313                exp.isConstant = true;
7314                if(inCompiler || inPreCompiler || inDebugger)
7315                {
7316                   char constant[256];
7317                   FreeExpContents(exp);
7318
7319                   exp.type = constantExp;
7320                   if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "char") || !strcmp(baseClass.dataTypeString, "short"))
7321                      sprintf(constant, FORMAT64D, value.data);
7322                   else
7323                      sprintf(constant, FORMAT64HEX, value.data);
7324                   exp.constant = CopyString(constant);
7325                }
7326                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7327                exp.expType = MkClassType(baseClass.fullName);
7328                break;
7329             }
7330          }
7331       }
7332       if(value)
7333          return true;
7334    }
7335    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7336    {
7337       ProcessMethodType(method);
7338       exp.expType = Type
7339       {
7340          refCount = 1;
7341          kind = methodType;
7342          method = method;
7343          // Crash here?
7344          // TOCHECK: Put it back to what it was...
7345          // methodClass = _class;
7346          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7347       };
7348       //id._class = null;
7349       return true;
7350    }
7351    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7352    {
7353       if(!prop.dataType)
7354          ProcessPropertyType(prop);
7355       exp.expType = prop.dataType;
7356       if(prop.dataType) prop.dataType.refCount++;
7357       return true;
7358    }
7359    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7360    {
7361       if(!member.dataType)
7362          member.dataType = ProcessTypeString(member.dataTypeString, false);
7363       exp.expType = member.dataType;
7364       if(member.dataType) member.dataType.refCount++;
7365       return true;
7366    }
7367    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7368    {
7369       if(!classProp.dataType)
7370          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7371
7372       if(classProp.constant)
7373       {
7374          FreeExpContents(exp);
7375
7376          exp.isConstant = true;
7377          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7378          {
7379             //char constant[256];
7380             exp.type = stringExp;
7381             exp.constant = QMkString((char *)(uintptr)classProp.Get(_class));
7382          }
7383          else
7384          {
7385             char constant[256];
7386             exp.type = constantExp;
7387             sprintf(constant, "%d", (int)classProp.Get(_class));
7388             exp.constant = CopyString(constant);
7389          }
7390       }
7391       else
7392       {
7393          // TO IMPLEMENT...
7394       }
7395
7396       exp.expType = classProp.dataType;
7397       if(classProp.dataType) classProp.dataType.refCount++;
7398       return true;
7399    }
7400    return false;
7401 }
7402
7403 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7404 {
7405    BinaryTree * tree = &nameSpace.functions;
7406    GlobalData data = (GlobalData)tree->FindString(name);
7407    NameSpace * child;
7408    if(!data)
7409    {
7410       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7411       {
7412          data = ScanGlobalData(child, name);
7413          if(data)
7414             break;
7415       }
7416    }
7417    return data;
7418 }
7419
7420 static GlobalData FindGlobalData(char * name)
7421 {
7422    int start = 0, c;
7423    NameSpace * nameSpace;
7424    nameSpace = globalData;
7425    for(c = 0; name[c]; c++)
7426    {
7427       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7428       {
7429          NameSpace * newSpace;
7430          char * spaceName = new char[c - start + 1];
7431          strncpy(spaceName, name + start, c - start);
7432          spaceName[c-start] = '\0';
7433          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7434          delete spaceName;
7435          if(!newSpace)
7436             return null;
7437          nameSpace = newSpace;
7438          if(name[c] == ':') c++;
7439          start = c+1;
7440       }
7441    }
7442    if(nameSpace && c - start)
7443    {
7444       return ScanGlobalData(nameSpace, name + start);
7445    }
7446    return null;
7447 }
7448
7449 static int definedExpStackPos;
7450 static void * definedExpStack[512];
7451
7452 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7453 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7454 {
7455    Expression prev = checkedExp.prev, next = checkedExp.next;
7456
7457    FreeExpContents(checkedExp);
7458    FreeType(checkedExp.expType);
7459    FreeType(checkedExp.destType);
7460
7461    *checkedExp = *newExp;
7462
7463    delete newExp;
7464
7465    checkedExp.prev = prev;
7466    checkedExp.next = next;
7467 }
7468
7469 void ApplyAnyObjectLogic(Expression e)
7470 {
7471    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7472 #ifdef _DEBUG
7473    char debugExpString[4096];
7474    debugExpString[0] = '\0';
7475    PrintExpression(e, debugExpString);
7476 #endif
7477
7478    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7479    {
7480       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7481       //ellipsisDestType = destType;
7482       if(e && e.expType)
7483       {
7484          Type type = e.expType;
7485          Class _class = null;
7486          //Type destType = e.destType;
7487
7488          if(type.kind == classType && type._class && type._class.registered)
7489          {
7490             _class = type._class.registered;
7491          }
7492          else if(type.kind == subClassType)
7493          {
7494             _class = FindClass("ecere::com::Class").registered;
7495          }
7496          else
7497          {
7498             char string[1024] = "";
7499             Symbol classSym;
7500
7501             PrintTypeNoConst(type, string, false, true);
7502             classSym = FindClass(string);
7503             if(classSym) _class = classSym.registered;
7504          }
7505
7506          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...
7507             (!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))) ||
7508             destType.byReference)))
7509          {
7510             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7511             {
7512                Expression checkedExp = e, newExp;
7513
7514                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7515                {
7516                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7517                   {
7518                      if(checkedExp.type == extensionCompoundExp)
7519                      {
7520                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7521                      }
7522                      else
7523                         checkedExp = checkedExp.list->last;
7524                   }
7525                   else if(checkedExp.type == castExp)
7526                      checkedExp = checkedExp.cast.exp;
7527                }
7528
7529                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7530                {
7531                   newExp = checkedExp.op.exp2;
7532                   checkedExp.op.exp2 = null;
7533                   FreeExpContents(checkedExp);
7534
7535                   if(e.expType && e.expType.passAsTemplate)
7536                   {
7537                      char size[100];
7538                      ComputeTypeSize(e.expType);
7539                      sprintf(size, "%d", e.expType.size);   // Potential 32/64 Bootstrap issue
7540                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7541                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7542                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7543                   }
7544
7545                   ReplaceExpContents(checkedExp, newExp);
7546                   e.byReference = true;
7547                }
7548                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7549                {
7550                   Expression checkedExp; //, newExp;
7551
7552                   {
7553                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7554                      bool hasAddress =
7555                         e.type == identifierExp ||
7556                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7557                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7558                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7559                         e.type == indexExp;
7560
7561                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7562                      {
7563                         Context context = PushContext();
7564                         Declarator decl;
7565                         OldList * specs = MkList();
7566                         char typeString[1024];
7567                         Expression newExp { };
7568
7569                         typeString[0] = '\0';
7570                         *newExp = *e;
7571
7572                         //if(e.destType) e.destType.refCount++;
7573                         // if(exp.expType) exp.expType.refCount++;
7574                         newExp.prev = null;
7575                         newExp.next = null;
7576                         newExp.expType = null;
7577
7578                         PrintTypeNoConst(e.expType, typeString, false, true);
7579                         decl = SpecDeclFromString(typeString, specs, null);
7580                         newExp.destType = ProcessType(specs, decl);
7581
7582                         curContext = context;
7583
7584                         // We need a current compound for this
7585                         if(curCompound)
7586                         {
7587                            char name[100];
7588                            OldList * stmts = MkList();
7589                            e.type = extensionCompoundExp;
7590                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7591                            if(!curCompound.compound.declarations)
7592                               curCompound.compound.declarations = MkList();
7593                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7594                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7595                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7596                            e.compound = MkCompoundStmt(null, stmts);
7597                         }
7598                         else
7599                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7600
7601                         /*
7602                         e.compound = MkCompoundStmt(
7603                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7604                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7605
7606                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7607                         */
7608
7609                         {
7610                            Type type = e.destType;
7611                            e.destType = { };
7612                            CopyTypeInto(e.destType, type);
7613                            e.destType.refCount = 1;
7614                            e.destType.classObjectType = none;
7615                            FreeType(type);
7616                         }
7617
7618                         e.compound.compound.context = context;
7619                         PopContext(context);
7620                         curContext = context.parent;
7621                      }
7622                   }
7623
7624                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7625                   checkedExp = e;
7626                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7627                   {
7628                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7629                      {
7630                         if(checkedExp.type == extensionCompoundExp)
7631                         {
7632                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7633                         }
7634                         else
7635                            checkedExp = checkedExp.list->last;
7636                      }
7637                      else if(checkedExp.type == castExp)
7638                         checkedExp = checkedExp.cast.exp;
7639                   }
7640                   {
7641                      Expression operand { };
7642                      operand = *checkedExp;
7643                      checkedExp.Clear();
7644                      checkedExp.destType = ProcessTypeString("void *", false);
7645                      checkedExp.expType = checkedExp.destType;
7646                      checkedExp.destType.refCount++;
7647
7648                      checkedExp.type = opExp;
7649                      checkedExp.op.op = '&';
7650                      checkedExp.op.exp1 = null;
7651                      checkedExp.op.exp2 = operand;
7652
7653                      //newExp = MkExpOp(null, '&', checkedExp);
7654                   }
7655                   //ReplaceExpContents(checkedExp, newExp);
7656                }
7657             }
7658          }
7659       }
7660    }
7661    {
7662       // If expression type is a simple class, make it an address
7663       // FixReference(e, true);
7664    }
7665 //#if 0
7666    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7667       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7668          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7669    {
7670       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"))
7671       {
7672          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7673       }
7674       else
7675       {
7676          Expression thisExp { };
7677
7678          *thisExp = *e;
7679          thisExp.prev = null;
7680          thisExp.next = null;
7681          e.Clear();
7682
7683          e.type = bracketsExp;
7684          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7685          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7686             ((Expression)e.list->first).byReference = true;
7687
7688          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7689          {
7690             e.expType = thisExp.expType;
7691             e.expType.refCount++;
7692          }
7693          else*/
7694          {
7695             e.expType = { };
7696             CopyTypeInto(e.expType, thisExp.expType);
7697             e.expType.byReference = false;
7698             e.expType.refCount = 1;
7699
7700             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7701                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7702             {
7703                e.expType.classObjectType = none;
7704             }
7705          }
7706       }
7707    }
7708 // TOFIX: Try this for a nice IDE crash!
7709 //#endif
7710    // The other way around
7711    else
7712 //#endif
7713    if(destType && e.expType &&
7714          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7715          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7716          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7717    {
7718       if(destType.kind == ellipsisType)
7719       {
7720          Compiler_Error($"Unspecified type\n");
7721       }
7722       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7723       {
7724          bool byReference = e.expType.byReference;
7725          Expression thisExp { };
7726          Declarator decl;
7727          OldList * specs = MkList();
7728          char typeString[1024]; // Watch buffer overruns
7729          Type type;
7730          ClassObjectType backupClassObjectType;
7731          bool backupByReference;
7732
7733          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7734             type = e.expType;
7735          else
7736             type = destType;
7737
7738          backupClassObjectType = type.classObjectType;
7739          backupByReference = type.byReference;
7740
7741          type.classObjectType = none;
7742          type.byReference = false;
7743
7744          typeString[0] = '\0';
7745          PrintType(type, typeString, false, true);
7746          decl = SpecDeclFromString(typeString, specs, null);
7747
7748          type.classObjectType = backupClassObjectType;
7749          type.byReference = backupByReference;
7750
7751          *thisExp = *e;
7752          thisExp.prev = null;
7753          thisExp.next = null;
7754          e.Clear();
7755
7756          if( ( type.kind == classType && type._class && type._class.registered &&
7757                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7758                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7759              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7760              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7761          {
7762             bool passAsTemplate = thisExp.destType.passAsTemplate;
7763             Type t;
7764
7765             destType.refCount++;
7766
7767             e.type = opExp;
7768             e.op.op = '*';
7769             e.op.exp1 = null;
7770             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7771
7772             t = { };
7773             CopyTypeInto(t, thisExp.destType);
7774             t.passAsTemplate = false;
7775             FreeType(thisExp.destType);
7776             thisExp.destType = t;
7777
7778             t = { };
7779             CopyTypeInto(t, destType);
7780             t.passAsTemplate = passAsTemplate;
7781             FreeType(destType);
7782             destType = t;
7783             destType.refCount = 0;
7784
7785             e.expType = { };
7786             CopyTypeInto(e.expType, type);
7787             if(type.passAsTemplate)
7788             {
7789                e.expType.classObjectType = none;
7790                e.expType.passAsTemplate = false;
7791             }
7792             e.expType.byReference = false;
7793             e.expType.refCount = 1;
7794          }
7795          else
7796          {
7797             e.type = castExp;
7798             e.cast.typeName = MkTypeName(specs, decl);
7799             e.cast.exp = thisExp;
7800             e.byReference = true;
7801             e.expType = type;
7802             type.refCount++;
7803          }
7804
7805          if(e.destType)
7806             FreeType(e.destType);
7807
7808          e.destType = destType;
7809          destType.refCount++;
7810       }
7811    }
7812 }
7813
7814 void ApplyLocation(Expression exp, Location loc)
7815 {
7816    exp.loc = loc;
7817    switch(exp.type)
7818    {
7819       case opExp:
7820          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7821          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7822          break;
7823       case bracketsExp:
7824          if(exp.list)
7825          {
7826             Expression e;
7827             for(e = exp.list->first; e; e = e.next)
7828                ApplyLocation(e, loc);
7829          }
7830          break;
7831       case indexExp:
7832          if(exp.index.index)
7833          {
7834             Expression e;
7835             for(e = exp.index.index->first; e; e = e.next)
7836                ApplyLocation(e, loc);
7837          }
7838          if(exp.index.exp)
7839             ApplyLocation(exp.index.exp, loc);
7840          break;
7841       case callExp:
7842          if(exp.call.arguments)
7843          {
7844             Expression arg;
7845             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7846                ApplyLocation(arg, loc);
7847          }
7848          if(exp.call.exp)
7849             ApplyLocation(exp.call.exp, loc);
7850          break;
7851       case memberExp:
7852       case pointerExp:
7853          if(exp.member.exp)
7854             ApplyLocation(exp.member.exp, loc);
7855          break;
7856       case castExp:
7857          if(exp.cast.exp)
7858             ApplyLocation(exp.cast.exp, loc);
7859          break;
7860       case conditionExp:
7861          if(exp.cond.exp)
7862          {
7863             Expression e;
7864             for(e = exp.cond.exp->first; e; e = e.next)
7865                ApplyLocation(e, loc);
7866          }
7867          if(exp.cond.cond)
7868             ApplyLocation(exp.cond.cond, loc);
7869          if(exp.cond.elseExp)
7870             ApplyLocation(exp.cond.elseExp, loc);
7871          break;
7872       case vaArgExp:
7873          if(exp.vaArg.exp)
7874             ApplyLocation(exp.vaArg.exp, loc);
7875          break;
7876       default:
7877          break;
7878    }
7879 }
7880
7881 bool RelatedUnits(Class c1, Class c2)
7882 {
7883    if(c1.base.type == unitClass) c1 = c1.base;
7884    if(c2.base.type == unitClass) c2 = c2.base;
7885    return c1 == c2;
7886 }
7887
7888 void ProcessExpressionType(Expression exp)
7889 {
7890    bool unresolved = false;
7891    Location oldyylloc = yylloc;
7892    bool notByReference = false;
7893 #ifdef _DEBUG
7894    char debugExpString[4096];
7895    debugExpString[0] = '\0';
7896    PrintExpression(exp, debugExpString);
7897 #endif
7898    if(!exp || exp.expType)
7899       return;
7900
7901    //eSystem_Logf("%s\n", expString);
7902
7903    // Testing this here
7904    yylloc = exp.loc;
7905    switch(exp.type)
7906    {
7907       case identifierExp:
7908       {
7909          Identifier id = exp.identifier;
7910          if(!id || !topContext) return;
7911
7912          // DOING THIS LATER NOW...
7913          if(id._class && id._class.name)
7914          {
7915             id.classSym = id._class.symbol; // FindClass(id._class.name);
7916             /* TODO: Name Space Fix ups
7917             if(!id.classSym)
7918                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7919             */
7920          }
7921
7922          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7923          {
7924             exp.expType = ProcessTypeString("Module", true);
7925             break;
7926          }
7927          else */
7928          if(!strcmp(id.string, "__runtimePlatform"))
7929          {
7930             exp.expType = ProcessTypeString("ecere::com::Platform", true);
7931             break;
7932          }
7933          else if(strstr(id.string, "__ecereClass") == id.string)
7934          {
7935             exp.expType = ProcessTypeString("ecere::com::Class", true);
7936             break;
7937          }
7938          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7939          {
7940             // Added this here as well
7941             ReplaceClassMembers(exp, thisClass);
7942             if(exp.type != identifierExp)
7943             {
7944                ProcessExpressionType(exp);
7945                break;
7946             }
7947
7948             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7949                break;
7950          }
7951          else
7952          {
7953             Symbol symbol = null;
7954             bool findInGlobal = false;
7955             if(!topContext.parent && exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == enumClass)
7956                findInGlobal = true;  // In global context, look at enum values first
7957             else
7958                symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7959
7960             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7961             if(!symbol/* && exp.destType*/)
7962             {
7963                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
7964                   break;
7965                else
7966                {
7967                   if(thisClass && strcmp(id.string, "this"))
7968                   {
7969                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7970                      if(exp.type != identifierExp)
7971                      {
7972                         ProcessExpressionType(exp);
7973                         break;
7974                      }
7975                   }
7976                   // Static methods called from inside the _class
7977                   else if(currentClass && !id._class)
7978                   {
7979                      if(ResolveIdWithClass(exp, currentClass, true))
7980                         break;
7981                   }
7982                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7983                }
7984             }
7985             if(findInGlobal)
7986                symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7987
7988             // If we manage to resolve this symbol
7989             if(symbol)
7990             {
7991                Type type = symbol.type;
7992                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7993
7994                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7995                {
7996                   Context context = SetupTemplatesContext(_class);
7997                   type = ReplaceThisClassType(_class);
7998                   FinishTemplatesContext(context);
7999                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
8000                }
8001
8002                FreeSpecifier(id._class);
8003                id._class = null;
8004                delete id.string;
8005                id.string = CopyString(symbol.string);
8006
8007                id.classSym = null;
8008                exp.expType = type;
8009                if(type)
8010                   type.refCount++;
8011
8012                                                 // Commented this out, it was making non-constant enum parameters seen as constant
8013                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
8014                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
8015                   // Add missing cases here... enum Classes...
8016                   exp.isConstant = true;
8017
8018                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
8019                if(symbol.isParam || !strcmp(id.string, "this"))
8020                {
8021                   if(_class && _class.type == structClass && !type.declaredWithStruct)
8022                      exp.byReference = true;
8023
8024                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
8025                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
8026                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
8027                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
8028                   {
8029                      Identifier id = exp.identifier;
8030                      exp.type = bracketsExp;
8031                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
8032                   }*/
8033                }
8034
8035                if(symbol.isIterator)
8036                {
8037                   if(symbol.isIterator == 3)
8038                   {
8039                      exp.type = bracketsExp;
8040                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
8041                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
8042                      exp.expType = null;
8043                      ProcessExpressionType(exp);
8044                   }
8045                   else if(symbol.isIterator != 4)
8046                   {
8047                      exp.type = memberExp;
8048                      exp.member.exp = MkExpIdentifier(exp.identifier);
8049                      exp.member.exp.expType = exp.expType;
8050                      /*if(symbol.isIterator == 6)
8051                         exp.member.member = MkIdentifier("key");
8052                      else*/
8053                         exp.member.member = MkIdentifier("data");
8054                      exp.expType = null;
8055                      ProcessExpressionType(exp);
8056                   }
8057                }
8058                break;
8059             }
8060             else
8061             {
8062                DefinedExpression definedExp = null;
8063                if(thisNameSpace && !(id._class && !id._class.name))
8064                {
8065                   char name[1024];
8066                   strcpy(name, thisNameSpace);
8067                   strcat(name, "::");
8068                   strcat(name, id.string);
8069                   definedExp = eSystem_FindDefine(privateModule, name);
8070                }
8071                if(!definedExp)
8072                   definedExp = eSystem_FindDefine(privateModule, id.string);
8073                if(definedExp)
8074                {
8075                   int c;
8076                   for(c = 0; c<definedExpStackPos; c++)
8077                      if(definedExpStack[c] == definedExp)
8078                         break;
8079                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
8080                   {
8081                      Location backupYylloc = yylloc;
8082                      File backInput = fileInput;
8083                      definedExpStack[definedExpStackPos++] = definedExp;
8084
8085                      fileInput = TempFile { };
8086                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
8087                      fileInput.Seek(0, start);
8088
8089                      echoOn = false;
8090                      parsedExpression = null;
8091                      resetScanner();
8092                      expression_yyparse();
8093                      delete fileInput;
8094                      if(backInput)
8095                         fileInput = backInput;
8096
8097                      yylloc = backupYylloc;
8098
8099                      if(parsedExpression)
8100                      {
8101                         FreeIdentifier(id);
8102                         exp.type = bracketsExp;
8103                         exp.list = MkListOne(parsedExpression);
8104                         ApplyLocation(parsedExpression, yylloc);
8105                         ProcessExpressionType(exp);
8106                         definedExpStackPos--;
8107                         return;
8108                      }
8109                      definedExpStackPos--;
8110                   }
8111                   else
8112                   {
8113                      if(inCompiler)
8114                      {
8115                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
8116                      }
8117                   }
8118                }
8119                else
8120                {
8121                   GlobalData data = null;
8122                   if(thisNameSpace && !(id._class && !id._class.name))
8123                   {
8124                      char name[1024];
8125                      strcpy(name, thisNameSpace);
8126                      strcat(name, "::");
8127                      strcat(name, id.string);
8128                      data = FindGlobalData(name);
8129                   }
8130                   if(!data)
8131                      data = FindGlobalData(id.string);
8132                   if(data)
8133                   {
8134                      DeclareGlobalData(curExternal, data);
8135                      exp.expType = data.dataType;
8136                      if(data.dataType) data.dataType.refCount++;
8137
8138                      delete id.string;
8139                      id.string = CopyString(data.fullName);
8140                      FreeSpecifier(id._class);
8141                      id._class = null;
8142
8143                      break;
8144                   }
8145                   else
8146                   {
8147                      GlobalFunction function = null;
8148                      if(thisNameSpace && !(id._class && !id._class.name))
8149                      {
8150                         char name[1024];
8151                         strcpy(name, thisNameSpace);
8152                         strcat(name, "::");
8153                         strcat(name, id.string);
8154                         function = eSystem_FindFunction(privateModule, name);
8155                      }
8156                      if(!function)
8157                         function = eSystem_FindFunction(privateModule, id.string);
8158                      if(function)
8159                      {
8160                         char name[1024];
8161                         delete id.string;
8162                         id.string = CopyString(function.name);
8163                         name[0] = 0;
8164
8165                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
8166                            strcpy(name, "__ecereFunction_");
8167                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
8168                         if(DeclareFunction(curExternal, function, name))
8169                         {
8170                            delete id.string;
8171                            id.string = CopyString(name);
8172                         }
8173                         exp.expType = function.dataType;
8174                         if(function.dataType) function.dataType.refCount++;
8175
8176                         FreeSpecifier(id._class);
8177                         id._class = null;
8178
8179                         break;
8180                      }
8181                   }
8182                }
8183             }
8184          }
8185          unresolved = true;
8186          break;
8187       }
8188       case instanceExp:
8189       {
8190          // Class _class;
8191          // Symbol classSym;
8192
8193          if(!exp.instance._class)
8194          {
8195             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8196             {
8197                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8198             }
8199          }
8200
8201          //classSym = FindClass(exp.instance._class.fullName);
8202          //_class = classSym ? classSym.registered : null;
8203
8204          ProcessInstantiationType(exp.instance);
8205
8206          exp.isConstant = exp.instance.isConstant;
8207
8208          /*
8209          if(_class.type == unitClass && _class.base.type != systemClass)
8210          {
8211             {
8212                Type destType = exp.destType;
8213
8214                exp.destType = MkClassType(_class.base.fullName);
8215                exp.expType = MkClassType(_class.fullName);
8216                CheckExpressionType(exp, exp.destType, true);
8217
8218                exp.destType = destType;
8219             }
8220             exp.expType = MkClassType(_class.fullName);
8221          }
8222          else*/
8223          if(exp.instance._class)
8224          {
8225             exp.expType = MkClassType(exp.instance._class.name);
8226             /*if(exp.expType._class && exp.expType._class.registered &&
8227                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8228                exp.expType.byReference = true;*/
8229          }
8230          break;
8231       }
8232       case constantExp:
8233       {
8234          if(!exp.expType)
8235          {
8236             char * constant = exp.constant;
8237             Type type
8238             {
8239                refCount = 1;
8240                constant = true;
8241             };
8242             exp.expType = type;
8243
8244             if(constant[0] == '\'')
8245             {
8246                if((int)((byte *)constant)[1] > 127)
8247                {
8248                   int nb;
8249                   unichar ch = UTF8GetChar(constant + 1, &nb);
8250                   if(nb < 2) ch = constant[1];
8251                   delete constant;
8252                   exp.constant = PrintUInt(ch);
8253                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8254                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8255                   type._class = FindClass("unichar");
8256
8257                   type.isSigned = false;
8258                }
8259                else
8260                {
8261                   type.kind = charType;
8262                   type.isSigned = true;
8263                }
8264             }
8265             else
8266             {
8267                char * dot = strchr(constant, '.');
8268                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8269                char * exponent;
8270                if(isHex)
8271                {
8272                   exponent = strchr(constant, 'p');
8273                   if(!exponent) exponent = strchr(constant, 'P');
8274                }
8275                else
8276                {
8277                   exponent = strchr(constant, 'e');
8278                   if(!exponent) exponent = strchr(constant, 'E');
8279                }
8280
8281                if(dot || exponent)
8282                {
8283                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8284                      type.kind = floatType;
8285                   else
8286                      type.kind = doubleType;
8287                   type.isSigned = true;
8288                }
8289                else
8290                {
8291                   bool isSigned = constant[0] == '-';
8292                   char * endP = null;
8293                   int64 i64 = strtoll(constant, &endP, 0);
8294                   uint64 ui64 = strtoull(constant, &endP, 0);
8295                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll") || !strcmp(endP, "LLU") || !strcmp(endP, "llu") || !strcmp(endP, "ull") || !strcmp(endP, "ULL"));
8296                   bool forceUnsigned = endP && (!strcmp(endP, "U") || !strcmp(endP, "u") || !strcmp(endP, "LLU") || !strcmp(endP, "llu") || !strcmp(endP, "ull") || !strcmp(endP, "ULL"));
8297                   if(isSigned)
8298                   {
8299                      if(i64 < MININT)
8300                         is64Bit = true;
8301                   }
8302                   else
8303                   {
8304                      if(ui64 > MAXINT)
8305                      {
8306                         if(ui64 > MAXDWORD)
8307                         {
8308                            is64Bit = true;
8309                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8310                               isSigned = true;
8311                         }
8312                      }
8313                      else if(constant[0] != '0' || !constant[1])
8314                         isSigned = true;
8315                   }
8316                   if(forceUnsigned)
8317                      isSigned = false;
8318                   type.kind = is64Bit ? int64Type : intType;
8319                   type.isSigned = isSigned;
8320                }
8321             }
8322             exp.isConstant = true;
8323             if(exp.destType && exp.destType.kind == doubleType)
8324                type.kind = doubleType;
8325             else if(exp.destType && exp.destType.kind == floatType)
8326                type.kind = floatType;
8327             else if(exp.destType && exp.destType.kind == int64Type)
8328                type.kind = int64Type;
8329          }
8330          break;
8331       }
8332       case stringExp:
8333       {
8334          exp.isConstant = true;      // Why wasn't this constant?
8335          exp.expType = Type
8336          {
8337             refCount = 1;
8338             kind = pointerType;
8339             type = Type
8340             {
8341                refCount = 1;
8342                kind = exp.wideString ? shortType : charType;
8343                constant = true;
8344                isSigned = exp.wideString ? false : true;
8345             }
8346          };
8347          break;
8348       }
8349       case newExp:
8350       case new0Exp:
8351          ProcessExpressionType(exp._new.size);
8352          exp.expType = Type
8353          {
8354             refCount = 1;
8355             kind = pointerType;
8356             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8357          };
8358          DeclareType(curExternal, exp.expType.type, true, false);
8359          break;
8360       case renewExp:
8361       case renew0Exp:
8362          ProcessExpressionType(exp._renew.size);
8363          ProcessExpressionType(exp._renew.exp);
8364          exp.expType = Type
8365          {
8366             refCount = 1;
8367             kind = pointerType;
8368             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8369          };
8370          DeclareType(curExternal, exp.expType.type, true, false);
8371          break;
8372       case opExp:
8373       {
8374          bool assign = false, boolResult = false, boolOps = false;
8375          Type type1 = null, type2 = null;
8376          bool useDestType = false, useSideType = false;
8377          Location oldyylloc = yylloc;
8378          bool useSideUnit = false;
8379          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8380          bool powerOp = false, relationOp = false;
8381          Class c1 = null, c2 = null;
8382
8383          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8384          Type dummy
8385          {
8386             count = 1;
8387             refCount = 1;
8388          };
8389
8390          switch(exp.op.op)
8391          {
8392             // Assignment Operators
8393             case '=':
8394             case MUL_ASSIGN:
8395             case DIV_ASSIGN:
8396             case MOD_ASSIGN:
8397             case ADD_ASSIGN:
8398             case SUB_ASSIGN:
8399             case LEFT_ASSIGN:
8400             case RIGHT_ASSIGN:
8401             case AND_ASSIGN:
8402             case XOR_ASSIGN:
8403             case OR_ASSIGN:
8404                assign = true;
8405                break;
8406             // boolean Operators
8407             case '!':
8408                // Expect boolean operators
8409                //boolOps = true;
8410                //boolResult = true;
8411                break;
8412             case AND_OP:
8413             case OR_OP:
8414                // Expect boolean operands
8415                boolOps = true;
8416                boolResult = true;
8417                break;
8418             // Comparisons
8419             case EQ_OP:
8420             case '<':
8421             case '>':
8422             case LE_OP:
8423             case GE_OP:
8424             case NE_OP:
8425                // Gives boolean result
8426                boolResult = true;
8427                useSideType = true;
8428                relationOp = true;
8429                break;
8430             case '+':
8431             case '-':
8432                useSideUnit = true;
8433                useSideType = true;
8434                useDestType = true;
8435                break;
8436
8437             case LEFT_OP:
8438             case RIGHT_OP:
8439                // useSideType = true;
8440                // useDestType = true;
8441                break;
8442
8443             case '|':
8444             case '^':
8445                useSideType = true;
8446                useDestType = true;
8447                break;
8448
8449             case '/':
8450             case '%':
8451                useSideType = true;
8452                useDestType = true;
8453                if(exp.op.op == '/') powerOp = true;
8454                break;
8455             case '&':
8456             case '*':
8457                if(exp.op.exp1)
8458                {
8459                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8460                   useSideType = true;
8461                   useDestType = true;
8462                   if(exp.op.op == '*') powerOp = true;
8463                }
8464                break;
8465
8466             /*// Implement speed etc.
8467             case '*':
8468             case '/':
8469                break;
8470             */
8471          }
8472          if(exp.op.op == '&')
8473          {
8474             // Added this here earlier for Iterator address as key
8475             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8476             {
8477                Identifier id = exp.op.exp2.identifier;
8478                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8479                if(symbol && symbol.isIterator == 2)
8480                {
8481                   exp.type = memberExp;
8482                   exp.member.exp = exp.op.exp2;
8483                   exp.member.member = MkIdentifier("key");
8484                   exp.expType = null;
8485                   exp.op.exp2.expType = symbol.type;
8486                   symbol.type.refCount++;
8487                   ProcessExpressionType(exp);
8488                   FreeType(dummy);
8489                   break;
8490                }
8491                // exp.op.exp2.usage.usageRef = true;
8492             }
8493          }
8494
8495          //dummy.kind = TypeDummy;
8496          if(exp.op.exp1)
8497          {
8498             // Added this check here to use the dest type only for units derived from the base unit
8499             // So that untyped units will use the side unit as opposed to the untyped destination unit
8500             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8501             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8502                useDestType = false;
8503
8504             if(destClass && useDestType &&
8505               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8506
8507               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8508             {
8509                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8510                exp.op.exp1.destType = exp.destType;
8511                exp.op.exp1.opDestType = true;
8512                if(exp.destType)
8513                   exp.destType.refCount++;
8514             }
8515             else if(!assign)
8516             {
8517                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8518                exp.op.exp1.destType = dummy;
8519                dummy.refCount++;
8520                if(powerOp)
8521                   exp.op.exp1.opDestType = true;
8522                if(relationOp)
8523                   exp.op.exp1.usedInComparison = true;
8524             }
8525             if(exp.op.op == '+' || exp.op.op == '-')
8526             {
8527                if(exp.opDestType)
8528                   exp.op.exp1.parentOpDestType = true;
8529                if(exp.usedInComparison)
8530                   exp.op.exp1.usedInComparison = true;
8531             }
8532
8533             // TESTING THIS HERE...
8534             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8535                ProcessExpressionType(exp.op.exp1);
8536             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8537
8538             exp.op.exp1.opDestType = false;
8539             exp.op.exp1.usedInComparison = false;
8540
8541             // Fix for unit and ++ / --
8542             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8543                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8544             {
8545                exp.op.exp2 = MkExpConstant("1");
8546                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8547                assign = true;
8548             }
8549
8550             if(exp.op.exp1.destType == dummy)
8551             {
8552                FreeType(dummy);
8553                exp.op.exp1.destType = null;
8554             }
8555
8556             if(exp.op.exp2)
8557             {
8558                if(!assign && exp.op.exp1.expType && (exp.op.exp1.expType.kind == charType || exp.op.exp1.expType.kind == shortType))
8559                {
8560                   Type type { kind = intType, isSigned = true, refCount = 1, signedBeforePromotion = exp.op.exp1.expType.isSigned, bitMemberSize = exp.op.exp1.expType.bitMemberSize, promotedFrom = exp.op.exp1.expType.kind };
8561                   FreeType(exp.op.exp1.expType);
8562                   exp.op.exp1.expType = type;
8563                }
8564             }
8565
8566             type1 = exp.op.exp1.expType;
8567          }
8568
8569          if(exp.op.exp2)
8570          {
8571             char expString[10240];
8572             expString[0] = '\0';
8573             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8574             {
8575                if(exp.op.exp1)
8576                {
8577                   exp.op.exp2.destType = exp.op.exp1.expType;
8578                   if(exp.op.exp1.expType)
8579                      exp.op.exp1.expType.refCount++;
8580                }
8581                else
8582                {
8583                   exp.op.exp2.destType = exp.destType;
8584                   if(!exp.op.exp1 || (exp.op.op != '&' && exp.op.op != '^'))
8585                      exp.op.exp2.opDestType = true;
8586                   if(exp.destType)
8587                      exp.destType.refCount++;
8588                }
8589
8590                if(type1) type1.refCount++;
8591                exp.expType = type1;
8592             }
8593             else if(assign)
8594             {
8595                if(inCompiler)
8596                   PrintExpression(exp.op.exp2, expString);
8597
8598                if(type1 && type1.kind == pointerType)
8599                {
8600                   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 ||
8601                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8602                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8603                   else if(exp.op.op == '=')
8604                   {
8605                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8606                      exp.op.exp2.destType = type1;
8607                      if(type1)
8608                         type1.refCount++;
8609                   }
8610                }
8611                else
8612                {
8613                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8614                   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/* ||
8615                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8616                   else
8617                   {
8618                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8619                      exp.op.exp2.destType = type1;
8620                      if(type1)
8621                         type1.refCount++;
8622                   }
8623                }
8624                if(type1) type1.refCount++;
8625                exp.expType = type1;
8626             }
8627             else if(destClass &&
8628                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8629                   (destClass.type == enumClass && useDestType)))
8630             {
8631                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8632                exp.op.exp2.destType = exp.destType;
8633                if(exp.op.op != '&' && exp.op.op != '^')
8634                   exp.op.exp2.opDestType = true;
8635                if(exp.destType)
8636                   exp.destType.refCount++;
8637             }
8638             else
8639             {
8640                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8641                exp.op.exp2.destType = dummy;
8642                dummy.refCount++;
8643                if(powerOp)
8644                   exp.op.exp2.opDestType = true;
8645                if(relationOp)
8646                   exp.op.exp2.usedInComparison = true;
8647             }
8648
8649             // TESTING THIS HERE... (DANGEROUS)
8650             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8651                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8652             {
8653                FreeType(exp.op.exp2.destType);
8654                exp.op.exp2.destType = type1;
8655                type1.refCount++;
8656             }
8657             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8658             // Cannot lose the cast on a sizeof
8659             if(exp.op.op == SIZEOF)
8660             {
8661                Expression e = exp.op.exp2;
8662                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8663                {
8664                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8665                   {
8666                      if(e.type == extensionCompoundExp)
8667                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8668                      else
8669                         e = e.list->last;
8670                   }
8671                }
8672                if(e.type == castExp && e.cast.exp)
8673                   e.cast.exp.needCast = true;
8674             }
8675             if(exp.op.op == '+' || exp.op.op == '-')
8676             {
8677                if(exp.opDestType)
8678                   exp.op.exp2.parentOpDestType = true;
8679                if(exp.usedInComparison)
8680                   exp.op.exp2.usedInComparison = true;
8681             }
8682             ProcessExpressionType(exp.op.exp2);
8683             exp.op.exp2.opDestType = false;
8684             exp.op.exp2.usedInComparison = false;
8685             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8686
8687             if(!assign && (exp.op.exp1 || exp.op.op == '~'))
8688             {
8689                if(exp.op.exp2.expType && (exp.op.exp2.expType.kind == charType || exp.op.exp2.expType.kind == shortType))
8690                {
8691                   Type type { kind = intType, isSigned = true, refCount = 1, signedBeforePromotion = exp.op.exp2.expType.isSigned, bitMemberSize = exp.op.exp2.expType.bitMemberSize, promotedFrom = exp.op.exp2.expType.kind };
8692                   FreeType(exp.op.exp2.expType);
8693                   exp.op.exp2.expType = type;
8694                }
8695             }
8696
8697             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8698             {
8699                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)
8700                {
8701                   if(exp.op.op != '=' && type1.type.kind == voidType)
8702                      Compiler_Error($"void *: unknown size\n");
8703                }
8704                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||
8705                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8706                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8707                               exp.op.exp2.expType._class.registered.type == structClass ||
8708                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8709                {
8710                   if(exp.op.op == ADD_ASSIGN)
8711                      Compiler_Error($"cannot add two pointers\n");
8712                }
8713                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8714                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8715                {
8716                   if(exp.op.op == ADD_ASSIGN)
8717                      Compiler_Error($"cannot add two pointers\n");
8718                }
8719                else if(inCompiler)
8720                {
8721                   char type1String[1024];
8722                   char type2String[1024];
8723                   type1String[0] = '\0';
8724                   type2String[0] = '\0';
8725
8726                   PrintType(exp.op.exp2.expType, type1String, false, true);
8727                   PrintType(type1, type2String, false, true);
8728                   ChangeCh(expString, '\n', ' ');
8729                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8730                }
8731             }
8732
8733             if(exp.op.exp2.destType == dummy)
8734             {
8735                FreeType(dummy);
8736                exp.op.exp2.destType = null;
8737             }
8738
8739             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8740             {
8741                type2 = { };
8742                type2.refCount = 1;
8743                CopyTypeInto(type2, exp.op.exp2.expType);
8744                type2.isSigned = true;
8745             }
8746             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8747             {
8748                type2 = { kind = intType };
8749                type2.refCount = 1;
8750                type2.isSigned = true;
8751             }
8752             else
8753             {
8754                type2 = exp.op.exp2.expType;
8755                if(type2) type2.refCount++;
8756             }
8757          }
8758          c1 = type1 && type1.kind == classType && type1._class ? type1._class.registered : null;
8759          c2 = type2 && type2.kind == classType && type2._class ? type2._class.registered : null;
8760
8761          if(relationOp &&
8762             ( (exp.op.exp1 && exp.op.exp1.ambiguousUnits && (!c2 || c2.type != unitClass)) ||
8763                (exp.op.exp2 && exp.op.exp2.ambiguousUnits && (!c1 || c1.type != unitClass))) )
8764             Compiler_Warning($"ambiguous units in relational operation\n");
8765
8766          if(!relationOp &&
8767             ((exp.op.exp1 && exp.op.exp1.ambiguousUnits) ||
8768              (exp.op.exp2 && exp.op.exp2.ambiguousUnits)) &&
8769              (!powerOp || !c1 || c1.type != unitClass || !c2 || c2.type != unitClass || !RelatedUnits(c1, c2)))
8770          {
8771             if(exp.opDestType || exp.usedInComparison)
8772                exp.ambiguousUnits = true;
8773             else
8774                Compiler_Warning($"ambiguous units\n");
8775          }
8776
8777          dummy.kind = voidType;
8778
8779          if(exp.op.op == SIZEOF)
8780          {
8781             exp.expType = Type
8782             {
8783                refCount = 1;
8784                kind = intSizeType;
8785             };
8786             exp.isConstant = true;
8787          }
8788          // Get type of dereferenced pointer
8789          else if(exp.op.op == '*' && !exp.op.exp1)
8790          {
8791             exp.expType = Dereference(type2);
8792             if(type2 && type2.kind == classType)
8793                notByReference = true;
8794          }
8795          else if(exp.op.op == '&' && !exp.op.exp1)
8796             exp.expType = Reference(type2);
8797          else if(exp.op.op == LEFT_OP || exp.op.op == RIGHT_OP)
8798          {
8799             if(exp.op.exp1.expType)
8800             {
8801                exp.expType = exp.op.exp1.expType;
8802                exp.expType.refCount++;
8803             }
8804          }
8805          else if(!assign)
8806          {
8807             if(c1 && !c1.dataType)
8808                c1.dataType = ProcessTypeString(c1.dataTypeString, false);
8809             if(c2 && !c2.dataType)
8810                c2.dataType = ProcessTypeString(c2.dataTypeString, false);
8811
8812             if(boolOps)
8813             {
8814                if(exp.op.exp1)
8815                {
8816                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8817                   exp.op.exp1.destType = MkClassType("bool");
8818                   exp.op.exp1.destType.truth = true;
8819                   if(!exp.op.exp1.expType)
8820                      ProcessExpressionType(exp.op.exp1);
8821                   else
8822                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8823                   FreeType(exp.op.exp1.expType);
8824                   exp.op.exp1.expType = MkClassType("bool");
8825                   exp.op.exp1.expType.truth = true;
8826                }
8827                if(exp.op.exp2)
8828                {
8829                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8830                   exp.op.exp2.destType = MkClassType("bool");
8831                   exp.op.exp2.destType.truth = true;
8832                   if(!exp.op.exp2.expType)
8833                      ProcessExpressionType(exp.op.exp2);
8834                   else
8835                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8836                   FreeType(exp.op.exp2.expType);
8837                   exp.op.exp2.expType = MkClassType("bool");
8838                   exp.op.exp2.expType.truth = true;
8839                }
8840             }
8841             else if(powerOp && exp.op.exp1 && exp.op.exp2 && ((c1 && c1.type == unitClass) || (c2 && c2.type == unitClass)))
8842             {
8843                // * or / with at least one unit operand
8844                if(c1 && c1.type == unitClass && c2 && c2.type == unitClass)
8845                {
8846                   // This is where we'd handle unit powers e.g. square meters or meters/seconds
8847                   // For now using base types
8848                   if(c1.dataType.kind == doubleType)        exp.expType = c1.dataType;
8849                   else if(c2.dataType.kind == doubleType)   exp.expType = c2.dataType;
8850                   else if(c1.dataType.kind == floatType)    exp.expType = c1.dataType;
8851                   else if(c2.dataType.kind == floatType)    exp.expType = c2.dataType;
8852                   else
8853                      exp.expType = c1.dataType;
8854                }
8855                else if((c1 && c1.type == unitClass) || exp.op.op == '/')   // 1/units should not be typed with unit
8856                   exp.expType = type1;
8857                else
8858                   exp.expType = type2;
8859
8860                if(exp.expType)
8861                   exp.expType.refCount++;
8862             }
8863             else if(exp.op.exp1 && exp.op.exp2 &&
8864                ((useSideType /*&&
8865                      (useSideUnit ||
8866                         ((!c1 || c1.type != unitClass) &&
8867                          (!c2 || c2.type != unitClass)))*/) ||
8868                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8869                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8870             {
8871                if(type1 && type2 &&
8872                   // If either both are class or both are not class
8873                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8874                {
8875                   // Added this check for enum subtraction to result in an int type:
8876                   if(exp.op.op == '-' && ((c1 && c1.type == enumClass) || (c2 && c2.type == enumClass)) )
8877                   {
8878                      Type intType = ProcessTypeString((c1 && c1.dataType.kind == int64Type) || (c2 && c2.dataType.kind == int64Type) ? "int64" : "int", false);
8879
8880                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8881                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8882                      exp.op.exp1.destType = intType;
8883                      exp.op.exp2.destType = intType;
8884                      intType.refCount++;
8885                   }
8886                   else
8887                   {
8888                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8889                      exp.op.exp2.destType = type1;
8890                      type1.refCount++;
8891                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8892                      exp.op.exp1.destType = type2;
8893                      type2.refCount++;
8894                   }
8895
8896                   // Warning here for adding Radians + Degrees with no destination type
8897                   if(!boolResult && !exp.opDestType && (!exp.destType || exp.destType.kind != classType) &&
8898                      c1 && c1.type == unitClass &&
8899                      c2 && c2.type == unitClass &&
8900                      c1 != c2)
8901                   {
8902                      if(exp.usedInComparison || exp.parentOpDestType)
8903                         exp.ambiguousUnits = true;
8904                      else
8905                         Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8906                            type1._class.string, type2._class.string, type1._class.string);
8907                   }
8908
8909                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8910                   {
8911                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, type1.type.thisClassFrom, thisClass, true);
8912                      if(argExp)
8913                      {
8914                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8915
8916                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8917                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8918                            exp.op.exp1)));
8919
8920                         ProcessExpressionType(exp.op.exp1);
8921
8922                         if(type2.kind != pointerType)
8923                         {
8924                            ProcessExpressionType(classExp);
8925
8926                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8927
8928                            if(!exp.op.exp2.expType)
8929                            {
8930                               if(type2)
8931                                  FreeType(type2);
8932                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false); c2 = null;
8933                               type2.refCount++;
8934                            }
8935
8936                            ProcessExpressionType(exp.op.exp2);
8937                         }
8938                      }
8939                   }
8940
8941                   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)))
8942                   {
8943                      if(type1.kind != classType && type1.type.kind == voidType)
8944                         Compiler_Error($"void *: unknown size\n");
8945                      exp.expType = type1;
8946                      if(type1) type1.refCount++;
8947                   }
8948                   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)))
8949                   {
8950                      if(type2.kind != classType && type2.type.kind == voidType)
8951                         Compiler_Error($"void *: unknown size\n");
8952                      exp.expType = type2;
8953                      if(type2) type2.refCount++;
8954                   }
8955                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8956                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8957                   {
8958                      Compiler_Warning($"different levels of indirection\n");
8959                   }
8960                   else
8961                   {
8962                      bool success = false;
8963                      if(type1.kind == pointerType && type2.kind == pointerType)
8964                      {
8965                         if(exp.op.op == '+')
8966                            Compiler_Error($"cannot add two pointers\n");
8967                         else if(exp.op.op == '-')
8968                         {
8969                            // Pointer Subtraction gives integer
8970                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8971                            {
8972                               exp.expType = Type
8973                               {
8974                                  kind = intType;
8975                                  refCount = 1;
8976                               };
8977                               success = true;
8978
8979                               if(type1.type.kind == templateType)
8980                               {
8981                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, type1.type.thisClassFrom, thisClass, true);
8982                                  if(argExp)
8983                                  {
8984                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8985
8986                                     ProcessExpressionType(classExp);
8987
8988                                     exp.type = bracketsExp;
8989                                     exp.list = MkListOne(MkExpOp(
8990                                        MkExpBrackets(MkListOne(MkExpOp(
8991                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8992                                              , exp.op.op,
8993                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8994                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8995
8996                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8997                                     FreeType(dummy);
8998                                     return;
8999                                  }
9000                               }
9001                            }
9002                         }
9003                      }
9004
9005                      if(!success && exp.op.exp1.type == constantExp)
9006                      {
9007                         // If first expression is constant, try to match that first
9008                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9009                         {
9010                            if(exp.expType) FreeType(exp.expType);
9011                            exp.expType = exp.op.exp1.destType;
9012                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9013                            success = true;
9014                         }
9015                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
9016                         {
9017                            if(exp.expType) FreeType(exp.expType);
9018                            exp.expType = exp.op.exp2.destType;
9019                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
9020                            success = true;
9021                         }
9022                      }
9023                      else if(!success)
9024                      {
9025                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
9026                         {
9027                            if(exp.expType) FreeType(exp.expType);
9028                            exp.expType = exp.op.exp2.destType;
9029                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
9030                            success = true;
9031                         }
9032                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9033                         {
9034                            if(exp.expType) FreeType(exp.expType);
9035                            exp.expType = exp.op.exp1.destType;
9036                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9037                            success = true;
9038                         }
9039                      }
9040                      if(!success)
9041                      {
9042                         char expString1[10240];
9043                         char expString2[10240];
9044                         char type1[1024];
9045                         char type2[1024];
9046                         expString1[0] = '\0';
9047                         expString2[0] = '\0';
9048                         type1[0] = '\0';
9049                         type2[0] = '\0';
9050                         if(inCompiler)
9051                         {
9052                            PrintExpression(exp.op.exp1, expString1);
9053                            ChangeCh(expString1, '\n', ' ');
9054                            PrintExpression(exp.op.exp2, expString2);
9055                            ChangeCh(expString2, '\n', ' ');
9056                            PrintType(exp.op.exp1.expType, type1, false, true);
9057                            PrintType(exp.op.exp2.expType, type2, false, true);
9058                         }
9059
9060                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
9061                      }
9062                   }
9063                }
9064                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
9065                else if(!boolResult && !useSideUnit && c2 && c2.type == unitClass && type1 && type1.kind != classType)
9066                {
9067                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9068                   // Convert e.g. / 4 into / 4.0
9069                   exp.op.exp1.destType = type2._class.registered.dataType;
9070                   if(type2._class.registered.dataType)
9071                      type2._class.registered.dataType.refCount++;
9072                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9073                   exp.expType = type2;
9074                   if(type2) type2.refCount++;
9075                }
9076                else if(!boolResult && !useSideUnit && c1 && c1.type == unitClass && type2 && type2.kind != classType)
9077                {
9078                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9079                   // Convert e.g. / 4 into / 4.0
9080                   exp.op.exp2.destType = type1._class.registered.dataType;
9081                   if(type1._class.registered.dataType)
9082                      type1._class.registered.dataType.refCount++;
9083                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9084                   exp.expType = type1;
9085                   if(type1) type1.refCount++;
9086                }
9087                else if(type1)
9088                {
9089                   bool valid = false;
9090
9091                   if(!boolResult && useSideUnit && c1 && c1.type == unitClass && type2 && type2.kind != classType)
9092                   {
9093                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9094
9095                      exp.op.exp2.destType = c1.dataType;
9096                      exp.op.exp2.destType.refCount++;
9097
9098                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9099                      if(type2)
9100                         FreeType(type2);
9101                      type2 = exp.op.exp2.destType;
9102                      c2 = type2 && type2.kind == classType && type2._class ? type2._class.registered : null;
9103                      if(type2) type2.refCount++;
9104
9105                      exp.expType = type2;
9106                      type2.refCount++;
9107                   }
9108
9109                   if(!boolResult && useSideUnit && c2 && c2.type == unitClass && type1 && type1.kind != classType)
9110                   {
9111                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9112
9113                      exp.op.exp1.destType = c2.dataType;
9114                      exp.op.exp1.destType.refCount++;
9115
9116                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9117                      type1 = exp.op.exp1.destType;
9118                      c1 = type1 && type1.kind == classType && type1._class ? type1._class.registered : null;
9119                      exp.expType = type1;
9120                      type1.refCount++;
9121                   }
9122
9123                   // TESTING THIS NEW CODE
9124                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
9125                   {
9126                      bool op1IsEnum = c1 && c1.type == enumClass;
9127                      bool op2IsEnum = c2 && c2.type == enumClass;
9128                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
9129                      {
9130                         // Convert the enum to an int instead for these operators
9131                         if(op1IsEnum && exp.op.exp2.expType)
9132                         {
9133                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
9134                            {
9135                               if(exp.expType) FreeType(exp.expType);
9136                               exp.expType = exp.op.exp2.expType;
9137                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9138                               valid = true;
9139                            }
9140                         }
9141                         else if(op2IsEnum && exp.op.exp1.expType)
9142                         {
9143                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
9144                            {
9145                               if(exp.expType) FreeType(exp.expType);
9146                               exp.expType = exp.op.exp1.expType;
9147                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9148                               valid = true;
9149                            }
9150                         }
9151                      }
9152                      else
9153                      {
9154                         if(op1IsEnum && exp.op.exp2.expType)
9155                         {
9156                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
9157                            {
9158                               if(exp.expType) FreeType(exp.expType);
9159                               exp.expType = exp.op.exp1.expType;
9160                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9161                               valid = true;
9162                            }
9163                         }
9164                         else if(op2IsEnum && exp.op.exp1.expType)
9165                         {
9166                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
9167                            {
9168                               if(exp.expType) FreeType(exp.expType);
9169                               exp.expType = exp.op.exp2.expType;
9170                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9171                               valid = true;
9172                            }
9173                         }
9174                      }
9175                   }
9176
9177                   if(!valid)
9178                   {
9179                      // 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
9180                      if(c2 && c2.type == unitClass && (!c1 || c1.type != unitClass))
9181                      {
9182                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9183                         exp.op.exp1.destType = type2;
9184                         type2.refCount++;
9185
9186                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9187                         {
9188                            if(exp.expType) FreeType(exp.expType);
9189                            exp.expType = exp.op.exp1.destType;
9190                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9191                         }
9192                      }
9193                      else
9194                      {
9195                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9196                         exp.op.exp2.destType = type1;
9197                         type1.refCount++;
9198
9199                      /*
9200                      // Maybe this was meant to be an enum...
9201                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9202                      {
9203                         Type oldType = exp.op.exp2.expType;
9204                         exp.op.exp2.expType = null;
9205                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
9206                            FreeType(oldType);
9207                         else
9208                            exp.op.exp2.expType = oldType;
9209                      }
9210                      */
9211
9212                      /*
9213                      // TESTING THIS HERE... LATEST ADDITION
9214                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9215                      {
9216                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9217                         exp.op.exp2.destType = type2._class.registered.dataType;
9218                         if(type2._class.registered.dataType)
9219                            type2._class.registered.dataType.refCount++;
9220                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
9221
9222                         //exp.expType = type2._class.registered.dataType; //type2;
9223                         //if(type2) type2.refCount++;
9224                      }
9225
9226                      // TESTING THIS HERE... LATEST ADDITION
9227                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9228                      {
9229                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9230                         exp.op.exp1.destType = type1._class.registered.dataType;
9231                         if(type1._class.registered.dataType)
9232                            type1._class.registered.dataType.refCount++;
9233                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
9234                         exp.expType = type1._class.registered.dataType; //type1;
9235                         if(type1) type1.refCount++;
9236                      }
9237                      */
9238
9239                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
9240                         {
9241                            if(exp.expType) FreeType(exp.expType);
9242                            exp.expType = exp.op.exp2.destType;
9243                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
9244                         }
9245                         else if(type1 && type2)
9246                         {
9247                            char expString1[10240];
9248                            char expString2[10240];
9249                            char type1String[1024];
9250                            char type2String[1024];
9251                            expString1[0] = '\0';
9252                            expString2[0] = '\0';
9253                            type1String[0] = '\0';
9254                            type2String[0] = '\0';
9255                            if(inCompiler)
9256                            {
9257                               PrintExpression(exp.op.exp1, expString1);
9258                               ChangeCh(expString1, '\n', ' ');
9259                               PrintExpression(exp.op.exp2, expString2);
9260                               ChangeCh(expString2, '\n', ' ');
9261                               PrintType(exp.op.exp1.expType, type1String, false, true);
9262                               PrintType(exp.op.exp2.expType, type2String, false, true);
9263                            }
9264
9265                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
9266
9267                            if(c1 && c1.type == enumClass)
9268                            {
9269                               exp.expType = exp.op.exp1.expType;
9270                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9271                            }
9272                            else if(c2 && c2.type == enumClass)
9273                            {
9274                               exp.expType = exp.op.exp2.expType;
9275                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9276                            }
9277                         }
9278                      }
9279                   }
9280                }
9281                else if(type2)
9282                {
9283                   // Maybe this was meant to be an enum...
9284                   if(c2 && c2.type == enumClass)
9285                   {
9286                      Type oldType = exp.op.exp1.expType;
9287                      exp.op.exp1.expType = null;
9288                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9289                         FreeType(oldType);
9290                      else
9291                         exp.op.exp1.expType = oldType;
9292                   }
9293
9294                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9295                   exp.op.exp1.destType = type2;
9296                   type2.refCount++;
9297                   /*
9298                   // TESTING THIS HERE... LATEST ADDITION
9299                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9300                   {
9301                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9302                      exp.op.exp1.destType = type1._class.registered.dataType;
9303                      if(type1._class.registered.dataType)
9304                         type1._class.registered.dataType.refCount++;
9305                   }
9306
9307                   // TESTING THIS HERE... LATEST ADDITION
9308                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9309                   {
9310                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9311                      exp.op.exp2.destType = type2._class.registered.dataType;
9312                      if(type2._class.registered.dataType)
9313                         type2._class.registered.dataType.refCount++;
9314                   }
9315                   */
9316
9317                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9318                   {
9319                      if(exp.expType) FreeType(exp.expType);
9320                      exp.expType = exp.op.exp1.destType;
9321                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9322                   }
9323                }
9324             }
9325             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9326             {
9327                if(type1 && c2 && c2.type == unitClass)
9328                {
9329                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9330                   // Convert e.g. / 4 into / 4.0
9331                   exp.op.exp1.destType = type2._class.registered.dataType;
9332                   if(type2._class.registered.dataType)
9333                      type2._class.registered.dataType.refCount++;
9334                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9335                }
9336                if(exp.op.op == '!')
9337                {
9338                   exp.expType = MkClassType("bool");
9339                   exp.expType.truth = true;
9340                }
9341                else
9342                {
9343                   exp.expType = type2;
9344                   if(type2) type2.refCount++;
9345                }
9346             }
9347             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9348             {
9349                if(c2 && c2.type == unitClass)
9350                {
9351                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9352                   // Convert e.g. / 4 into / 4.0
9353                   exp.op.exp2.destType = type1._class.registered.dataType;
9354                   if(type1._class.registered.dataType)
9355                      type1._class.registered.dataType.refCount++;
9356                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9357                }
9358                exp.expType = type1;
9359                if(type1) type1.refCount++;
9360             }
9361          }
9362
9363          yylloc = exp.loc;
9364          if(exp.op.exp1 && !exp.op.exp1.expType)
9365          {
9366             char expString[10000];
9367             expString[0] = '\0';
9368             if(inCompiler)
9369             {
9370                PrintExpression(exp.op.exp1, expString);
9371                ChangeCh(expString, '\n', ' ');
9372             }
9373             if(expString[0])
9374                Compiler_Error($"couldn't determine type of %s\n", expString);
9375          }
9376          if(exp.op.exp2 && !exp.op.exp2.expType)
9377          {
9378             char expString[10240];
9379             expString[0] = '\0';
9380             if(inCompiler)
9381             {
9382                PrintExpression(exp.op.exp2, expString);
9383                ChangeCh(expString, '\n', ' ');
9384             }
9385             if(expString[0])
9386                Compiler_Error($"couldn't determine type of %s\n", expString);
9387          }
9388
9389          if(boolResult)
9390          {
9391             FreeType(exp.expType);
9392             exp.expType = MkClassType("bool");
9393             exp.expType.truth = true;
9394          }
9395
9396          if(exp.op.op != SIZEOF)
9397             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9398                (!exp.op.exp2 || exp.op.exp2.isConstant);
9399
9400          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9401          {
9402             DeclareType(curExternal, exp.op.exp2.expType, true, false);
9403          }
9404
9405          if(exp.op.op == DELETE && exp.op.exp2 && exp.op.exp2.expType && exp.op.exp2.expType.specConst)
9406             Compiler_Warning($"deleting const qualified object\n");
9407
9408          yylloc = oldyylloc;
9409
9410          FreeType(dummy);
9411          if(type2)
9412             FreeType(type2);
9413          break;
9414       }
9415       case bracketsExp:
9416       case extensionExpressionExp:
9417       {
9418          Expression e;
9419          exp.isConstant = true;
9420          for(e = exp.list->first; e; e = e.next)
9421          {
9422             //bool inced = false;
9423             if(!e.next)
9424             {
9425                FreeType(e.destType);
9426                e.opDestType = exp.opDestType;
9427                e.usedInComparison = exp.usedInComparison;
9428                e.parentOpDestType = exp.parentOpDestType;
9429                e.destType = exp.destType;
9430                if(e.destType) { exp.destType.refCount++; /*e.destType.count++; inced = true;*/ }
9431             }
9432             ProcessExpressionType(e);
9433             if(e.ambiguousUnits)
9434                exp.ambiguousUnits = true;
9435             /*if(inced)
9436                exp.destType.count--;*/
9437             if(!exp.expType && !e.next)
9438             {
9439                exp.expType = e.expType;
9440                if(e.expType) e.expType.refCount++;
9441                exp.needCast = e.needCast;
9442             }
9443             if(!e.isConstant)
9444                exp.isConstant = false;
9445          }
9446
9447          // In case a cast became a member...
9448          e = exp.list->first;
9449          if(!e.next && e.type == memberExp)
9450          {
9451             // Preserve prev, next
9452             Expression next = exp.next, prev = exp.prev;
9453
9454
9455             FreeType(exp.expType);
9456             FreeType(exp.destType);
9457             delete exp.list;
9458
9459             *exp = *e;
9460
9461             exp.prev = prev;
9462             exp.next = next;
9463
9464             delete e;
9465
9466             ProcessExpressionType(exp);
9467          }
9468          break;
9469       }
9470       case indexExp:
9471       {
9472          Expression e;
9473          exp.isConstant = true;
9474
9475          ProcessExpressionType(exp.index.exp);
9476          if(!exp.index.exp.isConstant)
9477             exp.isConstant = false;
9478
9479          if(exp.index.exp.expType)
9480          {
9481             Type source = exp.index.exp.expType;
9482             if(source.kind == classType && source._class && source._class.registered)
9483             {
9484                Class _class = source._class.registered;
9485                Class c = _class.templateClass ? _class.templateClass : _class;
9486                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9487                {
9488                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9489
9490                   if(exp.index.index && exp.index.index->last)
9491                   {
9492                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9493
9494                      if(type.kind == classType) type.constant = true;
9495                      else if(type.kind == pointerType)
9496                      {
9497                         Type t = type;
9498                         while(t.kind == pointerType) t = t.type;
9499                         t.constant = true;
9500                      }
9501
9502                      ((Expression)exp.index.index->last).destType = type;
9503                   }
9504                }
9505             }
9506          }
9507
9508          for(e = exp.index.index->first; e; e = e.next)
9509          {
9510             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9511             {
9512                if(e.destType) FreeType(e.destType);
9513                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9514             }
9515             ProcessExpressionType(e);
9516             if(!e.next)
9517             {
9518                // Check if this type is int
9519             }
9520             if(!e.isConstant)
9521                exp.isConstant = false;
9522          }
9523
9524          if(!exp.expType)
9525             exp.expType = Dereference(exp.index.exp.expType);
9526          if(exp.expType)
9527             DeclareType(curExternal, exp.expType, true, false);
9528          break;
9529       }
9530       case callExp:
9531       {
9532          Expression e;
9533          Type functionType;
9534          Type methodType = null;
9535          char name[1024];
9536          name[0] = '\0';
9537
9538          if(inCompiler)
9539          {
9540             PrintExpression(exp.call.exp,  name);
9541             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9542             {
9543                //exp.call.exp.expType = null;
9544                PrintExpression(exp.call.exp,  name);
9545             }
9546          }
9547          if(exp.call.exp.type == identifierExp)
9548          {
9549             Expression idExp = exp.call.exp;
9550             Identifier id = idExp.identifier;
9551             if(!strcmp(id.string, "__builtin_frame_address"))
9552             {
9553                exp.expType = ProcessTypeString("void *", true);
9554                if(exp.call.arguments && exp.call.arguments->first)
9555                   ProcessExpressionType(exp.call.arguments->first);
9556                break;
9557             }
9558             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9559             {
9560                exp.expType = ProcessTypeString("int", true);
9561                if(exp.call.arguments && exp.call.arguments->first)
9562                   ProcessExpressionType(exp.call.arguments->first);
9563                break;
9564             }
9565             else if(!strcmp(id.string, "Max") ||
9566                !strcmp(id.string, "Min") ||
9567                !strcmp(id.string, "Sgn") ||
9568                !strcmp(id.string, "Abs"))
9569             {
9570                Expression a = null;
9571                Expression b = null;
9572                Expression tempExp1 = null, tempExp2 = null;
9573                if((!strcmp(id.string, "Max") ||
9574                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9575                {
9576                   a = exp.call.arguments->first;
9577                   b = exp.call.arguments->last;
9578                   tempExp1 = a;
9579                   tempExp2 = b;
9580                }
9581                else if(exp.call.arguments->count == 1)
9582                {
9583                   a = exp.call.arguments->first;
9584                   tempExp1 = a;
9585                }
9586
9587                if(a)
9588                {
9589                   exp.call.arguments->Clear();
9590                   idExp.identifier = null;
9591
9592                   FreeExpContents(exp);
9593
9594                   ProcessExpressionType(a);
9595                   if(b)
9596                      ProcessExpressionType(b);
9597
9598                   exp.type = bracketsExp;
9599                   exp.list = MkList();
9600
9601                   if(a.expType && (!b || b.expType))
9602                   {
9603                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9604                      {
9605                         // Use the simpleStruct name/ids for now...
9606                         if(inCompiler)
9607                         {
9608                            OldList * specs = MkList();
9609                            OldList * decls = MkList();
9610                            Declaration decl;
9611                            char temp1[1024], temp2[1024];
9612
9613                            GetTypeSpecs(a.expType, specs);
9614
9615                            if(a && !a.isConstant && a.type != identifierExp)
9616                            {
9617                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9618                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9619                               tempExp1 = QMkExpId(temp1);
9620                               tempExp1.expType = a.expType;
9621                               if(a.expType)
9622                                  a.expType.refCount++;
9623                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9624                            }
9625                            if(b && !b.isConstant && b.type != identifierExp)
9626                            {
9627                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9628                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9629                               tempExp2 = QMkExpId(temp2);
9630                               tempExp2.expType = b.expType;
9631                               if(b.expType)
9632                                  b.expType.refCount++;
9633                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9634                            }
9635
9636                            decl = MkDeclaration(specs, decls);
9637                            if(!curCompound.compound.declarations)
9638                               curCompound.compound.declarations = MkList();
9639                            curCompound.compound.declarations->Insert(null, decl);
9640                         }
9641                      }
9642                   }
9643
9644                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9645                   {
9646                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9647                      ListAdd(exp.list,
9648                         MkExpCondition(MkExpBrackets(MkListOne(
9649                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9650                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9651                      exp.expType = a.expType;
9652                      if(a.expType)
9653                         a.expType.refCount++;
9654                   }
9655                   else if(!strcmp(id.string, "Abs"))
9656                   {
9657                      ListAdd(exp.list,
9658                         MkExpCondition(MkExpBrackets(MkListOne(
9659                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9660                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9661                      exp.expType = a.expType;
9662                      if(a.expType)
9663                         a.expType.refCount++;
9664                   }
9665                   else if(!strcmp(id.string, "Sgn"))
9666                   {
9667                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9668                      ListAdd(exp.list,
9669                         MkExpCondition(MkExpBrackets(MkListOne(
9670                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9671                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9672                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9673                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9674                      exp.expType = ProcessTypeString("int", false);
9675                   }
9676
9677                   FreeExpression(tempExp1);
9678                   if(tempExp2) FreeExpression(tempExp2);
9679
9680                   FreeIdentifier(id);
9681                   break;
9682                }
9683             }
9684          }
9685
9686          {
9687             Type dummy
9688             {
9689                count = 1;
9690                refCount = 1;
9691             };
9692             if(!exp.call.exp.destType)
9693             {
9694                exp.call.exp.destType = dummy;
9695                dummy.refCount++;
9696             }
9697             ProcessExpressionType(exp.call.exp);
9698             if(exp.call.exp.destType == dummy)
9699             {
9700                FreeType(dummy);
9701                exp.call.exp.destType = null;
9702             }
9703             FreeType(dummy);
9704          }
9705
9706          // Check argument types against parameter types
9707          functionType = exp.call.exp.expType;
9708
9709          if(functionType && functionType.kind == TypeKind::methodType)
9710          {
9711             methodType = functionType;
9712             functionType = methodType.method.dataType;
9713
9714             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9715             // TOCHECK: Instead of doing this here could this be done per param?
9716             if(exp.call.exp.expType.usedClass)
9717             {
9718                char typeString[1024];
9719                typeString[0] = '\0';
9720                {
9721                   Symbol back = functionType.thisClass;
9722                   // Do not output class specifier here (thisclass was added to this)
9723                   functionType.thisClass = null;
9724                   PrintType(functionType, typeString, true, true);
9725                   functionType.thisClass = back;
9726                }
9727                if(strstr(typeString, "thisclass"))
9728                {
9729                   OldList * specs = MkList();
9730                   Declarator decl;
9731                   {
9732                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9733
9734                      decl = SpecDeclFromString(typeString, specs, null);
9735
9736                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9737                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9738                         exp.call.exp.expType.usedClass))
9739                         thisClassParams = false;
9740
9741                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9742                      {
9743                         Class backupThisClass = thisClass;
9744                         thisClass = exp.call.exp.expType.usedClass;
9745                         ProcessDeclarator(decl, true);
9746                         thisClass = backupThisClass;
9747                      }
9748
9749                      thisClassParams = true;
9750
9751                      functionType = ProcessType(specs, decl);
9752                      functionType.refCount = 0;
9753                      FinishTemplatesContext(context);
9754
9755                      // Mark parameters that were 'thisclass'
9756                      {
9757                         Type p, op;
9758                         for(p = functionType.params.first, op = methodType.method.dataType.params.first; p && op; p = p.next, op = op.next)
9759                         {
9760                            //p.wasThisClass = op.kind == thisClassType;
9761                            if(op.kind == thisClassType)
9762                               p.thisClassFrom = methodType.method._class;
9763                         }
9764                      }
9765                      if(methodType.method.dataType.returnType.kind == thisClassType)
9766                      {
9767                         // functionType.returnType.wasThisClass = true;
9768                         functionType.returnType.thisClassFrom = methodType.method._class;
9769                      }
9770                   }
9771
9772                   FreeList(specs, FreeSpecifier);
9773                   FreeDeclarator(decl);
9774                 }
9775             }
9776          }
9777          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9778          {
9779             Type type = functionType.type;
9780             if(!functionType.refCount)
9781             {
9782                functionType.type = null;
9783                FreeType(functionType);
9784             }
9785             //methodType = functionType;
9786             functionType = type;
9787          }
9788          if(functionType && functionType.kind != TypeKind::functionType)
9789          {
9790             Compiler_Error($"called object %s is not a function\n", name);
9791          }
9792          else if(functionType)
9793          {
9794             bool emptyParams = false, noParams = false;
9795             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9796             Type type = functionType.params.first;
9797             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9798             int extra = 0;
9799             Location oldyylloc = yylloc;
9800
9801             if(!type) emptyParams = true;
9802
9803             // WORKING ON THIS:
9804             if(functionType.extraParam && e && functionType.thisClass)
9805             {
9806                e.destType = MkClassType(functionType.thisClass.string);
9807                e = e.next;
9808             }
9809
9810             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9811             // Fixed #141 by adding '&& !functionType.extraParam'
9812             if(!functionType.staticMethod && !functionType.extraParam)
9813             {
9814                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9815                   memberExp.member.exp.expType._class)
9816                {
9817                   type = MkClassType(memberExp.member.exp.expType._class.string);
9818                   if(e)
9819                   {
9820                      e.destType = type;
9821                      e = e.next;
9822                      type = functionType.params.first;
9823                   }
9824                   else
9825                      type.refCount = 0;
9826                }
9827                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9828                {
9829                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9830                   type.byReference = functionType.byReference;
9831                   type.typedByReference = functionType.typedByReference;
9832                   if(e)
9833                   {
9834                      // Allow manually passing a class for typed object
9835                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9836                         e = e.next;
9837                      e.destType = type;
9838                      e = e.next;
9839                      type = functionType.params.first;
9840                   }
9841                   else
9842                      type.refCount = 0;
9843                   //extra = 1;
9844                }
9845             }
9846
9847             if(type && type.kind == voidType)
9848             {
9849                noParams = true;
9850                if(!type.refCount) FreeType(type);
9851                type = null;
9852             }
9853
9854             for( ; e; e = e.next)
9855             {
9856                if(!type && !emptyParams)
9857                {
9858                   yylloc = e.loc;
9859                   if(methodType && methodType.methodClass)
9860                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9861                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9862                         noParams ? 0 : functionType.params.count);
9863                   else
9864                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9865                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9866                         noParams ? 0 : functionType.params.count);
9867                   break;
9868                }
9869
9870                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9871                {
9872                   Type templatedType = null;
9873                   Class _class = methodType.usedClass;
9874                   ClassTemplateParameter curParam = null;
9875                   int id = 0;
9876                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9877                   {
9878                      Class sClass;
9879                      for(sClass = _class; sClass; sClass = sClass.base)
9880                      {
9881                         if(sClass.templateClass) sClass = sClass.templateClass;
9882                         id = 0;
9883                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9884                         {
9885                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9886                            {
9887                               Class nextClass;
9888                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9889                               {
9890                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9891                                  id += nextClass.templateParams.count;
9892                               }
9893                               break;
9894                            }
9895                            id++;
9896                         }
9897                         if(curParam) break;
9898                      }
9899                   }
9900                   if(curParam && _class.templateArgs[id].dataTypeString)
9901                   {
9902                      bool constant = type.constant;
9903                      ClassTemplateArgument arg = _class.templateArgs[id];
9904                      {
9905                         Context context = SetupTemplatesContext(_class);
9906
9907                         /*if(!arg.dataType)
9908                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9909                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9910                         FinishTemplatesContext(context);
9911                      }
9912
9913                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9914                      else if(templatedType.kind == pointerType)
9915                      {
9916                         Type t = templatedType.type;
9917                         while(t.kind == pointerType) t = t.type;
9918                         if(constant) t.constant = constant;
9919                      }
9920
9921                      e.destType = templatedType;
9922                      if(templatedType)
9923                      {
9924                         templatedType.passAsTemplate = true;
9925                         // templatedType.refCount++;
9926                      }
9927                   }
9928                   else
9929                   {
9930                      e.destType = type;
9931                      if(type) type.refCount++;
9932                   }
9933                }
9934                else
9935                {
9936                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9937                   {
9938                      e.destType = type.prev;
9939                      e.destType.refCount++;
9940                   }
9941                   else
9942                   {
9943                      e.destType = type;
9944                      if(type) type.refCount++;
9945                   }
9946                }
9947                // Don't reach the end for the ellipsis
9948                if(type && type.kind != ellipsisType)
9949                {
9950                   Type next = type.next;
9951                   if(!type.refCount) FreeType(type);
9952                   type = next;
9953                }
9954             }
9955
9956             if(type && type.kind != ellipsisType)
9957             {
9958                if(methodType && methodType.methodClass)
9959                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9960                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9961                      functionType.params.count + extra);
9962                else
9963                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9964                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9965                      functionType.params.count + extra);
9966             }
9967             yylloc = oldyylloc;
9968             if(type && !type.refCount) FreeType(type);
9969          }
9970          else
9971          {
9972             functionType = Type
9973             {
9974                refCount = 0;
9975                kind = TypeKind::functionType;
9976             };
9977
9978             if(exp.call.exp.type == identifierExp)
9979             {
9980                char * string = exp.call.exp.identifier.string;
9981                if(inCompiler)
9982                {
9983                   Symbol symbol;
9984                   Location oldyylloc = yylloc;
9985
9986                   yylloc = exp.call.exp.identifier.loc;
9987                   if(strstr(string, "__builtin_") == string)
9988                   {
9989                      if(exp.destType)
9990                      {
9991                         functionType.returnType = exp.destType;
9992                         exp.destType.refCount++;
9993                      }
9994                   }
9995                   else
9996                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9997                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9998                   globalContext.symbols.Add((BTNode)symbol);
9999                   if(strstr(symbol.string, "::"))
10000                      globalContext.hasNameSpace = true;
10001
10002                   yylloc = oldyylloc;
10003                }
10004             }
10005             else if(exp.call.exp.type == memberExp)
10006             {
10007                /*Compiler_Warning($"%s undefined; assuming returning int\n",
10008                   exp.call.exp.member.member.string);*/
10009             }
10010             else
10011                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
10012
10013             if(!functionType.returnType)
10014             {
10015                functionType.returnType = Type
10016                {
10017                   refCount = 1;
10018                   kind = intType;
10019                };
10020             }
10021          }
10022          if(functionType && functionType.kind == TypeKind::functionType)
10023          {
10024             exp.expType = functionType.returnType;
10025
10026             if(functionType.returnType)
10027                functionType.returnType.refCount++;
10028
10029             if(!functionType.refCount)
10030                FreeType(functionType);
10031          }
10032
10033          if(exp.call.arguments)
10034          {
10035             for(e = exp.call.arguments->first; e; e = e.next)
10036                ProcessExpressionType(e);
10037          }
10038          break;
10039       }
10040       case memberExp:
10041       {
10042          Type type;
10043          Location oldyylloc = yylloc;
10044          bool thisPtr;
10045          Expression checkExp = exp.member.exp;
10046          while(checkExp)
10047          {
10048             if(checkExp.type == castExp)
10049                checkExp = checkExp.cast.exp;
10050             else if(checkExp.type == bracketsExp)
10051                checkExp = checkExp.list ? checkExp.list->first : null;
10052             else
10053                break;
10054          }
10055
10056          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
10057          exp.thisPtr = thisPtr;
10058
10059          // DOING THIS LATER NOW...
10060          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10061          {
10062             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10063             /* TODO: Name Space Fix ups
10064             if(!exp.member.member.classSym)
10065                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
10066             */
10067          }
10068
10069          ProcessExpressionType(exp.member.exp);
10070          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
10071             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
10072          {
10073             exp.isConstant = false;
10074          }
10075          else
10076             exp.isConstant = exp.member.exp.isConstant;
10077          type = exp.member.exp.expType;
10078
10079          yylloc = exp.loc;
10080
10081          if(type && (type.kind == templateType))
10082          {
10083             Class _class = thisClass ? thisClass : currentClass;
10084             ClassTemplateParameter param = null;
10085             if(_class)
10086             {
10087                for(param = _class.templateParams.first; param; param = param.next)
10088                {
10089                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
10090                      break;
10091                }
10092             }
10093             if(param && param.defaultArg.member)
10094             {
10095                Expression argExp = GetTemplateArgExpByName(param.name, null, thisClass, TemplateParameterType::identifier);
10096                if(argExp)
10097                {
10098                   Expression expMember = exp.member.exp;
10099                   Declarator decl;
10100                   OldList * specs = MkList();
10101                   char thisClassTypeString[1024];
10102
10103                   FreeIdentifier(exp.member.member);
10104
10105                   ProcessExpressionType(argExp);
10106
10107                   {
10108                      char * colon = strstr(param.defaultArg.memberString, "::");
10109                      if(colon)
10110                      {
10111                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
10112                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
10113                      }
10114                      else
10115                         strcpy(thisClassTypeString, _class.fullName);
10116                   }
10117
10118                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
10119
10120                   exp.expType = ProcessType(specs, decl);
10121                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
10122                   {
10123                      Class expClass = exp.expType._class.registered;
10124                      Class cClass = null;
10125                      int paramCount = 0;
10126                      int lastParam = -1;
10127
10128                      char templateString[1024];
10129                      ClassTemplateParameter param;
10130                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
10131                      for(cClass = expClass; cClass; cClass = cClass.base)
10132                      {
10133                         int p = 0;
10134                         for(param = cClass.templateParams.first; param; param = param.next)
10135                         {
10136                            int id = p;
10137                            Class sClass;
10138                            ClassTemplateArgument arg;
10139                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
10140                            arg = expClass.templateArgs[id];
10141
10142                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
10143                            {
10144                               ClassTemplateParameter cParam;
10145                               //int p = numParams - sClass.templateParams.count;
10146                               int p = 0;
10147                               Class nextClass;
10148                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
10149
10150                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
10151                               {
10152                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
10153                                  {
10154                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10155                                     {
10156                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
10157                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
10158                                        break;
10159                                     }
10160                                  }
10161                               }
10162                            }
10163
10164                            {
10165                               char argument[256];
10166                               argument[0] = '\0';
10167                               /*if(arg.name)
10168                               {
10169                                  strcat(argument, arg.name.string);
10170                                  strcat(argument, " = ");
10171                               }*/
10172                               switch(param.type)
10173                               {
10174                                  case expression:
10175                                  {
10176                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10177                                     char expString[1024];
10178                                     OldList * specs = MkList();
10179                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10180                                     Expression exp;
10181                                     char * string = PrintHexUInt64(arg.expression.ui64);
10182                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10183                                     delete string;
10184
10185                                     ProcessExpressionType(exp);
10186                                     ComputeExpression(exp);
10187                                     expString[0] = '\0';
10188                                     PrintExpression(exp, expString);
10189                                     strcat(argument, expString);
10190                                     // delete exp;
10191                                     FreeExpression(exp);
10192                                     break;
10193                                  }
10194                                  case identifier:
10195                                  {
10196                                     strcat(argument, arg.member.name);
10197                                     break;
10198                                  }
10199                                  case TemplateParameterType::type:
10200                                  {
10201                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10202                                     {
10203                                        if(!strcmp(arg.dataTypeString, "thisclass"))
10204                                           strcat(argument, thisClassTypeString);
10205                                        else
10206                                           strcat(argument, arg.dataTypeString);
10207                                     }
10208                                     break;
10209                                  }
10210                               }
10211                               if(argument[0])
10212                               {
10213                                  if(paramCount) strcat(templateString, ", ");
10214                                  if(lastParam != p - 1)
10215                                  {
10216                                     strcat(templateString, param.name);
10217                                     strcat(templateString, " = ");
10218                                  }
10219                                  strcat(templateString, argument);
10220                                  paramCount++;
10221                                  lastParam = p;
10222                               }
10223                               p++;
10224                            }
10225                         }
10226                      }
10227                      {
10228                         int len = strlen(templateString);
10229                         if(templateString[len-1] == '>') templateString[len++] = ' ';
10230                         templateString[len++] = '>';
10231                         templateString[len++] = '\0';
10232                      }
10233                      {
10234                         Context context = SetupTemplatesContext(_class);
10235                         FreeType(exp.expType);
10236                         exp.expType = ProcessTypeString(templateString, false);
10237                         FinishTemplatesContext(context);
10238                      }
10239                   }
10240
10241                   if(!expMember.expType.isPointerType)
10242                      expMember = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), expMember);
10243                   // *([expType] *)(((byte *)(uintptr)[exp.member.exp]) + [argExp].member.offset)
10244                   exp.type = bracketsExp;
10245                   exp.list = MkListOne(MkExpOp(null, '*',
10246                   /*opExp;
10247                   exp.op.op = '*';
10248                   exp.op.exp1 = null;
10249                   exp.op.exp2 = */
10250                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
10251                      MkExpBrackets(MkListOne(
10252                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
10253                            expMember))),
10254                               '+',
10255                               MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
10256                               '+',
10257                               MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
10258
10259                            ));
10260                }
10261             }
10262             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
10263                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
10264             {
10265                type = ProcessTemplateParameterType(type.templateParameter);
10266             }
10267          }
10268          // TODO: *** This seems to be where we should add method support for all basic types ***
10269          if(type && (type.kind == templateType));
10270          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
10271                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
10272                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
10273                           (type.kind == pointerType && type.type.kind == charType)))
10274          {
10275             Identifier id = exp.member.member;
10276             TypeKind typeKind = type.kind;
10277             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10278             if(typeKind == subClassType && exp.member.exp.type == classExp)
10279             {
10280                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
10281                typeKind = classType;
10282             }
10283
10284             if(id)
10285             {
10286                if(typeKind == intType || typeKind == enumType)
10287                   _class = eSystem_FindClass(privateModule, "int");
10288                else if(!_class)
10289                {
10290                   if(type.kind == classType && type._class && type._class.registered)
10291                   {
10292                      _class = type._class.registered;
10293                   }
10294                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10295                   {
10296                      _class = FindClass("char *").registered;
10297                   }
10298                   else if(type.kind == pointerType)
10299                   {
10300                      _class = eSystem_FindClass(privateModule, "uintptr");
10301                      FreeType(exp.expType);
10302                      exp.expType = ProcessTypeString("uintptr", false);
10303                      exp.byReference = true;
10304                   }
10305                   else
10306                   {
10307                      char string[1024] = "";
10308                      Symbol classSym;
10309                      PrintTypeNoConst(type, string, false, true);
10310                      classSym = FindClass(string);
10311                      if(classSym) _class = classSym.registered;
10312                   }
10313                }
10314             }
10315
10316             if(_class && id)
10317             {
10318                /*bool thisPtr =
10319                   (exp.member.exp.type == identifierExp &&
10320                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10321                Property prop = null;
10322                Method method = null;
10323                DataMember member = null;
10324                Property revConvert = null;
10325                ClassProperty classProp = null;
10326
10327                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10328                   exp.member.memberType = propertyMember;
10329
10330                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10331                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10332
10333                if(typeKind != subClassType)
10334                {
10335                   // Prioritize data members over properties for "this"
10336                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10337                   {
10338                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10339                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10340                      {
10341                         prop = eClass_FindProperty(_class, id.string, privateModule);
10342                         if(prop)
10343                            member = null;
10344                      }
10345                      if(!member && !prop)
10346                         prop = eClass_FindProperty(_class, id.string, privateModule);
10347                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10348                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10349                         exp.member.thisPtr = true;
10350                   }
10351                   // Prioritize properties over data members otherwise
10352                   else
10353                   {
10354                      bool useMemberForNonConst = false;
10355                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10356                      if(!id.classSym)
10357                      {
10358                         prop = eClass_FindProperty(_class, id.string, null);
10359
10360                         useMemberForNonConst = prop && exp.destType &&
10361                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10362                               !strncmp(prop.dataTypeString, "const ", 6);
10363
10364                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10365                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10366                      }
10367
10368                      if((!prop || useMemberForNonConst) && !member)
10369                      {
10370                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10371                         if(!method)
10372                         {
10373                            prop = eClass_FindProperty(_class, id.string, privateModule);
10374
10375                            useMemberForNonConst |= prop && exp.destType &&
10376                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10377                                  !strncmp(prop.dataTypeString, "const ", 6);
10378
10379                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10380                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10381                         }
10382                      }
10383
10384                      if(member && prop)
10385                      {
10386                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10387                            prop = null;
10388                         else
10389                            member = null;
10390                      }
10391                   }
10392                }
10393                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10394                   method = eClass_FindMethod(_class, id.string, privateModule);
10395                if(!prop && !member && !method)
10396                {
10397                   if(typeKind == subClassType)
10398                   {
10399                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10400                      if(classProp)
10401                      {
10402                         exp.member.memberType = classPropertyMember;
10403                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10404                      }
10405                      else
10406                      {
10407                         // Assume this is a class_data member
10408                         char structName[1024];
10409                         Identifier id = exp.member.member;
10410                         Expression classExp = exp.member.exp;
10411                         type.refCount++;
10412
10413                         FreeType(classExp.expType);
10414                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10415
10416                         strcpy(structName, "__ecereClassData_");
10417                         FullClassNameCat(structName, type._class.string, false);
10418                         exp.type = pointerExp;
10419                         exp.member.member = id;
10420
10421                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10422                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10423                               MkExpBrackets(MkListOne(MkExpOp(
10424                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10425                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10426                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10427                                  )));
10428
10429                         FreeType(type);
10430
10431                         ProcessExpressionType(exp);
10432                         return;
10433                      }
10434                   }
10435                   else
10436                   {
10437                      // Check for reverse conversion
10438                      // (Convert in an instantiation later, so that we can use
10439                      //  deep properties system)
10440                      Symbol classSym = FindClass(id.string);
10441                      if(classSym)
10442                      {
10443                         Class convertClass = classSym.registered;
10444                         if(convertClass)
10445                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10446                      }
10447                   }
10448                }
10449
10450                //if(!exp.member.exp.destType)
10451                if(exp.member.exp.destType)
10452                   FreeType(exp.member.exp.destType);
10453                {
10454                   if(method && !method._class.symbol)
10455                      method._class.symbol = FindClass(method._class.fullName);
10456                   if(prop && !prop._class.symbol)
10457                      prop._class.symbol = FindClass(prop._class.fullName);
10458
10459                   exp.member.exp.destType = Type
10460                   {
10461                      refCount = 1;
10462                      kind = classType;
10463                      _class = prop ? prop._class.symbol : method ? method._class.symbol : _class.symbol;
10464                      // wasThisClass = type ? type.wasThisClass : false;
10465                      thisClassFrom = type ? type.thisClassFrom : null;
10466                   };
10467                }
10468
10469                if(prop)
10470                {
10471                   exp.member.memberType = propertyMember;
10472                   if(!prop.dataType)
10473                      ProcessPropertyType(prop);
10474                   exp.expType = prop.dataType;
10475                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10476                   {
10477                      Type type { };
10478                      CopyTypeInto(type, exp.expType);
10479                      type.refCount = 1;
10480                      type.constant = true;
10481                      exp.expType = type;
10482                   }
10483                   else if(prop.dataType)
10484                      prop.dataType.refCount++;
10485                }
10486                else if(member)
10487                {
10488                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10489                   {
10490                      FreeExpContents(exp);
10491                      exp.type = identifierExp;
10492                      exp.identifier = MkIdentifier("class");
10493                      ProcessExpressionType(exp);
10494                      return;
10495                   }
10496
10497                   exp.member.memberType = dataMember;
10498                   DeclareStruct(curExternal, _class.fullName, false, true);
10499                   if(member._class != _class)
10500                      DeclareStruct(curExternal, member._class.fullName, false, true);
10501
10502                   if(!member.dataType)
10503                   {
10504                      Context context = SetupTemplatesContext(_class);
10505                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10506                      FinishTemplatesContext(context);
10507                   }
10508                   if(exp.member.exp.expType.kind == classType && exp.member.exp.expType._class && exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == bitClass)
10509                      member.dataType.bitMemberSize = ((BitMember)member).size;
10510                   exp.expType = member.dataType;
10511                   if(member.dataType) member.dataType.refCount++;
10512                }
10513                else if(revConvert)
10514                {
10515                   exp.member.memberType = reverseConversionMember;
10516                   exp.expType = MkClassType(revConvert._class.fullName);
10517                }
10518                else if(method)
10519                {
10520                   //if(inCompiler)
10521                   {
10522                      /*if(id._class)
10523                      {
10524                         exp.type = identifierExp;
10525                         exp.identifier = exp.member.member;
10526                      }
10527                      else*/
10528                         exp.member.memberType = methodMember;
10529                   }
10530                   if(!method.dataType)
10531                      ProcessMethodType(method);
10532                   exp.expType = Type
10533                   {
10534                      refCount = 1;
10535                      kind = methodType;
10536                      method = method;
10537                   };
10538
10539                   // Tricky spot here... To use instance versus class virtual table
10540                   // Put it back to what it was... What did we break?
10541
10542                   // Had to put it back for overriding Main of Thread global instance
10543
10544                   //exp.expType.methodClass = _class;
10545                   exp.expType.methodClass = (id && id._class) ? _class : null;
10546
10547                   // Need the actual class used for templated classes
10548                   exp.expType.usedClass = _class;
10549                }
10550                else if(!classProp)
10551                {
10552                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10553                   {
10554                      FreeExpContents(exp);
10555                      exp.type = identifierExp;
10556                      exp.identifier = MkIdentifier("class");
10557                      FreeType(exp.expType);
10558                      exp.expType = MkClassType("ecere::com::Class");
10559                      return;
10560                   }
10561                   yylloc = exp.member.member.loc;
10562                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10563                   if(inCompiler)
10564                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10565                }
10566
10567                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10568                {
10569                   Class tClass;
10570
10571                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10572                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10573
10574                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10575                   {
10576                      int id = 0;
10577                      ClassTemplateParameter curParam = null;
10578                      Class sClass;
10579
10580                      for(sClass = tClass; sClass; sClass = sClass.base)
10581                      {
10582                         id = 0;
10583                         if(sClass.templateClass) sClass = sClass.templateClass;
10584                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10585                         {
10586                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10587                            {
10588                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10589                                  id += sClass.templateParams.count;
10590                               break;
10591                            }
10592                            id++;
10593                         }
10594                         if(curParam) break;
10595                      }
10596
10597                      if(curParam && tClass.templateArgs[id].dataTypeString)
10598                      {
10599                         ClassTemplateArgument arg = tClass.templateArgs[id];
10600                         Context context = SetupTemplatesContext(tClass);
10601                         bool constant = exp.expType.constant;
10602                         bool passAsTemplate = false;
10603                         Class thisClassFrom = null;
10604                         Type t = exp.expType.templateParameter.dataTypeString ? ProcessTypeString(exp.expType.templateParameter.dataTypeString, false) : null;
10605                         if(t && t.kind == classType && t._class)
10606                            thisClassFrom = t._class.registered;
10607                         else if(exp.expType.thisClassFrom)
10608                            thisClassFrom = thisClass;
10609                         else
10610                            // Mark that 'thisClassFrom' was set to something
10611                            thisClassFrom = eSystem_FindClass(GetPrivateModule(), "class");
10612
10613                         FreeType(t);
10614
10615                         passAsTemplate = tClass.templateClass && (exp.expType.kind != templateType ||
10616                            (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType)));
10617
10618                         /*if(!arg.dataType)
10619                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10620                         FreeType(exp.expType);
10621
10622                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10623                         exp.expType.thisClassFrom = thisClassFrom;
10624                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10625                         else if(exp.expType.kind == pointerType)
10626                         {
10627                            Type t = exp.expType.type;
10628                            while(t.kind == pointerType) t = t.type;
10629                            if(constant) t.constant = constant;
10630                         }
10631                         if(exp.expType)
10632                         {
10633                            if(exp.expType.kind == thisClassType)
10634                            {
10635                               FreeType(exp.expType);
10636                               exp.expType = ReplaceThisClassType(_class);
10637                            }
10638
10639                            if(passAsTemplate)
10640                               exp.expType.passAsTemplate = true;
10641                            //exp.expType.refCount++;
10642                            if(!exp.destType)
10643                            {
10644                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10645                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10646                               else if(exp.destType.kind == pointerType)
10647                               {
10648                                  Type t = exp.destType.type;
10649                                  while(t.kind == pointerType) t = t.type;
10650                                  if(constant) t.constant = constant;
10651                               }
10652
10653                               //exp.destType.refCount++;
10654
10655                               if(exp.destType.kind == thisClassType)
10656                               {
10657                                  FreeType(exp.destType);
10658                                  exp.destType = ReplaceThisClassType(_class);
10659                               }
10660                            }
10661                         }
10662                         FinishTemplatesContext(context);
10663                      }
10664                   }
10665                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10666                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10667                   {
10668                      int id = 0;
10669                      ClassTemplateParameter curParam = null;
10670                      Class sClass;
10671
10672                      for(sClass = tClass; sClass; sClass = sClass.base)
10673                      {
10674                         id = 0;
10675                         if(sClass.templateClass) sClass = sClass.templateClass;
10676                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10677                         {
10678                            if(curParam.type == TemplateParameterType::type &&
10679                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10680                            {
10681                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10682                                  id += sClass.templateParams.count;
10683                               break;
10684                            }
10685                            id++;
10686                         }
10687                         if(curParam) break;
10688                      }
10689
10690                      if(curParam)
10691                      {
10692                         ClassTemplateArgument arg = tClass.templateArgs[id];
10693                         Context context = SetupTemplatesContext(tClass);
10694                         Type basicType;
10695                         /*if(!arg.dataType)
10696                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10697
10698                         basicType = ProcessTypeString(arg.dataTypeString, false);
10699                         if(basicType)
10700                         {
10701                            if(basicType.kind == thisClassType)
10702                            {
10703                               FreeType(basicType);
10704                               basicType = ReplaceThisClassType(_class);
10705                            }
10706
10707                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10708                            if(tClass.templateClass)
10709                               basicType.passAsTemplate = true;
10710                            */
10711
10712                            FreeType(exp.expType);
10713
10714                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10715                            //exp.expType.refCount++;
10716                            if(!exp.destType)
10717                            {
10718                               exp.destType = exp.expType;
10719                               exp.destType.refCount++;
10720                            }
10721
10722                            {
10723                               Expression newExp { };
10724                               OldList * specs = MkList();
10725                               Declarator decl;
10726                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10727                               *newExp = *exp;
10728                               if(exp.destType) exp.destType.refCount++;
10729                               if(exp.expType)  exp.expType.refCount++;
10730                               exp.type = bracketsExp;
10731                               exp.list = MkListOne(MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), newExp));
10732                               //FreeType(exp.expType);
10733                               //exp.expType = null;
10734                               //ProcessExpressionType(sourceExp);
10735                            }
10736                         }
10737                         FinishTemplatesContext(context);
10738                      }
10739                   }
10740                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10741                   {
10742                      Class expClass = exp.expType._class.registered;
10743                      if(expClass)
10744                      {
10745                         Class cClass = null;
10746                         int p = 0;
10747                         int paramCount = 0;
10748                         int lastParam = -1;
10749                         char templateString[1024];
10750                         ClassTemplateParameter param;
10751                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10752                         while(cClass != expClass)
10753                         {
10754                            Class sClass;
10755                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10756                            cClass = sClass;
10757
10758                            for(param = cClass.templateParams.first; param; param = param.next)
10759                            {
10760                               Class cClassCur = null;
10761                               int cp = 0;
10762                               ClassTemplateParameter paramCur = null;
10763                               ClassTemplateArgument arg;
10764                               while(cClassCur != tClass && !paramCur)
10765                               {
10766                                  Class sClassCur;
10767                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10768                                  cClassCur = sClassCur;
10769
10770                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10771                                  {
10772                                     if(!strcmp(paramCur.name, param.name))
10773                                     {
10774
10775                                        break;
10776                                     }
10777                                     cp++;
10778                                  }
10779                               }
10780                               if(paramCur && paramCur.type == TemplateParameterType::type)
10781                                  arg = tClass.templateArgs[cp];
10782                               else
10783                                  arg = expClass.templateArgs[p];
10784
10785                               {
10786                                  char argument[256];
10787                                  argument[0] = '\0';
10788                                  /*if(arg.name)
10789                                  {
10790                                     strcat(argument, arg.name.string);
10791                                     strcat(argument, " = ");
10792                                  }*/
10793                                  switch(param.type)
10794                                  {
10795                                     case expression:
10796                                     {
10797                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10798                                        char expString[1024];
10799                                        OldList * specs = MkList();
10800                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10801                                        Expression exp;
10802                                        char * string = PrintHexUInt64(arg.expression.ui64);
10803                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10804                                        delete string;
10805
10806                                        ProcessExpressionType(exp);
10807                                        ComputeExpression(exp);
10808                                        expString[0] = '\0';
10809                                        PrintExpression(exp, expString);
10810                                        strcat(argument, expString);
10811                                        // delete exp;
10812                                        FreeExpression(exp);
10813                                        break;
10814                                     }
10815                                     case identifier:
10816                                     {
10817                                        strcat(argument, arg.member.name);
10818                                        break;
10819                                     }
10820                                     case TemplateParameterType::type:
10821                                     {
10822                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10823                                           strcat(argument, arg.dataTypeString);
10824                                        break;
10825                                     }
10826                                  }
10827                                  if(argument[0])
10828                                  {
10829                                     if(paramCount) strcat(templateString, ", ");
10830                                     if(lastParam != p - 1)
10831                                     {
10832                                        strcat(templateString, param.name);
10833                                        strcat(templateString, " = ");
10834                                     }
10835                                     strcat(templateString, argument);
10836                                     paramCount++;
10837                                     lastParam = p;
10838                                  }
10839                               }
10840                               p++;
10841                            }
10842                         }
10843                         {
10844                            int len = strlen(templateString);
10845                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10846                            templateString[len++] = '>';
10847                            templateString[len++] = '\0';
10848                         }
10849
10850                         FreeType(exp.expType);
10851                         {
10852                            Context context = SetupTemplatesContext(tClass);
10853                            exp.expType = ProcessTypeString(templateString, false);
10854                            FinishTemplatesContext(context);
10855                         }
10856                      }
10857                   }
10858                }
10859             }
10860             else
10861                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10862          }
10863          else if(type && (type.kind == structType || type.kind == unionType))
10864          {
10865             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10866             if(memberType)
10867             {
10868                exp.expType = memberType;
10869                if(memberType)
10870                   memberType.refCount++;
10871             }
10872          }
10873          else
10874          {
10875             char expString[10240];
10876             expString[0] = '\0';
10877             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10878             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10879          }
10880
10881          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10882          {
10883             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10884             {
10885                Identifier id = exp.member.member;
10886                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10887                if(_class)
10888                {
10889                   FreeType(exp.expType);
10890                   exp.expType = ReplaceThisClassType(_class);
10891                }
10892             }
10893          }
10894          yylloc = oldyylloc;
10895          break;
10896       }
10897       // Convert x->y into (*x).y
10898       case pointerExp:
10899       {
10900          Type destType = exp.destType;
10901
10902          // DOING THIS LATER NOW...
10903          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10904          {
10905             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10906             /* TODO: Name Space Fix ups
10907             if(!exp.member.member.classSym)
10908                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10909             */
10910          }
10911
10912          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10913          exp.type = memberExp;
10914          if(destType)
10915             destType.count++;
10916          ProcessExpressionType(exp);
10917          if(destType)
10918             destType.count--;
10919          break;
10920       }
10921       case classSizeExp:
10922       {
10923          //ComputeExpression(exp);
10924
10925          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10926          if(classSym && classSym.registered)
10927          {
10928             if(classSym.registered.type == noHeadClass || (classSym.registered.fixed && classSym.registered.structSize))
10929             {
10930                char name[1024];
10931                Class b = classSym.registered;
10932                name[0] = '\0';
10933                DeclareStruct(curExternal, classSym.string, false, true);
10934                FreeSpecifier(exp._class);
10935                FullClassNameCat(name, classSym.string, false);
10936
10937                if(b.offset == 0)
10938                {
10939                   exp.type = typeSizeExp;
10940                   exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10941                }
10942                else
10943                {
10944                   Expression e;
10945                   exp.type = opExp;
10946                   if(b.structSize == b.offset)
10947                      exp.op.exp1 = MkExpConstant("0");
10948                   else
10949                      exp.op.exp1 = MkExpTypeSize(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null));
10950                   exp.op.op = '+';
10951                   e = exp;
10952                   while(b.offset != 0)
10953                   {
10954                      Symbol sym;
10955                      Expression typeSize;
10956
10957                      b = b.base;
10958                      sym = FindClass(b.fullName);
10959
10960                      name[0] = '\0';
10961                      DeclareStruct(curExternal, sym.string, false, true);
10962                      FullClassNameCat(name, sym.string, false);
10963
10964                      if(b.structSize == b.offset)
10965                         typeSize = MkExpConstant("0");
10966                      else
10967                         typeSize = MkExpTypeSize(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null));
10968                      e.op.exp2 = b.offset ? MkExpOp(typeSize, '+', null) : typeSize;
10969                      e = e.op.exp2;
10970                   }
10971                }
10972             }
10973             else
10974             {
10975                if(classSym.registered.fixed && !classSym.registered.structSize)
10976                {
10977                   FreeSpecifier(exp._class);
10978                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10979                   exp.type = constantExp;
10980                }
10981                else
10982                {
10983                   char className[1024];
10984                   strcpy(className, "__ecereClass_");
10985                   FullClassNameCat(className, classSym.string, true);
10986
10987                   DeclareClass(curExternal, classSym, className);
10988
10989                   FreeExpContents(exp);
10990                   exp.type = pointerExp;
10991                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10992                   exp.member.member = MkIdentifier("structSize");
10993                }
10994             }
10995          }
10996
10997          exp.expType = Type
10998          {
10999             refCount = 1;
11000             kind = intSizeType;
11001          };
11002          // exp.isConstant = true;
11003          break;
11004       }
11005       case typeSizeExp:
11006       {
11007          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
11008
11009          exp.expType = Type
11010          {
11011             refCount = 1;
11012             kind = intSizeType;
11013          };
11014          exp.isConstant = true;
11015
11016          DeclareType(curExternal, type, true, false);
11017          FreeType(type);
11018          break;
11019       }
11020       case castExp:
11021       {
11022          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
11023          type.count = 1;
11024          FreeType(exp.cast.exp.destType);
11025          exp.cast.exp.destType = type;
11026          type.refCount++;
11027          type.casted = true;
11028          ProcessExpressionType(exp.cast.exp);
11029          type.casted = false;
11030          type.count = 0;
11031          exp.expType = type;
11032          //type.refCount++;
11033
11034          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
11035          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
11036          {
11037             void * prev = exp.prev, * next = exp.next;
11038             Type expType = exp.cast.exp.destType;
11039             Expression castExp = exp.cast.exp;
11040             Type destType = exp.destType;
11041
11042             if(expType) expType.refCount++;
11043
11044             //FreeType(exp.destType);
11045             FreeType(exp.expType);
11046             FreeTypeName(exp.cast.typeName);
11047
11048             *exp = *castExp;
11049             FreeType(exp.expType);
11050             FreeType(exp.destType);
11051
11052             exp.expType = expType;
11053             exp.destType = destType;
11054
11055             delete castExp;
11056
11057             exp.prev = prev;
11058             exp.next = next;
11059
11060          }
11061          else
11062          {
11063             exp.isConstant = exp.cast.exp.isConstant;
11064          }
11065          //FreeType(type);
11066          break;
11067       }
11068       case extensionInitializerExp:
11069       {
11070          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
11071          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
11072          // ProcessInitializer(exp.initializer.initializer, type);
11073          exp.expType = type;
11074          break;
11075       }
11076       case vaArgExp:
11077       {
11078          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
11079          ProcessExpressionType(exp.vaArg.exp);
11080          exp.expType = type;
11081          break;
11082       }
11083       case conditionExp:
11084       {
11085          Expression e;
11086          Type t = exp.destType;
11087          if(t && !exp.destType.casted)
11088          {
11089             t = { };
11090             CopyTypeInto(t, exp.destType);
11091             t.count = 0;
11092          }
11093          else if(t)
11094             t.refCount++;
11095
11096          exp.isConstant = true;
11097
11098          FreeType(exp.cond.cond.destType);
11099          exp.cond.cond.destType = MkClassType("bool");
11100          exp.cond.cond.destType.truth = true;
11101          ProcessExpressionType(exp.cond.cond);
11102          if(!exp.cond.cond.isConstant)
11103             exp.isConstant = false;
11104          for(e = exp.cond.exp->first; e; e = e.next)
11105          {
11106             if(!e.next)
11107             {
11108                FreeType(e.destType);
11109                e.destType = t;
11110                if(e.destType) e.destType.refCount++;
11111             }
11112             ProcessExpressionType(e);
11113             if(!e.next)
11114             {
11115                exp.expType = e.expType;
11116                if(e.expType) e.expType.refCount++;
11117             }
11118             if(!e.isConstant)
11119                exp.isConstant = false;
11120          }
11121
11122          FreeType(exp.cond.elseExp.destType);
11123          // Added this check if we failed to find an expType
11124          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
11125
11126          // Reversed it...
11127          exp.cond.elseExp.destType = t ? t : exp.expType;
11128
11129          if(exp.cond.elseExp.destType)
11130             exp.cond.elseExp.destType.refCount++;
11131          ProcessExpressionType(exp.cond.elseExp);
11132
11133          // FIXED THIS: Was done before calling process on elseExp
11134          if(!exp.cond.elseExp.isConstant)
11135             exp.isConstant = false;
11136
11137          FreeType(t);
11138          break;
11139       }
11140       case extensionCompoundExp:
11141       {
11142          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
11143          {
11144             Statement last = exp.compound.compound.statements->last;
11145             if(last.type == expressionStmt && last.expressions && last.expressions->last)
11146             {
11147                ((Expression)last.expressions->last).destType = exp.destType;
11148                if(exp.destType)
11149                   exp.destType.refCount++;
11150             }
11151             ProcessStatement(exp.compound);
11152             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
11153             if(exp.expType)
11154                exp.expType.refCount++;
11155          }
11156          break;
11157       }
11158       case classExp:
11159       {
11160          Specifier spec = exp._classExp.specifiers->first;
11161          if(spec && spec.type == nameSpecifier)
11162          {
11163             exp.expType = MkClassType(spec.name);
11164             exp.expType.kind = subClassType;
11165             exp.byReference = true;
11166          }
11167          else
11168          {
11169             exp.expType = MkClassType("ecere::com::Class");
11170             exp.byReference = true;
11171          }
11172          break;
11173       }
11174       case classDataExp:
11175       {
11176          Class _class = thisClass ? thisClass : currentClass;
11177          if(_class)
11178          {
11179             Identifier id = exp.classData.id;
11180             char structName[1024];
11181             Expression classExp;
11182             strcpy(structName, "__ecereClassData_");
11183             FullClassNameCat(structName, _class.fullName, false);
11184             exp.type = pointerExp;
11185             exp.member.member = id;
11186             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
11187                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
11188             else
11189                classExp = MkExpIdentifier(MkIdentifier("class"));
11190
11191             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
11192                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
11193                   MkExpBrackets(MkListOne(MkExpOp(
11194                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
11195                         MkExpMember(classExp, MkIdentifier("data"))), '+',
11196                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
11197                      )));
11198
11199             ProcessExpressionType(exp);
11200             return;
11201          }
11202          break;
11203       }
11204       case arrayExp:
11205       {
11206          Type type = null;
11207          const char * typeString = null;
11208          char typeStringBuf[1024];
11209          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
11210             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
11211          {
11212             Class templateClass = exp.destType._class.registered;
11213             typeString = templateClass.templateArgs[2].dataTypeString;
11214          }
11215          else if(exp.list)
11216          {
11217             // Guess type from expressions in the array
11218             Expression e;
11219             for(e = exp.list->first; e; e = e.next)
11220             {
11221                ProcessExpressionType(e);
11222                if(e.expType)
11223                {
11224                   if(!type) { type = e.expType; type.refCount++; }
11225                   else
11226                   {
11227                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11228                      if(!MatchTypeExpression(e, type, null, false, true))
11229                      {
11230                         FreeType(type);
11231                         type = e.expType;
11232                         e.expType = null;
11233
11234                         e = exp.list->first;
11235                         ProcessExpressionType(e);
11236                         if(e.expType)
11237                         {
11238                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
11239                            if(!MatchTypeExpression(e, type, null, false, true))
11240                            {
11241                               FreeType(e.expType);
11242                               e.expType = null;
11243                               FreeType(type);
11244                               type = null;
11245                               break;
11246                            }
11247                         }
11248                      }
11249                   }
11250                   if(e.expType)
11251                   {
11252                      FreeType(e.expType);
11253                      e.expType = null;
11254                   }
11255                }
11256             }
11257             if(type)
11258             {
11259                typeStringBuf[0] = '\0';
11260                PrintTypeNoConst(type, typeStringBuf, false, true);
11261                typeString = typeStringBuf;
11262                FreeType(type);
11263                type = null;
11264             }
11265          }
11266          if(typeString)
11267          {
11268             /*
11269             (Container)& (struct BuiltInContainer)
11270             {
11271                ._vTbl = class(BuiltInContainer)._vTbl,
11272                ._class = class(BuiltInContainer),
11273                .refCount = 0,
11274                .data = (int[]){ 1, 7, 3, 4, 5 },
11275                .count = 5,
11276                .type = class(int),
11277             }
11278             */
11279             char templateString[1024];
11280             OldList * initializers = MkList();
11281             OldList * structInitializers = MkList();
11282             OldList * specs = MkList();
11283             Expression expExt;
11284             Declarator decl = SpecDeclFromString(typeString, specs, null);
11285             sprintf(templateString, "Container<%s>", typeString);
11286
11287             if(exp.list)
11288             {
11289                Expression e;
11290                type = ProcessTypeString(typeString, false);
11291                while((e = exp.list->first))
11292                {
11293                   exp.list->Remove(e);
11294                   e.destType = type;
11295                   type.refCount++;
11296                   ProcessExpressionType(e);
11297                   ListAdd(initializers, MkInitializerAssignment(e));
11298                }
11299                FreeType(type);
11300                delete exp.list;
11301             }
11302
11303             DeclareStruct(curExternal, "ecere::com::BuiltInContainer", false, true);
11304
11305             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
11306                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11307             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
11308                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11309             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
11310                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11311             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
11312                MkTypeName(specs, MkDeclaratorArray(decl, null)),
11313                MkInitializerList(initializers))));
11314                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11315             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
11316                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11317             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
11318                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11319             exp.expType = ProcessTypeString(templateString, false);
11320             exp.type = bracketsExp;
11321             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
11322                MkExpOp(null, '&',
11323                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
11324                   MkInitializerList(structInitializers)))));
11325             ProcessExpressionType(expExt);
11326          }
11327          else
11328          {
11329             exp.expType = ProcessTypeString("Container", false);
11330             Compiler_Error($"Couldn't determine type of array elements\n");
11331          }
11332          break;
11333       }
11334    }
11335
11336    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
11337    {
11338       FreeType(exp.expType);
11339       exp.expType = ReplaceThisClassType(thisClass);
11340    }
11341
11342    // Resolve structures here
11343    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
11344    {
11345       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
11346       // TODO: Fix members reference...
11347       if(symbol)
11348       {
11349          if(exp.expType.kind != enumType)
11350          {
11351             Type member;
11352             String enumName = CopyString(exp.expType.enumName);
11353
11354             // Fixed a memory leak on self-referencing C structs typedefs
11355             // by instantiating a new type rather than simply copying members
11356             // into exp.expType
11357             FreeType(exp.expType);
11358             exp.expType = Type { };
11359             exp.expType.kind = symbol.type.kind;
11360             exp.expType.refCount++;
11361             exp.expType.enumName = enumName;
11362
11363             exp.expType.members = symbol.type.members;
11364             for(member = symbol.type.members.first; member; member = member.next)
11365                member.refCount++;
11366          }
11367          else
11368          {
11369             NamedLink64 member;
11370             for(member = symbol.type.members.first; member; member = member.next)
11371             {
11372                NamedLink64 value { name = CopyString(member.name) };
11373                exp.expType.members.Add(value);
11374             }
11375          }
11376       }
11377    }
11378
11379    // Trying to do this here before conversion properties kick in and this becomes a new expression... (Fixing Class c; const char * a = c;)
11380    // Mark nohead classes as by reference, unless we're casting them to an integral type
11381    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11382       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11383          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11384           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11385    {
11386       exp.byReference = true;
11387    }
11388
11389    yylloc = exp.loc;
11390    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11391    else if(exp.destType && !exp.destType.keepCast)
11392    {
11393       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11394          exp.needTemplateCast = 1;
11395
11396       if(exp.destType.kind == voidType);
11397       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11398       {
11399          // Warn for casting unrelated types to/from struct classes
11400          bool invalidCast = false;
11401          if(inCompiler && exp.destType.count && exp.expType)
11402          {
11403             Class c1 = (exp.expType.kind == classType && exp.expType._class) ? exp.expType._class.registered : null;
11404             Class c2 = (exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
11405             if(c1 && c1.type != structClass) c1 = null;
11406             if(c2 && c2.type != structClass) c2 = null;
11407             if((c1 && !exp.expType.byReference && !c2 && !exp.destType.isPointerType) || (c2 && !exp.destType.byReference && !c1 && !exp.expType.isPointerType))
11408                invalidCast = true;
11409          }
11410          if(!exp.destType.count || unresolved || invalidCast)
11411          {
11412             if(!exp.expType)
11413             {
11414                yylloc = exp.loc;
11415                if(exp.destType.kind != ellipsisType)
11416                {
11417                   char type2[1024];
11418                   type2[0] = '\0';
11419                   if(inCompiler)
11420                   {
11421                      char expString[10240];
11422                      expString[0] = '\0';
11423
11424                      PrintType(exp.destType, type2, false, true);
11425
11426                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11427                      if(unresolved)
11428                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11429                      else if(exp.type != dummyExp)
11430                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11431                   }
11432                }
11433                else
11434                {
11435                   char expString[10240] ;
11436                   expString[0] = '\0';
11437                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11438
11439                   if(unresolved)
11440                      Compiler_Error($"unresolved identifier %s\n", expString);
11441                   else if(exp.type != dummyExp)
11442                      Compiler_Error($"couldn't determine type of %s\n", expString);
11443                }
11444             }
11445             else
11446             {
11447                char type1[1024];
11448                char type2[1024];
11449                type1[0] = '\0';
11450                type2[0] = '\0';
11451                if(inCompiler)
11452                {
11453                   PrintType(exp.expType, type1, false, true);
11454                   PrintType(exp.destType, type2, false, true);
11455                }
11456
11457                //CheckExpressionType(exp, exp.destType, false);
11458
11459                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11460                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11461                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11462                else
11463                {
11464                   Expression nbExp = GetNonBracketsExp(exp);
11465                   bool skipWarning = false;
11466                   TypeKind kind = exp.destType.kind;
11467                   if(nbExp.type == conditionExp && nbExp.destType && !nbExp.destType.casted && nbExp.destType.kind == exp.destType.kind)
11468                      // The if/else operands have already been checked / warned about
11469                      skipWarning = true;
11470                   if((kind == charType || kind == shortType) && exp.destType.isSigned == exp.expType.signedBeforePromotion && nbExp.type == opExp && nbExp.op.exp1 && nbExp.op.exp2)
11471                   {
11472                      int op = nbExp.op.op;
11473                      Expression nbExp1, nbExp2;
11474                      TypeKind from;
11475
11476                      switch(op)
11477                      {
11478                         case '%': case '/':
11479                            nbExp1 = GetNonBracketsExp(nbExp.op.exp1);
11480                            from = nbExp1.expType.promotedFrom;
11481                            // Division and Modulo will not take more room than type before promotion
11482                            if(from == charType || (kind == shortType && from == shortType))
11483                               skipWarning = true;
11484                            break;
11485                         // Left shift
11486                         case LEFT_OP: case RIGHT_OP:
11487                            nbExp1 = GetNonBracketsExp(nbExp.op.exp1);
11488                            nbExp2 = GetNonBracketsExp(nbExp.op.exp2);
11489                            from = nbExp1.expType.promotedFrom;
11490                            // Right shift will not take more room than type before promotion
11491                            if(op == RIGHT_OP && (from == charType || (kind == shortType && from == shortType)))
11492                               skipWarning = true;
11493                            else if(nbExp2.isConstant && nbExp2.type == constantExp && (nbExp.op.op == RIGHT_OP || nbExp1.expType.bitMemberSize))
11494                            {
11495                               int n = (int)strtol(nbExp2.constant, null, 0);
11496                               int s = from == charType ? 8 : 16;
11497                               // Left shifting a bit member constrained in size may still fit in type before promotion
11498                               if(nbExp1.expType.bitMemberSize && nbExp1.expType.bitMemberSize < s)
11499                                  s = nbExp1.expType.bitMemberSize;
11500
11501                               // If right shifted enough things will fit in smaller type
11502                               if(nbExp.op.op == RIGHT_OP)
11503                                  s -= n;
11504                               else
11505                                  s += n;
11506                               if(s <= (kind == charType ? 8 : 16))
11507                                  skipWarning = true;
11508                            }
11509                            break;
11510                         case '-':
11511                            if(!exp.destType.isSigned)
11512                            {
11513                               nbExp1 = GetNonBracketsExp(nbExp.op.exp1);
11514                               nbExp2 = GetNonBracketsExp(nbExp.op.exp2);
11515                               from = nbExp2.expType.promotedFrom;
11516                               // Max value of unsigned type before promotion minus the same will always fit
11517                               if((from == charType || from == shortType) && nbExp1.isConstant && nbExp1.type == constantExp)
11518                               {
11519                                  int n = (int)strtol(nbExp1.constant, null, 0);
11520                                  if(n == (from == charType ? 255 : 65535))
11521                                     skipWarning = true;
11522                               }
11523                            }
11524                            break;
11525                         case '|':
11526                         {
11527                            TypeKind kind1, kind2;
11528                            nbExp1 = GetNonBracketsExp(nbExp.op.exp1);
11529                            nbExp2 = GetNonBracketsExp(nbExp.op.exp2);
11530                            kind1 = nbExp1.expType.promotedFrom ? nbExp1.expType.promotedFrom : nbExp1.expType.kind;
11531                            kind2 = nbExp2.expType.promotedFrom ? nbExp2.expType.promotedFrom : nbExp2.expType.kind;
11532                            if(((kind1 == charType || (kind1 == shortType && kind == shortType)) || MatchTypeExpression(nbExp1, exp.destType, null, false, false)) &&
11533                               ((kind2 == charType || (kind2 == shortType && kind == shortType)) || MatchTypeExpression(nbExp2, exp.destType, null, false, false)))
11534                               skipWarning = true;
11535                            break;
11536                         }
11537                         case '&':
11538                         {
11539                            TypeKind kind1, kind2;
11540                            nbExp1 = GetNonBracketsExp(nbExp.op.exp1);
11541                            nbExp2 = GetNonBracketsExp(nbExp.op.exp2);
11542                            kind1 = nbExp1.expType.promotedFrom ? nbExp1.expType.promotedFrom : nbExp1.expType.kind;
11543                            kind2 = nbExp2.expType.promotedFrom ? nbExp2.expType.promotedFrom : nbExp2.expType.kind;
11544                            if(((kind1 == charType || (kind1 == shortType && kind == shortType)) || MatchTypeExpression(nbExp1, exp.destType, null, false, false)) ||
11545                               ((kind2 == charType || (kind2 == shortType && kind == shortType)) || MatchTypeExpression(nbExp2, exp.destType, null, false, false)))
11546                               skipWarning = true;
11547                            break;
11548                         }
11549                      }
11550                   }
11551
11552                   if(!skipWarning)
11553                   {
11554                      char expString[10240];
11555                      expString[0] = '\0';
11556                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11557
11558 #ifdef _DEBUG
11559                      CheckExpressionType(exp, exp.destType, false, true);
11560 #endif
11561
11562                      // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11563                      if(!sourceFile || (!strstr(sourceFile, "src\\lexer.ec") && !strstr(sourceFile, "src/lexer.ec") &&
11564                                         !strstr(sourceFile, "src\\grammar.ec") && !strstr(sourceFile, "src/grammar.ec") &&
11565                                         !strstr(sourceFile, "src\\type.ec") && !strstr(sourceFile, "src/type.ec") &&
11566                                         !strstr(sourceFile, "src\\expression.ec") && !strstr(sourceFile, "src/expression.ec")))
11567                      {
11568                         if(invalidCast)
11569                            Compiler_Error($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11570                         else
11571                            Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11572                      }
11573                   }
11574
11575                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11576                   if(!inCompiler)
11577                   {
11578                      FreeType(exp.expType);
11579                      exp.destType.refCount++;
11580                      exp.expType = exp.destType;
11581                   }
11582                }
11583             }
11584          }
11585       }
11586       // Cast function pointers to void * as eC already checked compatibility
11587       else if(exp.destType && exp.destType.kind == pointerType && exp.destType.type && exp.destType.type.kind == functionType &&
11588               exp.expType && (exp.expType.kind == functionType || exp.expType.kind == methodType))
11589       {
11590          Expression nbExp = GetNonBracketsExp(exp);
11591          if(nbExp.type != castExp || !IsVoidPtrCast(nbExp.cast.typeName))
11592          {
11593             Expression e = MoveExpContents(exp);
11594             exp.cast.exp = MkExpBrackets(MkListOne(e));
11595             exp.type = castExp;
11596             exp.cast.exp.destType = exp.destType;
11597             if(exp.destType) exp.destType.refCount++;
11598             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
11599          }
11600       }
11601    }
11602    else if(unresolved)
11603    {
11604       if(exp.identifier._class && exp.identifier._class.name)
11605          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11606       else if(exp.identifier.string && exp.identifier.string[0])
11607          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11608    }
11609    else if(!exp.expType && exp.type != dummyExp)
11610    {
11611       char expString[10240];
11612       expString[0] = '\0';
11613       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11614       Compiler_Error($"couldn't determine type of %s\n", expString);
11615    }
11616
11617    // Let's try to support any_object & typed_object here:
11618    if(inCompiler)
11619       ApplyAnyObjectLogic(exp);
11620
11621    // Mark nohead classes as by reference, unless we're casting them to an integral type
11622    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11623       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11624          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11625           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11626    {
11627       exp.byReference = true;
11628    }
11629    yylloc = oldyylloc;
11630 }
11631
11632 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11633 {
11634    // THIS CODE WILL FIND NEXT MEMBER...
11635    if(*curMember)
11636    {
11637       *curMember = (*curMember).next;
11638
11639       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11640       {
11641          *curMember = subMemberStack[--(*subMemberStackPos)];
11642          *curMember = (*curMember).next;
11643       }
11644
11645       // SKIP ALL PROPERTIES HERE...
11646       while((*curMember) && (*curMember).isProperty)
11647          *curMember = (*curMember).next;
11648
11649       if(subMemberStackPos)
11650       {
11651          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11652          {
11653             subMemberStack[(*subMemberStackPos)++] = *curMember;
11654
11655             *curMember = (*curMember).members.first;
11656             while(*curMember && (*curMember).isProperty)
11657                *curMember = (*curMember).next;
11658          }
11659       }
11660    }
11661    while(!*curMember)
11662    {
11663       if(!*curMember)
11664       {
11665          if(subMemberStackPos && *subMemberStackPos)
11666          {
11667             *curMember = subMemberStack[--(*subMemberStackPos)];
11668             *curMember = (*curMember).next;
11669          }
11670          else
11671          {
11672             Class lastCurClass = *curClass;
11673
11674             if(*curClass == _class) break;     // REACHED THE END
11675
11676             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11677             *curMember = (*curClass).membersAndProperties.first;
11678          }
11679
11680          while((*curMember) && (*curMember).isProperty)
11681             *curMember = (*curMember).next;
11682          if(subMemberStackPos)
11683          {
11684             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11685             {
11686                subMemberStack[(*subMemberStackPos)++] = *curMember;
11687
11688                *curMember = (*curMember).members.first;
11689                while(*curMember && (*curMember).isProperty)
11690                   *curMember = (*curMember).next;
11691             }
11692          }
11693       }
11694    }
11695 }
11696
11697
11698 static void ProcessInitializer(Initializer init, Type type)
11699 {
11700    switch(init.type)
11701    {
11702       case expInitializer:
11703          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11704          {
11705             // TESTING THIS FOR SHUTTING = 0 WARNING
11706             if(init.exp && !init.exp.destType)
11707             {
11708                FreeType(init.exp.destType);
11709                init.exp.destType = type;
11710                if(type) type.refCount++;
11711             }
11712             if(init.exp)
11713             {
11714                ProcessExpressionType(init.exp);
11715                init.isConstant = init.exp.isConstant;
11716             }
11717             break;
11718          }
11719          else
11720          {
11721             Expression exp = init.exp;
11722             Instantiation inst = exp.instance;
11723             MembersInit members;
11724
11725             init.type = listInitializer;
11726             init.list = MkList();
11727
11728             if(inst.members)
11729             {
11730                for(members = inst.members->first; members; members = members.next)
11731                {
11732                   if(members.type == dataMembersInit)
11733                   {
11734                      MemberInit member;
11735                      for(member = members.dataMembers->first; member; member = member.next)
11736                      {
11737                         ListAdd(init.list, member.initializer);
11738                         member.initializer = null;
11739                      }
11740                   }
11741                   // Discard all MembersInitMethod
11742                }
11743             }
11744             FreeExpression(exp);
11745          }
11746       case listInitializer:
11747       {
11748          Initializer i;
11749          Type initializerType = null;
11750          Class curClass = null;
11751          DataMember curMember = null;
11752          DataMember subMemberStack[256];
11753          int subMemberStackPos = 0;
11754
11755          if(type && type.kind == arrayType)
11756             initializerType = Dereference(type);
11757          else if(type && (type.kind == structType || type.kind == unionType))
11758             initializerType = type.members.first;
11759
11760          for(i = init.list->first; i; i = i.next)
11761          {
11762             if(type && type.kind == classType && type._class && type._class.registered)
11763             {
11764                // 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)
11765                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11766                // TODO: Generate error on initializing a private data member this way from another module...
11767                if(curMember)
11768                {
11769                   if(!curMember.dataType)
11770                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11771                   initializerType = curMember.dataType;
11772                }
11773             }
11774             ProcessInitializer(i, initializerType);
11775             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11776                initializerType = initializerType.next;
11777             if(!i.isConstant)
11778                init.isConstant = false;
11779          }
11780
11781          if(type && type.kind == arrayType)
11782             FreeType(initializerType);
11783
11784          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11785          {
11786             Compiler_Error($"Assigning list initializer to non list\n");
11787          }
11788          break;
11789       }
11790    }
11791 }
11792
11793 static void ProcessSpecifier(Specifier spec, bool declareStruct, bool warnClasses)
11794 {
11795    switch(spec.type)
11796    {
11797       case baseSpecifier:
11798       {
11799          if(spec.specifier == THISCLASS)
11800          {
11801             if(thisClass)
11802             {
11803                spec.type = nameSpecifier;
11804                spec.name = ReplaceThisClass(thisClass);
11805                spec.symbol = FindClass(spec.name);
11806                ProcessSpecifier(spec, declareStruct, false);
11807             }
11808          }
11809          break;
11810       }
11811       case nameSpecifier:
11812       {
11813          Symbol symbol = FindType(curContext, spec.name);
11814          if(symbol)
11815             DeclareType(curExternal, symbol.type, true, true);
11816          else if(spec.symbol /*&& declareStruct*/)
11817          {
11818             Class c = spec.symbol.registered;
11819             if(warnClasses && !c)
11820                Compiler_Warning("Undeclared class %s\n", spec.name);
11821             DeclareStruct(curExternal, spec.name, c && c.type == noHeadClass, declareStruct && c && c.type == structClass);
11822          }
11823          break;
11824       }
11825       case enumSpecifier:
11826       {
11827          Enumerator e;
11828          if(spec.list)
11829          {
11830             for(e = spec.list->first; e; e = e.next)
11831             {
11832                if(e.exp)
11833                   ProcessExpressionType(e.exp);
11834             }
11835          }
11836          // Fall through for IDE type processing
11837          if(inCompiler)
11838             break;
11839       }
11840       case structSpecifier:
11841       case unionSpecifier:
11842       {
11843          if(spec.definitions)
11844          {
11845             //ClassDef def;
11846             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11847             //if(symbol)
11848                ProcessClass(spec.definitions, symbol);
11849             /*else
11850             {
11851                for(def = spec.definitions->first; def; def = def.next)
11852                {
11853                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11854                      ProcessDeclaration(def.decl);
11855                }
11856             }*/
11857          }
11858          break;
11859       }
11860       /*
11861       case classSpecifier:
11862       {
11863          Symbol classSym = FindClass(spec.name);
11864          if(classSym && classSym.registered && classSym.registered.type == structClass)
11865             DeclareStruct(spec.name, false, true);
11866          break;
11867       }
11868       */
11869    }
11870 }
11871
11872
11873 static void ProcessDeclarator(Declarator decl, bool isFunction)
11874 {
11875    switch(decl.type)
11876    {
11877       case identifierDeclarator:
11878          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11879          {
11880             FreeSpecifier(decl.identifier._class);
11881             decl.identifier._class = null;
11882          }
11883          break;
11884       case arrayDeclarator:
11885          if(decl.array.exp)
11886             ProcessExpressionType(decl.array.exp);
11887       case structDeclarator:
11888       case bracketsDeclarator:
11889       case functionDeclarator:
11890       case pointerDeclarator:
11891       case extendedDeclarator:
11892       case extendedDeclaratorEnd:
11893       {
11894          Identifier id = null;
11895          Specifier classSpec = null;
11896          if(decl.type == functionDeclarator)
11897          {
11898             id = GetDeclId(decl);
11899             if(id && id._class)
11900             {
11901                classSpec = id._class;
11902                id._class = null;
11903             }
11904          }
11905          if(decl.declarator)
11906             ProcessDeclarator(decl.declarator, isFunction);
11907          if(decl.type == functionDeclarator)
11908          {
11909             if(classSpec)
11910             {
11911                TypeName param
11912                {
11913                   qualifiers = MkListOne(classSpec);
11914                   declarator = null;
11915                };
11916                if(!decl.function.parameters)
11917                   decl.function.parameters = MkList();
11918                decl.function.parameters->Insert(null, param);
11919             }
11920             if(decl.function.parameters)
11921             {
11922                TypeName param;
11923
11924                for(param = decl.function.parameters->first; param; param = param.next)
11925                {
11926                   if(param.qualifiers)
11927                   {
11928                      Specifier spec;
11929                      for(spec = param.qualifiers->first; spec; spec = spec.next)
11930                      {
11931                         if(spec.type == baseSpecifier)
11932                         {
11933                            if(spec.specifier == TYPED_OBJECT)
11934                            {
11935                               Declarator d = param.declarator;
11936                               TypeName newParam
11937                               {
11938                                  qualifiers = MkListOne(MkSpecifier(VOID));
11939                                  declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11940                               };
11941                               if(!d || d.type != pointerDeclarator)
11942                                  newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11943
11944                               FreeList(param.qualifiers, FreeSpecifier);
11945
11946                               param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11947                               param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11948
11949                               DeclareStruct(curExternal, "ecere::com::Class", false, true);
11950
11951                               decl.function.parameters->Insert(param, newParam);
11952                               param = newParam;
11953                               break;
11954                            }
11955                            else if(spec.specifier == ANY_OBJECT)
11956                            {
11957                               Declarator d = param.declarator;
11958
11959                               FreeList(param.qualifiers, FreeSpecifier);
11960
11961                               param.qualifiers = MkListOne(MkSpecifier(VOID));
11962                               if(!d || d.type != pointerDeclarator)
11963                                  param.qualifiers->Insert(null, MkSpecifier(CONST));
11964                               param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11965                               break;
11966                            }
11967                            else if(spec.specifier == THISCLASS)
11968                            {
11969                               if(thisClass)
11970                               {
11971                                  spec.type = nameSpecifier;
11972                                  spec.name = ReplaceThisClass(thisClass);
11973                                  spec.symbol = FindClass(spec.name);
11974                                  ProcessSpecifier(spec, false, false);
11975                               }
11976                               break;
11977                            }
11978                         }
11979                         else if(spec.type == nameSpecifier)
11980                         {
11981                            ProcessSpecifier(spec, isFunction, true);
11982                         }
11983                         else if((spec.type == structSpecifier || spec.type == unionSpecifier) && !spec.definitions && spec.id && spec.id.string)
11984                         {
11985                            Declarator d = param.declarator;
11986                            if(!d || d.type != pointerDeclarator)
11987                               DeclareStruct(curExternal, spec.id.string, false, true);
11988                         }
11989                      }
11990                   }
11991
11992                   if(param.declarator)
11993                      ProcessDeclarator(param.declarator, false);
11994                }
11995             }
11996          }
11997          break;
11998       }
11999    }
12000 }
12001
12002 static void ProcessDeclaration(Declaration decl, bool warnClasses)
12003 {
12004    yylloc = decl.loc;
12005    switch(decl.type)
12006    {
12007       case initDeclaration:
12008       {
12009          bool declareStruct = false;
12010          /*
12011          lineNum = decl.pos.line;
12012          column = decl.pos.col;
12013          */
12014
12015          if(decl.declarators)
12016          {
12017             InitDeclarator d;
12018
12019             for(d = decl.declarators->first; d; d = d.next)
12020             {
12021                Type type, subType;
12022                ProcessDeclarator(d.declarator, false);
12023
12024                type = ProcessType(decl.specifiers, d.declarator);
12025
12026                if(d.initializer)
12027                {
12028                   ProcessInitializer(d.initializer, type);
12029
12030                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
12031
12032                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
12033                      d.initializer.exp.type == instanceExp)
12034                   {
12035                      if(type.kind == classType && type._class ==
12036                         d.initializer.exp.expType._class)
12037                      {
12038                         Instantiation inst = d.initializer.exp.instance;
12039                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
12040
12041                         d.initializer.exp.instance = null;
12042                         if(decl.specifiers)
12043                            FreeList(decl.specifiers, FreeSpecifier);
12044                         FreeList(decl.declarators, FreeInitDeclarator);
12045
12046                         d = null;
12047
12048                         decl.type = instDeclaration;
12049                         decl.inst = inst;
12050                      }
12051                   }
12052                }
12053                for(subType = type; subType;)
12054                {
12055                   if(subType.kind == classType)
12056                   {
12057                      declareStruct = true;
12058                      break;
12059                   }
12060                   else if(subType.kind == pointerType)
12061                      break;
12062                   else if(subType.kind == arrayType)
12063                      subType = subType.arrayType;
12064                   else
12065                      break;
12066                }
12067
12068                FreeType(type);
12069                if(!d) break;
12070             }
12071          }
12072
12073          if(decl.specifiers)
12074          {
12075             Specifier s;
12076             for(s = decl.specifiers->first; s; s = s.next)
12077             {
12078                ProcessSpecifier(s, declareStruct, true);
12079             }
12080          }
12081          break;
12082       }
12083       case instDeclaration:
12084       {
12085          ProcessInstantiationType(decl.inst);
12086          break;
12087       }
12088       case structDeclaration:
12089       {
12090          Specifier spec;
12091          Declarator d;
12092          bool declareStruct = false;
12093
12094          if(decl.declarators)
12095          {
12096             for(d = decl.declarators->first; d; d = d.next)
12097             {
12098                Type type = ProcessType(decl.specifiers, d.declarator);
12099                Type subType;
12100                ProcessDeclarator(d, false);
12101                for(subType = type; subType;)
12102                {
12103                   if(subType.kind == classType)
12104                   {
12105                      declareStruct = true;
12106                      break;
12107                   }
12108                   else if(subType.kind == pointerType)
12109                      break;
12110                   else if(subType.kind == arrayType)
12111                      subType = subType.arrayType;
12112                   else
12113                      break;
12114                }
12115                FreeType(type);
12116             }
12117          }
12118          if(decl.specifiers)
12119          {
12120             for(spec = decl.specifiers->first; spec; spec = spec.next)
12121                ProcessSpecifier(spec, declareStruct, warnClasses);
12122          }
12123          break;
12124       }
12125    }
12126 }
12127
12128 static FunctionDefinition curFunction;
12129
12130 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
12131 {
12132    char propName[1024], propNameM[1024];
12133    char getName[1024], setName[1024];
12134    OldList * args;
12135
12136    DeclareProperty(curExternal, prop, setName, getName);
12137
12138    // eInstance_FireWatchers(object, prop);
12139    strcpy(propName, "__ecereProp_");
12140    FullClassNameCat(propName, prop._class.fullName, false);
12141    strcat(propName, "_");
12142    FullClassNameCat(propName, prop.name, true);
12143
12144    strcpy(propNameM, "__ecerePropM_");
12145    FullClassNameCat(propNameM, prop._class.fullName, false);
12146    strcat(propNameM, "_");
12147    FullClassNameCat(propNameM, prop.name, true);
12148
12149    if(prop.isWatchable)
12150    {
12151       args = MkList();
12152       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
12153       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12154       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
12155
12156       args = MkList();
12157       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
12158       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
12159       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
12160
12161       DeclareFunctionUtil(curExternal, "eInstance_FireWatchers");
12162    }
12163
12164    {
12165       args = MkList();
12166       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
12167       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12168       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
12169
12170       args = MkList();
12171       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
12172       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
12173       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
12174
12175       DeclareFunctionUtil(curExternal, "eInstance_FireSelfWatchers");
12176    }
12177
12178    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
12179       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12180       curFunction.propSet.fireWatchersDone = true;
12181 }
12182
12183 static void ProcessStatement(Statement stmt)
12184 {
12185    yylloc = stmt.loc;
12186    /*
12187    lineNum = stmt.pos.line;
12188    column = stmt.pos.col;
12189    */
12190    switch(stmt.type)
12191    {
12192       case labeledStmt:
12193          ProcessStatement(stmt.labeled.stmt);
12194          break;
12195       case caseStmt:
12196          // This expression should be constant...
12197          if(stmt.caseStmt.exp)
12198          {
12199             FreeType(stmt.caseStmt.exp.destType);
12200             stmt.caseStmt.exp.destType = curSwitchType;
12201             if(curSwitchType) curSwitchType.refCount++;
12202             ProcessExpressionType(stmt.caseStmt.exp);
12203             ComputeExpression(stmt.caseStmt.exp);
12204          }
12205          if(stmt.caseStmt.stmt)
12206             ProcessStatement(stmt.caseStmt.stmt);
12207          break;
12208       case compoundStmt:
12209       {
12210          if(stmt.compound.context)
12211          {
12212             Declaration decl;
12213             Statement s;
12214
12215             Statement prevCompound = curCompound;
12216             Context prevContext = curContext;
12217
12218             if(!stmt.compound.isSwitch)
12219                curCompound = stmt;
12220             curContext = stmt.compound.context;
12221
12222             if(stmt.compound.declarations)
12223             {
12224                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
12225                   ProcessDeclaration(decl, true);
12226             }
12227             if(stmt.compound.statements)
12228             {
12229                for(s = stmt.compound.statements->first; s; s = s.next)
12230                   ProcessStatement(s);
12231             }
12232
12233             curContext = prevContext;
12234             curCompound = prevCompound;
12235          }
12236          break;
12237       }
12238       case expressionStmt:
12239       {
12240          Expression exp;
12241          if(stmt.expressions)
12242          {
12243             for(exp = stmt.expressions->first; exp; exp = exp.next)
12244                ProcessExpressionType(exp);
12245          }
12246          break;
12247       }
12248       case ifStmt:
12249       {
12250          Expression exp;
12251
12252          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
12253          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
12254          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
12255          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
12256          {
12257             ProcessExpressionType(exp);
12258          }
12259          if(stmt.ifStmt.stmt)
12260             ProcessStatement(stmt.ifStmt.stmt);
12261          if(stmt.ifStmt.elseStmt)
12262             ProcessStatement(stmt.ifStmt.elseStmt);
12263          break;
12264       }
12265       case switchStmt:
12266       {
12267          Type oldSwitchType = curSwitchType;
12268          if(stmt.switchStmt.exp)
12269          {
12270             Expression exp;
12271             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
12272             {
12273                if(!exp.next)
12274                {
12275                   /*
12276                   Type destType
12277                   {
12278                      kind = intType;
12279                      refCount = 1;
12280                   };
12281                   e.exp.destType = destType;
12282                   */
12283
12284                   ProcessExpressionType(exp);
12285                }
12286                if(!exp.next)
12287                   curSwitchType = exp.expType;
12288             }
12289          }
12290          ProcessStatement(stmt.switchStmt.stmt);
12291          curSwitchType = oldSwitchType;
12292          break;
12293       }
12294       case whileStmt:
12295       {
12296          if(stmt.whileStmt.exp)
12297          {
12298             Expression exp;
12299
12300             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
12301             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
12302             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
12303             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
12304             {
12305                ProcessExpressionType(exp);
12306             }
12307          }
12308          if(stmt.whileStmt.stmt)
12309             ProcessStatement(stmt.whileStmt.stmt);
12310          break;
12311       }
12312       case doWhileStmt:
12313       {
12314          if(stmt.doWhile.exp)
12315          {
12316             Expression exp;
12317
12318             if(stmt.doWhile.exp->last)
12319             {
12320                FreeType(((Expression)stmt.doWhile.exp->last).destType);
12321                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
12322                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
12323             }
12324             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
12325             {
12326                ProcessExpressionType(exp);
12327             }
12328          }
12329          if(stmt.doWhile.stmt)
12330             ProcessStatement(stmt.doWhile.stmt);
12331          break;
12332       }
12333       case forStmt:
12334       {
12335          Expression exp;
12336          if(stmt.forStmt.init)
12337             ProcessStatement(stmt.forStmt.init);
12338
12339          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
12340          {
12341             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
12342             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
12343             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
12344          }
12345
12346          if(stmt.forStmt.check)
12347             ProcessStatement(stmt.forStmt.check);
12348          if(stmt.forStmt.increment)
12349          {
12350             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
12351                ProcessExpressionType(exp);
12352          }
12353
12354          if(stmt.forStmt.stmt)
12355             ProcessStatement(stmt.forStmt.stmt);
12356          break;
12357       }
12358       case forEachStmt:
12359       {
12360          Identifier id = stmt.forEachStmt.id;
12361          OldList * exp = stmt.forEachStmt.exp;
12362          OldList * filter = stmt.forEachStmt.filter;
12363          Statement block = stmt.forEachStmt.stmt;
12364          char iteratorType[1024];
12365          Type source;
12366          Expression e;
12367          bool isBuiltin = exp && exp->last &&
12368             (((Expression)exp->last).type == ExpressionType::arrayExp ||
12369               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
12370          Expression arrayExp;
12371          const char * typeString = null;
12372          int builtinCount = 0;
12373
12374          for(e = exp ? exp->first : null; e; e = e.next)
12375          {
12376             if(!e.next)
12377             {
12378                FreeType(e.destType);
12379                e.destType = ProcessTypeString("Container", false);
12380             }
12381             if(!isBuiltin || e.next)
12382                ProcessExpressionType(e);
12383          }
12384
12385          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
12386          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
12387             eClass_IsDerived(source._class.registered, containerClass)))
12388          {
12389             Class _class = source ? source._class.registered : null;
12390             Symbol symbol;
12391             Expression expIt = null;
12392             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
12393             // TODO: Find these once on loadup
12394             Class arrayClass = eSystem_FindClass(privateModule, "Array");
12395             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
12396             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
12397
12398             if(inCompiler)
12399             {
12400                stmt.type = compoundStmt;
12401
12402                stmt.compound.context = Context { };
12403                stmt.compound.context.parent = curContext;
12404                curContext = stmt.compound.context;
12405             }
12406
12407             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
12408             {
12409                Class mapClass = eSystem_FindClass(privateModule, "Map");
12410                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
12411                isCustomAVLTree = true;
12412                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
12413                   isAVLTree = true;
12414                else */if(eClass_IsDerived(source._class.registered, mapClass))
12415                   isMap = true;
12416             }
12417             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
12418             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
12419             {
12420                Class listClass = eSystem_FindClass(privateModule, "List");
12421                isLinkList = true;
12422                isList = eClass_IsDerived(source._class.registered, listClass);
12423             }
12424
12425             if(inCompiler && isArray)
12426             {
12427                Declarator decl;
12428                OldList * specs = MkList();
12429                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12430                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12431                stmt.compound.declarations = MkListOne(
12432                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12433                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12434                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
12435                      MkInitializerAssignment(MkExpBrackets(exp))))));
12436             }
12437             else if(isBuiltin)
12438             {
12439                Type type = null;
12440                char typeStringBuf[1024];
12441
12442                // TODO: Merge this code?
12443                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
12444                if(((Expression)exp->last).type == castExp)
12445                {
12446                   TypeName typeName = ((Expression)exp->last).cast.typeName;
12447                   if(typeName)
12448                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
12449                }
12450
12451                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
12452                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
12453                   arrayExp.destType._class.registered.templateArgs)
12454                {
12455                   Class templateClass = arrayExp.destType._class.registered;
12456                   typeString = templateClass.templateArgs[2].dataTypeString;
12457                }
12458                else if(arrayExp.list)
12459                {
12460                   // Guess type from expressions in the array
12461                   Expression e;
12462                   for(e = arrayExp.list->first; e; e = e.next)
12463                   {
12464                      ProcessExpressionType(e);
12465                      if(e.expType)
12466                      {
12467                         if(!type) { type = e.expType; type.refCount++; }
12468                         else
12469                         {
12470                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
12471                            if(!MatchTypeExpression(e, type, null, false, true))
12472                            {
12473                               FreeType(type);
12474                               type = e.expType;
12475                               e.expType = null;
12476
12477                               e = arrayExp.list->first;
12478                               ProcessExpressionType(e);
12479                               if(e.expType)
12480                               {
12481                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
12482                                  if(!MatchTypeExpression(e, type, null, false, true))
12483                                  {
12484                                     FreeType(e.expType);
12485                                     e.expType = null;
12486                                     FreeType(type);
12487                                     type = null;
12488                                     break;
12489                                  }
12490                               }
12491                            }
12492                         }
12493                         if(e.expType)
12494                         {
12495                            FreeType(e.expType);
12496                            e.expType = null;
12497                         }
12498                      }
12499                   }
12500                   if(type)
12501                   {
12502                      typeStringBuf[0] = '\0';
12503                      PrintType(type, typeStringBuf, false, true);
12504                      typeString = typeStringBuf;
12505                      FreeType(type);
12506                   }
12507                }
12508                if(typeString)
12509                {
12510                   if(inCompiler)
12511                   {
12512                      OldList * initializers = MkList();
12513                      Declarator decl;
12514                      OldList * specs = MkList();
12515                      if(arrayExp.list)
12516                      {
12517                         Expression e;
12518
12519                         builtinCount = arrayExp.list->count;
12520                         type = ProcessTypeString(typeString, false);
12521                         while((e = arrayExp.list->first))
12522                         {
12523                            arrayExp.list->Remove(e);
12524                            e.destType = type;
12525                            type.refCount++;
12526                            ProcessExpressionType(e);
12527                            if(inCompiler)
12528                               ListAdd(initializers, MkInitializerAssignment(e));
12529                         }
12530                         FreeType(type);
12531                         delete arrayExp.list;
12532                      }
12533                      decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12534
12535                      stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12536                         MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12537
12538                      ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12539                         PlugDeclarator(
12540                            /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12541                            ), MkInitializerList(initializers)))));
12542                      FreeList(exp, FreeExpression);
12543                   }
12544                   else if(arrayExp.list)
12545                   {
12546                      Expression e;
12547                      type = ProcessTypeString(typeString, false);
12548                      for(e = arrayExp.list->first; e; e = e.next)
12549                      {
12550                         e.destType = type;
12551                         type.refCount++;
12552                         ProcessExpressionType(e);
12553                      }
12554                      FreeType(type);
12555                   }
12556                }
12557                else
12558                {
12559                   arrayExp.expType = ProcessTypeString("Container", false);
12560                   Compiler_Error($"Couldn't determine type of array elements\n");
12561                }
12562
12563                /*
12564                Declarator decl;
12565                OldList * specs = MkList();
12566
12567                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12568                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12569                stmt.compound.declarations = MkListOne(
12570                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12571                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12572                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12573                      MkInitializerAssignment(MkExpBrackets(exp))))));
12574                */
12575             }
12576             else if(inCompiler && isLinkList && !isList)
12577             {
12578                Declarator decl;
12579                OldList * specs = MkList();
12580                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12581                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12582                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12583                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12584                      MkInitializerAssignment(MkExpBrackets(exp))))));
12585             }
12586             /*else if(isCustomAVLTree)
12587             {
12588                Declarator decl;
12589                OldList * specs = MkList();
12590                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12591                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12592                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12593                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12594                      MkInitializerAssignment(MkExpBrackets(exp))))));
12595             }*/
12596             else if(inCompiler && _class.templateArgs)
12597             {
12598                if(isMap)
12599                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12600                else
12601                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12602
12603                stmt.compound.declarations = MkListOne(
12604                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12605                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12606                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12607             }
12608             if(inCompiler)
12609             {
12610                symbol = FindSymbol(id.string, curContext, curContext, false, false);
12611
12612                if(block)
12613                {
12614                   // Reparent sub-contexts in this statement
12615                   switch(block.type)
12616                   {
12617                      case compoundStmt:
12618                         if(block.compound.context)
12619                            block.compound.context.parent = stmt.compound.context;
12620                         break;
12621                      case ifStmt:
12622                         if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12623                            block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12624                         if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12625                            block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12626                         break;
12627                      case switchStmt:
12628                         if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12629                            block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12630                         break;
12631                      case whileStmt:
12632                         if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12633                            block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12634                         break;
12635                      case doWhileStmt:
12636                         if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12637                            block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12638                         break;
12639                      case forStmt:
12640                         if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12641                            block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12642                         break;
12643                      case forEachStmt:
12644                         if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12645                            block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12646                         break;
12647                      /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12648                      case labeledStmt:
12649                      case caseStmt
12650                      case expressionStmt:
12651                      case gotoStmt:
12652                      case continueStmt:
12653                      case breakStmt
12654                      case returnStmt:
12655                      case asmStmt:
12656                      case badDeclarationStmt:
12657                      case fireWatchersStmt:
12658                      case stopWatchingStmt:
12659                      case watchStmt:
12660                      */
12661                   }
12662                }
12663
12664                if(filter)
12665                {
12666                   block = MkIfStmt(filter, block, null);
12667                }
12668                if(isArray)
12669                {
12670                   stmt.compound.statements = MkListOne(MkForStmt(
12671                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12672                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12673                         MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12674                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12675                      block));
12676                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12677                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12678                  ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12679                }
12680                else if(isBuiltin)
12681                {
12682                   char count[128];
12683                   //OldList * specs = MkList();
12684                   // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12685
12686                   sprintf(count, "%d", builtinCount);
12687
12688                   stmt.compound.statements = MkListOne(MkForStmt(
12689                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12690                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12691                         MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12692                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12693                      block));
12694
12695                   /*
12696                   Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12697                   stmt.compound.statements = MkListOne(MkForStmt(
12698                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12699                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12700                         MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12701                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12702                      block));
12703                  */
12704                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12705                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12706                  ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12707                }
12708                else if(isLinkList && !isList)
12709                {
12710                   Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12711                   Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12712                   if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12713                      !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12714                   {
12715                      stmt.compound.statements = MkListOne(MkForStmt(
12716                         MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12717                         MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12718                         MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12719                         block));
12720                   }
12721                   else
12722                   {
12723                      OldList * specs = MkList();
12724                      Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12725                      stmt.compound.statements = MkListOne(MkForStmt(
12726                         MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12727                         MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12728                         MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12729                            MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12730                               MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12731                         block));
12732                   }
12733                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12734                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12735                   ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12736                }
12737                /*else if(isCustomAVLTree)
12738                {
12739                   stmt.compound.statements = MkListOne(MkForStmt(
12740                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12741                         MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12742                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12743                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12744                      block));
12745
12746                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12747                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12748                   ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12749                }*/
12750                else
12751                {
12752                   stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12753                      MkIdentifier("Next")), null)), block));
12754                }
12755                ProcessExpressionType(expIt);
12756                if(stmt.compound.declarations->first)
12757                   ProcessDeclaration(stmt.compound.declarations->first, true);
12758
12759                if(symbol)
12760                   symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12761
12762                ProcessStatement(stmt);
12763             }
12764             else
12765                ProcessStatement(stmt.forEachStmt.stmt);
12766             if(inCompiler)
12767                curContext = stmt.compound.context.parent;
12768             break;
12769          }
12770          else
12771          {
12772             Compiler_Error($"Expression is not a container\n");
12773          }
12774          break;
12775       }
12776       case gotoStmt:
12777          break;
12778       case continueStmt:
12779          break;
12780       case breakStmt:
12781          break;
12782       case returnStmt:
12783       {
12784          Expression exp;
12785          if(stmt.expressions)
12786          {
12787             for(exp = stmt.expressions->first; exp; exp = exp.next)
12788             {
12789                if(!exp.next)
12790                {
12791                   if(curFunction && !curFunction.type)
12792                      curFunction.type = ProcessType(
12793                         curFunction.specifiers, curFunction.declarator);
12794                   FreeType(exp.destType);
12795                   // TODO: current property if not compiling
12796                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12797                   if(exp.destType) exp.destType.refCount++;
12798                }
12799                ProcessExpressionType(exp);
12800             }
12801          }
12802          break;
12803       }
12804       case badDeclarationStmt:
12805       {
12806          ProcessDeclaration(stmt.decl, true);
12807          break;
12808       }
12809       case asmStmt:
12810       {
12811          AsmField field;
12812          if(stmt.asmStmt.inputFields)
12813          {
12814             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12815                if(field.expression)
12816                   ProcessExpressionType(field.expression);
12817          }
12818          if(stmt.asmStmt.outputFields)
12819          {
12820             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12821                if(field.expression)
12822                   ProcessExpressionType(field.expression);
12823          }
12824          if(stmt.asmStmt.clobberedFields)
12825          {
12826             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12827             {
12828                if(field.expression)
12829                   ProcessExpressionType(field.expression);
12830             }
12831          }
12832          break;
12833       }
12834       case watchStmt:
12835       {
12836          PropertyWatch propWatch;
12837          OldList * watches = stmt._watch.watches;
12838          Expression object = stmt._watch.object;
12839          Expression watcher = stmt._watch.watcher;
12840          if(watcher)
12841             ProcessExpressionType(watcher);
12842          if(object)
12843             ProcessExpressionType(object);
12844
12845          if(inCompiler)
12846          {
12847             if(watcher || thisClass)
12848             {
12849                External external = curExternal;
12850                Context context = curContext;
12851
12852                stmt.type = expressionStmt;
12853                stmt.expressions = MkList();
12854
12855                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12856                {
12857                   ClassFunction func;
12858                   char watcherName[1024];
12859                   Class watcherClass = watcher ?
12860                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12861                   External createdExternal;
12862
12863                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12864                   if(propWatch.deleteWatch)
12865                      strcat(watcherName, "_delete");
12866                   else
12867                   {
12868                      Identifier propID;
12869                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12870                      {
12871                         strcat(watcherName, "_");
12872                         strcat(watcherName, propID.string);
12873                      }
12874                   }
12875
12876                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12877                   {
12878                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12879                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12880                      ProcessClassFunctionBody(func, propWatch.compound);
12881                      propWatch.compound = null;
12882
12883                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12884
12885                      FreeClassFunction(func);
12886
12887                      curExternal = createdExternal;
12888                      ProcessFunction(createdExternal.function);
12889
12890                      if(propWatch.deleteWatch)
12891                      {
12892                         OldList * args = MkList();
12893                         ListAdd(args, CopyExpression(object));
12894                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12895                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12896                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12897                      }
12898                      else
12899                      {
12900                         Class _class = object.expType._class.registered;
12901                         Identifier propID;
12902
12903                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12904                         {
12905                            char propName[1024];
12906                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12907                            if(prop)
12908                            {
12909                               char getName[1024], setName[1024];
12910                               OldList * args = MkList();
12911
12912                               DeclareProperty(createdExternal, prop, setName, getName);
12913
12914                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12915                               strcpy(propName, "__ecereProp_");
12916                               FullClassNameCat(propName, prop._class.fullName, false);
12917                               strcat(propName, "_");
12918                               FullClassNameCat(propName, prop.name, true);
12919
12920                               ListAdd(args, CopyExpression(object));
12921                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12922                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12923                               ListAdd(args, MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpIdentifier(MkIdentifier(watcherName))));
12924
12925                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12926
12927                               external.CreateUniqueEdge(createdExternal, true);
12928                            }
12929                            else
12930                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12931                         }
12932                      }
12933                   }
12934                   else
12935                      Compiler_Error($"Invalid watched object\n");
12936                }
12937
12938                curExternal = external;
12939                curContext = context;
12940
12941                if(watcher)
12942                   FreeExpression(watcher);
12943                if(object)
12944                   FreeExpression(object);
12945                FreeList(watches, FreePropertyWatch);
12946             }
12947             else
12948                Compiler_Error($"No observer specified and not inside a class\n");
12949          }
12950          else
12951          {
12952             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12953             {
12954                ProcessStatement(propWatch.compound);
12955             }
12956
12957          }
12958          break;
12959       }
12960       case fireWatchersStmt:
12961       {
12962          OldList * watches = stmt._watch.watches;
12963          Expression object = stmt._watch.object;
12964          Class _class;
12965          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12966          // printf("%X\n", watches);
12967          // printf("%X\n", stmt._watch.watches);
12968          if(object)
12969             ProcessExpressionType(object);
12970
12971          if(inCompiler)
12972          {
12973             _class = object ?
12974                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12975
12976             if(_class)
12977             {
12978                Identifier propID;
12979
12980                stmt.type = expressionStmt;
12981                stmt.expressions = MkList();
12982
12983                // Check if we're inside a property set
12984                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12985                {
12986                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12987                }
12988                else if(!watches)
12989                {
12990                   //Compiler_Error($"No property specified and not inside a property set\n");
12991                }
12992                if(watches)
12993                {
12994                   for(propID = watches->first; propID; propID = propID.next)
12995                   {
12996                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12997                      if(prop)
12998                      {
12999                         CreateFireWatcher(prop, object, stmt);
13000                      }
13001                      else
13002                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
13003                   }
13004                }
13005                else
13006                {
13007                   // Fire all properties!
13008                   Property prop;
13009                   Class base;
13010                   for(base = _class; base; base = base.base)
13011                   {
13012                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
13013                      {
13014                         if(prop.isProperty && prop.isWatchable)
13015                         {
13016                            CreateFireWatcher(prop, object, stmt);
13017                         }
13018                      }
13019                   }
13020                }
13021
13022                if(object)
13023                   FreeExpression(object);
13024                FreeList(watches, FreeIdentifier);
13025             }
13026             else
13027                Compiler_Error($"Invalid object specified and not inside a class\n");
13028          }
13029          break;
13030       }
13031       case stopWatchingStmt:
13032       {
13033          OldList * watches = stmt._watch.watches;
13034          Expression object = stmt._watch.object;
13035          Expression watcher = stmt._watch.watcher;
13036          Class _class;
13037          if(object)
13038             ProcessExpressionType(object);
13039          if(watcher)
13040             ProcessExpressionType(watcher);
13041          if(inCompiler)
13042          {
13043             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
13044
13045             if(watcher || thisClass)
13046             {
13047                if(_class)
13048                {
13049                   Identifier propID;
13050
13051                   stmt.type = expressionStmt;
13052                   stmt.expressions = MkList();
13053
13054                   if(!watches)
13055                   {
13056                      OldList * args;
13057                      // eInstance_StopWatching(object, null, watcher);
13058                      args = MkList();
13059                      ListAdd(args, CopyExpression(object));
13060                      ListAdd(args, MkExpConstant("0"));
13061                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
13062                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
13063                   }
13064                   else
13065                   {
13066                      for(propID = watches->first; propID; propID = propID.next)
13067                      {
13068                         char propName[1024];
13069                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
13070                         if(prop)
13071                         {
13072                            char getName[1024], setName[1024];
13073                            OldList * args = MkList();
13074
13075                            DeclareProperty(curExternal, prop, setName, getName);
13076
13077                            // eInstance_StopWatching(object, prop, watcher);
13078                            strcpy(propName, "__ecereProp_");
13079                            FullClassNameCat(propName, prop._class.fullName, false);
13080                            strcat(propName, "_");
13081                            FullClassNameCat(propName, prop.name, true);
13082
13083                            ListAdd(args, CopyExpression(object));
13084                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
13085                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
13086                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
13087                         }
13088                         else
13089                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
13090                      }
13091                   }
13092
13093                   if(object)
13094                      FreeExpression(object);
13095                   if(watcher)
13096                      FreeExpression(watcher);
13097                   FreeList(watches, FreeIdentifier);
13098                }
13099                else
13100                   Compiler_Error($"Invalid object specified and not inside a class\n");
13101             }
13102             else
13103                Compiler_Error($"No observer specified and not inside a class\n");
13104          }
13105          break;
13106       }
13107    }
13108 }
13109
13110 static void ProcessFunction(FunctionDefinition function)
13111 {
13112    Identifier id = GetDeclId(function.declarator);
13113    Symbol symbol = function.declarator ? function.declarator.symbol : null;
13114    Type type = symbol ? symbol.type : null;
13115    Class oldThisClass = thisClass;
13116    Context oldTopContext = topContext;
13117
13118    yylloc = function.loc;
13119    // Process thisClass
13120
13121    if(type && type.thisClass)
13122    {
13123       Symbol classSym = type.thisClass;
13124       Class _class = type.thisClass.registered;
13125       char className[1024];
13126       char structName[1024];
13127       Declarator funcDecl;
13128       Symbol thisSymbol;
13129
13130       bool typedObject = false;
13131
13132       if(_class && !_class.base)
13133       {
13134          _class = currentClass;
13135          if(_class && !_class.symbol)
13136             _class.symbol = FindClass(_class.fullName);
13137          classSym = _class ? _class.symbol : null;
13138          typedObject = true;
13139       }
13140
13141       thisClass = _class;
13142
13143       if(inCompiler && _class)
13144       {
13145          if(type.kind == functionType)
13146          {
13147             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
13148             {
13149                //TypeName param = symbol.type.params.first;
13150                Type param = symbol.type.params.first;
13151                symbol.type.params.Remove(param);
13152                //FreeTypeName(param);
13153                FreeType(param);
13154             }
13155             if(type.classObjectType != classPointer)
13156             {
13157                symbol.type.params.Insert(null, MkClassType(_class.fullName));
13158                symbol.type.staticMethod = true;
13159                symbol.type.thisClass = null;
13160
13161                // HIGH DANGER: VERIFYING THIS...
13162                symbol.type.extraParam = false;
13163             }
13164          }
13165
13166          strcpy(className, "__ecereClass_");
13167          FullClassNameCat(className, _class.fullName, true);
13168
13169          structName[0] = 0;
13170          FullClassNameCat(structName, _class.fullName, false);
13171
13172          // [class] this
13173          funcDecl = GetFuncDecl(function.declarator);
13174          if(funcDecl)
13175          {
13176             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
13177             {
13178                TypeName param = funcDecl.function.parameters->first;
13179                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
13180                {
13181                   funcDecl.function.parameters->Remove(param);
13182                   FreeTypeName(param);
13183                }
13184             }
13185
13186             if(!function.propertyNoThis)
13187             {
13188                TypeName thisParam = null;
13189
13190                if(type.classObjectType != classPointer)
13191                {
13192                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
13193                   if(!funcDecl.function.parameters)
13194                      funcDecl.function.parameters = MkList();
13195                   funcDecl.function.parameters->Insert(null, thisParam);
13196                }
13197
13198                if(typedObject)
13199                {
13200                   if(type.classObjectType != classPointer)
13201                   {
13202                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
13203                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
13204                   }
13205
13206                   thisParam = TypeName
13207                   {
13208                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
13209                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
13210                   };
13211                   DeclareStruct(curExternal, "ecere::com::Class", false, true);
13212                   funcDecl.function.parameters->Insert(null, thisParam);
13213                }
13214             }
13215          }
13216
13217          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
13218          {
13219             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
13220             funcDecl = GetFuncDecl(initDecl.declarator);
13221             if(funcDecl)
13222             {
13223                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
13224                {
13225                   TypeName param = funcDecl.function.parameters->first;
13226                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
13227                   {
13228                      funcDecl.function.parameters->Remove(param);
13229                      FreeTypeName(param);
13230                   }
13231                }
13232
13233                if(type.classObjectType != classPointer)
13234                {
13235                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
13236                   {
13237                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
13238
13239                      if(!funcDecl.function.parameters)
13240                         funcDecl.function.parameters = MkList();
13241                      funcDecl.function.parameters->Insert(null, thisParam);
13242                   }
13243                }
13244             }
13245          }
13246       }
13247
13248       // Add this to the context
13249       if(function.body)
13250       {
13251          if(type.classObjectType != classPointer)
13252          {
13253             thisSymbol = Symbol
13254             {
13255                string = CopyString("this");
13256                type = classSym ? MkClassType(classSym.string) : null;
13257             };
13258             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
13259
13260             if(typedObject && thisSymbol.type)
13261             {
13262                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
13263                thisSymbol.type.byReference = type.byReference;
13264                thisSymbol.type.typedByReference = type.byReference;
13265             }
13266          }
13267       }
13268
13269       // Pointer to class data
13270       if(inCompiler && _class && _class.type == normalClass && type.classObjectType != classPointer)
13271       {
13272          DataMember member = null;
13273          {
13274             Class base;
13275             for(base = _class; base && base.type != systemClass; base = base.next)
13276             {
13277                for(member = base.membersAndProperties.first; member; member = member.next)
13278                   if(!member.isProperty)
13279                      break;
13280                if(member)
13281                   break;
13282             }
13283          }
13284          for(member = _class.membersAndProperties.first; member; member = member.next)
13285             if(!member.isProperty)
13286                break;
13287          if(member)
13288          {
13289             char pointerName[1024];
13290
13291             Declaration decl;
13292             Initializer initializer;
13293             Expression exp, bytePtr;
13294
13295             strcpy(pointerName, "__ecerePointer_");
13296             FullClassNameCat(pointerName, _class.fullName, false);
13297             {
13298                char className[1024];
13299                strcpy(className, "__ecereClass_");
13300                FullClassNameCat(className, classSym.string, true);
13301
13302                DeclareClass(curExternal, classSym, className);
13303             }
13304
13305             // ((byte *) this)
13306             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
13307
13308             if(_class.fixed)
13309             {
13310                Expression e;
13311                if(_class.offset && _class.offset == (_class.base.type == noHeadClass ? _class.base.memberOffset : _class.base.structSize))
13312                {
13313                   e = MkExpClassSize(MkSpecifierName(_class.base.fullName));
13314                   ProcessExpressionType(e);
13315                }
13316                else
13317                {
13318                   char string[256];
13319                   sprintf(string, "%d", _class.offset);  // Need Bootstrap Fix
13320                   e = MkExpConstant(string);
13321                }
13322                exp = QBrackets(MkExpOp(bytePtr, '+', e));
13323             }
13324             else
13325             {
13326                // ([bytePtr] + [className]->offset)
13327                exp = QBrackets(MkExpOp(bytePtr, '+',
13328                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
13329             }
13330
13331             // (this ? [exp] : 0)
13332             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
13333             exp.expType = Type
13334             {
13335                refCount = 1;
13336                kind = pointerType;
13337                type = Type { refCount = 1, kind = voidType };
13338             };
13339
13340             if(function.body)
13341             {
13342                yylloc = function.body.loc;
13343                // ([structName] *) [exp]
13344                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
13345                initializer = MkInitializerAssignment(
13346                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
13347
13348                // [structName] * [pointerName] = [initializer];
13349                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
13350
13351                {
13352                   Context prevContext = curContext;
13353                   OldList * list;
13354                   curContext = function.body.compound.context;
13355
13356                   decl = MkDeclaration((list = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null))),
13357                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
13358                   list->Insert(null, MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
13359
13360                   curContext = prevContext;
13361                }
13362
13363                // WHY?
13364                decl.symbol = null;
13365
13366                if(!function.body.compound.declarations)
13367                   function.body.compound.declarations = MkList();
13368                function.body.compound.declarations->Insert(null, decl);
13369             }
13370          }
13371       }
13372
13373
13374       // Loop through the function and replace undeclared identifiers
13375       // which are a member of the class (methods, properties or data)
13376       // by "this.[member]"
13377    }
13378    else
13379       thisClass = null;
13380
13381    if(id)
13382    {
13383       FreeSpecifier(id._class);
13384       id._class = null;
13385
13386       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
13387       {
13388          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
13389          id = GetDeclId(initDecl.declarator);
13390
13391          FreeSpecifier(id._class);
13392          id._class = null;
13393       }
13394    }
13395    if(function.body)
13396       topContext = function.body.compound.context;
13397    {
13398       FunctionDefinition oldFunction = curFunction;
13399       curFunction = function;
13400       if(function.body)
13401          ProcessStatement(function.body);
13402
13403       // If this is a property set and no firewatchers has been done yet, add one here
13404       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
13405       {
13406          Statement prevCompound = curCompound;
13407          Context prevContext = curContext;
13408
13409          Statement fireWatchers = MkFireWatchersStmt(null, null);
13410          if(!function.body.compound.statements) function.body.compound.statements = MkList();
13411          ListAdd(function.body.compound.statements, fireWatchers);
13412
13413          curCompound = function.body;
13414          curContext = function.body.compound.context;
13415
13416          ProcessStatement(fireWatchers);
13417
13418          curContext = prevContext;
13419          curCompound = prevCompound;
13420
13421       }
13422
13423       curFunction = oldFunction;
13424    }
13425
13426    if(function.declarator)
13427    {
13428       ProcessDeclarator(function.declarator, true);
13429    }
13430
13431    topContext = oldTopContext;
13432    thisClass = oldThisClass;
13433 }
13434
13435 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
13436 static void ProcessClass(OldList definitions, Symbol symbol)
13437 {
13438    ClassDef def;
13439    External external = curExternal;
13440    Class regClass = symbol ? symbol.registered : null;
13441
13442    // Process all functions
13443    for(def = definitions.first; def; def = def.next)
13444    {
13445       if(def.type == functionClassDef)
13446       {
13447          if(def.function.declarator)
13448             curExternal = def.function.declarator.symbol.pointerExternal;
13449          else
13450             curExternal = external;
13451
13452          ProcessFunction((FunctionDefinition)def.function);
13453       }
13454       else if(def.type == declarationClassDef)
13455       {
13456          if(def.decl.type == instDeclaration)
13457          {
13458             thisClass = regClass;
13459             ProcessInstantiationType(def.decl.inst);
13460             thisClass = null;
13461          }
13462          // Testing this
13463          else
13464          {
13465             Class backThisClass = thisClass;
13466             if(regClass) thisClass = regClass;
13467             ProcessDeclaration(def.decl, symbol ? true : false);
13468             thisClass = backThisClass;
13469          }
13470       }
13471       else if(def.type == defaultPropertiesClassDef && def.defProperties)
13472       {
13473          MemberInit defProperty;
13474
13475          // Add this to the context
13476          Symbol thisSymbol = Symbol
13477          {
13478             string = CopyString("this");
13479             type = regClass ? MkClassType(regClass.fullName) : null;
13480          };
13481          globalContext.symbols.Add((BTNode)thisSymbol);
13482
13483          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
13484          {
13485             thisClass = regClass;
13486             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
13487             thisClass = null;
13488          }
13489
13490          globalContext.symbols.Remove((BTNode)thisSymbol);
13491          FreeSymbol(thisSymbol);
13492       }
13493       else if(def.type == propertyClassDef && def.propertyDef)
13494       {
13495          PropertyDef prop = def.propertyDef;
13496
13497          // Add this to the context
13498          thisClass = regClass;
13499          if(prop.setStmt)
13500          {
13501             if(regClass)
13502             {
13503                Symbol thisSymbol
13504                {
13505                   string = CopyString("this");
13506                   type = MkClassType(regClass.fullName);
13507                };
13508                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13509             }
13510
13511             curExternal = prop.symbol ? prop.symbol.externalSet : null;
13512             ProcessStatement(prop.setStmt);
13513          }
13514          if(prop.getStmt)
13515          {
13516             if(regClass)
13517             {
13518                Symbol thisSymbol
13519                {
13520                   string = CopyString("this");
13521                   type = MkClassType(regClass.fullName);
13522                };
13523                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13524             }
13525
13526             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13527             ProcessStatement(prop.getStmt);
13528          }
13529          if(prop.issetStmt)
13530          {
13531             if(regClass)
13532             {
13533                Symbol thisSymbol
13534                {
13535                   string = CopyString("this");
13536                   type = MkClassType(regClass.fullName);
13537                };
13538                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13539             }
13540
13541             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13542             ProcessStatement(prop.issetStmt);
13543          }
13544
13545          thisClass = null;
13546       }
13547       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13548       {
13549          PropertyWatch propertyWatch = def.propertyWatch;
13550
13551          thisClass = regClass;
13552          if(propertyWatch.compound)
13553          {
13554             Symbol thisSymbol
13555             {
13556                string = CopyString("this");
13557                type = regClass ? MkClassType(regClass.fullName) : null;
13558             };
13559
13560             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13561
13562             curExternal = null;
13563             ProcessStatement(propertyWatch.compound);
13564          }
13565          thisClass = null;
13566       }
13567    }
13568 }
13569
13570 void DeclareFunctionUtil(External neededBy, const String s)
13571 {
13572    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13573    if(function)
13574    {
13575       char name[1024];
13576       name[0] = 0;
13577       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13578          strcpy(name, "__ecereFunction_");
13579       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13580       DeclareFunction(neededBy, function, name);
13581    }
13582    else if(neededBy)
13583       FindSymbol(s, globalContext, globalContext, false, false);
13584 }
13585
13586 bool reachedPass15;
13587
13588 void ComputeDataTypes()
13589 {
13590    External external;
13591
13592    currentClass = null;
13593
13594    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13595
13596    DeclareStruct(null, "ecere::com::Class", false, true);
13597    DeclareStruct(null, "ecere::com::Instance", false, true);
13598    DeclareStruct(null, "ecere::com::Property", false, true);
13599    DeclareStruct(null, "ecere::com::DataMember", false, true);
13600    DeclareStruct(null, "ecere::com::Method", false, true);
13601    DeclareStruct(null, "ecere::com::SerialBuffer", false, true);
13602    DeclareStruct(null, "ecere::com::ClassTemplateArgument", false, true);
13603
13604    DeclareFunctionUtil(null, "eSystem_New");
13605    DeclareFunctionUtil(null, "eSystem_New0");
13606    DeclareFunctionUtil(null, "eSystem_Renew");
13607    DeclareFunctionUtil(null, "eSystem_Renew0");
13608    DeclareFunctionUtil(null, "eSystem_Delete");
13609    DeclareFunctionUtil(null, "eClass_GetProperty");
13610    DeclareFunctionUtil(null, "eClass_SetProperty");
13611    DeclareFunctionUtil(null, "eInstance_FireSelfWatchers");
13612    DeclareFunctionUtil(null, "eInstance_SetMethod");
13613    DeclareFunctionUtil(null, "eInstance_IncRef");
13614    DeclareFunctionUtil(null, "eInstance_StopWatching");
13615    DeclareFunctionUtil(null, "eInstance_Watch");
13616    DeclareFunctionUtil(null, "eInstance_FireWatchers");
13617    reachedPass15 = true;
13618
13619    for(external = ast->first; external; external = external.next)
13620    {
13621       afterExternal = curExternal = external;
13622       if(external.type == functionExternal)
13623       {
13624          if(memoryGuard)
13625          {
13626             DeclareFunctionUtil(external, "MemoryGuard_PushLoc");
13627             DeclareFunctionUtil(external, "MemoryGuard_PopLoc");
13628          }
13629
13630          currentClass = external.function._class;
13631          ProcessFunction(external.function);
13632       }
13633       // There shouldn't be any _class member access here anyways...
13634       else if(external.type == declarationExternal)
13635       {
13636          if(memoryGuard && external.declaration && external.declaration.type == instDeclaration)
13637          {
13638             DeclareFunctionUtil(external, "MemoryGuard_PushLoc");
13639             DeclareFunctionUtil(external, "MemoryGuard_PopLoc");
13640          }
13641
13642          currentClass = null;
13643          if(external.declaration)
13644             ProcessDeclaration(external.declaration, true);
13645       }
13646       else if(external.type == classExternal)
13647       {
13648          ClassDefinition _class = external._class;
13649          currentClass = external.symbol.registered;
13650          if(memoryGuard)
13651          {
13652             DeclareFunctionUtil(external, "MemoryGuard_PushLoc");
13653             DeclareFunctionUtil(external, "MemoryGuard_PopLoc");
13654          }
13655          if(_class.definitions)
13656          {
13657             ProcessClass(_class.definitions, _class.symbol);
13658          }
13659          if(inCompiler)
13660          {
13661             // Free class data...
13662             ast->Remove(external);
13663             delete external;
13664          }
13665       }
13666       else if(external.type == nameSpaceExternal)
13667       {
13668          thisNameSpace = external.id.string;
13669       }
13670    }
13671    currentClass = null;
13672    thisNameSpace = null;
13673    curExternal = null;
13674 }