compiler/libec: Fixed warnings for related struct (and class:struct) classes
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50       bool backOutputLineNumbers = outputLineNumbers;
51       outputLineNumbers = false;
52
53       if(exp)
54          OutputExpression(exp, f);
55       f.Seek(0, start);
56       count = strlen(string);
57       count += f.Read(string + count, 1, 1023);
58       string[count] = '\0';
59       delete f;
60
61       outputLineNumbers = backOutputLineNumbers;
62    }
63 }
64
65 Type ProcessTemplateParameterType(TemplateParameter param)
66 {
67    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
68    {
69       // TOFIX: Will need to free this Type
70       if(!param.baseType)
71       {
72          if(param.dataTypeString)
73             param.baseType = ProcessTypeString(param.dataTypeString, false);
74          else
75             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
76       }
77       return param.baseType;
78    }
79    return null;
80 }
81
82 bool NeedCast(Type type1, Type type2)
83 {
84    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
85
86    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
87    {
88       return false;
89    }
90
91    if(type1.kind == type2.kind)
92    {
93       switch(type1.kind)
94       {
95          case _BoolType:
96          case charType:
97          case shortType:
98          case intType:
99          case int64Type:
100          case intPtrType:
101          case intSizeType:
102             if(type1.passAsTemplate && !type2.passAsTemplate)
103                return true;
104             return type1.isSigned != type2.isSigned;
105          case classType:
106             return type1._class != type2._class;
107          case pointerType:
108             return (type1.type && type2.type && type1.type.constant != type2.type.constant) || NeedCast(type1.type, type2.type);
109          default:
110             return true; //false; ????
111       }
112    }
113    return true;
114 }
115
116 static void ReplaceClassMembers(Expression exp, Class _class)
117 {
118    if(exp.type == identifierExp && exp.identifier)
119    {
120       Identifier id = exp.identifier;
121       Context ctx;
122       Symbol symbol = null;
123       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
124       {
125          // First, check if the identifier is declared inside the function
126          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
127          {
128             symbol = (Symbol)ctx.symbols.FindString(id.string);
129             if(symbol) break;
130          }
131       }
132
133       // If it is not, check if it is a member of the _class
134       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
135       {
136          Property prop = eClass_FindProperty(_class, id.string, privateModule);
137          Method method = null;
138          DataMember member = null;
139          ClassProperty classProp = null;
140          if(!prop)
141          {
142             method = eClass_FindMethod(_class, id.string, privateModule);
143          }
144          if(!prop && !method)
145             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
146          if(!prop && !method && !member)
147          {
148             classProp = eClass_FindClassProperty(_class, id.string);
149          }
150          if(prop || method || member || classProp)
151          {
152             // Replace by this.[member]
153             exp.type = memberExp;
154             exp.member.member = id;
155             exp.member.memberType = unresolvedMember;
156             exp.member.exp = QMkExpId("this");
157             //exp.member.exp.loc = exp.loc;
158             exp.addedThis = true;
159          }
160          else if(_class && _class.templateParams.first)
161          {
162             Class sClass;
163             for(sClass = _class; sClass; sClass = sClass.base)
164             {
165                if(sClass.templateParams.first)
166                {
167                   ClassTemplateParameter param;
168                   for(param = sClass.templateParams.first; param; param = param.next)
169                   {
170                      if(param.type == expression && !strcmp(param.name, id.string))
171                      {
172                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
173
174                         if(argExp)
175                         {
176                            Declarator decl;
177                            OldList * specs = MkList();
178
179                            FreeIdentifier(exp.member.member);
180
181                            ProcessExpressionType(argExp);
182
183                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
184
185                            exp.expType = ProcessType(specs, decl);
186
187                            // *[expType] *[argExp]
188                            exp.type = bracketsExp;
189                            exp.list = MkListOne(MkExpOp(null, '*',
190                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
191                         }
192                      }
193                   }
194                }
195             }
196          }
197       }
198    }
199 }
200
201 ////////////////////////////////////////////////////////////////////////
202 // PRINTING ////////////////////////////////////////////////////////////
203 ////////////////////////////////////////////////////////////////////////
204
205 public char * PrintInt(int64 result)
206 {
207    char temp[100];
208    if(result > MAXINT)
209       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
210    else
211       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
212    if(result > MAXINT || result < MININT)
213       strcat(temp, "LL");
214    return CopyString(temp);
215 }
216
217 public char * PrintUInt(uint64 result)
218 {
219    char temp[100];
220    if(result > MAXDWORD)
221       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
222    else if(result > MAXINT)
223       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
224    else
225       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
226    return CopyString(temp);
227 }
228
229 public char * PrintInt64(int64 result)
230 {
231    char temp[100];
232    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
233    return CopyString(temp);
234 }
235
236 public char * PrintUInt64(uint64 result)
237 {
238    char temp[100];
239    if(result > MAXINT64)
240       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
241    else
242       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
243    return CopyString(temp);
244 }
245
246 public char * PrintHexUInt(uint64 result)
247 {
248    char temp[100];
249    if(result > MAXDWORD)
250       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
251    else
252       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
253    if(result > MAXDWORD)
254       strcat(temp, "LL");
255    return CopyString(temp);
256 }
257
258 public char * PrintHexUInt64(uint64 result)
259 {
260    char temp[100];
261    if(result > MAXDWORD)
262       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
263    else
264       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
265    return CopyString(temp);
266 }
267
268 public char * PrintShort(short result)
269 {
270    char temp[100];
271    sprintf(temp, "%d", (unsigned short)result);
272    return CopyString(temp);
273 }
274
275 public char * PrintUShort(unsigned short result)
276 {
277    char temp[100];
278    if(result > 32767)
279       sprintf(temp, "0x%X", (int)result);
280    else
281       sprintf(temp, "%d", (int)result);
282    return CopyString(temp);
283 }
284
285 public char * PrintChar(char result)
286 {
287    char temp[100];
288    if(result > 0 && isprint(result))
289       sprintf(temp, "'%c'", result);
290    else if(result < 0)
291       sprintf(temp, "%d", (int)result);
292    else
293       //sprintf(temp, "%#X", result);
294       sprintf(temp, "0x%X", (unsigned char)result);
295    return CopyString(temp);
296 }
297
298 public char * PrintUChar(unsigned char result)
299 {
300    char temp[100];
301    sprintf(temp, "0x%X", result);
302    return CopyString(temp);
303 }
304
305 public char * PrintFloat(float result)
306 {
307    char temp[350];
308    if(result.isInf)
309    {
310       if(result.signBit)
311          strcpy(temp, "-inf");
312       else
313          strcpy(temp, "inf");
314    }
315    else if(result.isNan)
316    {
317       if(result.signBit)
318          strcpy(temp, "-nan");
319       else
320          strcpy(temp, "nan");
321    }
322    else
323       sprintf(temp, "%.16ff", result);
324    return CopyString(temp);
325 }
326
327 public char * PrintDouble(double result)
328 {
329    char temp[350];
330    if(result.isInf)
331    {
332       if(result.signBit)
333          strcpy(temp, "-inf");
334       else
335          strcpy(temp, "inf");
336    }
337    else if(result.isNan)
338    {
339       if(result.signBit)
340          strcpy(temp, "-nan");
341       else
342          strcpy(temp, "nan");
343    }
344    else
345       sprintf(temp, "%.16f", result);
346    return CopyString(temp);
347 }
348
349 ////////////////////////////////////////////////////////////////////////
350 ////////////////////////////////////////////////////////////////////////
351
352 //public Operand GetOperand(Expression exp);
353
354 #define GETVALUE(name, t) \
355    public bool GetOp##name(Operand op2, t * value2) \
356    {                                                        \
357       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
358       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
359       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
360       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
361       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
362       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
363       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
364       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
365       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
366       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
367       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
368       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
369       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
370       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
371       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
372       else                                                                          \
373          return false;                                                              \
374       return true;                                                                  \
375    } \
376    public bool Get##name(Expression exp, t * value2) \
377    {                                                        \
378       Operand op2 = GetOperand(exp);                        \
379       return GetOp##name(op2, value2); \
380    }
381
382 // To help the deubugger currently not preprocessing...
383 #define HELP(x) x
384
385 GETVALUE(Int, HELP(int));
386 GETVALUE(UInt, HELP(unsigned int));
387 GETVALUE(Int64, HELP(int64));
388 GETVALUE(UInt64, HELP(uint64));
389 GETVALUE(IntPtr, HELP(intptr));
390 GETVALUE(UIntPtr, HELP(uintptr));
391 GETVALUE(IntSize, HELP(intsize));
392 GETVALUE(UIntSize, HELP(uintsize));
393 GETVALUE(Short, HELP(short));
394 GETVALUE(UShort, HELP(unsigned short));
395 GETVALUE(Char, HELP(char));
396 GETVALUE(UChar, HELP(unsigned char));
397 GETVALUE(Float, HELP(float));
398 GETVALUE(Double, HELP(double));
399
400 void ComputeExpression(Expression exp);
401
402 void ComputeClassMembers(Class _class, bool isMember)
403 {
404    DataMember member = isMember ? (DataMember) _class : null;
405    Context context = isMember ? null : SetupTemplatesContext(_class);
406    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
407                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
408    {
409       int unionMemberOffset = 0;
410       int bitFields = 0;
411
412       /*
413       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
414          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
415       */
416
417       if(member)
418       {
419          member.memberOffset = 0;
420          if(targetBits < sizeof(void *) * 8)
421             member.structAlignment = 0;
422       }
423       else if(targetBits < sizeof(void *) * 8)
424          _class.structAlignment = 0;
425
426       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
427       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
428          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
429
430       if(!member && _class.destructionWatchOffset)
431          _class.memberOffset += sizeof(OldList);
432
433       // To avoid reentrancy...
434       //_class.structSize = -1;
435
436       {
437          DataMember dataMember;
438          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
439          {
440             if(!dataMember.isProperty)
441             {
442                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
443                {
444                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
445                   /*if(!dataMember.dataType)
446                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
447                      */
448                }
449             }
450          }
451       }
452
453       {
454          DataMember dataMember;
455          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
456          {
457             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
458             {
459                if(!isMember && _class.type == bitClass && dataMember.dataType)
460                {
461                   BitMember bitMember = (BitMember) dataMember;
462                   uint64 mask = 0;
463                   int d;
464
465                   ComputeTypeSize(dataMember.dataType);
466
467                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
468                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
469
470                   _class.memberOffset = bitMember.pos + bitMember.size;
471                   for(d = 0; d<bitMember.size; d++)
472                   {
473                      if(d)
474                         mask <<= 1;
475                      mask |= 1;
476                   }
477                   bitMember.mask = mask << bitMember.pos;
478                }
479                else if(dataMember.type == normalMember && dataMember.dataType)
480                {
481                   int size;
482                   int alignment = 0;
483
484                   // Prevent infinite recursion
485                   if(dataMember.dataType.kind != classType ||
486                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
487                      _class.type != structClass)))
488                      ComputeTypeSize(dataMember.dataType);
489
490                   if(dataMember.dataType.bitFieldCount)
491                   {
492                      bitFields += dataMember.dataType.bitFieldCount;
493                      size = 0;
494                   }
495                   else
496                   {
497                      if(bitFields)
498                      {
499                         int size = (bitFields + 7) / 8;
500
501                         if(isMember)
502                         {
503                            // TESTING THIS PADDING CODE
504                            if(alignment)
505                            {
506                               member.structAlignment = Max(member.structAlignment, alignment);
507
508                               if(member.memberOffset % alignment)
509                                  member.memberOffset += alignment - (member.memberOffset % alignment);
510                            }
511
512                            dataMember.offset = member.memberOffset;
513                            if(member.type == unionMember)
514                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
515                            else
516                            {
517                               member.memberOffset += size;
518                            }
519                         }
520                         else
521                         {
522                            // TESTING THIS PADDING CODE
523                            if(alignment)
524                            {
525                               _class.structAlignment = Max(_class.structAlignment, alignment);
526
527                               if(_class.memberOffset % alignment)
528                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
529                            }
530
531                            dataMember.offset = _class.memberOffset;
532                            _class.memberOffset += size;
533                         }
534                         bitFields = 0;
535                      }
536                      size = dataMember.dataType.size;
537                      alignment = dataMember.dataType.alignment;
538                   }
539
540                   if(isMember)
541                   {
542                      // TESTING THIS PADDING CODE
543                      if(alignment)
544                      {
545                         member.structAlignment = Max(member.structAlignment, alignment);
546
547                         if(member.memberOffset % alignment)
548                            member.memberOffset += alignment - (member.memberOffset % alignment);
549                      }
550
551                      dataMember.offset = member.memberOffset;
552                      if(member.type == unionMember)
553                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
554                      else
555                      {
556                         member.memberOffset += size;
557                      }
558                   }
559                   else
560                   {
561                      // TESTING THIS PADDING CODE
562                      if(alignment)
563                      {
564                         _class.structAlignment = Max(_class.structAlignment, alignment);
565
566                         if(_class.memberOffset % alignment)
567                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
568                      }
569
570                      dataMember.offset = _class.memberOffset;
571                      _class.memberOffset += size;
572                   }
573                }
574                else
575                {
576                   int alignment;
577
578                   ComputeClassMembers((Class)dataMember, true);
579                   alignment = dataMember.structAlignment;
580
581                   if(isMember)
582                   {
583                      if(alignment)
584                      {
585                         if(member.memberOffset % alignment)
586                            member.memberOffset += alignment - (member.memberOffset % alignment);
587
588                         member.structAlignment = Max(member.structAlignment, alignment);
589                      }
590                      dataMember.offset = member.memberOffset;
591                      if(member.type == unionMember)
592                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
593                      else
594                         member.memberOffset += dataMember.memberOffset;
595                   }
596                   else
597                   {
598                      if(alignment)
599                      {
600                         if(_class.memberOffset % alignment)
601                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
602                         _class.structAlignment = Max(_class.structAlignment, alignment);
603                      }
604                      dataMember.offset = _class.memberOffset;
605                      _class.memberOffset += dataMember.memberOffset;
606                   }
607                }
608             }
609          }
610          if(bitFields)
611          {
612             int alignment = 0;
613             int size = (bitFields + 7) / 8;
614
615             if(isMember)
616             {
617                // TESTING THIS PADDING CODE
618                if(alignment)
619                {
620                   member.structAlignment = Max(member.structAlignment, alignment);
621
622                   if(member.memberOffset % alignment)
623                      member.memberOffset += alignment - (member.memberOffset % alignment);
624                }
625
626                if(member.type == unionMember)
627                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
628                else
629                {
630                   member.memberOffset += size;
631                }
632             }
633             else
634             {
635                // TESTING THIS PADDING CODE
636                if(alignment)
637                {
638                   _class.structAlignment = Max(_class.structAlignment, alignment);
639
640                   if(_class.memberOffset % alignment)
641                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
642                }
643                _class.memberOffset += size;
644             }
645             bitFields = 0;
646          }
647       }
648       if(member && member.type == unionMember)
649       {
650          member.memberOffset = unionMemberOffset;
651       }
652
653       if(!isMember)
654       {
655          /*if(_class.type == structClass)
656             _class.size = _class.memberOffset;
657          else
658          */
659
660          if(_class.type != bitClass)
661          {
662             int extra = 0;
663             if(_class.structAlignment)
664             {
665                if(_class.memberOffset % _class.structAlignment)
666                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
667             }
668             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
669             if(!member)
670             {
671                Property prop;
672                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
673                {
674                   if(prop.isProperty && prop.isWatchable)
675                   {
676                      prop.watcherOffset = _class.structSize;
677                      _class.structSize += sizeof(OldList);
678                   }
679                }
680             }
681
682             // Fix Derivatives
683             {
684                OldLink derivative;
685                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
686                {
687                   Class deriv = derivative.data;
688
689                   if(deriv.computeSize)
690                   {
691                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
692                      deriv.offset = /*_class.offset + */_class.structSize;
693                      deriv.memberOffset = 0;
694                      // ----------------------
695
696                      deriv.structSize = deriv.offset;
697
698                      ComputeClassMembers(deriv, false);
699                   }
700                }
701             }
702          }
703       }
704    }
705    if(context)
706       FinishTemplatesContext(context);
707 }
708
709 public void ComputeModuleClasses(Module module)
710 {
711    Class _class;
712    OldLink subModule;
713
714    for(subModule = module.modules.first; subModule; subModule = subModule.next)
715       ComputeModuleClasses(subModule.data);
716    for(_class = module.classes.first; _class; _class = _class.next)
717       ComputeClassMembers(_class, false);
718 }
719
720
721 public int ComputeTypeSize(Type type)
722 {
723    uint size = type ? type.size : 0;
724    if(!size && type && !type.computing)
725    {
726       type.computing = true;
727       switch(type.kind)
728       {
729          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
730          case charType: type.alignment = size = sizeof(char); break;
731          case intType: type.alignment = size = sizeof(int); break;
732          case int64Type: type.alignment = size = sizeof(int64); break;
733          case intPtrType: type.alignment = size = targetBits / 8; break;
734          case intSizeType: type.alignment = size = targetBits / 8; break;
735          case longType: type.alignment = size = sizeof(long); break;
736          case shortType: type.alignment = size = sizeof(short); break;
737          case floatType: type.alignment = size = sizeof(float); break;
738          case doubleType: type.alignment = size = sizeof(double); break;
739          case classType:
740          {
741             Class _class = type._class ? type._class.registered : null;
742
743             if(_class && _class.type == structClass)
744             {
745                // Ensure all members are properly registered
746                ComputeClassMembers(_class, false);
747                type.alignment = _class.structAlignment;
748                size = _class.structSize;
749                if(type.alignment && size % type.alignment)
750                   size += type.alignment - (size % type.alignment);
751
752             }
753             else if(_class && (_class.type == unitClass ||
754                    _class.type == enumClass ||
755                    _class.type == bitClass))
756             {
757                if(!_class.dataType)
758                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
759                size = type.alignment = ComputeTypeSize(_class.dataType);
760             }
761             else
762                size = type.alignment = targetBits / 8; // sizeof(Instance *);
763             break;
764          }
765          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
766          case arrayType:
767             if(type.arraySizeExp)
768             {
769                ProcessExpressionType(type.arraySizeExp);
770                ComputeExpression(type.arraySizeExp);
771                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType &&
772                   type.arraySizeExp.expType.kind != shortType &&
773                   type.arraySizeExp.expType.kind != charType &&
774                   type.arraySizeExp.expType.kind != longType &&
775                   type.arraySizeExp.expType.kind != int64Type &&
776                   type.arraySizeExp.expType.kind != intSizeType &&
777                   type.arraySizeExp.expType.kind != intPtrType &&
778                   type.arraySizeExp.expType.kind != enumType &&
779                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
780                {
781                   Location oldLoc = yylloc;
782                   // bool isConstant = type.arraySizeExp.isConstant;
783                   char expression[10240];
784                   expression[0] = '\0';
785                   type.arraySizeExp.expType = null;
786                   yylloc = type.arraySizeExp.loc;
787                   if(inCompiler)
788                      PrintExpression(type.arraySizeExp, expression);
789                   Compiler_Error($"Array size not constant int (%s)\n", expression);
790                   yylloc = oldLoc;
791                }
792                GetInt(type.arraySizeExp, &type.arraySize);
793             }
794             else if(type.enumClass)
795             {
796                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
797                {
798                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
799                }
800                else
801                   type.arraySize = 0;
802             }
803             else
804             {
805                // Unimplemented auto size
806                type.arraySize = 0;
807             }
808
809             size = ComputeTypeSize(type.type) * type.arraySize;
810             if(type.type)
811                type.alignment = type.type.alignment;
812
813             break;
814          case structType:
815          {
816             Type member;
817             for(member = type.members.first; member; member = member.next)
818             {
819                uint addSize = ComputeTypeSize(member);
820
821                member.offset = size;
822                if(member.alignment && size % member.alignment)
823                   member.offset += member.alignment - (size % member.alignment);
824                size = member.offset;
825
826                type.alignment = Max(type.alignment, member.alignment);
827                size += addSize;
828             }
829             if(type.alignment && size % type.alignment)
830                size += type.alignment - (size % type.alignment);
831             break;
832          }
833          case unionType:
834          {
835             Type member;
836             for(member = type.members.first; member; member = member.next)
837             {
838                uint addSize = ComputeTypeSize(member);
839
840                member.offset = size;
841                if(member.alignment && size % member.alignment)
842                   member.offset += member.alignment - (size % member.alignment);
843                size = member.offset;
844
845                type.alignment = Max(type.alignment, member.alignment);
846                size = Max(size, addSize);
847             }
848             if(type.alignment && size % type.alignment)
849                size += type.alignment - (size % type.alignment);
850             break;
851          }
852          case templateType:
853          {
854             TemplateParameter param = type.templateParameter;
855             Type baseType = ProcessTemplateParameterType(param);
856             if(baseType)
857             {
858                size = ComputeTypeSize(baseType);
859                type.alignment = baseType.alignment;
860             }
861             else
862                type.alignment = size = sizeof(uint64);
863             break;
864          }
865          case enumType:
866          {
867             type.alignment = size = sizeof(enum { test });
868             break;
869          }
870          case thisClassType:
871          {
872             type.alignment = size = targetBits / 8; //sizeof(void *);
873             break;
874          }
875       }
876       type.size = size;
877       type.computing = false;
878    }
879    return size;
880 }
881
882
883 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
884 {
885    // This function is in need of a major review when implementing private members etc.
886    DataMember topMember = isMember ? (DataMember) _class : null;
887    uint totalSize = 0;
888    uint maxSize = 0;
889    int alignment;
890    uint size;
891    DataMember member;
892    int anonID = 1;
893    Context context = isMember ? null : SetupTemplatesContext(_class);
894    if(addedPadding)
895       *addedPadding = false;
896
897    if(!isMember && _class.base)
898    {
899       maxSize = _class.structSize;
900       //if(_class.base.type != systemClass) // Commented out with new Instance _class
901       {
902          // DANGER: Testing this noHeadClass here...
903          if(_class.type == structClass || _class.type == noHeadClass)
904             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
905          else
906          {
907             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
908             if(maxSize > baseSize)
909                maxSize -= baseSize;
910             else
911                maxSize = 0;
912          }
913       }
914    }
915
916    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
917    {
918       if(!member.isProperty)
919       {
920          switch(member.type)
921          {
922             case normalMember:
923             {
924                if(member.dataTypeString)
925                {
926                   OldList * specs = MkList(), * decls = MkList();
927                   Declarator decl;
928
929                   decl = SpecDeclFromString(member.dataTypeString, specs,
930                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
931                   ListAdd(decls, MkStructDeclarator(decl, null));
932                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
933
934                   if(!member.dataType)
935                      member.dataType = ProcessType(specs, decl);
936
937                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
938
939                   {
940                      Type type = ProcessType(specs, decl);
941                      DeclareType(member.dataType, false, false);
942                      FreeType(type);
943                   }
944                   /*
945                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
946                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
947                      DeclareStruct(member.dataType._class.string, false);
948                   */
949
950                   ComputeTypeSize(member.dataType);
951                   size = member.dataType.size;
952                   alignment = member.dataType.alignment;
953
954                   if(alignment)
955                   {
956                      if(totalSize % alignment)
957                         totalSize += alignment - (totalSize % alignment);
958                   }
959                   totalSize += size;
960                }
961                break;
962             }
963             case unionMember:
964             case structMember:
965             {
966                OldList * specs = MkList(), * list = MkList();
967                char id[100];
968                sprintf(id, "__anon%d", anonID++);
969
970                size = 0;
971                AddMembers(list, (Class)member, true, &size, topClass, null);
972                ListAdd(specs,
973                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
974                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, MkListOne(MkDeclaratorIdentifier(MkIdentifier(id))),null)));
975                alignment = member.structAlignment;
976
977                if(alignment)
978                {
979                   if(totalSize % alignment)
980                      totalSize += alignment - (totalSize % alignment);
981                }
982                totalSize += size;
983                break;
984             }
985          }
986       }
987    }
988    if(retSize)
989    {
990       if(topMember && topMember.type == unionMember)
991          *retSize = Max(*retSize, totalSize);
992       else
993          *retSize += totalSize;
994    }
995    else if(totalSize < maxSize && _class.type != systemClass)
996    {
997       int autoPadding = 0;
998       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
999          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
1000       if(totalSize + autoPadding < maxSize)
1001       {
1002          char sizeString[50];
1003          sprintf(sizeString, "%d", maxSize - totalSize);
1004          ListAdd(declarations,
1005             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
1006             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
1007          if(addedPadding)
1008             *addedPadding = true;
1009       }
1010    }
1011    if(context)
1012       FinishTemplatesContext(context);
1013    return topMember ? topMember.memberID : _class.memberID;
1014 }
1015
1016 static int DeclareMembers(Class _class, bool isMember)
1017 {
1018    DataMember topMember = isMember ? (DataMember) _class : null;
1019    DataMember member;
1020    Context context = isMember ? null : SetupTemplatesContext(_class);
1021
1022    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1023       DeclareMembers(_class.base, false);
1024
1025    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1026    {
1027       if(!member.isProperty)
1028       {
1029          switch(member.type)
1030          {
1031             case normalMember:
1032             {
1033                /*
1034                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1035                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1036                   DeclareStruct(member.dataType._class.string, false);
1037                   */
1038                if(!member.dataType && member.dataTypeString)
1039                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1040                if(member.dataType)
1041                   DeclareType(member.dataType, false, false);
1042                break;
1043             }
1044             case unionMember:
1045             case structMember:
1046             {
1047                DeclareMembers((Class)member, true);
1048                break;
1049             }
1050          }
1051       }
1052    }
1053    if(context)
1054       FinishTemplatesContext(context);
1055
1056    return topMember ? topMember.memberID : _class.memberID;
1057 }
1058
1059 static void IdentifyAnonStructs(OldList/*<ClassDef>*/ *  definitions)
1060 {
1061    ClassDef def;
1062    int anonID = 1;
1063    for(def = definitions->first; def; def = def.next)
1064    {
1065       if(def.type == declarationClassDef)
1066       {
1067          Declaration decl = def.decl;
1068          if(decl && decl.specifiers)
1069          {
1070             Specifier spec;
1071             bool isStruct = false;
1072             for(spec = decl.specifiers->first; spec; spec = spec.next)
1073             {
1074                if(spec.type == structSpecifier || spec.type == unionSpecifier)
1075                {
1076                   if(spec.definitions)
1077                      IdentifyAnonStructs(spec.definitions);
1078                   isStruct = true;
1079                }
1080             }
1081             if(isStruct)
1082             {
1083                Declarator d = null;
1084                if(decl.declarators)
1085                {
1086                   for(d = decl.declarators->first; d; d = d.next)
1087                   {
1088                      Identifier idDecl = GetDeclId(d);
1089                      if(idDecl)
1090                         break;
1091                   }
1092                }
1093                if(!d)
1094                {
1095                   char id[100];
1096                   sprintf(id, "__anon%d", anonID++);
1097                   if(!decl.declarators)
1098                      decl.declarators = MkList();
1099                   ListAdd(decl.declarators, MkDeclaratorIdentifier(MkIdentifier(id)));
1100                }
1101             }
1102          }
1103       }
1104    }
1105 }
1106
1107 void DeclareStruct(const char * name, bool skipNoHead)
1108 {
1109    External external = null;
1110    Symbol classSym = FindClass(name);
1111
1112    if(!inCompiler || !classSym) return;
1113
1114    // We don't need any declaration for bit classes...
1115    if(classSym.registered &&
1116       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1117       return;
1118
1119    /*if(classSym.registered.templateClass)
1120       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1121    */
1122
1123    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1124    {
1125       // Add typedef struct
1126       Declaration decl;
1127       OldList * specifiers, * declarators;
1128       OldList * declarations = null;
1129       char structName[1024];
1130       Specifier spec = null;
1131       external = (classSym.registered && classSym.registered.type == structClass) ?
1132          classSym.pointerExternal : classSym.structExternal;
1133
1134       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1135       // Moved this one up because DeclareClass done later will need it
1136
1137       classSym.declaring++;
1138
1139       if(strchr(classSym.string, '<'))
1140       {
1141          if(classSym.registered.templateClass)
1142          {
1143             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1144             classSym.declaring--;
1145          }
1146          return;
1147       }
1148
1149       //if(!skipNoHead)
1150          DeclareMembers(classSym.registered, false);
1151
1152       structName[0] = 0;
1153       FullClassNameCat(structName, name, false);
1154
1155       if(external && external.declaration && external.declaration.specifiers)
1156       {
1157          for(spec = external.declaration.specifiers->first; spec; spec = spec.next)
1158          {
1159             if(spec.type == structSpecifier || spec.type == unionSpecifier)
1160                break;
1161          }
1162       }
1163
1164       /*if(!external)
1165          external = MkExternalDeclaration(null);*/
1166
1167       if(!skipNoHead && (!spec || !spec.definitions))
1168       {
1169          bool addedPadding = false;
1170          classSym.declaredStructSym = true;
1171
1172          declarations = MkList();
1173
1174          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1175
1176          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1177          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1178
1179          if(!declarations->count || (declarations->count == 1 && addedPadding))
1180          {
1181             FreeList(declarations, FreeClassDef);
1182             declarations = null;
1183          }
1184       }
1185       if(skipNoHead || declarations)
1186       {
1187          if(spec)
1188          {
1189             if(declarations)
1190                spec.definitions = declarations;
1191
1192             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1193             {
1194                // TODO: Fix this
1195                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1196
1197                // DANGER
1198                if(classSym.structExternal)
1199                   ast->Move(classSym.structExternal, curExternal.prev);
1200                ast->Move(classSym.pointerExternal, curExternal.prev);
1201
1202                classSym.id = curExternal.symbol.idCode;
1203                classSym.idCode = curExternal.symbol.idCode;
1204                // external = classSym.pointerExternal;
1205                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1206             }
1207          }
1208          else
1209          {
1210             if(!external)
1211                external = MkExternalDeclaration(null);
1212
1213             specifiers = MkList();
1214             declarators = MkList();
1215             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1216
1217             /*
1218             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1219             ListAdd(declarators, MkInitDeclarator(d, null));
1220             */
1221             external.declaration = decl = MkDeclaration(specifiers, declarators);
1222             if(decl.symbol && !decl.symbol.pointerExternal)
1223                decl.symbol.pointerExternal = external;
1224
1225             // For simple classes, keep the declaration as the external to move around
1226             if(classSym.registered && classSym.registered.type == structClass)
1227             {
1228                char className[1024];
1229                strcpy(className, "__ecereClass_");
1230                FullClassNameCat(className, classSym.string, true);
1231                //MangleClassName(className);
1232
1233                // Testing This
1234                DeclareClass(classSym, className);
1235
1236                external.symbol = classSym;
1237                classSym.pointerExternal = external;
1238                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1239                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1240             }
1241             else
1242             {
1243                char className[1024];
1244                strcpy(className, "__ecereClass_");
1245                FullClassNameCat(className, classSym.string, true);
1246                //MangleClassName(className);
1247
1248                // TOFIX: TESTING THIS...
1249                classSym.structExternal = external;
1250                DeclareClass(classSym, className);
1251                external.symbol = classSym;
1252             }
1253
1254             //if(curExternal)
1255                ast->Insert(curExternal ? curExternal.prev : null, external);
1256          }
1257       }
1258
1259       classSym.declaring--;
1260    }
1261    else
1262    {
1263       if(classSym.structExternal && classSym.structExternal.declaration && classSym.structExternal.declaration.specifiers)
1264       {
1265          Specifier spec;
1266          for(spec = classSym.structExternal.declaration.specifiers->first; spec; spec = spec.next)
1267          {
1268             IdentifyAnonStructs(spec.definitions);
1269          }
1270       }
1271
1272       if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1273       {
1274          // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1275          // Moved this one up because DeclareClass done later will need it
1276
1277          // TESTING THIS:
1278          classSym.declaring++;
1279
1280          //if(!skipNoHead)
1281          {
1282             if(classSym.registered)
1283                DeclareMembers(classSym.registered, false);
1284          }
1285
1286          if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1287          {
1288             // TODO: Fix this
1289             //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1290
1291             // DANGER
1292             if(classSym.structExternal)
1293                ast->Move(classSym.structExternal, curExternal.prev);
1294             ast->Move(classSym.pointerExternal, curExternal.prev);
1295
1296             classSym.id = curExternal.symbol.idCode;
1297             classSym.idCode = curExternal.symbol.idCode;
1298             // external = classSym.pointerExternal;
1299             // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1300          }
1301
1302          classSym.declaring--;
1303       }
1304    }
1305    //return external;
1306 }
1307
1308 void DeclareProperty(Property prop, char * setName, char * getName)
1309 {
1310    Symbol symbol = prop.symbol;
1311
1312    strcpy(setName, "__ecereProp_");
1313    FullClassNameCat(setName, prop._class.fullName, false);
1314    strcat(setName, "_Set_");
1315    // strcat(setName, prop.name);
1316    FullClassNameCat(setName, prop.name, true);
1317    //MangleClassName(setName);
1318
1319    strcpy(getName, "__ecereProp_");
1320    FullClassNameCat(getName, prop._class.fullName, false);
1321    strcat(getName, "_Get_");
1322    FullClassNameCat(getName, prop.name, true);
1323    // strcat(getName, prop.name);
1324
1325    // To support "char *" property
1326    //MangleClassName(getName);
1327
1328    if(prop._class.type == structClass)
1329       DeclareStruct(prop._class.fullName, false);
1330
1331    if(!symbol || curExternal.symbol.idCode < symbol.id)
1332    {
1333       bool imported = false;
1334       bool dllImport = false;
1335
1336       if(!symbol || symbol._import)
1337       {
1338          if(!symbol)
1339          {
1340             Symbol classSym;
1341             if(!prop._class.symbol)
1342                prop._class.symbol = FindClass(prop._class.fullName);
1343             classSym = prop._class.symbol;
1344             if(classSym && !classSym._import)
1345             {
1346                ModuleImport module;
1347
1348                if(prop._class.module)
1349                   module = FindModule(prop._class.module);
1350                else
1351                   module = mainModule;
1352
1353                classSym._import = ClassImport
1354                {
1355                   name = CopyString(prop._class.fullName);
1356                   isRemote = prop._class.isRemote;
1357                };
1358                module.classes.Add(classSym._import);
1359             }
1360             symbol = prop.symbol = Symbol { };
1361             symbol._import = (ClassImport)PropertyImport
1362             {
1363                name = CopyString(prop.name);
1364                isVirtual = false; //prop.isVirtual;
1365                hasSet = prop.Set ? true : false;
1366                hasGet = prop.Get ? true : false;
1367             };
1368             if(classSym)
1369                classSym._import.properties.Add(symbol._import);
1370          }
1371          imported = true;
1372          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1373          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1374             prop._class.module.importType != staticImport)
1375             dllImport = true;
1376       }
1377
1378       if(!symbol.type)
1379       {
1380          Context context = SetupTemplatesContext(prop._class);
1381          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1382          FinishTemplatesContext(context);
1383       }
1384
1385       // Get
1386       if(prop.Get)
1387       {
1388          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1389          {
1390             Declaration decl;
1391             OldList * specifiers, * declarators;
1392             Declarator d;
1393             OldList * params;
1394             Specifier spec;
1395             External external;
1396             Declarator typeDecl;
1397             bool simple = false;
1398
1399             specifiers = MkList();
1400             declarators = MkList();
1401             params = MkList();
1402
1403             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1404                MkDeclaratorIdentifier(MkIdentifier("this"))));
1405
1406             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1407             //if(imported)
1408             if(dllImport)
1409                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1410
1411             {
1412                Context context = SetupTemplatesContext(prop._class);
1413                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1414                FinishTemplatesContext(context);
1415             }
1416
1417             // Make sure the simple _class's type is declared
1418             for(spec = specifiers->first; spec; spec = spec.next)
1419             {
1420                if(spec.type == nameSpecifier /*SpecifierClass*/)
1421                {
1422                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1423                   {
1424                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1425                      symbol._class = classSym.registered;
1426                      if(classSym.registered && classSym.registered.type == structClass)
1427                      {
1428                         DeclareStruct(spec.name, false);
1429                         simple = true;
1430                      }
1431                   }
1432                }
1433             }
1434
1435             if(!simple)
1436                d = PlugDeclarator(typeDecl, d);
1437             else
1438             {
1439                ListAdd(params, MkTypeName(specifiers,
1440                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1441                specifiers = MkList();
1442             }
1443
1444             d = MkDeclaratorFunction(d, params);
1445
1446             //if(imported)
1447             if(dllImport)
1448                specifiers->Insert(null, MkSpecifier(EXTERN));
1449             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1450                specifiers->Insert(null, MkSpecifier(STATIC));
1451             if(simple)
1452                ListAdd(specifiers, MkSpecifier(VOID));
1453
1454             ListAdd(declarators, MkInitDeclarator(d, null));
1455
1456             decl = MkDeclaration(specifiers, declarators);
1457
1458             external = MkExternalDeclaration(decl);
1459             ast->Insert(curExternal.prev, external);
1460             external.symbol = symbol;
1461             symbol.externalGet = external;
1462
1463             ReplaceThisClassSpecifiers(specifiers, prop._class);
1464
1465             if(typeDecl)
1466                FreeDeclarator(typeDecl);
1467          }
1468          else
1469          {
1470             // Move declaration higher...
1471             ast->Move(symbol.externalGet, curExternal.prev);
1472          }
1473       }
1474
1475       // Set
1476       if(prop.Set)
1477       {
1478          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1479          {
1480             Declaration decl;
1481             OldList * specifiers, * declarators;
1482             Declarator d;
1483             OldList * params;
1484             Specifier spec;
1485             External external;
1486             Declarator typeDecl;
1487
1488             declarators = MkList();
1489             params = MkList();
1490
1491             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1492             if(!prop.conversion || prop._class.type == structClass)
1493             {
1494                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1495                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1496             }
1497
1498             specifiers = MkList();
1499
1500             {
1501                Context context = SetupTemplatesContext(prop._class);
1502                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1503                   MkDeclaratorIdentifier(MkIdentifier("value")));
1504                FinishTemplatesContext(context);
1505             }
1506             if(!strcmp(prop._class.base.fullName, "eda::Row") || !strcmp(prop._class.base.fullName, "eda::Id"))
1507                specifiers->Insert(null, MkSpecifier(CONST));
1508
1509             ListAdd(params, MkTypeName(specifiers, d));
1510
1511             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1512             //if(imported)
1513             if(dllImport)
1514                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1515             d = MkDeclaratorFunction(d, params);
1516
1517             // Make sure the simple _class's type is declared
1518             for(spec = specifiers->first; spec; spec = spec.next)
1519             {
1520                if(spec.type == nameSpecifier /*SpecifierClass*/)
1521                {
1522                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1523                   {
1524                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1525                      symbol._class = classSym.registered;
1526                      if(classSym.registered && classSym.registered.type == structClass)
1527                         DeclareStruct(spec.name, false);
1528                   }
1529                }
1530             }
1531
1532             ListAdd(declarators, MkInitDeclarator(d, null));
1533
1534             specifiers = MkList();
1535             //if(imported)
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             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1542             if(!prop.conversion || prop._class.type == structClass)
1543                ListAdd(specifiers, MkSpecifier(VOID));
1544             else
1545                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1546
1547             decl = MkDeclaration(specifiers, declarators);
1548
1549             external = MkExternalDeclaration(decl);
1550             ast->Insert(curExternal.prev, external);
1551             external.symbol = symbol;
1552             symbol.externalSet = external;
1553
1554             ReplaceThisClassSpecifiers(specifiers, prop._class);
1555          }
1556          else
1557          {
1558             // Move declaration higher...
1559             ast->Move(symbol.externalSet, curExternal.prev);
1560          }
1561       }
1562
1563       // Property (for Watchers)
1564       if(!symbol.externalPtr)
1565       {
1566          Declaration decl;
1567          External external;
1568          OldList * specifiers = MkList();
1569          char propName[1024];
1570
1571          if(imported)
1572             specifiers->Insert(null, MkSpecifier(EXTERN));
1573          else
1574             specifiers->Insert(null, MkSpecifier(STATIC));
1575
1576          ListAdd(specifiers, MkSpecifierName("Property"));
1577
1578          strcpy(propName, "__ecereProp_");
1579          FullClassNameCat(propName, prop._class.fullName, false);
1580          strcat(propName, "_");
1581          FullClassNameCat(propName, prop.name, true);
1582          // strcat(propName, prop.name);
1583          //MangleClassName(propName);
1584
1585          {
1586             OldList * list = MkList();
1587             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1588                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1589
1590             if(!imported)
1591             {
1592                strcpy(propName, "__ecerePropM_");
1593                FullClassNameCat(propName, prop._class.fullName, false);
1594                strcat(propName, "_");
1595                // strcat(propName, prop.name);
1596                FullClassNameCat(propName, prop.name, true);
1597
1598                //MangleClassName(propName);
1599
1600                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1601                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1602             }
1603             decl = MkDeclaration(specifiers, list);
1604          }
1605
1606          external = MkExternalDeclaration(decl);
1607          ast->Insert(curExternal.prev, external);
1608          external.symbol = symbol;
1609          symbol.externalPtr = external;
1610       }
1611       else
1612       {
1613          // Move declaration higher...
1614          ast->Move(symbol.externalPtr, curExternal.prev);
1615       }
1616
1617       symbol.id = curExternal.symbol.idCode;
1618    }
1619 }
1620
1621 // ***************** EXPRESSION PROCESSING ***************************
1622 public Type Dereference(Type source)
1623 {
1624    Type type = null;
1625    if(source)
1626    {
1627       if(source.kind == pointerType || source.kind == arrayType)
1628       {
1629          type = source.type;
1630          source.type.refCount++;
1631       }
1632       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1633       {
1634          type = Type
1635          {
1636             kind = charType;
1637             refCount = 1;
1638          };
1639       }
1640       // Support dereferencing of no head classes for now...
1641       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1642       {
1643          type = source;
1644          source.refCount++;
1645       }
1646       else
1647          Compiler_Error($"cannot dereference type\n");
1648    }
1649    return type;
1650 }
1651
1652 static Type Reference(Type source)
1653 {
1654    Type type = null;
1655    if(source)
1656    {
1657       type = Type
1658       {
1659          kind = pointerType;
1660          type = source;
1661          refCount = 1;
1662       };
1663       source.refCount++;
1664    }
1665    return type;
1666 }
1667
1668 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1669 {
1670    Identifier ident = member.identifiers ? member.identifiers->first : null;
1671    bool found = false;
1672    DataMember dataMember = null;
1673    Method method = null;
1674    bool freeType = false;
1675
1676    yylloc = member.loc;
1677
1678    if(!ident)
1679    {
1680       if(curMember)
1681       {
1682          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1683          if(*curMember)
1684          {
1685             found = true;
1686             dataMember = *curMember;
1687          }
1688       }
1689    }
1690    else
1691    {
1692       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1693       DataMember _subMemberStack[256];
1694       int _subMemberStackPos = 0;
1695
1696       // FILL MEMBER STACK
1697       if(!thisMember)
1698          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1699       if(thisMember)
1700       {
1701          dataMember = thisMember;
1702          if(curMember && thisMember.memberAccess == publicAccess)
1703          {
1704             *curMember = thisMember;
1705             *curClass = thisMember._class;
1706             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1707             *subMemberStackPos = _subMemberStackPos;
1708          }
1709          found = true;
1710       }
1711       else
1712       {
1713          // Setting a method
1714          method = eClass_FindMethod(_class, ident.string, privateModule);
1715          if(method && method.type == virtualMethod)
1716             found = true;
1717          else
1718             method = null;
1719       }
1720    }
1721
1722    if(found)
1723    {
1724       Type type = null;
1725       if(dataMember)
1726       {
1727          if(!dataMember.dataType && dataMember.dataTypeString)
1728          {
1729             //Context context = SetupTemplatesContext(dataMember._class);
1730             Context context = SetupTemplatesContext(_class);
1731             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1732             FinishTemplatesContext(context);
1733          }
1734          type = dataMember.dataType;
1735       }
1736       else if(method)
1737       {
1738          // This is for destination type...
1739          if(!method.dataType)
1740             ProcessMethodType(method);
1741          //DeclareMethod(method);
1742          // method.dataType = ((Symbol)method.symbol)->type;
1743          type = method.dataType;
1744       }
1745
1746       if(ident && ident.next)
1747       {
1748          for(ident = ident.next; ident && type; ident = ident.next)
1749          {
1750             if(type.kind == classType)
1751             {
1752                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1753                if(!dataMember)
1754                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1755                if(dataMember)
1756                   type = dataMember.dataType;
1757             }
1758             else if(type.kind == structType || type.kind == unionType)
1759             {
1760                Type memberType;
1761                for(memberType = type.members.first; memberType; memberType = memberType.next)
1762                {
1763                   if(!strcmp(memberType.name, ident.string))
1764                   {
1765                      type = memberType;
1766                      break;
1767                   }
1768                }
1769             }
1770          }
1771       }
1772
1773       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1774       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1775       {
1776          int id = 0;
1777          ClassTemplateParameter curParam = null;
1778          Class sClass;
1779          for(sClass = _class; sClass; sClass = sClass.base)
1780          {
1781             id = 0;
1782             if(sClass.templateClass) sClass = sClass.templateClass;
1783             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1784             {
1785                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1786                {
1787                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1788                   {
1789                      if(sClass.templateClass) sClass = sClass.templateClass;
1790                      id += sClass.templateParams.count;
1791                   }
1792                   break;
1793                }
1794                id++;
1795             }
1796             if(curParam) break;
1797          }
1798
1799          if(curParam)
1800          {
1801             ClassTemplateArgument arg = _class.templateArgs[id];
1802             if(arg.dataTypeString)
1803             {
1804                bool constant = type.constant;
1805                // FreeType(type);
1806                type = ProcessTypeString(arg.dataTypeString, false);
1807                if(type.kind == classType && constant) type.constant = true;
1808                else if(type.kind == pointerType)
1809                {
1810                   Type t = type.type;
1811                   while(t.kind == pointerType) t = t.type;
1812                   if(constant) t.constant = constant;
1813                }
1814                freeType = true;
1815                if(type && _class.templateClass)
1816                   type.passAsTemplate = true;
1817                if(type)
1818                {
1819                   // type.refCount++;
1820                   /*if(!exp.destType)
1821                   {
1822                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1823                      exp.destType.refCount++;
1824                   }*/
1825                }
1826             }
1827          }
1828       }
1829       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1830       {
1831          Class expClass = type._class.registered;
1832          Class cClass = null;
1833          int paramCount = 0;
1834          int lastParam = -1;
1835
1836          char templateString[1024];
1837          ClassTemplateParameter param;
1838          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1839          for(cClass = expClass; cClass; cClass = cClass.base)
1840          {
1841             int p = 0;
1842             if(cClass.templateClass) cClass = cClass.templateClass;
1843             for(param = cClass.templateParams.first; param; param = param.next)
1844             {
1845                int id = p;
1846                Class sClass;
1847                ClassTemplateArgument arg;
1848                for(sClass = cClass.base; sClass; sClass = sClass.base)
1849                {
1850                   if(sClass.templateClass) sClass = sClass.templateClass;
1851                   id += sClass.templateParams.count;
1852                }
1853                arg = expClass.templateArgs[id];
1854
1855                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1856                {
1857                   ClassTemplateParameter cParam;
1858                   //int p = numParams - sClass.templateParams.count;
1859                   int p = 0;
1860                   Class nextClass;
1861                   if(sClass.templateClass) sClass = sClass.templateClass;
1862
1863                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1864                   {
1865                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1866                      p += nextClass.templateParams.count;
1867                   }
1868
1869                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1870                   {
1871                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1872                      {
1873                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1874                         {
1875                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1876                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1877                            break;
1878                         }
1879                      }
1880                   }
1881                }
1882
1883                {
1884                   char argument[256];
1885                   argument[0] = '\0';
1886                   /*if(arg.name)
1887                   {
1888                      strcat(argument, arg.name.string);
1889                      strcat(argument, " = ");
1890                   }*/
1891                   switch(param.type)
1892                   {
1893                      case expression:
1894                      {
1895                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1896                         char expString[1024];
1897                         OldList * specs = MkList();
1898                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1899                         Expression exp;
1900                         char * string = PrintHexUInt64(arg.expression.ui64);
1901                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1902                         delete string;
1903
1904                         ProcessExpressionType(exp);
1905                         ComputeExpression(exp);
1906                         expString[0] = '\0';
1907                         PrintExpression(exp, expString);
1908                         strcat(argument, expString);
1909                         //delete exp;
1910                         FreeExpression(exp);
1911                         break;
1912                      }
1913                      case identifier:
1914                      {
1915                         strcat(argument, arg.member.name);
1916                         break;
1917                      }
1918                      case TemplateParameterType::type:
1919                      {
1920                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1921                            strcat(argument, arg.dataTypeString);
1922                         break;
1923                      }
1924                   }
1925                   if(argument[0])
1926                   {
1927                      if(paramCount) strcat(templateString, ", ");
1928                      if(lastParam != p - 1)
1929                      {
1930                         strcat(templateString, param.name);
1931                         strcat(templateString, " = ");
1932                      }
1933                      strcat(templateString, argument);
1934                      paramCount++;
1935                      lastParam = p;
1936                   }
1937                   p++;
1938                }
1939             }
1940          }
1941          {
1942             int len = strlen(templateString);
1943             if(templateString[len-1] == '<')
1944                len--;
1945             else
1946             {
1947                if(templateString[len-1] == '>')
1948                   templateString[len++] = ' ';
1949                templateString[len++] = '>';
1950             }
1951             templateString[len++] = '\0';
1952          }
1953          {
1954             Context context = SetupTemplatesContext(_class);
1955             if(freeType) FreeType(type);
1956             type = ProcessTypeString(templateString, false);
1957             freeType = true;
1958             FinishTemplatesContext(context);
1959          }
1960       }
1961
1962       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1963       {
1964          ProcessExpressionType(member.initializer.exp);
1965          if(!member.initializer.exp.expType)
1966          {
1967             if(inCompiler)
1968             {
1969                char expString[10240];
1970                expString[0] = '\0';
1971                PrintExpression(member.initializer.exp, expString);
1972                ChangeCh(expString, '\n', ' ');
1973                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1974             }
1975          }
1976          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1977          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false, true))
1978          {
1979             Compiler_Error($"incompatible instance method %s\n", ident.string);
1980          }
1981       }
1982       else if(member.initializer)
1983       {
1984          /*
1985          FreeType(member.exp.destType);
1986          member.exp.destType = type;
1987          if(member.exp.destType)
1988             member.exp.destType.refCount++;
1989          ProcessExpressionType(member.exp);
1990          */
1991
1992          ProcessInitializer(member.initializer, type);
1993       }
1994       if(freeType) FreeType(type);
1995    }
1996    else
1997    {
1998       if(_class && _class.type == unitClass)
1999       {
2000          if(member.initializer)
2001          {
2002             /*
2003             FreeType(member.exp.destType);
2004             member.exp.destType = MkClassType(_class.fullName);
2005             ProcessExpressionType(member.initializer, type);
2006             */
2007             Type type = MkClassType(_class.fullName);
2008             ProcessInitializer(member.initializer, type);
2009             FreeType(type);
2010          }
2011       }
2012       else
2013       {
2014          if(member.initializer)
2015          {
2016             //ProcessExpressionType(member.exp);
2017             ProcessInitializer(member.initializer, null);
2018          }
2019          if(ident)
2020          {
2021             if(method)
2022             {
2023                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
2024             }
2025             else if(_class)
2026             {
2027                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
2028                if(inCompiler)
2029                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
2030             }
2031          }
2032          else if(_class)
2033             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
2034       }
2035    }
2036 }
2037
2038 void ProcessInstantiationType(Instantiation inst)
2039 {
2040    yylloc = inst.loc;
2041    if(inst._class)
2042    {
2043       MembersInit members;
2044       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
2045       Class _class;
2046
2047       /*if(!inst._class.symbol)
2048          inst._class.symbol = FindClass(inst._class.name);*/
2049       classSym = inst._class.symbol;
2050       _class = classSym ? classSym.registered : null;
2051
2052       // DANGER: Patch for mutex not declaring its struct when not needed
2053       if(!_class || _class.type != noHeadClass)
2054          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
2055
2056       afterExternal = afterExternal ? afterExternal : curExternal;
2057
2058       if(inst.exp)
2059          ProcessExpressionType(inst.exp);
2060
2061       inst.isConstant = true;
2062       if(inst.members)
2063       {
2064          DataMember curMember = null;
2065          Class curClass = null;
2066          DataMember subMemberStack[256];
2067          int subMemberStackPos = 0;
2068
2069          for(members = inst.members->first; members; members = members.next)
2070          {
2071             switch(members.type)
2072             {
2073                case methodMembersInit:
2074                {
2075                   char name[1024];
2076                   static uint instMethodID = 0;
2077                   External external = curExternal;
2078                   Context context = curContext;
2079                   Declarator declarator = members.function.declarator;
2080                   Identifier nameID = GetDeclId(declarator);
2081                   char * unmangled = nameID ? nameID.string : null;
2082                   Expression exp;
2083                   External createdExternal = null;
2084
2085                   if(inCompiler)
2086                   {
2087                      char number[16];
2088                      //members.function.dontMangle = true;
2089                      strcpy(name, "__ecereInstMeth_");
2090                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2091                      strcat(name, "_");
2092                      strcat(name, nameID.string);
2093                      strcat(name, "_");
2094                      sprintf(number, "_%08d", instMethodID++);
2095                      strcat(name, number);
2096                      nameID.string = CopyString(name);
2097                   }
2098
2099                   // Do modifications here...
2100                   if(declarator)
2101                   {
2102                      Symbol symbol = declarator.symbol;
2103                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2104
2105                      if(method && method.type == virtualMethod)
2106                      {
2107                         symbol.method = method;
2108                         ProcessMethodType(method);
2109
2110                         if(!symbol.type.thisClass)
2111                         {
2112                            if(method.dataType.thisClass && currentClass &&
2113                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2114                            {
2115                               if(!currentClass.symbol)
2116                                  currentClass.symbol = FindClass(currentClass.fullName);
2117                               symbol.type.thisClass = currentClass.symbol;
2118                            }
2119                            else
2120                            {
2121                               if(!_class.symbol)
2122                                  _class.symbol = FindClass(_class.fullName);
2123                               symbol.type.thisClass = _class.symbol;
2124                            }
2125                         }
2126                         // TESTING THIS HERE:
2127                         DeclareType(symbol.type, true, true);
2128
2129                      }
2130                      else if(classSym)
2131                      {
2132                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2133                            unmangled, classSym.string);
2134                      }
2135                   }
2136
2137                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2138                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2139
2140                   if(nameID)
2141                   {
2142                      FreeSpecifier(nameID._class);
2143                      nameID._class = null;
2144                   }
2145
2146                   if(inCompiler)
2147                   {
2148                      //Type type = declarator.symbol.type;
2149                      External oldExternal = curExternal;
2150
2151                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2152                      // *** It was commented out for problems such as
2153                      /*
2154                            class VirtualDesktop : Window
2155                            {
2156                               clientSize = Size { };
2157                               Timer timer
2158                               {
2159                                  bool DelayExpired()
2160                                  {
2161                                     clientSize.w;
2162                                     return true;
2163                                  }
2164                               };
2165                            }
2166                      */
2167                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2168
2169                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2170
2171                      /*
2172                      if(strcmp(declarator.symbol.string, name))
2173                      {
2174                         printf("TOCHECK: Look out for this\n");
2175                         delete declarator.symbol.string;
2176                         declarator.symbol.string = CopyString(name);
2177                      }
2178
2179                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2180                      {
2181                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2182                         excludedSymbols->Remove(declarator.symbol);
2183                         globalContext.symbols.Add((BTNode)declarator.symbol);
2184                         if(strstr(declarator.symbol.string), "::")
2185                            globalContext.hasNameSpace = true;
2186
2187                      }
2188                      */
2189
2190                      //curExternal = curExternal.prev;
2191                      //afterExternal = afterExternal->next;
2192
2193                      //ProcessFunction(afterExternal->function);
2194
2195                      //curExternal = afterExternal;
2196                      {
2197                         External externalDecl;
2198                         externalDecl = MkExternalDeclaration(null);
2199                         ast->Insert(oldExternal.prev, externalDecl);
2200
2201                         // Which function does this process?
2202                         if(createdExternal.function)
2203                         {
2204                            ProcessFunction(createdExternal.function);
2205
2206                            //curExternal = oldExternal;
2207
2208                            {
2209                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2210
2211                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2212                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2213
2214                               //externalDecl = MkExternalDeclaration(decl);
2215
2216                               //***** ast->Insert(external.prev, externalDecl);
2217                               //ast->Insert(curExternal.prev, externalDecl);
2218                               externalDecl.declaration = decl;
2219                               if(decl.symbol && !decl.symbol.pointerExternal)
2220                                  decl.symbol.pointerExternal = externalDecl;
2221
2222                               // Trying this out...
2223                               declarator.symbol.pointerExternal = externalDecl;
2224                            }
2225                         }
2226                      }
2227                   }
2228                   else if(declarator)
2229                   {
2230                      curExternal = declarator.symbol.pointerExternal;
2231                      ProcessFunction((FunctionDefinition)members.function);
2232                   }
2233                   curExternal = external;
2234                   curContext = context;
2235
2236                   if(inCompiler)
2237                   {
2238                      FreeClassFunction(members.function);
2239
2240                      // In this pass, turn this into a MemberInitData
2241                      exp = QMkExpId(name);
2242                      members.type = dataMembersInit;
2243                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2244
2245                      delete unmangled;
2246                   }
2247                   break;
2248                }
2249                case dataMembersInit:
2250                {
2251                   if(members.dataMembers && classSym)
2252                   {
2253                      MemberInit member;
2254                      Location oldyyloc = yylloc;
2255                      for(member = members.dataMembers->first; member; member = member.next)
2256                      {
2257                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2258                         if(member.initializer && !member.initializer.isConstant)
2259                            inst.isConstant = false;
2260                      }
2261                      yylloc = oldyyloc;
2262                   }
2263                   break;
2264                }
2265             }
2266          }
2267       }
2268    }
2269 }
2270
2271 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2272 {
2273    // OPTIMIZATIONS: TESTING THIS...
2274    if(inCompiler)
2275    {
2276       if(type.kind == functionType)
2277       {
2278          Type param;
2279          if(declareParams)
2280          {
2281             for(param = type.params.first; param; param = param.next)
2282                DeclareType(param, declarePointers, true);
2283          }
2284          DeclareType(type.returnType, declarePointers, true);
2285       }
2286       else if(type.kind == pointerType && declarePointers)
2287          DeclareType(type.type, declarePointers, false);
2288       else if(type.kind == classType)
2289       {
2290          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2291             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2292       }
2293       else if(type.kind == structType || type.kind == unionType)
2294       {
2295          Type member;
2296          for(member = type.members.first; member; member = member.next)
2297             DeclareType(member, false, false);
2298       }
2299       else if(type.kind == arrayType)
2300          DeclareType(type.arrayType, declarePointers, false);
2301    }
2302 }
2303
2304 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2305 {
2306    ClassTemplateArgument * arg = null;
2307    int id = 0;
2308    ClassTemplateParameter curParam = null;
2309    Class sClass;
2310    for(sClass = _class; sClass; sClass = sClass.base)
2311    {
2312       id = 0;
2313       if(sClass.templateClass) sClass = sClass.templateClass;
2314       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2315       {
2316          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2317          {
2318             for(sClass = sClass.base; sClass; sClass = sClass.base)
2319             {
2320                if(sClass.templateClass) sClass = sClass.templateClass;
2321                id += sClass.templateParams.count;
2322             }
2323             break;
2324          }
2325          id++;
2326       }
2327       if(curParam) break;
2328    }
2329    if(curParam)
2330    {
2331       arg = &_class.templateArgs[id];
2332       if(arg && param.type == type)
2333          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2334    }
2335    return arg;
2336 }
2337
2338 public Context SetupTemplatesContext(Class _class)
2339 {
2340    Context context = PushContext();
2341    context.templateTypesOnly = true;
2342    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2343    {
2344       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2345       for(; param; param = param.next)
2346       {
2347          if(param.type == type && param.identifier)
2348          {
2349             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2350             curContext.templateTypes.Add((BTNode)type);
2351          }
2352       }
2353    }
2354    else if(_class)
2355    {
2356       Class sClass;
2357       for(sClass = _class; sClass; sClass = sClass.base)
2358       {
2359          ClassTemplateParameter p;
2360          for(p = sClass.templateParams.first; p; p = p.next)
2361          {
2362             //OldList * specs = MkList();
2363             //Declarator decl = null;
2364             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2365             if(p.type == type)
2366             {
2367                TemplateParameter param = p.param;
2368                TemplatedType type;
2369                if(!param)
2370                {
2371                   // ADD DATA TYPE HERE...
2372                   p.param = param = TemplateParameter
2373                   {
2374                      identifier = MkIdentifier(p.name), type = p.type,
2375                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2376                   };
2377                }
2378                type = TemplatedType { key = (uintptr)p.name, param = param };
2379                curContext.templateTypes.Add((BTNode)type);
2380             }
2381          }
2382       }
2383    }
2384    return context;
2385 }
2386
2387 public void FinishTemplatesContext(Context context)
2388 {
2389    PopContext(context);
2390    FreeContext(context);
2391    delete context;
2392 }
2393
2394 public void ProcessMethodType(Method method)
2395 {
2396    if(!method.dataType)
2397    {
2398       Context context = SetupTemplatesContext(method._class);
2399
2400       method.dataType = ProcessTypeString(method.dataTypeString, false);
2401
2402       FinishTemplatesContext(context);
2403
2404       if(method.type != virtualMethod && method.dataType)
2405       {
2406          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2407          {
2408             if(!method._class.symbol)
2409                method._class.symbol = FindClass(method._class.fullName);
2410             method.dataType.thisClass = method._class.symbol;
2411          }
2412       }
2413
2414       // Why was this commented out? Working fine without now...
2415
2416       /*
2417       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2418          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2419          */
2420    }
2421
2422    /*
2423    if(type)
2424    {
2425       char * par = strstr(type, "(");
2426       char * classOp = null;
2427       int classOpLen = 0;
2428       if(par)
2429       {
2430          int c;
2431          for(c = par-type-1; c >= 0; c++)
2432          {
2433             if(type[c] == ':' && type[c+1] == ':')
2434             {
2435                classOp = type + c - 1;
2436                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2437                {
2438                   classOp--;
2439                   classOpLen++;
2440                }
2441                break;
2442             }
2443             else if(!isspace(type[c]))
2444                break;
2445          }
2446       }
2447       if(classOp)
2448       {
2449          char temp[1024];
2450          int typeLen = strlen(type);
2451          memcpy(temp, classOp, classOpLen);
2452          temp[classOpLen] = '\0';
2453          if(temp[0])
2454             _class = eSystem_FindClass(module, temp);
2455          else
2456             _class = null;
2457          method.dataTypeString = new char[typeLen - classOpLen + 1];
2458          memcpy(method.dataTypeString, type, classOp - type);
2459          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2460       }
2461       else
2462          method.dataTypeString = type;
2463    }
2464    */
2465 }
2466
2467
2468 public void ProcessPropertyType(Property prop)
2469 {
2470    if(!prop.dataType)
2471    {
2472       Context context = SetupTemplatesContext(prop._class);
2473       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2474       FinishTemplatesContext(context);
2475    }
2476 }
2477
2478 public void DeclareMethod(Method method, const char * name)
2479 {
2480    Symbol symbol = method.symbol;
2481    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2482    {
2483       //bool imported = false;
2484       bool dllImport = false;
2485
2486       if(!method.dataType)
2487          method.dataType = ProcessTypeString(method.dataTypeString, false);
2488
2489       if(!symbol || symbol._import || method.type == virtualMethod)
2490       {
2491          if(!symbol || method.type == virtualMethod)
2492          {
2493             Symbol classSym;
2494             if(!method._class.symbol)
2495                method._class.symbol = FindClass(method._class.fullName);
2496             classSym = method._class.symbol;
2497             if(!classSym._import)
2498             {
2499                ModuleImport module;
2500
2501                if(method._class.module && method._class.module.name)
2502                   module = FindModule(method._class.module);
2503                else
2504                   module = mainModule;
2505                classSym._import = ClassImport
2506                {
2507                   name = CopyString(method._class.fullName);
2508                   isRemote = method._class.isRemote;
2509                };
2510                module.classes.Add(classSym._import);
2511             }
2512             if(!symbol)
2513             {
2514                symbol = method.symbol = Symbol { };
2515             }
2516             if(!symbol._import)
2517             {
2518                symbol._import = (ClassImport)MethodImport
2519                {
2520                   name = CopyString(method.name);
2521                   isVirtual = method.type == virtualMethod;
2522                };
2523                classSym._import.methods.Add(symbol._import);
2524             }
2525             if(!symbol)
2526             {
2527                // Set the symbol type
2528                /*
2529                if(!type.thisClass)
2530                {
2531                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2532                }
2533                else if(type.thisClass == (void *)-1)
2534                {
2535                   type.thisClass = null;
2536                }
2537                */
2538                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2539                symbol.type = method.dataType;
2540                if(symbol.type) symbol.type.refCount++;
2541             }
2542             /*
2543             if(!method.thisClass || strcmp(method.thisClass, "void"))
2544                symbol.type.params.Insert(null,
2545                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2546             */
2547          }
2548          if(!method.dataType.dllExport)
2549          {
2550             //imported = true;
2551             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2552                dllImport = true;
2553          }
2554       }
2555
2556       /* MOVING THIS UP
2557       if(!method.dataType)
2558          method.dataType = ((Symbol)method.symbol).type;
2559          //ProcessMethodType(method);
2560       */
2561
2562       if(method.type != virtualMethod && method.dataType)
2563          DeclareType(method.dataType, true, true);
2564
2565       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2566       {
2567          // We need a declaration here :)
2568          Declaration decl;
2569          OldList * specifiers, * declarators;
2570          Declarator d;
2571          Declarator funcDecl;
2572          External external;
2573
2574          specifiers = MkList();
2575          declarators = MkList();
2576
2577          //if(imported)
2578          if(dllImport)
2579             ListAdd(specifiers, MkSpecifier(EXTERN));
2580          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2581             ListAdd(specifiers, MkSpecifier(STATIC));
2582
2583          if(method.type == virtualMethod)
2584          {
2585             ListAdd(specifiers, MkSpecifier(INT));
2586             d = MkDeclaratorIdentifier(MkIdentifier(name));
2587          }
2588          else
2589          {
2590             d = MkDeclaratorIdentifier(MkIdentifier(name));
2591             //if(imported)
2592             if(dllImport)
2593                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2594             {
2595                Context context = SetupTemplatesContext(method._class);
2596                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2597                FinishTemplatesContext(context);
2598             }
2599             funcDecl = GetFuncDecl(d);
2600
2601             if(dllImport)
2602             {
2603                Specifier spec, next;
2604                for(spec = specifiers->first; spec; spec = next)
2605                {
2606                   next = spec.next;
2607                   if(spec.type == extendedSpecifier)
2608                   {
2609                      specifiers->Remove(spec);
2610                      FreeSpecifier(spec);
2611                   }
2612                }
2613             }
2614
2615             // Add this parameter if not a static method
2616             if(method.dataType && !method.dataType.staticMethod)
2617             {
2618                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2619                {
2620                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2621                   TypeName thisParam = MkTypeName(MkListOne(
2622                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2623                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2624                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2625                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2626
2627                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2628                   {
2629                      TypeName param = funcDecl.function.parameters->first;
2630                      funcDecl.function.parameters->Remove(param);
2631                      FreeTypeName(param);
2632                   }
2633
2634                   if(!funcDecl.function.parameters)
2635                      funcDecl.function.parameters = MkList();
2636                   funcDecl.function.parameters->Insert(null, thisParam);
2637                }
2638             }
2639             // Make sure we don't have empty parameter declarations for static methods...
2640             /*
2641             else if(!funcDecl.function.parameters)
2642             {
2643                funcDecl.function.parameters = MkList();
2644                funcDecl.function.parameters->Insert(null,
2645                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2646             }*/
2647          }
2648          // TESTING THIS:
2649          ProcessDeclarator(d);
2650
2651          ListAdd(declarators, MkInitDeclarator(d, null));
2652
2653          decl = MkDeclaration(specifiers, declarators);
2654
2655          ReplaceThisClassSpecifiers(specifiers, method._class);
2656
2657          // Keep a different symbol for the function definition than the declaration...
2658          if(symbol.pointerExternal)
2659          {
2660             Symbol functionSymbol { };
2661
2662             // Copy symbol
2663             {
2664                *functionSymbol = *symbol;
2665                functionSymbol.string = CopyString(symbol.string);
2666                if(functionSymbol.type)
2667                   functionSymbol.type.refCount++;
2668             }
2669
2670             excludedSymbols->Add(functionSymbol);
2671             symbol.pointerExternal.symbol = functionSymbol;
2672          }
2673          external = MkExternalDeclaration(decl);
2674          if(curExternal)
2675             ast->Insert(curExternal ? curExternal.prev : null, external);
2676          external.symbol = symbol;
2677          symbol.pointerExternal = external;
2678       }
2679       else if(ast)
2680       {
2681          // Move declaration higher...
2682          ast->Move(symbol.pointerExternal, curExternal.prev);
2683       }
2684
2685       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2686    }
2687 }
2688
2689 char * ReplaceThisClass(Class _class)
2690 {
2691    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2692    {
2693       bool first = true;
2694       int p = 0;
2695       ClassTemplateParameter param;
2696       int lastParam = -1;
2697
2698       char className[1024];
2699       strcpy(className, _class.fullName);
2700       for(param = _class.templateParams.first; param; param = param.next)
2701       {
2702          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2703          {
2704             if(first) strcat(className, "<");
2705             if(!first) strcat(className, ", ");
2706             if(lastParam + 1 != p)
2707             {
2708                strcat(className, param.name);
2709                strcat(className, " = ");
2710             }
2711             strcat(className, param.name);
2712             first = false;
2713             lastParam = p;
2714          }
2715          p++;
2716       }
2717       if(!first)
2718       {
2719          int len = strlen(className);
2720          if(className[len-1] == '>') className[len++] = ' ';
2721          className[len++] = '>';
2722          className[len++] = '\0';
2723       }
2724       return CopyString(className);
2725    }
2726    else
2727       return CopyString(_class.fullName);
2728 }
2729
2730 Type ReplaceThisClassType(Class _class)
2731 {
2732    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2733    {
2734       bool first = true;
2735       int p = 0;
2736       ClassTemplateParameter param;
2737       int lastParam = -1;
2738       char className[1024];
2739       strcpy(className, _class.fullName);
2740
2741       for(param = _class.templateParams.first; param; param = param.next)
2742       {
2743          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2744          {
2745             if(first) strcat(className, "<");
2746             if(!first) strcat(className, ", ");
2747             if(lastParam + 1 != p)
2748             {
2749                strcat(className, param.name);
2750                strcat(className, " = ");
2751             }
2752             strcat(className, param.name);
2753             first = false;
2754             lastParam = p;
2755          }
2756          p++;
2757       }
2758       if(!first)
2759       {
2760          int len = strlen(className);
2761          if(className[len-1] == '>') className[len++] = ' ';
2762          className[len++] = '>';
2763          className[len++] = '\0';
2764       }
2765       return MkClassType(className);
2766       //return ProcessTypeString(className, false);
2767    }
2768    else
2769    {
2770       return MkClassType(_class.fullName);
2771       //return ProcessTypeString(_class.fullName, false);
2772    }
2773 }
2774
2775 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2776 {
2777    if(specs != null && _class)
2778    {
2779       Specifier spec;
2780       for(spec = specs.first; spec; spec = spec.next)
2781       {
2782          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2783          {
2784             spec.type = nameSpecifier;
2785             spec.name = ReplaceThisClass(_class);
2786             spec.symbol = FindClass(spec.name); //_class.symbol;
2787          }
2788       }
2789    }
2790 }
2791
2792 // Returns imported or not
2793 bool DeclareFunction(GlobalFunction function, char * name)
2794 {
2795    Symbol symbol = function.symbol;
2796    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2797    {
2798       bool imported = false;
2799       bool dllImport = false;
2800
2801       if(!function.dataType)
2802       {
2803          function.dataType = ProcessTypeString(function.dataTypeString, false);
2804          if(!function.dataType.thisClass)
2805             function.dataType.staticMethod = true;
2806       }
2807
2808       if(inCompiler)
2809       {
2810          if(!symbol)
2811          {
2812             ModuleImport module = FindModule(function.module);
2813             // WARNING: This is not added anywhere...
2814             symbol = function.symbol = Symbol {  };
2815
2816             if(module.name)
2817             {
2818                if(!function.dataType.dllExport)
2819                {
2820                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2821                   module.functions.Add(symbol._import);
2822                }
2823             }
2824             // Set the symbol type
2825             {
2826                symbol.type = ProcessTypeString(function.dataTypeString, false);
2827                if(!symbol.type.thisClass)
2828                   symbol.type.staticMethod = true;
2829             }
2830          }
2831          imported = symbol._import ? true : false;
2832          if(imported && function.module != privateModule && function.module.importType != staticImport)
2833             dllImport = true;
2834       }
2835
2836       DeclareType(function.dataType, true, true);
2837
2838       if(inCompiler)
2839       {
2840          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2841          {
2842             // We need a declaration here :)
2843             Declaration decl;
2844             OldList * specifiers, * declarators;
2845             Declarator d;
2846             Declarator funcDecl;
2847             External external;
2848
2849             specifiers = MkList();
2850             declarators = MkList();
2851
2852             //if(imported)
2853                ListAdd(specifiers, MkSpecifier(EXTERN));
2854             /*
2855             else
2856                ListAdd(specifiers, MkSpecifier(STATIC));
2857             */
2858
2859             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2860             //if(imported)
2861             if(dllImport)
2862                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2863
2864             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2865             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2866             if(function.module.importType == staticImport)
2867             {
2868                Specifier spec;
2869                for(spec = specifiers->first; spec; spec = spec.next)
2870                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2871                   {
2872                      specifiers->Remove(spec);
2873                      FreeSpecifier(spec);
2874                      break;
2875                   }
2876             }
2877
2878             funcDecl = GetFuncDecl(d);
2879
2880             // Make sure we don't have empty parameter declarations for static methods...
2881             if(funcDecl && !funcDecl.function.parameters)
2882             {
2883                funcDecl.function.parameters = MkList();
2884                funcDecl.function.parameters->Insert(null,
2885                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2886             }
2887
2888             ListAdd(declarators, MkInitDeclarator(d, null));
2889
2890             {
2891                Context oldCtx = curContext;
2892                curContext = globalContext;
2893                decl = MkDeclaration(specifiers, declarators);
2894                curContext = oldCtx;
2895             }
2896
2897             // Keep a different symbol for the function definition than the declaration...
2898             if(symbol.pointerExternal)
2899             {
2900                Symbol functionSymbol { };
2901                // Copy symbol
2902                {
2903                   *functionSymbol = *symbol;
2904                   functionSymbol.string = CopyString(symbol.string);
2905                   if(functionSymbol.type)
2906                      functionSymbol.type.refCount++;
2907                }
2908
2909                excludedSymbols->Add(functionSymbol);
2910
2911                symbol.pointerExternal.symbol = functionSymbol;
2912             }
2913             external = MkExternalDeclaration(decl);
2914             if(curExternal)
2915                ast->Insert(curExternal.prev, external);
2916             external.symbol = symbol;
2917             symbol.pointerExternal = external;
2918          }
2919          else
2920          {
2921             // Move declaration higher...
2922             ast->Move(symbol.pointerExternal, curExternal.prev);
2923          }
2924
2925          if(curExternal)
2926             symbol.id = curExternal.symbol.idCode;
2927       }
2928    }
2929    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2930 }
2931
2932 void DeclareGlobalData(GlobalData data)
2933 {
2934    Symbol symbol = data.symbol;
2935    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2936    {
2937       if(inCompiler)
2938       {
2939          if(!symbol)
2940             symbol = data.symbol = Symbol { };
2941       }
2942       if(!data.dataType)
2943          data.dataType = ProcessTypeString(data.dataTypeString, false);
2944       DeclareType(data.dataType, true, true);
2945       if(inCompiler)
2946       {
2947          if(!symbol.pointerExternal)
2948          {
2949             // We need a declaration here :)
2950             Declaration decl;
2951             OldList * specifiers, * declarators;
2952             Declarator d;
2953             External external;
2954
2955             specifiers = MkList();
2956             declarators = MkList();
2957
2958             ListAdd(specifiers, MkSpecifier(EXTERN));
2959             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2960             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2961
2962             ListAdd(declarators, MkInitDeclarator(d, null));
2963
2964             decl = MkDeclaration(specifiers, declarators);
2965             external = MkExternalDeclaration(decl);
2966             if(curExternal)
2967                ast->Insert(curExternal.prev, external);
2968             external.symbol = symbol;
2969             symbol.pointerExternal = external;
2970          }
2971          else
2972          {
2973             // Move declaration higher...
2974             ast->Move(symbol.pointerExternal, curExternal.prev);
2975          }
2976
2977          if(curExternal)
2978             symbol.id = curExternal.symbol.idCode;
2979       }
2980    }
2981 }
2982
2983 class Conversion : struct
2984 {
2985    Conversion prev, next;
2986    Property convert;
2987    bool isGet;
2988    Type resultType;
2989 };
2990
2991 static bool CheckConstCompatibility(Type source, Type dest, bool warn)
2992 {
2993    bool status = true;
2994    if(((source.kind == classType && source._class && source._class.registered) || source.kind == arrayType || source.kind == pointerType) &&
2995       ((dest.kind == classType && dest._class && dest._class.registered) || /*dest.kind == arrayType || */dest.kind == pointerType))
2996    {
2997       Class sourceClass = source.kind == classType ? source._class.registered : null;
2998       Class destClass = dest.kind == classType ? dest._class.registered : null;
2999       if((!sourceClass || (sourceClass && sourceClass.type == normalClass && !sourceClass.structSize)) &&
3000          (!destClass || (destClass && destClass.type == normalClass && !destClass.structSize)))
3001       {
3002          Type sourceType = source, destType = dest;
3003          while((sourceType.kind == pointerType || sourceType.kind == arrayType) && sourceType.type) sourceType = sourceType.type;
3004          while((destType.kind == pointerType || destType.kind == arrayType) && destType.type) destType = destType.type;
3005          if(!destType.constant && sourceType.constant)
3006          {
3007             status = false;
3008             if(warn)
3009                Compiler_Warning($"discarding const qualifier\n");
3010          }
3011       }
3012    }
3013    return status;
3014 }
3015
3016 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams,
3017                        bool isConversionExploration, bool warnConst)
3018 {
3019    if(source && dest)
3020    {
3021       if(warnConst)
3022          CheckConstCompatibility(source, dest, true);
3023       // Property convert;
3024
3025       if(source.kind == templateType && dest.kind != templateType)
3026       {
3027          Type type = ProcessTemplateParameterType(source.templateParameter);
3028          if(type) source = type;
3029       }
3030
3031       if(dest.kind == templateType && source.kind != templateType)
3032       {
3033          Type type = ProcessTemplateParameterType(dest.templateParameter);
3034          if(type) dest = type;
3035       }
3036
3037       if(dest.classObjectType == typedObject && dest.kind != functionType)
3038       {
3039          if(source.classObjectType != anyObject)
3040             return true;
3041          else
3042          {
3043             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
3044             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
3045             {
3046                return true;
3047             }
3048          }
3049       }
3050       else
3051       {
3052          if(source.kind != functionType && source.classObjectType == anyObject)
3053             return true;
3054          if(dest.kind != functionType && dest.classObjectType == anyObject && source.classObjectType != typedObject)
3055             return true;
3056       }
3057
3058       if((dest.kind == structType && source.kind == structType) ||
3059          (dest.kind == unionType && source.kind == unionType))
3060       {
3061          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
3062              (source.members.first && source.members.first == dest.members.first))
3063             return true;
3064       }
3065
3066       if(dest.kind == ellipsisType && source.kind != voidType)
3067          return true;
3068
3069       if(dest.kind == pointerType && dest.type.kind == voidType &&
3070          ((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))
3071          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
3072
3073          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
3074
3075          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
3076          return true;
3077       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
3078          ((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))
3079          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
3080          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
3081
3082          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
3083          return true;
3084
3085       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
3086       {
3087          if(source._class.registered && source._class.registered.type == unitClass)
3088          {
3089             if(conversions != null)
3090             {
3091                if(source._class.registered == dest._class.registered)
3092                   return true;
3093             }
3094             else
3095             {
3096                Class sourceBase, destBase;
3097                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
3098                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
3099                if(sourceBase == destBase)
3100                   return true;
3101             }
3102          }
3103          // Don't match enum inheriting from other enum if resolving enumeration values
3104          // TESTING: !dest.classObjectType
3105          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
3106             (enumBaseType ||
3107                (!source._class.registered || source._class.registered.type != enumClass) ||
3108                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
3109             return true;
3110          else
3111          {
3112             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
3113             if(enumBaseType &&
3114                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
3115                ((source._class && source._class.registered && source._class.registered.type != enumClass) || source.kind == classType)) // Added this here for a base enum to be acceptable for a derived enum (#139)
3116             {
3117                if(eClass_IsDerived(dest._class.registered, source._class.registered))
3118                {
3119                   return true;
3120                }
3121             }
3122          }
3123       }
3124
3125       // JUST ADDED THIS...
3126       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3127          return true;
3128
3129       if(doConversion)
3130       {
3131          // Just added this for Straight conversion of ColorAlpha => Color
3132          if(source.kind == classType)
3133          {
3134             Class _class;
3135             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3136             {
3137                Property convert;
3138                for(convert = _class.conversions.first; convert; convert = convert.next)
3139                {
3140                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3141                   {
3142                      Conversion after = (conversions != null) ? conversions.last : null;
3143
3144                      if(!convert.dataType)
3145                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3146                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3147                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3148                         MatchTypes(convert.dataType, dest, conversions, null, null,
3149                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3150                               convert.dataType.kind == classType, false, true, warnConst))
3151                      {
3152                         if(!conversions && !convert.Get)
3153                            return true;
3154                         else if(conversions != null)
3155                         {
3156                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3157                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3158                               (dest.kind != classType || dest._class.registered != _class.base))
3159                               return true;
3160                            else
3161                            {
3162                               Conversion conv { convert = convert, isGet = true };
3163                               // conversions.Add(conv);
3164                               conversions.Insert(after, conv);
3165
3166                               return true;
3167                            }
3168                         }
3169                      }
3170                   }
3171                }
3172             }
3173          }
3174
3175          // MOVING THIS??
3176
3177          if(dest.kind == classType)
3178          {
3179             Class _class;
3180             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3181             {
3182                Property convert;
3183                for(convert = _class.conversions.first; convert; convert = convert.next)
3184                {
3185                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3186                   {
3187                      Type constType = null;
3188                      bool success = false;
3189                      // Conversion after = (conversions != null) ? conversions.last : null;
3190
3191                      if(!convert.dataType)
3192                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3193
3194                      if(warnConst && convert.dataType.kind == pointerType && convert.dataType.type && dest.constant)
3195                      {
3196                         Type ptrType { };
3197                         constType = { kind = pointerType, refCount = 1, type = ptrType };
3198                         CopyTypeInto(ptrType, convert.dataType.type);
3199                         ptrType.constant = true;
3200                      }
3201
3202                      // Just added this equality check to prevent recursion.... Make it safer?
3203                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3204                      if((constType || convert.dataType != dest) && MatchTypes(source, constType ? constType : convert.dataType, conversions, null, null, true, false /*true*/, false, true, warnConst))
3205                      {
3206                         if(!conversions && !convert.Set)
3207                            success = true;
3208                         else if(conversions != null)
3209                         {
3210                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3211                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3212                               (source.kind != classType || source._class.registered != _class.base))
3213                               success = true;
3214                            else
3215                            {
3216                               // *** Testing this! ***
3217                               Conversion conv { convert = convert };
3218                               conversions.Add(conv);
3219                               //conversions.Insert(after, conv);
3220                               success = true;
3221                            }
3222                         }
3223                      }
3224                      if(constType)
3225                         FreeType(constType);
3226                      if(success)
3227                         return true;
3228                   }
3229                }
3230             }
3231             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3232             {
3233                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3234                   (source.kind != classType || source._class.registered.type != structClass))
3235                   return true;
3236             }*/
3237
3238             // TESTING THIS... IS THIS OK??
3239             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3240             {
3241                if(!dest._class.registered.dataType)
3242                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3243                // Only support this for classes...
3244                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3245                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3246                {
3247                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false, warnConst))
3248                   {
3249                      return true;
3250                   }
3251                }
3252             }
3253          }
3254
3255          // Moved this lower
3256          if(source.kind == classType)
3257          {
3258             Class _class;
3259             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3260             {
3261                Property convert;
3262                for(convert = _class.conversions.first; convert; convert = convert.next)
3263                {
3264                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3265                   {
3266                      Conversion after = (conversions != null) ? conversions.last : null;
3267
3268                      if(!convert.dataType)
3269                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3270                      if(convert.dataType != source &&
3271                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3272                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true, warnConst))
3273                      {
3274                         if(!conversions && !convert.Get)
3275                            return true;
3276                         else if(conversions != null)
3277                         {
3278                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3279                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3280                               (dest.kind != classType || dest._class.registered != _class.base))
3281                               return true;
3282                            else
3283                            {
3284                               Conversion conv { convert = convert, isGet = true };
3285
3286                               // conversions.Add(conv);
3287                               conversions.Insert(after, conv);
3288                               return true;
3289                            }
3290                         }
3291                      }
3292                   }
3293                }
3294             }
3295
3296             // TESTING THIS... IS THIS OK??
3297             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3298             {
3299                if(!source._class.registered.dataType)
3300                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3301                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3302                {
3303                   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))
3304                      return true;
3305                   // For bool to be accepted by byte, short, etc.
3306                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false, warnConst))
3307                      return true;
3308                }
3309             }
3310          }
3311       }
3312
3313       if(source.kind == classType || source.kind == subClassType)
3314          ;
3315       else if(dest.kind == source.kind &&
3316          (dest.kind != structType && dest.kind != unionType &&
3317           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3318           return true;
3319       // RECENTLY ADDED THESE
3320       else if(dest.kind == doubleType && source.kind == floatType)
3321          return true;
3322       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3323          return true;
3324       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3325          return true;
3326       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3327          return true;
3328       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3329          return true;
3330       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3331          return true;
3332       else if(source.kind == enumType &&
3333          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3334           return true;
3335       else if(dest.kind == enumType && !isConversionExploration &&
3336          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3337           return true;
3338       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3339               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3340       {
3341          Type paramSource, paramDest;
3342
3343          if(dest.kind == methodType)
3344             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3345          if(source.kind == methodType)
3346             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3347
3348          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3349          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3350          if(dest.kind == methodType)
3351             dest = dest.method.dataType;
3352          if(source.kind == methodType)
3353             source = source.method.dataType;
3354
3355          paramSource = source.params.first;
3356          if(paramSource && paramSource.kind == voidType) paramSource = null;
3357          paramDest = dest.params.first;
3358          if(paramDest && paramDest.kind == voidType) paramDest = null;
3359
3360
3361          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3362             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3363          {
3364             // Source thisClass must be derived from destination thisClass
3365             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3366                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3367             {
3368                if(paramDest && paramDest.kind == classType)
3369                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3370                else
3371                   Compiler_Error($"method class should not take an object\n");
3372                return false;
3373             }
3374             paramDest = paramDest.next;
3375          }
3376          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3377          {
3378             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3379             {
3380                if(dest.thisClass)
3381                {
3382                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3383                   {
3384                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3385                      return false;
3386                   }
3387                }
3388                else
3389                {
3390                   // THIS WAS BACKWARDS:
3391                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3392                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3393                   {
3394                      if(owningClassDest)
3395                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3396                      else
3397                         Compiler_Error($"overriding class expected to be derived from method class\n");
3398                      return false;
3399                   }
3400                }
3401                paramSource = paramSource.next;
3402             }
3403             else
3404             {
3405                if(dest.thisClass)
3406                {
3407                   // Source thisClass must be derived from destination thisClass
3408                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3409                   {
3410                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3411                      return false;
3412                   }
3413                }
3414                else
3415                {
3416                   // THIS WAS BACKWARDS TOO??
3417                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3418                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3419                   {
3420                      //if(owningClass)
3421                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3422                      //else
3423                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3424                      return false;
3425                   }
3426                }
3427             }
3428          }
3429
3430
3431          // Source return type must be derived from destination return type
3432          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false, warnConst))
3433          {
3434             Compiler_Warning($"incompatible return type for function\n");
3435             return false;
3436          }
3437          // The const check is backwards from the MatchTypes above (for derivative classes checks)
3438          else
3439             CheckConstCompatibility(dest.returnType, source.returnType, true);
3440
3441          // Check parameters
3442
3443          for(; paramDest; paramDest = paramDest.next)
3444          {
3445             if(!paramSource)
3446             {
3447                //Compiler_Warning($"not enough parameters\n");
3448                Compiler_Error($"not enough parameters\n");
3449                return false;
3450             }
3451             {
3452                Type paramDestType = paramDest;
3453                Type paramSourceType = paramSource;
3454                Type type = paramDestType;
3455
3456                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3457                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3458                   paramSource.kind != templateType)
3459                {
3460                   int id = 0;
3461                   ClassTemplateParameter curParam = null;
3462                   Class sClass;
3463                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3464                   {
3465                      id = 0;
3466                      if(sClass.templateClass) sClass = sClass.templateClass;
3467                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3468                      {
3469                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3470                         {
3471                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3472                            {
3473                               if(sClass.templateClass) sClass = sClass.templateClass;
3474                               id += sClass.templateParams.count;
3475                            }
3476                            break;
3477                         }
3478                         id++;
3479                      }
3480                      if(curParam) break;
3481                   }
3482
3483                   if(curParam)
3484                   {
3485                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3486                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3487                   }
3488                }
3489
3490                // paramDest must be derived from paramSource
3491                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false, warnConst) &&
3492                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false, warnConst)))
3493                {
3494                   char type[1024];
3495                   type[0] = 0;
3496                   PrintType(paramDest, type, false, true);
3497                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3498
3499                   if(paramDestType != paramDest)
3500                      FreeType(paramDestType);
3501                   return false;
3502                }
3503                if(paramDestType != paramDest)
3504                   FreeType(paramDestType);
3505             }
3506
3507             paramSource = paramSource.next;
3508          }
3509          if(paramSource)
3510          {
3511             Compiler_Error($"too many parameters\n");
3512             return false;
3513          }
3514          return true;
3515       }
3516       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3517       {
3518          return true;
3519       }
3520       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3521          (source.kind == arrayType || source.kind == pointerType))
3522       {
3523          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false, warnConst))
3524             return true;
3525       }
3526    }
3527    return false;
3528 }
3529
3530 static void FreeConvert(Conversion convert)
3531 {
3532    if(convert.resultType)
3533       FreeType(convert.resultType);
3534 }
3535
3536 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3537                               char * string, OldList conversions)
3538 {
3539    BTNamedLink link;
3540
3541    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3542    {
3543       Class _class = link.data;
3544       if(_class.type == enumClass)
3545       {
3546          OldList converts { };
3547          Type type { };
3548          type.kind = classType;
3549
3550          if(!_class.symbol)
3551             _class.symbol = FindClass(_class.fullName);
3552          type._class = _class.symbol;
3553
3554          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false, false))
3555          {
3556             NamedLink value;
3557             Class enumClass = eSystem_FindClass(privateModule, "enum");
3558             if(enumClass)
3559             {
3560                Class baseClass;
3561                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3562                {
3563                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3564                   for(value = e.values.first; value; value = value.next)
3565                   {
3566                      if(!strcmp(value.name, string))
3567                         break;
3568                   }
3569                   if(value)
3570                   {
3571                      FreeExpContents(sourceExp);
3572                      FreeType(sourceExp.expType);
3573
3574                      sourceExp.isConstant = true;
3575                      sourceExp.expType = MkClassType(baseClass.fullName);
3576                      //if(inCompiler)
3577                      {
3578                         char constant[256];
3579                         sourceExp.type = constantExp;
3580                         if(!strcmp(baseClass.dataTypeString, "int"))
3581                            sprintf(constant, "%d",(int)value.data);
3582                         else
3583                            sprintf(constant, "0x%X",(int)value.data);
3584                         sourceExp.constant = CopyString(constant);
3585                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3586                      }
3587
3588                      while(converts.first)
3589                      {
3590                         Conversion convert = converts.first;
3591                         converts.Remove(convert);
3592                         conversions.Add(convert);
3593                      }
3594                      delete type;
3595                      return true;
3596                   }
3597                }
3598             }
3599          }
3600          if(converts.first)
3601             converts.Free(FreeConvert);
3602          delete type;
3603       }
3604    }
3605    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3606       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3607          return true;
3608    return false;
3609 }
3610
3611 public bool ModuleVisibility(Module searchIn, Module searchFor)
3612 {
3613    SubModule subModule;
3614
3615    if(searchFor == searchIn)
3616       return true;
3617
3618    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3619    {
3620       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3621       {
3622          if(ModuleVisibility(subModule.module, searchFor))
3623             return true;
3624       }
3625    }
3626    return false;
3627 }
3628
3629 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3630 {
3631    Module module;
3632
3633    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3634       return true;
3635    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3636       return true;
3637    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3638       return true;
3639
3640    for(module = mainModule.application.allModules.first; module; module = module.next)
3641    {
3642       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3643          return true;
3644    }
3645    return false;
3646 }
3647
3648 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla, bool warnConst)
3649 {
3650    Type source;
3651    Type realDest = dest;
3652    Type backupSourceExpType = null;
3653    Expression computedExp = sourceExp;
3654    dest.refCount++;
3655
3656    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3657       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3658    {
3659       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3660       ComputeExpression(computedExp /*sourceExp*/);
3661    }
3662
3663    source = sourceExp.expType;
3664
3665    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3666    {
3667       if(computedExp != sourceExp)
3668       {
3669          FreeExpression(computedExp);
3670          computedExp = sourceExp;
3671       }
3672       FreeType(dest);
3673       return true;
3674    }
3675
3676    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3677    {
3678        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3679        {
3680           Class sourceBase, destBase;
3681           for(sourceBase = source._class.registered;
3682               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3683               sourceBase = sourceBase.base);
3684           for(destBase = dest._class.registered;
3685               destBase && destBase.base && destBase.base.type != systemClass;
3686               destBase = destBase.base);
3687           //if(source._class.registered == dest._class.registered)
3688           if(sourceBase == destBase)
3689           {
3690             if(computedExp != sourceExp)
3691             {
3692                FreeExpression(computedExp);
3693                computedExp = sourceExp;
3694             }
3695             FreeType(dest);
3696             return true;
3697          }
3698       }
3699    }
3700
3701    if(source)
3702    {
3703       OldList * specs;
3704       bool flag = false;
3705       int64 value = MAXINT;
3706
3707       source.refCount++;
3708
3709       if(computedExp.type == constantExp)
3710       {
3711          if(source.isSigned)
3712             value = strtoll(computedExp.constant, null, 0);
3713          else
3714             value = strtoull(computedExp.constant, null, 0);
3715       }
3716       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3717       {
3718          if(source.isSigned)
3719             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3720          else
3721             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3722       }
3723       if(computedExp != sourceExp)
3724       {
3725          FreeExpression(computedExp);
3726          computedExp = sourceExp;
3727       }
3728
3729       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3730          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3731       {
3732          FreeType(source);
3733          source = Type { kind = intType, isSigned = false, refCount = 1 };
3734       }
3735
3736       if(dest.kind == classType)
3737       {
3738          Class _class = dest._class ? dest._class.registered : null;
3739
3740          if(_class && _class.type == unitClass)
3741          {
3742             if(source.kind != classType)
3743             {
3744                Type tempType { };
3745                Type tempDest, tempSource;
3746
3747                for(; _class.base.type != systemClass; _class = _class.base);
3748                tempSource = dest;
3749                tempDest = tempType;
3750
3751                tempType.kind = classType;
3752                if(!_class.symbol)
3753                   _class.symbol = FindClass(_class.fullName);
3754
3755                tempType._class = _class.symbol;
3756                tempType.truth = dest.truth;
3757                if(tempType._class)
3758                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3759
3760                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3761                backupSourceExpType = sourceExp.expType;
3762                sourceExp.expType = dest; dest.refCount++;
3763                //sourceExp.expType = MkClassType(_class.fullName);
3764                flag = true;
3765
3766                delete tempType;
3767             }
3768          }
3769
3770
3771          // Why wasn't there something like this?
3772          if(_class && _class.type == bitClass && source.kind != classType)
3773          {
3774             if(!dest._class.registered.dataType)
3775                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3776             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false, warnConst))
3777             {
3778                FreeType(source);
3779                FreeType(sourceExp.expType);
3780                source = sourceExp.expType = MkClassType(dest._class.string);
3781                source.refCount++;
3782
3783                //source.kind = classType;
3784                //source._class = dest._class;
3785             }
3786          }
3787
3788          // Adding two enumerations
3789          /*
3790          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3791          {
3792             if(!source._class.registered.dataType)
3793                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3794             if(!dest._class.registered.dataType)
3795                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3796
3797             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3798             {
3799                FreeType(source);
3800                source = sourceExp.expType = MkClassType(dest._class.string);
3801                source.refCount++;
3802
3803                //source.kind = classType;
3804                //source._class = dest._class;
3805             }
3806          }*/
3807
3808          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3809          {
3810             OldList * specs = MkList();
3811             Declarator decl;
3812             char string[1024];
3813
3814             ReadString(string, sourceExp.string);
3815             decl = SpecDeclFromString(string, specs, null);
3816
3817             FreeExpContents(sourceExp);
3818             FreeType(sourceExp.expType);
3819
3820             sourceExp.type = classExp;
3821             sourceExp._classExp.specifiers = specs;
3822             sourceExp._classExp.decl = decl;
3823             sourceExp.expType = dest;
3824             dest.refCount++;
3825
3826             FreeType(source);
3827             FreeType(dest);
3828             if(backupSourceExpType) FreeType(backupSourceExpType);
3829             return true;
3830          }
3831       }
3832       else if(source.kind == classType)
3833       {
3834          Class _class = source._class ? source._class.registered : null;
3835
3836          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3837          {
3838             /*
3839             if(dest.kind != classType)
3840             {
3841                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3842                if(!source._class.registered.dataType)
3843                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3844
3845                FreeType(dest);
3846                dest = MkClassType(source._class.string);
3847                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3848                //   dest = MkClassType(source._class.string);
3849             }
3850             */
3851
3852             if(dest.kind != classType)
3853             {
3854                Type tempType { };
3855                Type tempDest, tempSource;
3856
3857                if(!source._class.registered.dataType)
3858                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3859
3860                for(; _class.base.type != systemClass; _class = _class.base);
3861                tempDest = source;
3862                tempSource = tempType;
3863                tempType.kind = classType;
3864                tempType._class = FindClass(_class.fullName);
3865                tempType.truth = source.truth;
3866                tempType.classObjectType = source.classObjectType;
3867
3868                if(tempType._class)
3869                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3870
3871                // PUT THIS BACK TESTING UNITS?
3872                if(conversions.last)
3873                {
3874                   ((Conversion)(conversions.last)).resultType = dest;
3875                   dest.refCount++;
3876                }
3877
3878                FreeType(sourceExp.expType);
3879                sourceExp.expType = MkClassType(_class.fullName);
3880                sourceExp.expType.truth = source.truth;
3881                sourceExp.expType.classObjectType = source.classObjectType;
3882
3883                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3884
3885                if(!sourceExp.destType)
3886                {
3887                   FreeType(sourceExp.destType);
3888                   sourceExp.destType = sourceExp.expType;
3889                   if(sourceExp.expType)
3890                      sourceExp.expType.refCount++;
3891                }
3892                //flag = true;
3893                //source = _class.dataType;
3894
3895
3896                // TOCHECK: TESTING THIS NEW CODE
3897                if(!_class.dataType)
3898                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3899                FreeType(dest);
3900                dest = MkClassType(source._class.string);
3901                dest.truth = source.truth;
3902                dest.classObjectType = source.classObjectType;
3903
3904                FreeType(source);
3905                source = _class.dataType;
3906                source.refCount++;
3907
3908                delete tempType;
3909             }
3910          }
3911       }
3912
3913       if(!flag)
3914       {
3915          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false, warnConst))
3916          {
3917             FreeType(source);
3918             FreeType(dest);
3919             return true;
3920          }
3921       }
3922
3923       // Implicit Casts
3924       /*
3925       if(source.kind == classType)
3926       {
3927          Class _class = source._class.registered;
3928          if(_class.type == unitClass)
3929          {
3930             if(!_class.dataType)
3931                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3932             source = _class.dataType;
3933          }
3934       }*/
3935
3936       if(dest.kind == classType)
3937       {
3938          Class _class = dest._class ? dest._class.registered : null;
3939          bool fittingValue = false;
3940          if(_class && _class.type == enumClass)
3941          {
3942             Class enumClass = eSystem_FindClass(privateModule, "enum");
3943             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3944             if(c && value >= 0 && value <= c.largest)
3945                fittingValue = true;
3946          }
3947
3948          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3949             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3950          {
3951             if(_class.type == normalClass || _class.type == noHeadClass)
3952             {
3953                Expression newExp { };
3954                *newExp = *sourceExp;
3955                if(sourceExp.destType) sourceExp.destType.refCount++;
3956                if(sourceExp.expType)  sourceExp.expType.refCount++;
3957                sourceExp.type = castExp;
3958                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3959                sourceExp.cast.exp = newExp;
3960                FreeType(sourceExp.expType);
3961                sourceExp.expType = null;
3962                ProcessExpressionType(sourceExp);
3963
3964                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3965                if(!inCompiler)
3966                {
3967                   FreeType(sourceExp.expType);
3968                   sourceExp.expType = dest;
3969                }
3970
3971                FreeType(source);
3972                if(inCompiler) FreeType(dest);
3973
3974                if(backupSourceExpType) FreeType(backupSourceExpType);
3975                return true;
3976             }
3977
3978             if(!_class.dataType)
3979                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3980             FreeType(dest);
3981             dest = _class.dataType;
3982             dest.refCount++;
3983          }
3984
3985          // Accept lower precision types for units, since we want to keep the unit type
3986          if(dest.kind == doubleType &&
3987             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3988              source.kind == charType || source.kind == _BoolType))
3989          {
3990             specs = MkListOne(MkSpecifier(DOUBLE));
3991          }
3992          else if(dest.kind == floatType &&
3993             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3994             source.kind == _BoolType || source.kind == doubleType))
3995          {
3996             specs = MkListOne(MkSpecifier(FLOAT));
3997          }
3998          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3999             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4000          {
4001             specs = MkList();
4002             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4003             ListAdd(specs, MkSpecifier(INT64));
4004          }
4005          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
4006             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4007          {
4008             specs = MkList();
4009             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4010             ListAdd(specs, MkSpecifier(INT));
4011          }
4012          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
4013             source.kind == floatType || source.kind == doubleType))
4014          {
4015             specs = MkList();
4016             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4017             ListAdd(specs, MkSpecifier(SHORT));
4018          }
4019          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
4020             source.kind == floatType || source.kind == doubleType))
4021          {
4022             specs = MkList();
4023             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4024             ListAdd(specs, MkSpecifier(CHAR));
4025          }
4026          else
4027          {
4028             FreeType(source);
4029             FreeType(dest);
4030             if(backupSourceExpType)
4031             {
4032                // Failed to convert: revert previous exp type
4033                if(sourceExp.expType) FreeType(sourceExp.expType);
4034                sourceExp.expType = backupSourceExpType;
4035             }
4036             return false;
4037          }
4038       }
4039       else if(dest.kind == doubleType &&
4040          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
4041           source.kind == _BoolType || source.kind == charType))
4042       {
4043          specs = MkListOne(MkSpecifier(DOUBLE));
4044       }
4045       else if(dest.kind == floatType &&
4046          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4047       {
4048          specs = MkListOne(MkSpecifier(FLOAT));
4049       }
4050       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4051          (value == 1 || value == 0))
4052       {
4053          specs = MkList();
4054          ListAdd(specs, MkSpecifier(BOOL));
4055       }
4056       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4057          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
4058       {
4059          specs = MkList();
4060          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4061          ListAdd(specs, MkSpecifier(CHAR));
4062       }
4063       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
4064          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
4065       {
4066          specs = MkList();
4067          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4068          ListAdd(specs, MkSpecifier(SHORT));
4069       }
4070       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
4071       {
4072          specs = MkList();
4073          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4074          ListAdd(specs, MkSpecifier(INT));
4075       }
4076       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
4077       {
4078          specs = MkList();
4079          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4080          ListAdd(specs, MkSpecifier(INT64));
4081       }
4082       else if(dest.kind == enumType &&
4083          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4084       {
4085          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
4086       }
4087       else
4088       {
4089          FreeType(source);
4090          FreeType(dest);
4091          if(backupSourceExpType)
4092          {
4093             // Failed to convert: revert previous exp type
4094             if(sourceExp.expType) FreeType(sourceExp.expType);
4095             sourceExp.expType = backupSourceExpType;
4096          }
4097          return false;
4098       }
4099
4100       if(!flag && !sourceExp.opDestType)
4101       {
4102          Expression newExp { };
4103          *newExp = *sourceExp;
4104          newExp.prev = null;
4105          newExp.next = null;
4106          if(sourceExp.destType) sourceExp.destType.refCount++;
4107          if(sourceExp.expType)  sourceExp.expType.refCount++;
4108
4109          sourceExp.type = castExp;
4110          if(realDest.kind == classType)
4111          {
4112             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
4113             FreeList(specs, FreeSpecifier);
4114          }
4115          else
4116             sourceExp.cast.typeName = MkTypeName(specs, null);
4117          if(newExp.type == opExp)
4118          {
4119             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4120          }
4121          else
4122             sourceExp.cast.exp = newExp;
4123
4124          FreeType(sourceExp.expType);
4125          sourceExp.expType = null;
4126          ProcessExpressionType(sourceExp);
4127       }
4128       else
4129          FreeList(specs, FreeSpecifier);
4130
4131       FreeType(dest);
4132       FreeType(source);
4133       if(backupSourceExpType) FreeType(backupSourceExpType);
4134
4135       return true;
4136    }
4137    else
4138    {
4139       if(computedExp != sourceExp)
4140       {
4141          FreeExpression(computedExp);
4142          computedExp = sourceExp;
4143       }
4144
4145       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4146       if(sourceExp.type == identifierExp)
4147       {
4148          Identifier id = sourceExp.identifier;
4149          if(dest.kind == classType)
4150          {
4151             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4152             {
4153                Class _class = dest._class.registered;
4154                Class enumClass = eSystem_FindClass(privateModule, "enum");
4155                if(enumClass)
4156                {
4157                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4158                   {
4159                      NamedLink value;
4160                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4161                      for(value = e.values.first; value; value = value.next)
4162                      {
4163                         if(!strcmp(value.name, id.string))
4164                            break;
4165                      }
4166                      if(value)
4167                      {
4168                         FreeExpContents(sourceExp);
4169                         FreeType(sourceExp.expType);
4170
4171                         sourceExp.isConstant = true;
4172                         sourceExp.expType = MkClassType(_class.fullName);
4173                         //if(inCompiler)
4174                         {
4175                            char constant[256];
4176                            sourceExp.type = constantExp;
4177                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
4178                               sprintf(constant, "%d", (int) value.data);
4179                            else
4180                               sprintf(constant, "0x%X", (int) value.data);
4181                            sourceExp.constant = CopyString(constant);
4182                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4183                         }
4184                         FreeType(dest);
4185                         return true;
4186                      }
4187                   }
4188                }
4189             }
4190          }
4191
4192          // Loop through all enum classes
4193          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4194          {
4195             FreeType(dest);
4196             return true;
4197          }
4198       }
4199       FreeType(dest);
4200    }
4201    return false;
4202 }
4203
4204 #define TERTIARY(o, name, m, t, p) \
4205    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4206    {                                                              \
4207       exp.type = constantExp;                                    \
4208       exp.string = p(op1.m ? op2.m : op3.m);                     \
4209       if(!exp.expType) \
4210          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4211       return true;                                                \
4212    }
4213
4214 #define BINARY(o, name, m, t, p) \
4215    static bool name(Expression exp, Operand op1, Operand op2)   \
4216    {                                                              \
4217       t value2 = op2.m;                                           \
4218       exp.type = constantExp;                                    \
4219       exp.string = p((t)(op1.m o value2));                     \
4220       if(!exp.expType) \
4221          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4222       return true;                                                \
4223    }
4224
4225 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4226    static bool name(Expression exp, Operand op1, Operand op2)   \
4227    {                                                              \
4228       t value2 = op2.m;                                           \
4229       exp.type = constantExp;                                    \
4230       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4231       if(!exp.expType) \
4232          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4233       return true;                                                \
4234    }
4235
4236 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4237    static bool name(Expression exp, Operand op1, Operand op2)   \
4238    {                                                              \
4239       t value2 = op2.m;                                           \
4240       exp.type = constantExp;                                    \
4241       exp.string = p(op1.m o value2);             \
4242       if(!exp.expType) \
4243          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4244       return true;                                                \
4245    }
4246
4247 #define UNARY(o, name, m, t, p) \
4248    static bool name(Expression exp, Operand op1)                \
4249    {                                                              \
4250       exp.type = constantExp;                                    \
4251       exp.string = p((t)(o op1.m));                                   \
4252       if(!exp.expType) \
4253          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4254       return true;                                                \
4255    }
4256
4257 #define OPERATOR_ALL(macro, o, name) \
4258    macro(o, Int##name, i, int, PrintInt) \
4259    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4260    macro(o, Int64##name, i64, int64, PrintInt64) \
4261    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4262    macro(o, Short##name, s, short, PrintShort) \
4263    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4264    macro(o, Char##name, c, char, PrintChar) \
4265    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4266    macro(o, Float##name, f, float, PrintFloat) \
4267    macro(o, Double##name, d, double, PrintDouble)
4268
4269 #define OPERATOR_INTTYPES(macro, o, name) \
4270    macro(o, Int##name, i, int, PrintInt) \
4271    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4272    macro(o, Int64##name, i64, int64, PrintInt64) \
4273    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4274    macro(o, Short##name, s, short, PrintShort) \
4275    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4276    macro(o, Char##name, c, char, PrintChar) \
4277    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4278
4279 #define OPERATOR_REALTYPES(macro, o, name) \
4280    macro(o, Float##name, f, float, PrintFloat) \
4281    macro(o, Double##name, d, double, PrintDouble)
4282
4283 // binary arithmetic
4284 OPERATOR_ALL(BINARY, +, Add)
4285 OPERATOR_ALL(BINARY, -, Sub)
4286 OPERATOR_ALL(BINARY, *, Mul)
4287 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4288 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4289 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4290
4291 // unary arithmetic
4292 OPERATOR_ALL(UNARY, -, Neg)
4293
4294 // unary arithmetic increment and decrement
4295 OPERATOR_ALL(UNARY, ++, Inc)
4296 OPERATOR_ALL(UNARY, --, Dec)
4297
4298 // binary arithmetic assignment
4299 OPERATOR_ALL(BINARY, =, Asign)
4300 OPERATOR_ALL(BINARY, +=, AddAsign)
4301 OPERATOR_ALL(BINARY, -=, SubAsign)
4302 OPERATOR_ALL(BINARY, *=, MulAsign)
4303 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4304 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4305 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4306
4307 // binary bitwise
4308 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4309 OPERATOR_INTTYPES(BINARY, |, BitOr)
4310 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4311 OPERATOR_INTTYPES(BINARY, <<, LShift)
4312 OPERATOR_INTTYPES(BINARY, >>, RShift)
4313
4314 // unary bitwise
4315 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4316
4317 // binary bitwise assignment
4318 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4319 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4320 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4321 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4322 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4323
4324 // unary logical negation
4325 OPERATOR_INTTYPES(UNARY, !, Not)
4326
4327 // binary logical equality
4328 OPERATOR_ALL(BINARY, ==, Equ)
4329 OPERATOR_ALL(BINARY, !=, Nqu)
4330
4331 // binary logical
4332 OPERATOR_ALL(BINARY, &&, And)
4333 OPERATOR_ALL(BINARY, ||, Or)
4334
4335 // binary logical relational
4336 OPERATOR_ALL(BINARY, >, Grt)
4337 OPERATOR_ALL(BINARY, <, Sma)
4338 OPERATOR_ALL(BINARY, >=, GrtEqu)
4339 OPERATOR_ALL(BINARY, <=, SmaEqu)
4340
4341 // tertiary condition operator
4342 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4343
4344 //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
4345 #define OPERATOR_TABLE_ALL(name, type) \
4346     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4347                           type##Neg, \
4348                           type##Inc, type##Dec, \
4349                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4350                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4351                           type##BitNot, \
4352                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4353                           type##Not, \
4354                           type##Equ, type##Nqu, \
4355                           type##And, type##Or, \
4356                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4357                         }; \
4358
4359 #define OPERATOR_TABLE_INTTYPES(name, type) \
4360     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4361                           type##Neg, \
4362                           type##Inc, type##Dec, \
4363                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4364                           null, null, null, null, null, \
4365                           null, \
4366                           null, null, null, null, null, \
4367                           null, \
4368                           type##Equ, type##Nqu, \
4369                           type##And, type##Or, \
4370                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4371                         }; \
4372
4373 OPERATOR_TABLE_ALL(int, Int)
4374 OPERATOR_TABLE_ALL(uint, UInt)
4375 OPERATOR_TABLE_ALL(int64, Int64)
4376 OPERATOR_TABLE_ALL(uint64, UInt64)
4377 OPERATOR_TABLE_ALL(short, Short)
4378 OPERATOR_TABLE_ALL(ushort, UShort)
4379 OPERATOR_TABLE_INTTYPES(float, Float)
4380 OPERATOR_TABLE_INTTYPES(double, Double)
4381 OPERATOR_TABLE_ALL(char, Char)
4382 OPERATOR_TABLE_ALL(uchar, UChar)
4383
4384 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4385 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4386 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4387 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4388 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4389 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4390 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4391 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4392
4393 public void ReadString(char * output,  char * string)
4394 {
4395    int len = strlen(string);
4396    int c,d = 0;
4397    bool quoted = false, escaped = false;
4398    for(c = 0; c<len; c++)
4399    {
4400       char ch = string[c];
4401       if(escaped)
4402       {
4403          switch(ch)
4404          {
4405             case 'n': output[d] = '\n'; break;
4406             case 't': output[d] = '\t'; break;
4407             case 'a': output[d] = '\a'; break;
4408             case 'b': output[d] = '\b'; break;
4409             case 'f': output[d] = '\f'; break;
4410             case 'r': output[d] = '\r'; break;
4411             case 'v': output[d] = '\v'; break;
4412             case '\\': output[d] = '\\'; break;
4413             case '\"': output[d] = '\"'; break;
4414             case '\'': output[d] = '\''; break;
4415             default: output[d] = ch;
4416          }
4417          d++;
4418          escaped = false;
4419       }
4420       else
4421       {
4422          if(ch == '\"')
4423             quoted ^= true;
4424          else if(quoted)
4425          {
4426             if(ch == '\\')
4427                escaped = true;
4428             else
4429                output[d++] = ch;
4430          }
4431       }
4432    }
4433    output[d] = '\0';
4434 }
4435
4436 // String Unescape Copy
4437
4438 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4439 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4440 public int UnescapeString(char * d, char * s, int len)
4441 {
4442    int j = 0, k = 0;
4443    char ch;
4444    while(j < len && (ch = s[j]))
4445    {
4446       switch(ch)
4447       {
4448          case '\\':
4449             switch((ch = s[++j]))
4450             {
4451                case 'n': d[k] = '\n'; break;
4452                case 't': d[k] = '\t'; break;
4453                case 'a': d[k] = '\a'; break;
4454                case 'b': d[k] = '\b'; break;
4455                case 'f': d[k] = '\f'; break;
4456                case 'r': d[k] = '\r'; break;
4457                case 'v': d[k] = '\v'; break;
4458                case '\\': d[k] = '\\'; break;
4459                case '\"': d[k] = '\"'; break;
4460                case '\'': d[k] = '\''; break;
4461                default: d[k] = '\\'; d[k] = ch;
4462             }
4463             break;
4464          default:
4465             d[k] = ch;
4466       }
4467       j++, k++;
4468    }
4469    d[k] = '\0';
4470    return k;
4471 }
4472
4473 public char * OffsetEscapedString(char * s, int len, int offset)
4474 {
4475    char ch;
4476    int j = 0, k = 0;
4477    while(j < len && k < offset && (ch = s[j]))
4478    {
4479       if(ch == '\\') ++j;
4480       j++, k++;
4481    }
4482    return (k == offset) ? s + j : null;
4483 }
4484
4485 public Operand GetOperand(Expression exp)
4486 {
4487    Operand op { };
4488    Type type = exp.expType;
4489    if(type)
4490    {
4491       while(type.kind == classType &&
4492          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4493       {
4494          if(!type._class.registered.dataType)
4495             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4496          type = type._class.registered.dataType;
4497
4498       }
4499       if(exp.type == stringExp && op.kind == pointerType)
4500       {
4501          op.ui64 = (uint64)exp.string;
4502          op.kind = pointerType;
4503          op.ops = uint64Ops;
4504       }
4505       else if(exp.isConstant && exp.type == constantExp)
4506       {
4507          op.kind = type.kind;
4508          op.type = exp.expType;
4509
4510          switch(op.kind)
4511          {
4512             case _BoolType:
4513             case charType:
4514             {
4515                if(exp.constant[0] == '\'')
4516                {
4517                   op.c = exp.constant[1];
4518                   op.ops = charOps;
4519                }
4520                else if(type.isSigned)
4521                {
4522                   op.c = (char)strtol(exp.constant, null, 0);
4523                   op.ops = charOps;
4524                }
4525                else
4526                {
4527                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4528                   op.ops = ucharOps;
4529                }
4530                break;
4531             }
4532             case shortType:
4533                if(type.isSigned)
4534                {
4535                   op.s = (short)strtol(exp.constant, null, 0);
4536                   op.ops = shortOps;
4537                }
4538                else
4539                {
4540                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4541                   op.ops = ushortOps;
4542                }
4543                break;
4544             case intType:
4545             case longType:
4546                if(type.isSigned)
4547                {
4548                   op.i = (int)strtol(exp.constant, null, 0);
4549                   op.ops = intOps;
4550                }
4551                else
4552                {
4553                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4554                   op.ops = uintOps;
4555                }
4556                op.kind = intType;
4557                break;
4558             case int64Type:
4559                if(type.isSigned)
4560                {
4561                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4562                   op.ops = int64Ops;
4563                }
4564                else
4565                {
4566                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4567                   op.ops = uint64Ops;
4568                }
4569                op.kind = int64Type;
4570                break;
4571             case intPtrType:
4572                if(type.isSigned)
4573                {
4574                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4575                   op.ops = int64Ops;
4576                }
4577                else
4578                {
4579                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4580                   op.ops = uint64Ops;
4581                }
4582                op.kind = int64Type;
4583                break;
4584             case intSizeType:
4585                if(type.isSigned)
4586                {
4587                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4588                   op.ops = int64Ops;
4589                }
4590                else
4591                {
4592                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4593                   op.ops = uint64Ops;
4594                }
4595                op.kind = int64Type;
4596                break;
4597             case floatType:
4598                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4599                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4600                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4601                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4602                else
4603                   op.f = (float)strtod(exp.constant, null);
4604                op.ops = floatOps;
4605                break;
4606             case doubleType:
4607                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4608                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4609                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4610                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4611                else
4612                   op.d = (double)strtod(exp.constant, null);
4613                op.ops = doubleOps;
4614                break;
4615             //case classType:    For when we have operator overloading...
4616             // Pointer additions
4617             //case functionType:
4618             case arrayType:
4619             case pointerType:
4620             case classType:
4621                op.ui64 = _strtoui64(exp.constant, null, 0);
4622                op.kind = pointerType;
4623                op.ops = uint64Ops;
4624                // op.ptrSize =
4625                break;
4626          }
4627       }
4628    }
4629    return op;
4630 }
4631
4632 static __attribute__((unused)) void UnusedFunction()
4633 {
4634    int a;
4635    a.OnGetString(0,0,0);
4636 }
4637 default:
4638 extern int __ecereVMethodID_class_OnGetString;
4639 public:
4640
4641 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4642 {
4643    DataMember dataMember;
4644    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4645    {
4646       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4647          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4648       else
4649       {
4650          Expression exp { };
4651          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4652          Type type;
4653          void * ptr = inst.data + dataMember.offset + offset;
4654          char * result = null;
4655          exp.loc = member.loc = inst.loc;
4656          ((Identifier)member.identifiers->first).loc = inst.loc;
4657
4658          if(!dataMember.dataType)
4659             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4660          type = dataMember.dataType;
4661          if(type.kind == classType)
4662          {
4663             Class _class = type._class.registered;
4664             if(_class.type == enumClass)
4665             {
4666                Class enumClass = eSystem_FindClass(privateModule, "enum");
4667                if(enumClass)
4668                {
4669                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4670                   NamedLink item;
4671                   for(item = e.values.first; item; item = item.next)
4672                   {
4673                      if((int)item.data == *(int *)ptr)
4674                      {
4675                         result = item.name;
4676                         break;
4677                      }
4678                   }
4679                   if(result)
4680                   {
4681                      exp.identifier = MkIdentifier(result);
4682                      exp.type = identifierExp;
4683                      exp.destType = MkClassType(_class.fullName);
4684                      ProcessExpressionType(exp);
4685                   }
4686                }
4687             }
4688             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4689             {
4690                if(!_class.dataType)
4691                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4692                type = _class.dataType;
4693             }
4694          }
4695          if(!result)
4696          {
4697             switch(type.kind)
4698             {
4699                case floatType:
4700                {
4701                   FreeExpContents(exp);
4702
4703                   exp.constant = PrintFloat(*(float*)ptr);
4704                   exp.type = constantExp;
4705                   break;
4706                }
4707                case doubleType:
4708                {
4709                   FreeExpContents(exp);
4710
4711                   exp.constant = PrintDouble(*(double*)ptr);
4712                   exp.type = constantExp;
4713                   break;
4714                }
4715                case intType:
4716                {
4717                   FreeExpContents(exp);
4718
4719                   exp.constant = PrintInt(*(int*)ptr);
4720                   exp.type = constantExp;
4721                   break;
4722                }
4723                case int64Type:
4724                {
4725                   FreeExpContents(exp);
4726
4727                   exp.constant = PrintInt64(*(int64*)ptr);
4728                   exp.type = constantExp;
4729                   break;
4730                }
4731                case intPtrType:
4732                {
4733                   FreeExpContents(exp);
4734                   // TODO: This should probably use proper type
4735                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4736                   exp.type = constantExp;
4737                   break;
4738                }
4739                case intSizeType:
4740                {
4741                   FreeExpContents(exp);
4742                   // TODO: This should probably use proper type
4743                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4744                   exp.type = constantExp;
4745                   break;
4746                }
4747                default:
4748                   Compiler_Error($"Unhandled type populating instance\n");
4749             }
4750          }
4751          ListAdd(memberList, member);
4752       }
4753
4754       if(parentDataMember.type == unionMember)
4755          break;
4756    }
4757 }
4758
4759 void PopulateInstance(Instantiation inst)
4760 {
4761    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4762    Class _class = classSym.registered;
4763    DataMember dataMember;
4764    OldList * memberList = MkList();
4765    // Added this check and ->Add to prevent memory leaks on bad code
4766    if(!inst.members)
4767       inst.members = MkListOne(MkMembersInitList(memberList));
4768    else
4769       inst.members->Add(MkMembersInitList(memberList));
4770    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4771    {
4772       if(!dataMember.isProperty)
4773       {
4774          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4775             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4776          else
4777          {
4778             Expression exp { };
4779             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4780             Type type;
4781             void * ptr = inst.data + dataMember.offset;
4782             char * result = null;
4783
4784             exp.loc = member.loc = inst.loc;
4785             ((Identifier)member.identifiers->first).loc = inst.loc;
4786
4787             if(!dataMember.dataType)
4788                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4789             type = dataMember.dataType;
4790             if(type.kind == classType)
4791             {
4792                Class _class = type._class.registered;
4793                if(_class.type == enumClass)
4794                {
4795                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4796                   if(enumClass)
4797                   {
4798                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4799                      NamedLink item;
4800                      for(item = e.values.first; item; item = item.next)
4801                      {
4802                         if((int)item.data == *(int *)ptr)
4803                         {
4804                            result = item.name;
4805                            break;
4806                         }
4807                      }
4808                   }
4809                   if(result)
4810                   {
4811                      exp.identifier = MkIdentifier(result);
4812                      exp.type = identifierExp;
4813                      exp.destType = MkClassType(_class.fullName);
4814                      ProcessExpressionType(exp);
4815                   }
4816                }
4817                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4818                {
4819                   if(!_class.dataType)
4820                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4821                   type = _class.dataType;
4822                }
4823             }
4824             if(!result)
4825             {
4826                switch(type.kind)
4827                {
4828                   case floatType:
4829                   {
4830                      exp.constant = PrintFloat(*(float*)ptr);
4831                      exp.type = constantExp;
4832                      break;
4833                   }
4834                   case doubleType:
4835                   {
4836                      exp.constant = PrintDouble(*(double*)ptr);
4837                      exp.type = constantExp;
4838                      break;
4839                   }
4840                   case intType:
4841                   {
4842                      exp.constant = PrintInt(*(int*)ptr);
4843                      exp.type = constantExp;
4844                      break;
4845                   }
4846                   case int64Type:
4847                   {
4848                      exp.constant = PrintInt64(*(int64*)ptr);
4849                      exp.type = constantExp;
4850                      break;
4851                   }
4852                   case intPtrType:
4853                   {
4854                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4855                      exp.type = constantExp;
4856                      break;
4857                   }
4858                   default:
4859                      Compiler_Error($"Unhandled type populating instance\n");
4860                }
4861             }
4862             ListAdd(memberList, member);
4863          }
4864       }
4865    }
4866 }
4867
4868 void ComputeInstantiation(Expression exp)
4869 {
4870    Instantiation inst = exp.instance;
4871    MembersInit members;
4872    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4873    Class _class = classSym ? classSym.registered : null;
4874    DataMember curMember = null;
4875    Class curClass = null;
4876    DataMember subMemberStack[256];
4877    int subMemberStackPos = 0;
4878    uint64 bits = 0;
4879
4880    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4881    {
4882       // Don't recompute the instantiation...
4883       // Non Simple classes will have become constants by now
4884       if(inst.data)
4885          return;
4886
4887       if(_class.type == normalClass || _class.type == noHeadClass)
4888       {
4889          inst.data = (byte *)eInstance_New(_class);
4890          if(_class.type == normalClass)
4891             ((Instance)inst.data)._refCount++;
4892       }
4893       else
4894          inst.data = new0 byte[_class.structSize];
4895    }
4896
4897    if(inst.members)
4898    {
4899       for(members = inst.members->first; members; members = members.next)
4900       {
4901          switch(members.type)
4902          {
4903             case dataMembersInit:
4904             {
4905                if(members.dataMembers)
4906                {
4907                   MemberInit member;
4908                   for(member = members.dataMembers->first; member; member = member.next)
4909                   {
4910                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4911                      bool found = false;
4912
4913                      Property prop = null;
4914                      DataMember dataMember = null;
4915                      uint dataMemberOffset;
4916
4917                      if(!ident)
4918                      {
4919                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4920                         if(curMember)
4921                         {
4922                            if(curMember.isProperty)
4923                               prop = (Property)curMember;
4924                            else
4925                            {
4926                               dataMember = curMember;
4927
4928                               // CHANGED THIS HERE
4929                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4930
4931                               // 2013/17/29 -- It seems that this was missing here!
4932                               if(_class.type == normalClass)
4933                                  dataMemberOffset += _class.base.structSize;
4934                               // dataMemberOffset = dataMember.offset;
4935                            }
4936                            found = true;
4937                         }
4938                      }
4939                      else
4940                      {
4941                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4942                         if(prop)
4943                         {
4944                            found = true;
4945                            if(prop.memberAccess == publicAccess)
4946                            {
4947                               curMember = (DataMember)prop;
4948                               curClass = prop._class;
4949                            }
4950                         }
4951                         else
4952                         {
4953                            DataMember _subMemberStack[256];
4954                            int _subMemberStackPos = 0;
4955
4956                            // FILL MEMBER STACK
4957                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4958
4959                            if(dataMember)
4960                            {
4961                               found = true;
4962                               if(dataMember.memberAccess == publicAccess)
4963                               {
4964                                  curMember = dataMember;
4965                                  curClass = dataMember._class;
4966                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4967                                  subMemberStackPos = _subMemberStackPos;
4968                               }
4969                            }
4970                         }
4971                      }
4972
4973                      if(found && member.initializer && member.initializer.type == expInitializer)
4974                      {
4975                         Expression value = member.initializer.exp;
4976                         Type type = null;
4977                         bool deepMember = false;
4978                         if(prop)
4979                         {
4980                            type = prop.dataType;
4981                         }
4982                         else if(dataMember)
4983                         {
4984                            if(!dataMember.dataType)
4985                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4986
4987                            type = dataMember.dataType;
4988                         }
4989
4990                         if(ident && ident.next)
4991                         {
4992                            deepMember = true;
4993
4994                            // for(; ident && type; ident = ident.next)
4995                            for(ident = ident.next; ident && type; ident = ident.next)
4996                            {
4997                               if(type.kind == classType)
4998                               {
4999                                  prop = eClass_FindProperty(type._class.registered,
5000                                     ident.string, privateModule);
5001                                  if(prop)
5002                                     type = prop.dataType;
5003                                  else
5004                                  {
5005                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
5006                                        ident.string, &dataMemberOffset, privateModule, null, null);
5007                                     if(dataMember)
5008                                        type = dataMember.dataType;
5009                                  }
5010                               }
5011                               else if(type.kind == structType || type.kind == unionType)
5012                               {
5013                                  Type memberType;
5014                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
5015                                  {
5016                                     if(!strcmp(memberType.name, ident.string))
5017                                     {
5018                                        type = memberType;
5019                                        break;
5020                                     }
5021                                  }
5022                               }
5023                            }
5024                         }
5025                         if(value)
5026                         {
5027                            FreeType(value.destType);
5028                            value.destType = type;
5029                            if(type) type.refCount++;
5030                            ComputeExpression(value);
5031                         }
5032                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
5033                         {
5034                            if(type.kind == classType)
5035                            {
5036                               Class _class = type._class.registered;
5037                               if(_class.type == bitClass || _class.type == unitClass ||
5038                                  _class.type == enumClass)
5039                               {
5040                                  if(!_class.dataType)
5041                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5042                                  type = _class.dataType;
5043                               }
5044                            }
5045
5046                            if(dataMember)
5047                            {
5048                               void * ptr = inst.data + dataMemberOffset;
5049
5050                               if(value.type == constantExp)
5051                               {
5052                                  switch(type.kind)
5053                                  {
5054                                     case intType:
5055                                     {
5056                                        GetInt(value, (int*)ptr);
5057                                        break;
5058                                     }
5059                                     case int64Type:
5060                                     {
5061                                        GetInt64(value, (int64*)ptr);
5062                                        break;
5063                                     }
5064                                     case intPtrType:
5065                                     {
5066                                        GetIntPtr(value, (intptr*)ptr);
5067                                        break;
5068                                     }
5069                                     case intSizeType:
5070                                     {
5071                                        GetIntSize(value, (intsize*)ptr);
5072                                        break;
5073                                     }
5074                                     case floatType:
5075                                     {
5076                                        GetFloat(value, (float*)ptr);
5077                                        break;
5078                                     }
5079                                     case doubleType:
5080                                     {
5081                                        GetDouble(value, (double *)ptr);
5082                                        break;
5083                                     }
5084                                  }
5085                               }
5086                               else if(value.type == instanceExp)
5087                               {
5088                                  if(type.kind == classType)
5089                                  {
5090                                     Class _class = type._class.registered;
5091                                     if(_class.type == structClass)
5092                                     {
5093                                        ComputeTypeSize(type);
5094                                        if(value.instance.data)
5095                                           memcpy(ptr, value.instance.data, type.size);
5096                                     }
5097                                  }
5098                               }
5099                            }
5100                            else if(prop)
5101                            {
5102                               if(value.type == instanceExp && value.instance.data)
5103                               {
5104                                  if(type.kind == classType)
5105                                  {
5106                                     Class _class = type._class.registered;
5107                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
5108                                     {
5109                                        void (*Set)(void *, void *) = (void *)prop.Set;
5110                                        Set(inst.data, value.instance.data);
5111                                        PopulateInstance(inst);
5112                                     }
5113                                  }
5114                               }
5115                               else if(value.type == constantExp)
5116                               {
5117                                  switch(type.kind)
5118                                  {
5119                                     case doubleType:
5120                                     {
5121                                        void (*Set)(void *, double) = (void *)prop.Set;
5122                                        Set(inst.data, strtod(value.constant, null) );
5123                                        break;
5124                                     }
5125                                     case floatType:
5126                                     {
5127                                        void (*Set)(void *, float) = (void *)prop.Set;
5128                                        Set(inst.data, (float)(strtod(value.constant, null)));
5129                                        break;
5130                                     }
5131                                     case intType:
5132                                     {
5133                                        void (*Set)(void *, int) = (void *)prop.Set;
5134                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5135                                        break;
5136                                     }
5137                                     case int64Type:
5138                                     {
5139                                        void (*Set)(void *, int64) = (void *)prop.Set;
5140                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5141                                        break;
5142                                     }
5143                                     case intPtrType:
5144                                     {
5145                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5146                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5147                                        break;
5148                                     }
5149                                     case intSizeType:
5150                                     {
5151                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5152                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5153                                        break;
5154                                     }
5155                                  }
5156                               }
5157                               else if(value.type == stringExp)
5158                               {
5159                                  char temp[1024];
5160                                  ReadString(temp, value.string);
5161                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5162                               }
5163                            }
5164                         }
5165                         else if(!deepMember && type && _class.type == unitClass)
5166                         {
5167                            if(prop)
5168                            {
5169                               // Only support converting units to units for now...
5170                               if(value.type == constantExp)
5171                               {
5172                                  if(type.kind == classType)
5173                                  {
5174                                     Class _class = type._class.registered;
5175                                     if(_class.type == unitClass)
5176                                     {
5177                                        if(!_class.dataType)
5178                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5179                                        type = _class.dataType;
5180                                     }
5181                                  }
5182                                  // TODO: Assuming same base type for units...
5183                                  switch(type.kind)
5184                                  {
5185                                     case floatType:
5186                                     {
5187                                        float fValue;
5188                                        float (*Set)(float) = (void *)prop.Set;
5189                                        GetFloat(member.initializer.exp, &fValue);
5190                                        exp.constant = PrintFloat(Set(fValue));
5191                                        exp.type = constantExp;
5192                                        break;
5193                                     }
5194                                     case doubleType:
5195                                     {
5196                                        double dValue;
5197                                        double (*Set)(double) = (void *)prop.Set;
5198                                        GetDouble(member.initializer.exp, &dValue);
5199                                        exp.constant = PrintDouble(Set(dValue));
5200                                        exp.type = constantExp;
5201                                        break;
5202                                     }
5203                                  }
5204                               }
5205                            }
5206                         }
5207                         else if(!deepMember && type && _class.type == bitClass)
5208                         {
5209                            if(prop)
5210                            {
5211                               if(value.type == instanceExp && value.instance.data)
5212                               {
5213                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5214                                  bits = Set(value.instance.data);
5215                               }
5216                               else if(value.type == constantExp)
5217                               {
5218                               }
5219                            }
5220                            else if(dataMember)
5221                            {
5222                               BitMember bitMember = (BitMember) dataMember;
5223                               Type type;
5224                               uint64 part;
5225                               bits = (bits & ~bitMember.mask);
5226                               if(!bitMember.dataType)
5227                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5228                               type = bitMember.dataType;
5229                               if(type.kind == classType && type._class && type._class.registered)
5230                               {
5231                                  if(!type._class.registered.dataType)
5232                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5233                                  type = type._class.registered.dataType;
5234                               }
5235                               switch(type.kind)
5236                               {
5237                                  case _BoolType:
5238                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5239                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (short *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5240                                  case intType:
5241                                  case longType:       { uint v; type.isSigned ? GetInt(value, (int *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5242                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (int64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5243                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (intptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5244                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (intsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5245                               }
5246                               bits |= part << bitMember.pos;
5247                            }
5248                         }
5249                      }
5250                      else
5251                      {
5252                         if(_class && _class.type == unitClass)
5253                         {
5254                            ComputeExpression(member.initializer.exp);
5255                            exp.constant = member.initializer.exp.constant;
5256                            exp.type = constantExp;
5257
5258                            member.initializer.exp.constant = null;
5259                         }
5260                      }
5261                   }
5262                }
5263                break;
5264             }
5265          }
5266       }
5267    }
5268    if(_class && _class.type == bitClass)
5269    {
5270       exp.constant = PrintHexUInt(bits);
5271       exp.type = constantExp;
5272    }
5273    if(exp.type != instanceExp)
5274    {
5275       FreeInstance(inst);
5276    }
5277 }
5278
5279 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5280 {
5281    bool result = false;
5282    switch(kind)
5283    {
5284       case shortType:
5285          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5286             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5287          break;
5288       case intType:
5289       case longType:
5290          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5291             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5292          break;
5293       case int64Type:
5294          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5295             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5296             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5297          break;
5298       case floatType:
5299          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5300             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5301             result = GetOpFloat(op, &op.f);
5302          break;
5303       case doubleType:
5304          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5305             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5306             result = GetOpDouble(op, &op.d);
5307          break;
5308       case pointerType:
5309          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5310             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5311             result = GetOpUIntPtr(op, &op.ui64);
5312          break;
5313       case enumType:
5314          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5315             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5316             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5317          break;
5318       case intPtrType:
5319          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5320             result = isSigned ? GetOpIntPtr(op, &op.i64) : GetOpUIntPtr(op, &op.ui64);
5321          break;
5322       case intSizeType:
5323          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5324             result = isSigned ? GetOpIntSize(op, &op.i64) : GetOpUIntSize(op, &op.ui64);
5325          break;
5326    }
5327    return result;
5328 }
5329
5330 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5331 {
5332    if(exp.op.op == SIZEOF)
5333    {
5334       FreeExpContents(exp);
5335       exp.type = constantExp;
5336       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5337    }
5338    else
5339    {
5340       if(!exp.op.exp1)
5341       {
5342          switch(exp.op.op)
5343          {
5344             // unary arithmetic
5345             case '+':
5346             {
5347                // Provide default unary +
5348                Expression exp2 = exp.op.exp2;
5349                exp.op.exp2 = null;
5350                FreeExpContents(exp);
5351                FreeType(exp.expType);
5352                FreeType(exp.destType);
5353                *exp = *exp2;
5354                delete exp2;
5355                break;
5356             }
5357             case '-':
5358                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5359                break;
5360             // unary arithmetic increment and decrement
5361                   //OPERATOR_ALL(UNARY, ++, Inc)
5362                   //OPERATOR_ALL(UNARY, --, Dec)
5363             // unary bitwise
5364             case '~':
5365                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5366                break;
5367             // unary logical negation
5368             case '!':
5369                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5370                break;
5371          }
5372       }
5373       else
5374       {
5375          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5376          {
5377             if(Promote(op2, op1.kind, op1.type.isSigned))
5378                op2.kind = op1.kind, op2.ops = op1.ops;
5379             else if(Promote(op1, op2.kind, op2.type.isSigned))
5380                op1.kind = op2.kind, op1.ops = op2.ops;
5381          }
5382          switch(exp.op.op)
5383          {
5384             // binary arithmetic
5385             case '+':
5386                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5387                break;
5388             case '-':
5389                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5390                break;
5391             case '*':
5392                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5393                break;
5394             case '/':
5395                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5396                break;
5397             case '%':
5398                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5399                break;
5400             // binary arithmetic assignment
5401                   //OPERATOR_ALL(BINARY, =, Asign)
5402                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5403                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5404                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5405                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5406                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5407             // binary bitwise
5408             case '&':
5409                if(exp.op.exp2)
5410                {
5411                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5412                }
5413                break;
5414             case '|':
5415                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5416                break;
5417             case '^':
5418                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5419                break;
5420             case LEFT_OP:
5421                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5422                break;
5423             case RIGHT_OP:
5424                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5425                break;
5426             // binary bitwise assignment
5427                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5428                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5429                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5430                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5431                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5432             // binary logical equality
5433             case EQ_OP:
5434                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5435                break;
5436             case NE_OP:
5437                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5438                break;
5439             // binary logical
5440             case AND_OP:
5441                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5442                break;
5443             case OR_OP:
5444                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5445                break;
5446             // binary logical relational
5447             case '>':
5448                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5449                break;
5450             case '<':
5451                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5452                break;
5453             case GE_OP:
5454                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5455                break;
5456             case LE_OP:
5457                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5458                break;
5459          }
5460       }
5461    }
5462 }
5463
5464 void ComputeExpression(Expression exp)
5465 {
5466    char expString[10240];
5467    expString[0] = '\0';
5468 #ifdef _DEBUG
5469    PrintExpression(exp, expString);
5470 #endif
5471
5472    switch(exp.type)
5473    {
5474       case instanceExp:
5475       {
5476          ComputeInstantiation(exp);
5477          break;
5478       }
5479       /*
5480       case constantExp:
5481          break;
5482       */
5483       case opExp:
5484       {
5485          Expression exp1, exp2 = null;
5486          Operand op1 { };
5487          Operand op2 { };
5488
5489          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5490          if(exp.op.exp2)
5491          {
5492             Expression e = exp.op.exp2;
5493
5494             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5495             {
5496                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5497                {
5498                   if(e.type == extensionCompoundExp)
5499                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5500                   else
5501                      e = e.list->last;
5502                }
5503             }
5504             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5505             {
5506                if(e.type == stringExp && e.string)
5507                {
5508                   char * string = e.string;
5509                   int len = strlen(string);
5510                   char * tmp = new char[len-2+1];
5511                   len = UnescapeString(tmp, string + 1, len - 2);
5512                   delete tmp;
5513                   FreeExpContents(exp);
5514                   exp.type = constantExp;
5515                   exp.constant = PrintUInt(len + 1);
5516                }
5517                else
5518                {
5519                   Type type = e.expType;
5520                   type.refCount++;
5521                   FreeExpContents(exp);
5522                   exp.type = constantExp;
5523                   exp.constant = PrintUInt(ComputeTypeSize(type));
5524                   FreeType(type);
5525                }
5526                break;
5527             }
5528             else
5529                ComputeExpression(exp.op.exp2);
5530          }
5531          if(exp.op.exp1)
5532          {
5533             ComputeExpression(exp.op.exp1);
5534             exp1 = exp.op.exp1;
5535             exp2 = exp.op.exp2;
5536             op1 = GetOperand(exp1);
5537             if(op1.type) op1.type.refCount++;
5538             if(exp2)
5539             {
5540                op2 = GetOperand(exp2);
5541                if(op2.type) op2.type.refCount++;
5542             }
5543          }
5544          else
5545          {
5546             exp1 = exp.op.exp2;
5547             op1 = GetOperand(exp1);
5548             if(op1.type) op1.type.refCount++;
5549          }
5550
5551          CallOperator(exp, exp1, exp2, op1, op2);
5552          /*
5553          switch(exp.op.op)
5554          {
5555             // Unary operators
5556             case '&':
5557                // Also binary
5558                if(exp.op.exp1 && exp.op.exp2)
5559                {
5560                   // Binary And
5561                   if(op1.ops.BitAnd)
5562                   {
5563                      FreeExpContents(exp);
5564                      op1.ops.BitAnd(exp, op1, op2);
5565                   }
5566                }
5567                break;
5568             case '*':
5569                if(exp.op.exp1)
5570                {
5571                   if(op1.ops.Mul)
5572                   {
5573                      FreeExpContents(exp);
5574                      op1.ops.Mul(exp, op1, op2);
5575                   }
5576                }
5577                break;
5578             case '+':
5579                if(exp.op.exp1)
5580                {
5581                   if(op1.ops.Add)
5582                   {
5583                      FreeExpContents(exp);
5584                      op1.ops.Add(exp, op1, op2);
5585                   }
5586                }
5587                else
5588                {
5589                   // Provide default unary +
5590                   Expression exp2 = exp.op.exp2;
5591                   exp.op.exp2 = null;
5592                   FreeExpContents(exp);
5593                   FreeType(exp.expType);
5594                   FreeType(exp.destType);
5595
5596                   *exp = *exp2;
5597                   delete exp2;
5598                }
5599                break;
5600             case '-':
5601                if(exp.op.exp1)
5602                {
5603                   if(op1.ops.Sub)
5604                   {
5605                      FreeExpContents(exp);
5606                      op1.ops.Sub(exp, op1, op2);
5607                   }
5608                }
5609                else
5610                {
5611                   if(op1.ops.Neg)
5612                   {
5613                      FreeExpContents(exp);
5614                      op1.ops.Neg(exp, op1);
5615                   }
5616                }
5617                break;
5618             case '~':
5619                if(op1.ops.BitNot)
5620                {
5621                   FreeExpContents(exp);
5622                   op1.ops.BitNot(exp, op1);
5623                }
5624                break;
5625             case '!':
5626                if(op1.ops.Not)
5627                {
5628                   FreeExpContents(exp);
5629                   op1.ops.Not(exp, op1);
5630                }
5631                break;
5632             // Binary only operators
5633             case '/':
5634                if(op1.ops.Div)
5635                {
5636                   FreeExpContents(exp);
5637                   op1.ops.Div(exp, op1, op2);
5638                }
5639                break;
5640             case '%':
5641                if(op1.ops.Mod)
5642                {
5643                   FreeExpContents(exp);
5644                   op1.ops.Mod(exp, op1, op2);
5645                }
5646                break;
5647             case LEFT_OP:
5648                break;
5649             case RIGHT_OP:
5650                break;
5651             case '<':
5652                if(exp.op.exp1)
5653                {
5654                   if(op1.ops.Sma)
5655                   {
5656                      FreeExpContents(exp);
5657                      op1.ops.Sma(exp, op1, op2);
5658                   }
5659                }
5660                break;
5661             case '>':
5662                if(exp.op.exp1)
5663                {
5664                   if(op1.ops.Grt)
5665                   {
5666                      FreeExpContents(exp);
5667                      op1.ops.Grt(exp, op1, op2);
5668                   }
5669                }
5670                break;
5671             case LE_OP:
5672                if(exp.op.exp1)
5673                {
5674                   if(op1.ops.SmaEqu)
5675                   {
5676                      FreeExpContents(exp);
5677                      op1.ops.SmaEqu(exp, op1, op2);
5678                   }
5679                }
5680                break;
5681             case GE_OP:
5682                if(exp.op.exp1)
5683                {
5684                   if(op1.ops.GrtEqu)
5685                   {
5686                      FreeExpContents(exp);
5687                      op1.ops.GrtEqu(exp, op1, op2);
5688                   }
5689                }
5690                break;
5691             case EQ_OP:
5692                if(exp.op.exp1)
5693                {
5694                   if(op1.ops.Equ)
5695                   {
5696                      FreeExpContents(exp);
5697                      op1.ops.Equ(exp, op1, op2);
5698                   }
5699                }
5700                break;
5701             case NE_OP:
5702                if(exp.op.exp1)
5703                {
5704                   if(op1.ops.Nqu)
5705                   {
5706                      FreeExpContents(exp);
5707                      op1.ops.Nqu(exp, op1, op2);
5708                   }
5709                }
5710                break;
5711             case '|':
5712                if(op1.ops.BitOr)
5713                {
5714                   FreeExpContents(exp);
5715                   op1.ops.BitOr(exp, op1, op2);
5716                }
5717                break;
5718             case '^':
5719                if(op1.ops.BitXor)
5720                {
5721                   FreeExpContents(exp);
5722                   op1.ops.BitXor(exp, op1, op2);
5723                }
5724                break;
5725             case AND_OP:
5726                break;
5727             case OR_OP:
5728                break;
5729             case SIZEOF:
5730                FreeExpContents(exp);
5731                exp.type = constantExp;
5732                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5733                break;
5734          }
5735          */
5736          if(op1.type) FreeType(op1.type);
5737          if(op2.type) FreeType(op2.type);
5738          break;
5739       }
5740       case bracketsExp:
5741       case extensionExpressionExp:
5742       {
5743          Expression e, n;
5744          for(e = exp.list->first; e; e = n)
5745          {
5746             n = e.next;
5747             if(!n)
5748             {
5749                OldList * list = exp.list;
5750                Expression prev = exp.prev;
5751                Expression next = exp.next;
5752                ComputeExpression(e);
5753                //FreeExpContents(exp);
5754                FreeType(exp.expType);
5755                FreeType(exp.destType);
5756                *exp = *e;
5757                exp.prev = prev;
5758                exp.next = next;
5759                delete e;
5760                delete list;
5761             }
5762             else
5763             {
5764                FreeExpression(e);
5765             }
5766          }
5767          break;
5768       }
5769       /*
5770
5771       case ExpIndex:
5772       {
5773          Expression e;
5774          exp.isConstant = true;
5775
5776          ComputeExpression(exp.index.exp);
5777          if(!exp.index.exp.isConstant)
5778             exp.isConstant = false;
5779
5780          for(e = exp.index.index->first; e; e = e.next)
5781          {
5782             ComputeExpression(e);
5783             if(!e.next)
5784             {
5785                // Check if this type is int
5786             }
5787             if(!e.isConstant)
5788                exp.isConstant = false;
5789          }
5790          exp.expType = Dereference(exp.index.exp.expType);
5791          break;
5792       }
5793       */
5794       case memberExp:
5795       {
5796          Expression memberExp = exp.member.exp;
5797          Identifier memberID = exp.member.member;
5798
5799          Type type;
5800          ComputeExpression(exp.member.exp);
5801          type = exp.member.exp.expType;
5802          if(type)
5803          {
5804             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);
5805             Property prop = null;
5806             DataMember member = null;
5807             Class convertTo = null;
5808             if(type.kind == subClassType && exp.member.exp.type == classExp)
5809                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5810
5811             if(!_class)
5812             {
5813                char string[256];
5814                Symbol classSym;
5815                string[0] = '\0';
5816                PrintTypeNoConst(type, string, false, true);
5817                classSym = FindClass(string);
5818                _class = classSym ? classSym.registered : null;
5819             }
5820
5821             if(exp.member.member)
5822             {
5823                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5824                if(!prop)
5825                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5826             }
5827             if(!prop && !member && _class && exp.member.member)
5828             {
5829                Symbol classSym = FindClass(exp.member.member.string);
5830                convertTo = _class;
5831                _class = classSym ? classSym.registered : null;
5832                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5833             }
5834
5835             if(prop)
5836             {
5837                if(prop.compiled)
5838                {
5839                   Type type = prop.dataType;
5840                   // TODO: Assuming same base type for units...
5841                   if(_class.type == unitClass)
5842                   {
5843                      if(type.kind == classType)
5844                      {
5845                         Class _class = type._class.registered;
5846                         if(_class.type == unitClass)
5847                         {
5848                            if(!_class.dataType)
5849                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5850                            type = _class.dataType;
5851                         }
5852                      }
5853                      switch(type.kind)
5854                      {
5855                         case floatType:
5856                         {
5857                            float value;
5858                            float (*Get)(float) = (void *)prop.Get;
5859                            GetFloat(exp.member.exp, &value);
5860                            exp.constant = PrintFloat(Get ? Get(value) : value);
5861                            exp.type = constantExp;
5862                            break;
5863                         }
5864                         case doubleType:
5865                         {
5866                            double value;
5867                            double (*Get)(double);
5868                            GetDouble(exp.member.exp, &value);
5869
5870                            if(convertTo)
5871                               Get = (void *)prop.Set;
5872                            else
5873                               Get = (void *)prop.Get;
5874                            exp.constant = PrintDouble(Get ? Get(value) : value);
5875                            exp.type = constantExp;
5876                            break;
5877                         }
5878                      }
5879                   }
5880                   else
5881                   {
5882                      if(convertTo)
5883                      {
5884                         Expression value = exp.member.exp;
5885                         Type type;
5886                         if(!prop.dataType)
5887                            ProcessPropertyType(prop);
5888
5889                         type = prop.dataType;
5890                         if(!type)
5891                         {
5892                             // printf("Investigate this\n");
5893                         }
5894                         else if(_class.type == structClass)
5895                         {
5896                            switch(type.kind)
5897                            {
5898                               case classType:
5899                               {
5900                                  Class propertyClass = type._class.registered;
5901                                  if(propertyClass.type == structClass && value.type == instanceExp)
5902                                  {
5903                                     void (*Set)(void *, void *) = (void *)prop.Set;
5904                                     exp.instance = Instantiation { };
5905                                     exp.instance.data = new0 byte[_class.structSize];
5906                                     exp.instance._class = MkSpecifierName(_class.fullName);
5907                                     exp.instance.loc = exp.loc;
5908                                     exp.type = instanceExp;
5909                                     Set(exp.instance.data, value.instance.data);
5910                                     PopulateInstance(exp.instance);
5911                                  }
5912                                  break;
5913                               }
5914                               case intType:
5915                               {
5916                                  int intValue;
5917                                  void (*Set)(void *, int) = (void *)prop.Set;
5918
5919                                  exp.instance = Instantiation { };
5920                                  exp.instance.data = new0 byte[_class.structSize];
5921                                  exp.instance._class = MkSpecifierName(_class.fullName);
5922                                  exp.instance.loc = exp.loc;
5923                                  exp.type = instanceExp;
5924
5925                                  GetInt(value, &intValue);
5926
5927                                  Set(exp.instance.data, intValue);
5928                                  PopulateInstance(exp.instance);
5929                                  break;
5930                               }
5931                               case int64Type:
5932                               {
5933                                  int64 intValue;
5934                                  void (*Set)(void *, int64) = (void *)prop.Set;
5935
5936                                  exp.instance = Instantiation { };
5937                                  exp.instance.data = new0 byte[_class.structSize];
5938                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5939                                  exp.instance.loc = exp.loc;
5940                                  exp.type = instanceExp;
5941
5942                                  GetInt64(value, &intValue);
5943
5944                                  Set(exp.instance.data, intValue);
5945                                  PopulateInstance(exp.instance);
5946                                  break;
5947                               }
5948                               case intPtrType:
5949                               {
5950                                  // TOFIX:
5951                                  intptr intValue;
5952                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5953
5954                                  exp.instance = Instantiation { };
5955                                  exp.instance.data = new0 byte[_class.structSize];
5956                                  exp.instance._class = MkSpecifierName(_class.fullName);
5957                                  exp.instance.loc = exp.loc;
5958                                  exp.type = instanceExp;
5959
5960                                  GetIntPtr(value, &intValue);
5961
5962                                  Set(exp.instance.data, intValue);
5963                                  PopulateInstance(exp.instance);
5964                                  break;
5965                               }
5966                               case intSizeType:
5967                               {
5968                                  // TOFIX:
5969                                  intsize intValue;
5970                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5971
5972                                  exp.instance = Instantiation { };
5973                                  exp.instance.data = new0 byte[_class.structSize];
5974                                  exp.instance._class = MkSpecifierName(_class.fullName);
5975                                  exp.instance.loc = exp.loc;
5976                                  exp.type = instanceExp;
5977
5978                                  GetIntSize(value, &intValue);
5979
5980                                  Set(exp.instance.data, intValue);
5981                                  PopulateInstance(exp.instance);
5982                                  break;
5983                               }
5984                               case floatType:
5985                               {
5986                                  float floatValue;
5987                                  void (*Set)(void *, float) = (void *)prop.Set;
5988
5989                                  exp.instance = Instantiation { };
5990                                  exp.instance.data = new0 byte[_class.structSize];
5991                                  exp.instance._class = MkSpecifierName(_class.fullName);
5992                                  exp.instance.loc = exp.loc;
5993                                  exp.type = instanceExp;
5994
5995                                  GetFloat(value, &floatValue);
5996
5997                                  Set(exp.instance.data, floatValue);
5998                                  PopulateInstance(exp.instance);
5999                                  break;
6000                               }
6001                               case doubleType:
6002                               {
6003                                  double doubleValue;
6004                                  void (*Set)(void *, double) = (void *)prop.Set;
6005
6006                                  exp.instance = Instantiation { };
6007                                  exp.instance.data = new0 byte[_class.structSize];
6008                                  exp.instance._class = MkSpecifierName(_class.fullName);
6009                                  exp.instance.loc = exp.loc;
6010                                  exp.type = instanceExp;
6011
6012                                  GetDouble(value, &doubleValue);
6013
6014                                  Set(exp.instance.data, doubleValue);
6015                                  PopulateInstance(exp.instance);
6016                                  break;
6017                               }
6018                            }
6019                         }
6020                         else if(_class.type == bitClass)
6021                         {
6022                            switch(type.kind)
6023                            {
6024                               case classType:
6025                               {
6026                                  Class propertyClass = type._class.registered;
6027                                  if(propertyClass.type == structClass && value.instance.data)
6028                                  {
6029                                     unsigned int (*Set)(void *) = (void *)prop.Set;
6030                                     unsigned int bits = Set(value.instance.data);
6031                                     exp.constant = PrintHexUInt(bits);
6032                                     exp.type = constantExp;
6033                                     break;
6034                                  }
6035                                  else if(_class.type == bitClass)
6036                                  {
6037                                     unsigned int value;
6038                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
6039                                     unsigned int bits;
6040
6041                                     GetUInt(exp.member.exp, &value);
6042                                     bits = Set(value);
6043                                     exp.constant = PrintHexUInt(bits);
6044                                     exp.type = constantExp;
6045                                  }
6046                               }
6047                            }
6048                         }
6049                      }
6050                      else
6051                      {
6052                         if(_class.type == bitClass)
6053                         {
6054                            unsigned int value;
6055                            GetUInt(exp.member.exp, &value);
6056
6057                            switch(type.kind)
6058                            {
6059                               case classType:
6060                               {
6061                                  Class _class = type._class.registered;
6062                                  if(_class.type == structClass)
6063                                  {
6064                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
6065
6066                                     exp.instance = Instantiation { };
6067                                     exp.instance.data = new0 byte[_class.structSize];
6068                                     exp.instance._class = MkSpecifierName(_class.fullName);
6069                                     exp.instance.loc = exp.loc;
6070                                     //exp.instance.fullSet = true;
6071                                     exp.type = instanceExp;
6072                                     Get(value, exp.instance.data);
6073                                     PopulateInstance(exp.instance);
6074                                  }
6075                                  else if(_class.type == bitClass)
6076                                  {
6077                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
6078                                     uint64 bits = Get(value);
6079                                     exp.constant = PrintHexUInt64(bits);
6080                                     exp.type = constantExp;
6081                                  }
6082                                  break;
6083                               }
6084                            }
6085                         }
6086                         else if(_class.type == structClass)
6087                         {
6088                            byte * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
6089                            switch(type.kind)
6090                            {
6091                               case classType:
6092                               {
6093                                  Class _class = type._class.registered;
6094                                  if(_class.type == structClass && value)
6095                                  {
6096                                     void (*Get)(void *, void *) = (void *)prop.Get;
6097
6098                                     exp.instance = Instantiation { };
6099                                     exp.instance.data = new0 byte[_class.structSize];
6100                                     exp.instance._class = MkSpecifierName(_class.fullName);
6101                                     exp.instance.loc = exp.loc;
6102                                     //exp.instance.fullSet = true;
6103                                     exp.type = instanceExp;
6104                                     Get(value, exp.instance.data);
6105                                     PopulateInstance(exp.instance);
6106                                  }
6107                                  break;
6108                               }
6109                            }
6110                         }
6111                         /*else
6112                         {
6113                            char * value = exp.member.exp.instance.data;
6114                            switch(type.kind)
6115                            {
6116                               case classType:
6117                               {
6118                                  Class _class = type._class.registered;
6119                                  if(_class.type == normalClass)
6120                                  {
6121                                     void *(*Get)(void *) = (void *)prop.Get;
6122
6123                                     exp.instance = Instantiation { };
6124                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6125                                     exp.type = instanceExp;
6126                                     exp.instance.data = Get(value, exp.instance.data);
6127                                  }
6128                                  break;
6129                               }
6130                            }
6131                         }
6132                         */
6133                      }
6134                   }
6135                }
6136                else
6137                {
6138                   exp.isConstant = false;
6139                }
6140             }
6141             else if(member)
6142             {
6143             }
6144          }
6145
6146          if(exp.type != ExpressionType::memberExp)
6147          {
6148             FreeExpression(memberExp);
6149             FreeIdentifier(memberID);
6150          }
6151          break;
6152       }
6153       case typeSizeExp:
6154       {
6155          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6156          FreeExpContents(exp);
6157          exp.constant = PrintUInt(ComputeTypeSize(type));
6158          exp.type = constantExp;
6159          FreeType(type);
6160          break;
6161       }
6162       case classSizeExp:
6163       {
6164          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6165          if(classSym && classSym.registered)
6166          {
6167             if(classSym.registered.fixed)
6168             {
6169                FreeSpecifier(exp._class);
6170                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6171                exp.type = constantExp;
6172             }
6173             else
6174             {
6175                char className[1024];
6176                strcpy(className, "__ecereClass_");
6177                FullClassNameCat(className, classSym.string, true);
6178                //MangleClassName(className);
6179
6180                DeclareClass(classSym, className);
6181
6182                FreeExpContents(exp);
6183                exp.type = pointerExp;
6184                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6185                exp.member.member = MkIdentifier("structSize");
6186             }
6187          }
6188          break;
6189       }
6190       case castExp:
6191       //case constantExp:
6192       {
6193          Type type;
6194          Expression e = exp;
6195          if(exp.type == castExp)
6196          {
6197             if(exp.cast.exp)
6198                ComputeExpression(exp.cast.exp);
6199             e = exp.cast.exp;
6200          }
6201          if(e && exp.expType)
6202          {
6203             /*if(exp.destType)
6204                type = exp.destType;
6205             else*/
6206                type = exp.expType;
6207             if(type.kind == classType)
6208             {
6209                Class _class = type._class.registered;
6210                if(_class && (_class.type == unitClass || _class.type == bitClass))
6211                {
6212                   if(!_class.dataType)
6213                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6214                   type = _class.dataType;
6215                }
6216             }
6217
6218             switch(type.kind)
6219             {
6220                case _BoolType:
6221                case charType:
6222                   if(type.isSigned)
6223                   {
6224                      char value = 0;
6225                      if(GetChar(e, &value))
6226                      {
6227                         FreeExpContents(exp);
6228                         exp.constant = PrintChar(value);
6229                         exp.type = constantExp;
6230                      }
6231                   }
6232                   else
6233                   {
6234                      unsigned char value = 0;
6235                      if(GetUChar(e, &value))
6236                      {
6237                         FreeExpContents(exp);
6238                         exp.constant = PrintUChar(value);
6239                         exp.type = constantExp;
6240                      }
6241                   }
6242                   break;
6243                case shortType:
6244                   if(type.isSigned)
6245                   {
6246                      short value = 0;
6247                      if(GetShort(e, &value))
6248                      {
6249                         FreeExpContents(exp);
6250                         exp.constant = PrintShort(value);
6251                         exp.type = constantExp;
6252                      }
6253                   }
6254                   else
6255                   {
6256                      unsigned short value = 0;
6257                      if(GetUShort(e, &value))
6258                      {
6259                         FreeExpContents(exp);
6260                         exp.constant = PrintUShort(value);
6261                         exp.type = constantExp;
6262                      }
6263                   }
6264                   break;
6265                case intType:
6266                   if(type.isSigned)
6267                   {
6268                      int value = 0;
6269                      if(GetInt(e, &value))
6270                      {
6271                         FreeExpContents(exp);
6272                         exp.constant = PrintInt(value);
6273                         exp.type = constantExp;
6274                      }
6275                   }
6276                   else
6277                   {
6278                      unsigned int value = 0;
6279                      if(GetUInt(e, &value))
6280                      {
6281                         FreeExpContents(exp);
6282                         exp.constant = PrintUInt(value);
6283                         exp.type = constantExp;
6284                      }
6285                   }
6286                   break;
6287                case int64Type:
6288                   if(type.isSigned)
6289                   {
6290                      int64 value = 0;
6291                      if(GetInt64(e, &value))
6292                      {
6293                         FreeExpContents(exp);
6294                         exp.constant = PrintInt64(value);
6295                         exp.type = constantExp;
6296                      }
6297                   }
6298                   else
6299                   {
6300                      uint64 value = 0;
6301                      if(GetUInt64(e, &value))
6302                      {
6303                         FreeExpContents(exp);
6304                         exp.constant = PrintUInt64(value);
6305                         exp.type = constantExp;
6306                      }
6307                   }
6308                   break;
6309                case intPtrType:
6310                   if(type.isSigned)
6311                   {
6312                      intptr value = 0;
6313                      if(GetIntPtr(e, &value))
6314                      {
6315                         FreeExpContents(exp);
6316                         exp.constant = PrintInt64((int64)value);
6317                         exp.type = constantExp;
6318                      }
6319                   }
6320                   else
6321                   {
6322                      uintptr value = 0;
6323                      if(GetUIntPtr(e, &value))
6324                      {
6325                         FreeExpContents(exp);
6326                         exp.constant = PrintUInt64((uint64)value);
6327                         exp.type = constantExp;
6328                      }
6329                   }
6330                   break;
6331                case intSizeType:
6332                   if(type.isSigned)
6333                   {
6334                      intsize value = 0;
6335                      if(GetIntSize(e, &value))
6336                      {
6337                         FreeExpContents(exp);
6338                         exp.constant = PrintInt64((int64)value);
6339                         exp.type = constantExp;
6340                      }
6341                   }
6342                   else
6343                   {
6344                      uintsize value = 0;
6345                      if(GetUIntSize(e, &value))
6346                      {
6347                         FreeExpContents(exp);
6348                         exp.constant = PrintUInt64((uint64)value);
6349                         exp.type = constantExp;
6350                      }
6351                   }
6352                   break;
6353                case floatType:
6354                {
6355                   float value = 0;
6356                   if(GetFloat(e, &value))
6357                   {
6358                      FreeExpContents(exp);
6359                      exp.constant = PrintFloat(value);
6360                      exp.type = constantExp;
6361                   }
6362                   break;
6363                }
6364                case doubleType:
6365                {
6366                   double value = 0;
6367                   if(GetDouble(e, &value))
6368                   {
6369                      FreeExpContents(exp);
6370                      exp.constant = PrintDouble(value);
6371                      exp.type = constantExp;
6372                   }
6373                   break;
6374                }
6375             }
6376          }
6377          break;
6378       }
6379       case conditionExp:
6380       {
6381          Operand op1 { };
6382          Operand op2 { };
6383          Operand op3 { };
6384
6385          if(exp.cond.exp)
6386             // Caring only about last expression for now...
6387             ComputeExpression(exp.cond.exp->last);
6388          if(exp.cond.elseExp)
6389             ComputeExpression(exp.cond.elseExp);
6390          if(exp.cond.cond)
6391             ComputeExpression(exp.cond.cond);
6392
6393          op1 = GetOperand(exp.cond.cond);
6394          if(op1.type) op1.type.refCount++;
6395          op2 = GetOperand(exp.cond.exp->last);
6396          if(op2.type) op2.type.refCount++;
6397          op3 = GetOperand(exp.cond.elseExp);
6398          if(op3.type) op3.type.refCount++;
6399
6400          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6401          if(op1.type) FreeType(op1.type);
6402          if(op2.type) FreeType(op2.type);
6403          if(op3.type) FreeType(op3.type);
6404          break;
6405       }
6406    }
6407 }
6408
6409 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla, bool warnConst)
6410 {
6411    bool result = true;
6412    if(destType)
6413    {
6414       OldList converts { };
6415       Conversion convert;
6416
6417       if(destType.kind == voidType)
6418          return false;
6419
6420       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla, warnConst))
6421          result = false;
6422       if(converts.count)
6423       {
6424          // for(convert = converts.last; convert; convert = convert.prev)
6425          for(convert = converts.first; convert; convert = convert.next)
6426          {
6427             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6428             if(!empty)
6429             {
6430                Expression newExp { };
6431                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6432
6433                // TODO: Check this...
6434                *newExp = *exp;
6435                newExp.prev = null;
6436                newExp.next = null;
6437                newExp.destType = null;
6438
6439                if(convert.isGet)
6440                {
6441                   // [exp].ColorRGB
6442                   exp.type = memberExp;
6443                   exp.addedThis = true;
6444                   exp.member.exp = newExp;
6445                   FreeType(exp.member.exp.expType);
6446
6447                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6448                   exp.member.exp.expType.classObjectType = objectType;
6449                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6450                   exp.member.memberType = propertyMember;
6451                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6452                   // TESTING THIS... for (int)degrees
6453                   exp.needCast = true;
6454                   if(exp.expType) exp.expType.refCount++;
6455                   ApplyAnyObjectLogic(exp.member.exp);
6456                }
6457                else
6458                {
6459
6460                   /*if(exp.isConstant)
6461                   {
6462                      // Color { ColorRGB = [exp] };
6463                      exp.type = instanceExp;
6464                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6465                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6466                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6467                   }
6468                   else*/
6469                   {
6470                      // If not constant, don't turn it yet into an instantiation
6471                      // (Go through the deep members system first)
6472                      exp.type = memberExp;
6473                      exp.addedThis = true;
6474                      exp.member.exp = newExp;
6475
6476                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6477                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6478                         newExp.expType._class.registered.type == noHeadClass)
6479                      {
6480                         newExp.byReference = true;
6481                      }
6482
6483                      FreeType(exp.member.exp.expType);
6484                      /*exp.member.exp.expType = convert.convert.dataType;
6485                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6486                      exp.member.exp.expType = null;
6487                      if(convert.convert.dataType)
6488                      {
6489                         exp.member.exp.expType = { };
6490                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6491                         exp.member.exp.expType.refCount = 1;
6492                         exp.member.exp.expType.classObjectType = objectType;
6493                         ApplyAnyObjectLogic(exp.member.exp);
6494                      }
6495
6496                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6497                      exp.member.memberType = reverseConversionMember;
6498                      exp.expType = convert.resultType ? convert.resultType :
6499                         MkClassType(convert.convert._class.fullName);
6500                      exp.needCast = true;
6501                      if(convert.resultType) convert.resultType.refCount++;
6502                   }
6503                }
6504             }
6505             else
6506             {
6507                FreeType(exp.expType);
6508                if(convert.isGet)
6509                {
6510                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6511                   if(exp.destType.casted)
6512                      exp.needCast = true;
6513                   if(exp.expType) exp.expType.refCount++;
6514                }
6515                else
6516                {
6517                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6518                   if(exp.destType.casted)
6519                      exp.needCast = true;
6520                   if(convert.resultType)
6521                      convert.resultType.refCount++;
6522                }
6523             }
6524          }
6525          if(exp.isConstant && inCompiler)
6526             ComputeExpression(exp);
6527
6528          converts.Free(FreeConvert);
6529       }
6530
6531       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6532       {
6533          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false, warnConst);
6534       }
6535       if(!result && exp.expType && exp.destType)
6536       {
6537          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6538              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6539             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6540             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6541             result = true;
6542       }
6543    }
6544    // if(result) CheckTemplateTypes(exp);
6545    return result;
6546 }
6547
6548 void CheckTemplateTypes(Expression exp)
6549 {
6550    Expression nbExp = GetNonBracketsExp(exp);
6551    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate &&
6552       (nbExp == exp || nbExp.type != castExp))
6553    {
6554       Expression newExp { };
6555       Context context;
6556       *newExp = *exp;
6557       if(exp.destType) exp.destType.refCount++;
6558       if(exp.expType)  exp.expType.refCount++;
6559       newExp.prev = null;
6560       newExp.next = null;
6561
6562       switch(exp.expType.kind)
6563       {
6564          case doubleType:
6565             if(exp.destType.classObjectType)
6566             {
6567                // We need to pass the address, just pass it along (Undo what was done above)
6568                if(exp.destType) exp.destType.refCount--;
6569                if(exp.expType)  exp.expType.refCount--;
6570                delete newExp;
6571             }
6572             else
6573             {
6574                // If we're looking for value:
6575                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6576                OldList * specs;
6577                OldList * unionDefs = MkList();
6578                OldList * statements = MkList();
6579                context = PushContext();
6580                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6581                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6582                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6583                exp.type = extensionCompoundExp;
6584                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6585                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6586                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6587                exp.compound.compound.context = context;
6588                PopContext(context);
6589             }
6590             break;
6591          default:
6592             exp.type = castExp;
6593             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6594             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6595             exp.needCast = true;
6596             break;
6597       }
6598    }
6599    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6600    {
6601       Expression newExp { };
6602       Context context;
6603       *newExp = *exp;
6604       if(exp.destType) exp.destType.refCount++;
6605       if(exp.expType)  exp.expType.refCount++;
6606       newExp.prev = null;
6607       newExp.next = null;
6608
6609       switch(exp.expType.kind)
6610       {
6611          case doubleType:
6612             if(exp.destType.classObjectType)
6613             {
6614                // We need to pass the address, just pass it along (Undo what was done above)
6615                if(exp.destType) exp.destType.refCount--;
6616                if(exp.expType)  exp.expType.refCount--;
6617                delete newExp;
6618             }
6619             else
6620             {
6621                // If we're looking for value:
6622                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6623                OldList * specs;
6624                OldList * unionDefs = MkList();
6625                OldList * statements = MkList();
6626                context = PushContext();
6627                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6628                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6629                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6630                exp.type = extensionCompoundExp;
6631                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6632                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6633                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6634                exp.compound.compound.context = context;
6635                PopContext(context);
6636             }
6637             break;
6638          case classType:
6639          {
6640             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6641             {
6642                exp.type = bracketsExp;
6643                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6644                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6645                ProcessExpressionType(exp.list->first);
6646                break;
6647             }
6648             else
6649             {
6650                exp.type = bracketsExp;
6651                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6652                exp.needTemplateCast = 2;
6653                newExp.needCast = true;
6654                newExp.needTemplateCast = 2;
6655                ProcessExpressionType(exp.list->first);
6656                break;
6657             }
6658          }
6659          default:
6660          {
6661             if(exp.expType.kind == templateType)
6662             {
6663                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6664                if(type)
6665                {
6666                   FreeType(exp.destType);
6667                   FreeType(exp.expType);
6668                   delete newExp;
6669                   break;
6670                }
6671             }
6672             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6673             {
6674                exp.type = opExp;
6675                exp.op.op = '*';
6676                exp.op.exp1 = null;
6677                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6678                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6679             }
6680             else
6681             {
6682                char typeString[1024];
6683                Declarator decl;
6684                OldList * specs = MkList();
6685                typeString[0] = '\0';
6686                PrintType(exp.expType, typeString, false, false);
6687                decl = SpecDeclFromString(typeString, specs, null);
6688
6689                exp.type = castExp;
6690                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6691                exp.cast.typeName = MkTypeName(specs, decl);
6692                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6693                exp.cast.exp.needCast = true;
6694             }
6695             break;
6696          }
6697       }
6698    }
6699 }
6700 // TODO: The Symbol tree should be reorganized by namespaces
6701 // Name Space:
6702 //    - Tree of all symbols within (stored without namespace)
6703 //    - Tree of sub-namespaces
6704
6705 static Symbol ScanWithNameSpace(BinaryTree tree, const char * nameSpace, const char * name)
6706 {
6707    int nsLen = strlen(nameSpace);
6708    Symbol symbol;
6709    // Start at the name space prefix
6710    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6711    {
6712       char * s = symbol.string;
6713       if(!strncmp(s, nameSpace, nsLen))
6714       {
6715          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6716          int c;
6717          char * namePart;
6718          for(c = strlen(s)-1; c >= 0; c--)
6719             if(s[c] == ':')
6720                break;
6721
6722          namePart = s+c+1;
6723          if(!strcmp(namePart, name))
6724          {
6725             // TODO: Error on ambiguity
6726             return symbol;
6727          }
6728       }
6729       else
6730          break;
6731    }
6732    return null;
6733 }
6734
6735 static Symbol FindWithNameSpace(BinaryTree tree, const char * name)
6736 {
6737    int c;
6738    char nameSpace[1024];
6739    const char * namePart;
6740    bool gotColon = false;
6741
6742    nameSpace[0] = '\0';
6743    for(c = strlen(name)-1; c >= 0; c--)
6744       if(name[c] == ':')
6745       {
6746          gotColon = true;
6747          break;
6748       }
6749
6750    namePart = name+c+1;
6751    while(c >= 0 && name[c] == ':') c--;
6752    if(c >= 0)
6753    {
6754       // Try an exact match first
6755       Symbol symbol = (Symbol)tree.FindString(name);
6756       if(symbol)
6757          return symbol;
6758
6759       // Namespace specified
6760       memcpy(nameSpace, name, c + 1);
6761       nameSpace[c+1] = 0;
6762
6763       return ScanWithNameSpace(tree, nameSpace, namePart);
6764    }
6765    else if(gotColon)
6766    {
6767       // Looking for a global symbol, e.g. ::Sleep()
6768       Symbol symbol = (Symbol)tree.FindString(namePart);
6769       return symbol;
6770    }
6771    else
6772    {
6773       // Name only (no namespace specified)
6774       Symbol symbol = (Symbol)tree.FindString(namePart);
6775       if(symbol)
6776          return symbol;
6777       return ScanWithNameSpace(tree, "", namePart);
6778    }
6779    return null;
6780 }
6781
6782 static void ProcessDeclaration(Declaration decl);
6783
6784 /*static */Symbol FindSymbol(const char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6785 {
6786 #ifdef _DEBUG
6787    //Time startTime = GetTime();
6788 #endif
6789    // Optimize this later? Do this before/less?
6790    Context ctx;
6791    Symbol symbol = null;
6792    // First, check if the identifier is declared inside the function
6793    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6794
6795    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6796    {
6797       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6798       {
6799          symbol = null;
6800          if(thisNameSpace)
6801          {
6802             char curName[1024];
6803             strcpy(curName, thisNameSpace);
6804             strcat(curName, "::");
6805             strcat(curName, name);
6806             // Try to resolve in current namespace first
6807             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6808          }
6809          if(!symbol)
6810             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6811       }
6812       else
6813          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6814
6815       if(symbol || ctx == endContext) break;
6816    }
6817    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6818    {
6819       if(symbol.pointerExternal.type == functionExternal)
6820       {
6821          FunctionDefinition function = symbol.pointerExternal.function;
6822
6823          // Modified this recently...
6824          Context tmpContext = curContext;
6825          curContext = null;
6826          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6827          curContext = tmpContext;
6828
6829          symbol.pointerExternal.symbol = symbol;
6830
6831          // TESTING THIS:
6832          DeclareType(symbol.type, true, true);
6833
6834          ast->Insert(curExternal.prev, symbol.pointerExternal);
6835
6836          symbol.id = curExternal.symbol.idCode;
6837
6838       }
6839       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6840       {
6841          ast->Move(symbol.pointerExternal, curExternal.prev);
6842          symbol.id = curExternal.symbol.idCode;
6843       }
6844    }
6845 #ifdef _DEBUG
6846    //findSymbolTotalTime += GetTime() - startTime;
6847 #endif
6848    return symbol;
6849 }
6850
6851 static void GetTypeSpecs(Type type, OldList * specs)
6852 {
6853    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6854    switch(type.kind)
6855    {
6856       case classType:
6857       {
6858          if(type._class.registered)
6859          {
6860             if(!type._class.registered.dataType)
6861                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6862             GetTypeSpecs(type._class.registered.dataType, specs);
6863          }
6864          break;
6865       }
6866       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6867       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6868       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6869       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6870       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6871       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6872       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6873       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6874       case intType:
6875       default:
6876          ListAdd(specs, MkSpecifier(INT)); break;
6877    }
6878 }
6879
6880 static void PrintArraySize(Type arrayType, char * string)
6881 {
6882    char size[256];
6883    size[0] = '\0';
6884    strcat(size, "[");
6885    if(arrayType.enumClass)
6886       strcat(size, arrayType.enumClass.string);
6887    else if(arrayType.arraySizeExp)
6888       PrintExpression(arrayType.arraySizeExp, size);
6889    strcat(size, "]");
6890    strcat(string, size);
6891 }
6892
6893 // WARNING : This function expects a null terminated string since it recursively concatenate...
6894 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6895 {
6896    if(type)
6897    {
6898       if(printConst && type.constant)
6899          strcat(string, "const ");
6900       switch(type.kind)
6901       {
6902          case classType:
6903          {
6904             Symbol c = type._class;
6905             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6906             //       look into merging with thisclass ?
6907             if(type.classObjectType == typedObject)
6908                strcat(string, "typed_object");
6909             else if(type.classObjectType == anyObject)
6910                strcat(string, "any_object");
6911             else
6912             {
6913                if(c && c.string)
6914                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6915             }
6916             if(type.byReference)
6917                strcat(string, " &");
6918             break;
6919          }
6920          case voidType: strcat(string, "void"); break;
6921          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6922          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6923          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6924          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6925          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6926          case _BoolType: strcat(string, "_Bool"); break;
6927          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6928          case floatType: strcat(string, "float"); break;
6929          case doubleType: strcat(string, "double"); break;
6930          case structType:
6931             if(type.enumName)
6932             {
6933                strcat(string, "struct ");
6934                strcat(string, type.enumName);
6935             }
6936             else if(type.typeName)
6937                strcat(string, type.typeName);
6938             else
6939             {
6940                Type member;
6941                strcat(string, "struct { ");
6942                for(member = type.members.first; member; member = member.next)
6943                {
6944                   PrintType(member, string, true, fullName);
6945                   strcat(string,"; ");
6946                }
6947                strcat(string,"}");
6948             }
6949             break;
6950          case unionType:
6951             if(type.enumName)
6952             {
6953                strcat(string, "union ");
6954                strcat(string, type.enumName);
6955             }
6956             else if(type.typeName)
6957                strcat(string, type.typeName);
6958             else
6959             {
6960                strcat(string, "union ");
6961                strcat(string,"(unnamed)");
6962             }
6963             break;
6964          case enumType:
6965             if(type.enumName)
6966             {
6967                strcat(string, "enum ");
6968                strcat(string, type.enumName);
6969             }
6970             else if(type.typeName)
6971                strcat(string, type.typeName);
6972             else
6973                strcat(string, "int"); // "enum");
6974             break;
6975          case ellipsisType:
6976             strcat(string, "...");
6977             break;
6978          case subClassType:
6979             strcat(string, "subclass(");
6980             strcat(string, type._class ? type._class.string : "int");
6981             strcat(string, ")");
6982             break;
6983          case templateType:
6984             strcat(string, type.templateParameter.identifier.string);
6985             break;
6986          case thisClassType:
6987             strcat(string, "thisclass");
6988             break;
6989          case vaListType:
6990             strcat(string, "__builtin_va_list");
6991             break;
6992       }
6993    }
6994 }
6995
6996 static void PrintName(Type type, char * string, bool fullName)
6997 {
6998    if(type.name && type.name[0])
6999    {
7000       if(fullName)
7001          strcat(string, type.name);
7002       else
7003       {
7004          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
7005          if(name) name += 2; else name = type.name;
7006          strcat(string, name);
7007       }
7008    }
7009 }
7010
7011 static void PrintAttribs(Type type, char * string)
7012 {
7013    if(type)
7014    {
7015       if(type.dllExport)   strcat(string, "dllexport ");
7016       if(type.attrStdcall) strcat(string, "stdcall ");
7017    }
7018 }
7019
7020 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
7021 {
7022    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7023    {
7024       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
7025          PrintAttribs(type, string);
7026       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
7027          strcat(string, " const");
7028       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
7029       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7030          strcat(string, " (");
7031       if(type.kind == pointerType)
7032       {
7033          if(type.type.kind == functionType || type.type.kind == methodType)
7034             PrintAttribs(type.type, string);
7035       }
7036       if(type.kind == pointerType)
7037       {
7038          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
7039             strcat(string, "*");
7040          else
7041             strcat(string, " *");
7042       }
7043       if(printConst && type.constant && type.kind == pointerType)
7044          strcat(string, " const");
7045    }
7046    else
7047       PrintTypeSpecs(type, string, fullName, printConst);
7048 }
7049
7050 static void PostPrintType(Type type, char * string, bool fullName)
7051 {
7052    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7053       strcat(string, ")");
7054    if(type.kind == arrayType)
7055       PrintArraySize(type, string);
7056    else if(type.kind == functionType)
7057    {
7058       Type param;
7059       strcat(string, "(");
7060       for(param = type.params.first; param; param = param.next)
7061       {
7062          PrintType(param, string, true, fullName);
7063          if(param.next) strcat(string, ", ");
7064       }
7065       strcat(string, ")");
7066    }
7067    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7068       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7069 }
7070
7071 // *****
7072 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7073 // *****
7074 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7075 {
7076    PrePrintType(type, string, fullName, null, printConst);
7077
7078    if(type.thisClass || (printName && type.name && type.name[0]))
7079       strcat(string, " ");
7080    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7081    {
7082       Symbol _class = type.thisClass;
7083       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7084       {
7085          if(type.classObjectType == classPointer)
7086             strcat(string, "class");
7087          else
7088             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7089       }
7090       else if(_class && _class.string)
7091       {
7092          String s = _class.string;
7093          if(fullName)
7094             strcat(string, s);
7095          else
7096          {
7097             char * name = RSearchString(s, "::", strlen(s), true, false);
7098             if(name) name += 2; else name = s;
7099             strcat(string, name);
7100          }
7101       }
7102       strcat(string, "::");
7103    }
7104
7105    if(printName && type.name)
7106       PrintName(type, string, fullName);
7107    PostPrintType(type, string, fullName);
7108    if(type.bitFieldCount)
7109    {
7110       char count[100];
7111       sprintf(count, ":%d", type.bitFieldCount);
7112       strcat(string, count);
7113    }
7114 }
7115
7116 void PrintType(Type type, char * string, bool printName, bool fullName)
7117 {
7118    _PrintType(type, string, printName, fullName, true);
7119 }
7120
7121 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7122 {
7123    _PrintType(type, string, printName, fullName, false);
7124 }
7125
7126 static Type FindMember(Type type, char * string)
7127 {
7128    Type memberType;
7129    for(memberType = type.members.first; memberType; memberType = memberType.next)
7130    {
7131       if(!memberType.name)
7132       {
7133          Type subType = FindMember(memberType, string);
7134          if(subType)
7135             return subType;
7136       }
7137       else if(!strcmp(memberType.name, string))
7138          return memberType;
7139    }
7140    return null;
7141 }
7142
7143 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7144 {
7145    Type memberType;
7146    for(memberType = type.members.first; memberType; memberType = memberType.next)
7147    {
7148       if(!memberType.name)
7149       {
7150          Type subType = FindMember(memberType, string);
7151          if(subType)
7152          {
7153             *offset += memberType.offset;
7154             return subType;
7155          }
7156       }
7157       else if(!strcmp(memberType.name, string))
7158       {
7159          *offset += memberType.offset;
7160          return memberType;
7161       }
7162    }
7163    return null;
7164 }
7165
7166 public bool GetParseError() { return parseError; }
7167
7168 Expression ParseExpressionString(char * expression)
7169 {
7170    parseError = false;
7171
7172    fileInput = TempFile { };
7173    fileInput.Write(expression, 1, strlen(expression));
7174    fileInput.Seek(0, start);
7175
7176    echoOn = false;
7177    parsedExpression = null;
7178    resetScanner();
7179    expression_yyparse();
7180    delete fileInput;
7181
7182    return parsedExpression;
7183 }
7184
7185 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7186 {
7187    Identifier id = exp.identifier;
7188    Method method = null;
7189    Property prop = null;
7190    DataMember member = null;
7191    ClassProperty classProp = null;
7192
7193    if(_class && _class.type == enumClass)
7194    {
7195       NamedLink value = null;
7196       Class enumClass = eSystem_FindClass(privateModule, "enum");
7197       if(enumClass)
7198       {
7199          Class baseClass;
7200          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7201          {
7202             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7203             for(value = e.values.first; value; value = value.next)
7204             {
7205                if(!strcmp(value.name, id.string))
7206                   break;
7207             }
7208             if(value)
7209             {
7210                char constant[256];
7211
7212                FreeExpContents(exp);
7213
7214                exp.type = constantExp;
7215                exp.isConstant = true;
7216                if(!strcmp(baseClass.dataTypeString, "int"))
7217                   sprintf(constant, "%d",(int)value.data);
7218                else
7219                   sprintf(constant, "0x%X",(int)value.data);
7220                exp.constant = CopyString(constant);
7221                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7222                exp.expType = MkClassType(baseClass.fullName);
7223                break;
7224             }
7225          }
7226       }
7227       if(value)
7228          return true;
7229    }
7230    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7231    {
7232       ProcessMethodType(method);
7233       exp.expType = Type
7234       {
7235          refCount = 1;
7236          kind = methodType;
7237          method = method;
7238          // Crash here?
7239          // TOCHECK: Put it back to what it was...
7240          // methodClass = _class;
7241          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7242       };
7243       //id._class = null;
7244       return true;
7245    }
7246    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7247    {
7248       if(!prop.dataType)
7249          ProcessPropertyType(prop);
7250       exp.expType = prop.dataType;
7251       if(prop.dataType) prop.dataType.refCount++;
7252       return true;
7253    }
7254    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7255    {
7256       if(!member.dataType)
7257          member.dataType = ProcessTypeString(member.dataTypeString, false);
7258       exp.expType = member.dataType;
7259       if(member.dataType) member.dataType.refCount++;
7260       return true;
7261    }
7262    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7263    {
7264       if(!classProp.dataType)
7265          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7266
7267       if(classProp.constant)
7268       {
7269          FreeExpContents(exp);
7270
7271          exp.isConstant = true;
7272          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7273          {
7274             //char constant[256];
7275             exp.type = stringExp;
7276             exp.constant = QMkString((char *)classProp.Get(_class));
7277          }
7278          else
7279          {
7280             char constant[256];
7281             exp.type = constantExp;
7282             sprintf(constant, "%d", (int)classProp.Get(_class));
7283             exp.constant = CopyString(constant);
7284          }
7285       }
7286       else
7287       {
7288          // TO IMPLEMENT...
7289       }
7290
7291       exp.expType = classProp.dataType;
7292       if(classProp.dataType) classProp.dataType.refCount++;
7293       return true;
7294    }
7295    return false;
7296 }
7297
7298 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7299 {
7300    BinaryTree * tree = &nameSpace.functions;
7301    GlobalData data = (GlobalData)tree->FindString(name);
7302    NameSpace * child;
7303    if(!data)
7304    {
7305       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7306       {
7307          data = ScanGlobalData(child, name);
7308          if(data)
7309             break;
7310       }
7311    }
7312    return data;
7313 }
7314
7315 static GlobalData FindGlobalData(char * name)
7316 {
7317    int start = 0, c;
7318    NameSpace * nameSpace;
7319    nameSpace = globalData;
7320    for(c = 0; name[c]; c++)
7321    {
7322       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7323       {
7324          NameSpace * newSpace;
7325          char * spaceName = new char[c - start + 1];
7326          strncpy(spaceName, name + start, c - start);
7327          spaceName[c-start] = '\0';
7328          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7329          delete spaceName;
7330          if(!newSpace)
7331             return null;
7332          nameSpace = newSpace;
7333          if(name[c] == ':') c++;
7334          start = c+1;
7335       }
7336    }
7337    if(c - start)
7338    {
7339       return ScanGlobalData(nameSpace, name + start);
7340    }
7341    return null;
7342 }
7343
7344 static int definedExpStackPos;
7345 static void * definedExpStack[512];
7346
7347 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7348 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7349 {
7350    Expression prev = checkedExp.prev, next = checkedExp.next;
7351
7352    FreeExpContents(checkedExp);
7353    FreeType(checkedExp.expType);
7354    FreeType(checkedExp.destType);
7355
7356    *checkedExp = *newExp;
7357
7358    delete newExp;
7359
7360    checkedExp.prev = prev;
7361    checkedExp.next = next;
7362 }
7363
7364 void ApplyAnyObjectLogic(Expression e)
7365 {
7366    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7367 #ifdef _DEBUG
7368    char debugExpString[4096];
7369    debugExpString[0] = '\0';
7370    PrintExpression(e, debugExpString);
7371 #endif
7372
7373    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7374    {
7375       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7376       //ellipsisDestType = destType;
7377       if(e && e.expType)
7378       {
7379          Type type = e.expType;
7380          Class _class = null;
7381          //Type destType = e.destType;
7382
7383          if(type.kind == classType && type._class && type._class.registered)
7384          {
7385             _class = type._class.registered;
7386          }
7387          else if(type.kind == subClassType)
7388          {
7389             _class = FindClass("ecere::com::Class").registered;
7390          }
7391          else
7392          {
7393             char string[1024] = "";
7394             Symbol classSym;
7395
7396             PrintTypeNoConst(type, string, false, true);
7397             classSym = FindClass(string);
7398             if(classSym) _class = classSym.registered;
7399          }
7400
7401          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...
7402             (!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))) ||
7403             destType.byReference)))
7404          {
7405             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7406             {
7407                Expression checkedExp = e, newExp;
7408
7409                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7410                {
7411                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7412                   {
7413                      if(checkedExp.type == extensionCompoundExp)
7414                      {
7415                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7416                      }
7417                      else
7418                         checkedExp = checkedExp.list->last;
7419                   }
7420                   else if(checkedExp.type == castExp)
7421                      checkedExp = checkedExp.cast.exp;
7422                }
7423
7424                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7425                {
7426                   newExp = checkedExp.op.exp2;
7427                   checkedExp.op.exp2 = null;
7428                   FreeExpContents(checkedExp);
7429
7430                   if(e.expType && e.expType.passAsTemplate)
7431                   {
7432                      char size[100];
7433                      ComputeTypeSize(e.expType);
7434                      sprintf(size, "%d", e.expType.size);
7435                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7436                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7437                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7438                   }
7439
7440                   ReplaceExpContents(checkedExp, newExp);
7441                   e.byReference = true;
7442                }
7443                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7444                {
7445                   Expression checkedExp; //, newExp;
7446
7447                   {
7448                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7449                      bool hasAddress =
7450                         e.type == identifierExp ||
7451                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7452                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7453                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7454                         e.type == indexExp;
7455
7456                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7457                      {
7458                         Context context = PushContext();
7459                         Declarator decl;
7460                         OldList * specs = MkList();
7461                         char typeString[1024];
7462                         Expression newExp { };
7463
7464                         typeString[0] = '\0';
7465                         *newExp = *e;
7466
7467                         //if(e.destType) e.destType.refCount++;
7468                         // if(exp.expType) exp.expType.refCount++;
7469                         newExp.prev = null;
7470                         newExp.next = null;
7471                         newExp.expType = null;
7472
7473                         PrintTypeNoConst(e.expType, typeString, false, true);
7474                         decl = SpecDeclFromString(typeString, specs, null);
7475                         newExp.destType = ProcessType(specs, decl);
7476
7477                         curContext = context;
7478
7479                         // We need a current compound for this
7480                         if(curCompound)
7481                         {
7482                            char name[100];
7483                            OldList * stmts = MkList();
7484                            e.type = extensionCompoundExp;
7485                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7486                            if(!curCompound.compound.declarations)
7487                               curCompound.compound.declarations = MkList();
7488                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7489                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7490                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7491                            e.compound = MkCompoundStmt(null, stmts);
7492                         }
7493                         else
7494                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7495
7496                         /*
7497                         e.compound = MkCompoundStmt(
7498                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7499                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7500
7501                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7502                         */
7503
7504                         {
7505                            Type type = e.destType;
7506                            e.destType = { };
7507                            CopyTypeInto(e.destType, type);
7508                            e.destType.refCount = 1;
7509                            e.destType.classObjectType = none;
7510                            FreeType(type);
7511                         }
7512
7513                         e.compound.compound.context = context;
7514                         PopContext(context);
7515                         curContext = context.parent;
7516                      }
7517                   }
7518
7519                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7520                   checkedExp = e;
7521                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7522                   {
7523                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7524                      {
7525                         if(checkedExp.type == extensionCompoundExp)
7526                         {
7527                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7528                         }
7529                         else
7530                            checkedExp = checkedExp.list->last;
7531                      }
7532                      else if(checkedExp.type == castExp)
7533                         checkedExp = checkedExp.cast.exp;
7534                   }
7535                   {
7536                      Expression operand { };
7537                      operand = *checkedExp;
7538                      checkedExp.destType = null;
7539                      checkedExp.expType = null;
7540                      checkedExp.Clear();
7541                      checkedExp.type = opExp;
7542                      checkedExp.op.op = '&';
7543                      checkedExp.op.exp1 = null;
7544                      checkedExp.op.exp2 = operand;
7545
7546                      //newExp = MkExpOp(null, '&', checkedExp);
7547                   }
7548                   //ReplaceExpContents(checkedExp, newExp);
7549                }
7550             }
7551          }
7552       }
7553    }
7554    {
7555       // If expression type is a simple class, make it an address
7556       // FixReference(e, true);
7557    }
7558 //#if 0
7559    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7560       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7561          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7562    {
7563       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"))
7564       {
7565          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7566       }
7567       else
7568       {
7569          Expression thisExp { };
7570
7571          *thisExp = *e;
7572          thisExp.prev = null;
7573          thisExp.next = null;
7574          e.Clear();
7575
7576          e.type = bracketsExp;
7577          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7578          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7579             ((Expression)e.list->first).byReference = true;
7580
7581          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7582          {
7583             e.expType = thisExp.expType;
7584             e.expType.refCount++;
7585          }
7586          else*/
7587          {
7588             e.expType = { };
7589             CopyTypeInto(e.expType, thisExp.expType);
7590             e.expType.byReference = false;
7591             e.expType.refCount = 1;
7592
7593             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7594                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7595             {
7596                e.expType.classObjectType = none;
7597             }
7598          }
7599       }
7600    }
7601 // TOFIX: Try this for a nice IDE crash!
7602 //#endif
7603    // The other way around
7604    else
7605 //#endif
7606    if(destType && e.expType &&
7607          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7608          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7609          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7610    {
7611       if(destType.kind == ellipsisType)
7612       {
7613          Compiler_Error($"Unspecified type\n");
7614       }
7615       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7616       {
7617          bool byReference = e.expType.byReference;
7618          Expression thisExp { };
7619          Declarator decl;
7620          OldList * specs = MkList();
7621          char typeString[1024]; // Watch buffer overruns
7622          Type type;
7623          ClassObjectType backupClassObjectType;
7624          bool backupByReference;
7625
7626          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7627             type = e.expType;
7628          else
7629             type = destType;
7630
7631          backupClassObjectType = type.classObjectType;
7632          backupByReference = type.byReference;
7633
7634          type.classObjectType = none;
7635          type.byReference = false;
7636
7637          typeString[0] = '\0';
7638          PrintType(type, typeString, false, true);
7639          decl = SpecDeclFromString(typeString, specs, null);
7640
7641          type.classObjectType = backupClassObjectType;
7642          type.byReference = backupByReference;
7643
7644          *thisExp = *e;
7645          thisExp.prev = null;
7646          thisExp.next = null;
7647          e.Clear();
7648
7649          if( ( type.kind == classType && type._class && type._class.registered &&
7650                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7651                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7652              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7653              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7654          {
7655             e.type = opExp;
7656             e.op.op = '*';
7657             e.op.exp1 = null;
7658             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7659
7660             e.expType = { };
7661             CopyTypeInto(e.expType, type);
7662             e.expType.byReference = false;
7663             e.expType.refCount = 1;
7664          }
7665          else
7666          {
7667             e.type = castExp;
7668             e.cast.typeName = MkTypeName(specs, decl);
7669             e.cast.exp = thisExp;
7670             e.byReference = true;
7671             e.expType = type;
7672             type.refCount++;
7673          }
7674          e.destType = destType;
7675          destType.refCount++;
7676       }
7677    }
7678 }
7679
7680 void ApplyLocation(Expression exp, Location loc)
7681 {
7682    exp.loc = loc;
7683    switch(exp.type)
7684    {
7685       case opExp:
7686          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7687          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7688          break;
7689       case bracketsExp:
7690          if(exp.list)
7691          {
7692             Expression e;
7693             for(e = exp.list->first; e; e = e.next)
7694                ApplyLocation(e, loc);
7695          }
7696          break;
7697       case indexExp:
7698          if(exp.index.index)
7699          {
7700             Expression e;
7701             for(e = exp.index.index->first; e; e = e.next)
7702                ApplyLocation(e, loc);
7703          }
7704          if(exp.index.exp)
7705             ApplyLocation(exp.index.exp, loc);
7706          break;
7707       case callExp:
7708          if(exp.call.arguments)
7709          {
7710             Expression arg;
7711             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7712                ApplyLocation(arg, loc);
7713          }
7714          if(exp.call.exp)
7715             ApplyLocation(exp.call.exp, loc);
7716          break;
7717       case memberExp:
7718       case pointerExp:
7719          if(exp.member.exp)
7720             ApplyLocation(exp.member.exp, loc);
7721          break;
7722       case castExp:
7723          if(exp.cast.exp)
7724             ApplyLocation(exp.cast.exp, loc);
7725          break;
7726       case conditionExp:
7727          if(exp.cond.exp)
7728          {
7729             Expression e;
7730             for(e = exp.cond.exp->first; e; e = e.next)
7731                ApplyLocation(e, loc);
7732          }
7733          if(exp.cond.cond)
7734             ApplyLocation(exp.cond.cond, loc);
7735          if(exp.cond.elseExp)
7736             ApplyLocation(exp.cond.elseExp, loc);
7737          break;
7738       case vaArgExp:
7739          if(exp.vaArg.exp)
7740             ApplyLocation(exp.vaArg.exp, loc);
7741          break;
7742       default:
7743          break;
7744    }
7745 }
7746
7747 void ProcessExpressionType(Expression exp)
7748 {
7749    bool unresolved = false;
7750    Location oldyylloc = yylloc;
7751    bool notByReference = false;
7752 #ifdef _DEBUG
7753    char debugExpString[4096];
7754    debugExpString[0] = '\0';
7755    PrintExpression(exp, debugExpString);
7756 #endif
7757    if(!exp || exp.expType)
7758       return;
7759
7760    //eSystem_Logf("%s\n", expString);
7761
7762    // Testing this here
7763    yylloc = exp.loc;
7764    switch(exp.type)
7765    {
7766       case identifierExp:
7767       {
7768          Identifier id = exp.identifier;
7769          if(!id || !topContext) return;
7770
7771          // DOING THIS LATER NOW...
7772          if(id._class && id._class.name)
7773          {
7774             id.classSym = id._class.symbol; // FindClass(id._class.name);
7775             /* TODO: Name Space Fix ups
7776             if(!id.classSym)
7777                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7778             */
7779          }
7780
7781          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7782          {
7783             exp.expType = ProcessTypeString("Module", true);
7784             break;
7785          }
7786          else */if(strstr(id.string, "__ecereClass") == id.string)
7787          {
7788             exp.expType = ProcessTypeString("ecere::com::Class", true);
7789             break;
7790          }
7791          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7792          {
7793             // Added this here as well
7794             ReplaceClassMembers(exp, thisClass);
7795             if(exp.type != identifierExp)
7796             {
7797                ProcessExpressionType(exp);
7798                break;
7799             }
7800
7801             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7802                break;
7803          }
7804          else
7805          {
7806             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7807             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7808             if(!symbol/* && exp.destType*/)
7809             {
7810                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
7811                   break;
7812                else
7813                {
7814                   if(thisClass)
7815                   {
7816                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7817                      if(exp.type != identifierExp)
7818                      {
7819                         ProcessExpressionType(exp);
7820                         break;
7821                      }
7822                   }
7823                   // Static methods called from inside the _class
7824                   else if(currentClass && !id._class)
7825                   {
7826                      if(ResolveIdWithClass(exp, currentClass, true))
7827                         break;
7828                   }
7829                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7830                }
7831             }
7832
7833             // If we manage to resolve this symbol
7834             if(symbol)
7835             {
7836                Type type = symbol.type;
7837                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7838
7839                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7840                {
7841                   Context context = SetupTemplatesContext(_class);
7842                   type = ReplaceThisClassType(_class);
7843                   FinishTemplatesContext(context);
7844                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7845                }
7846
7847                FreeSpecifier(id._class);
7848                id._class = null;
7849                delete id.string;
7850                id.string = CopyString(symbol.string);
7851
7852                id.classSym = null;
7853                exp.expType = type;
7854                if(type)
7855                   type.refCount++;
7856
7857                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7858                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7859                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7860                   // Add missing cases here... enum Classes...
7861                   exp.isConstant = true;
7862
7863                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7864                if(symbol.isParam || !strcmp(id.string, "this"))
7865                {
7866                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7867                      exp.byReference = true;
7868
7869                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7870                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7871                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7872                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7873                   {
7874                      Identifier id = exp.identifier;
7875                      exp.type = bracketsExp;
7876                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7877                   }*/
7878                }
7879
7880                if(symbol.isIterator)
7881                {
7882                   if(symbol.isIterator == 3)
7883                   {
7884                      exp.type = bracketsExp;
7885                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7886                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7887                      exp.expType = null;
7888                      ProcessExpressionType(exp);
7889                   }
7890                   else if(symbol.isIterator != 4)
7891                   {
7892                      exp.type = memberExp;
7893                      exp.member.exp = MkExpIdentifier(exp.identifier);
7894                      exp.member.exp.expType = exp.expType;
7895                      /*if(symbol.isIterator == 6)
7896                         exp.member.member = MkIdentifier("key");
7897                      else*/
7898                         exp.member.member = MkIdentifier("data");
7899                      exp.expType = null;
7900                      ProcessExpressionType(exp);
7901                   }
7902                }
7903                break;
7904             }
7905             else
7906             {
7907                DefinedExpression definedExp = null;
7908                if(thisNameSpace && !(id._class && !id._class.name))
7909                {
7910                   char name[1024];
7911                   strcpy(name, thisNameSpace);
7912                   strcat(name, "::");
7913                   strcat(name, id.string);
7914                   definedExp = eSystem_FindDefine(privateModule, name);
7915                }
7916                if(!definedExp)
7917                   definedExp = eSystem_FindDefine(privateModule, id.string);
7918                if(definedExp)
7919                {
7920                   int c;
7921                   for(c = 0; c<definedExpStackPos; c++)
7922                      if(definedExpStack[c] == definedExp)
7923                         break;
7924                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7925                   {
7926                      Location backupYylloc = yylloc;
7927                      File backInput = fileInput;
7928                      definedExpStack[definedExpStackPos++] = definedExp;
7929
7930                      fileInput = TempFile { };
7931                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7932                      fileInput.Seek(0, start);
7933
7934                      echoOn = false;
7935                      parsedExpression = null;
7936                      resetScanner();
7937                      expression_yyparse();
7938                      delete fileInput;
7939                      if(backInput)
7940                         fileInput = backInput;
7941
7942                      yylloc = backupYylloc;
7943
7944                      if(parsedExpression)
7945                      {
7946                         FreeIdentifier(id);
7947                         exp.type = bracketsExp;
7948                         exp.list = MkListOne(parsedExpression);
7949                         ApplyLocation(parsedExpression, yylloc);
7950                         ProcessExpressionType(exp);
7951                         definedExpStackPos--;
7952                         return;
7953                      }
7954                      definedExpStackPos--;
7955                   }
7956                   else
7957                   {
7958                      if(inCompiler)
7959                      {
7960                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7961                      }
7962                   }
7963                }
7964                else
7965                {
7966                   GlobalData data = null;
7967                   if(thisNameSpace && !(id._class && !id._class.name))
7968                   {
7969                      char name[1024];
7970                      strcpy(name, thisNameSpace);
7971                      strcat(name, "::");
7972                      strcat(name, id.string);
7973                      data = FindGlobalData(name);
7974                   }
7975                   if(!data)
7976                      data = FindGlobalData(id.string);
7977                   if(data)
7978                   {
7979                      DeclareGlobalData(data);
7980                      exp.expType = data.dataType;
7981                      if(data.dataType) data.dataType.refCount++;
7982
7983                      delete id.string;
7984                      id.string = CopyString(data.fullName);
7985                      FreeSpecifier(id._class);
7986                      id._class = null;
7987
7988                      break;
7989                   }
7990                   else
7991                   {
7992                      GlobalFunction function = null;
7993                      if(thisNameSpace && !(id._class && !id._class.name))
7994                      {
7995                         char name[1024];
7996                         strcpy(name, thisNameSpace);
7997                         strcat(name, "::");
7998                         strcat(name, id.string);
7999                         function = eSystem_FindFunction(privateModule, name);
8000                      }
8001                      if(!function)
8002                         function = eSystem_FindFunction(privateModule, id.string);
8003                      if(function)
8004                      {
8005                         char name[1024];
8006                         delete id.string;
8007                         id.string = CopyString(function.name);
8008                         name[0] = 0;
8009
8010                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
8011                            strcpy(name, "__ecereFunction_");
8012                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
8013                         if(DeclareFunction(function, name))
8014                         {
8015                            delete id.string;
8016                            id.string = CopyString(name);
8017                         }
8018                         exp.expType = function.dataType;
8019                         if(function.dataType) function.dataType.refCount++;
8020
8021                         FreeSpecifier(id._class);
8022                         id._class = null;
8023
8024                         break;
8025                      }
8026                   }
8027                }
8028             }
8029          }
8030          unresolved = true;
8031          break;
8032       }
8033       case instanceExp:
8034       {
8035          // Class _class;
8036          // Symbol classSym;
8037
8038          if(!exp.instance._class)
8039          {
8040             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8041             {
8042                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8043             }
8044          }
8045
8046          //classSym = FindClass(exp.instance._class.fullName);
8047          //_class = classSym ? classSym.registered : null;
8048
8049          ProcessInstantiationType(exp.instance);
8050
8051          exp.isConstant = exp.instance.isConstant;
8052
8053          /*
8054          if(_class.type == unitClass && _class.base.type != systemClass)
8055          {
8056             {
8057                Type destType = exp.destType;
8058
8059                exp.destType = MkClassType(_class.base.fullName);
8060                exp.expType = MkClassType(_class.fullName);
8061                CheckExpressionType(exp, exp.destType, true);
8062
8063                exp.destType = destType;
8064             }
8065             exp.expType = MkClassType(_class.fullName);
8066          }
8067          else*/
8068          if(exp.instance._class)
8069          {
8070             exp.expType = MkClassType(exp.instance._class.name);
8071             /*if(exp.expType._class && exp.expType._class.registered &&
8072                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8073                exp.expType.byReference = true;*/
8074          }
8075          break;
8076       }
8077       case constantExp:
8078       {
8079          if(!exp.expType)
8080          {
8081             char * constant = exp.constant;
8082             Type type
8083             {
8084                refCount = 1;
8085                constant = true;
8086             };
8087             exp.expType = type;
8088
8089             if(constant[0] == '\'')
8090             {
8091                if((int)((byte *)constant)[1] > 127)
8092                {
8093                   int nb;
8094                   unichar ch = UTF8GetChar(constant + 1, &nb);
8095                   if(nb < 2) ch = constant[1];
8096                   delete constant;
8097                   exp.constant = PrintUInt(ch);
8098                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8099                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8100                   type._class = FindClass("unichar");
8101
8102                   type.isSigned = false;
8103                }
8104                else
8105                {
8106                   type.kind = charType;
8107                   type.isSigned = true;
8108                }
8109             }
8110             else
8111             {
8112                char * dot = strchr(constant, '.');
8113                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8114                char * exponent;
8115                if(isHex)
8116                {
8117                   exponent = strchr(constant, 'p');
8118                   if(!exponent) exponent = strchr(constant, 'P');
8119                }
8120                else
8121                {
8122                   exponent = strchr(constant, 'e');
8123                   if(!exponent) exponent = strchr(constant, 'E');
8124                }
8125
8126                if(dot || exponent)
8127                {
8128                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8129                      type.kind = floatType;
8130                   else
8131                      type.kind = doubleType;
8132                   type.isSigned = true;
8133                }
8134                else
8135                {
8136                   bool isSigned = constant[0] == '-';
8137                   char * endP = null;
8138                   int64 i64 = strtoll(constant, &endP, 0);
8139                   uint64 ui64 = strtoull(constant, &endP, 0);
8140                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
8141                   if(isSigned)
8142                   {
8143                      if(i64 < MININT)
8144                         is64Bit = true;
8145                   }
8146                   else
8147                   {
8148                      if(ui64 > MAXINT)
8149                      {
8150                         if(ui64 > MAXDWORD)
8151                         {
8152                            is64Bit = true;
8153                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8154                               isSigned = true;
8155                         }
8156                      }
8157                      else if(constant[0] != '0' || !constant[1])
8158                         isSigned = true;
8159                   }
8160                   type.kind = is64Bit ? int64Type : intType;
8161                   type.isSigned = isSigned;
8162                }
8163             }
8164             exp.isConstant = true;
8165             if(exp.destType && exp.destType.kind == doubleType)
8166                type.kind = doubleType;
8167             else if(exp.destType && exp.destType.kind == floatType)
8168                type.kind = floatType;
8169             else if(exp.destType && exp.destType.kind == int64Type)
8170                type.kind = int64Type;
8171          }
8172          break;
8173       }
8174       case stringExp:
8175       {
8176          exp.isConstant = true;      // Why wasn't this constant?
8177          exp.expType = Type
8178          {
8179             refCount = 1;
8180             kind = pointerType;
8181             type = Type
8182             {
8183                refCount = 1;
8184                kind = charType;
8185                constant = true;
8186                isSigned = true;
8187             }
8188          };
8189          break;
8190       }
8191       case newExp:
8192       case new0Exp:
8193          ProcessExpressionType(exp._new.size);
8194          exp.expType = Type
8195          {
8196             refCount = 1;
8197             kind = pointerType;
8198             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8199          };
8200          DeclareType(exp.expType.type, false, false);
8201          break;
8202       case renewExp:
8203       case renew0Exp:
8204          ProcessExpressionType(exp._renew.size);
8205          ProcessExpressionType(exp._renew.exp);
8206          exp.expType = Type
8207          {
8208             refCount = 1;
8209             kind = pointerType;
8210             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8211          };
8212          DeclareType(exp.expType.type, false, false);
8213          break;
8214       case opExp:
8215       {
8216          bool assign = false, boolResult = false, boolOps = false;
8217          Type type1 = null, type2 = null;
8218          bool useDestType = false, useSideType = false;
8219          Location oldyylloc = yylloc;
8220          bool useSideUnit = false;
8221          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8222
8223          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8224          Type dummy
8225          {
8226             count = 1;
8227             refCount = 1;
8228          };
8229
8230          switch(exp.op.op)
8231          {
8232             // Assignment Operators
8233             case '=':
8234             case MUL_ASSIGN:
8235             case DIV_ASSIGN:
8236             case MOD_ASSIGN:
8237             case ADD_ASSIGN:
8238             case SUB_ASSIGN:
8239             case LEFT_ASSIGN:
8240             case RIGHT_ASSIGN:
8241             case AND_ASSIGN:
8242             case XOR_ASSIGN:
8243             case OR_ASSIGN:
8244                assign = true;
8245                break;
8246             // boolean Operators
8247             case '!':
8248                // Expect boolean operators
8249                //boolOps = true;
8250                //boolResult = true;
8251                break;
8252             case AND_OP:
8253             case OR_OP:
8254                // Expect boolean operands
8255                boolOps = true;
8256                boolResult = true;
8257                break;
8258             // Comparisons
8259             case EQ_OP:
8260             case '<':
8261             case '>':
8262             case LE_OP:
8263             case GE_OP:
8264             case NE_OP:
8265                // Gives boolean result
8266                boolResult = true;
8267                useSideType = true;
8268                break;
8269             case '+':
8270             case '-':
8271                useSideUnit = true;
8272                useSideType = true;
8273                useDestType = true;
8274                break;
8275
8276             case LEFT_OP:
8277             case RIGHT_OP:
8278                useSideType = true;
8279                useDestType = true;
8280                break;
8281
8282             case '|':
8283             case '^':
8284                useSideType = true;
8285                useDestType = true;
8286                break;
8287
8288             case '/':
8289             case '%':
8290                useSideType = true;
8291                useDestType = true;
8292                break;
8293             case '&':
8294             case '*':
8295                if(exp.op.exp1)
8296                {
8297                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8298                   useSideType = true;
8299                   useDestType = true;
8300                }
8301                break;
8302
8303             /*// Implement speed etc.
8304             case '*':
8305             case '/':
8306                break;
8307             */
8308          }
8309          if(exp.op.op == '&')
8310          {
8311             // Added this here earlier for Iterator address as key
8312             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8313             {
8314                Identifier id = exp.op.exp2.identifier;
8315                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8316                if(symbol && symbol.isIterator == 2)
8317                {
8318                   exp.type = memberExp;
8319                   exp.member.exp = exp.op.exp2;
8320                   exp.member.member = MkIdentifier("key");
8321                   exp.expType = null;
8322                   exp.op.exp2.expType = symbol.type;
8323                   symbol.type.refCount++;
8324                   ProcessExpressionType(exp);
8325                   FreeType(dummy);
8326                   break;
8327                }
8328                // exp.op.exp2.usage.usageRef = true;
8329             }
8330          }
8331
8332          //dummy.kind = TypeDummy;
8333          if(exp.op.exp1)
8334          {
8335             // Added this check here to use the dest type only for units derived from the base unit
8336             // So that untyped units will use the side unit as opposed to the untyped destination unit
8337             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8338             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8339                useDestType = false;
8340
8341             if(destClass && useDestType &&
8342               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8343
8344               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8345             {
8346                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8347                exp.op.exp1.destType = exp.destType;
8348                exp.op.exp1.opDestType = true;
8349                if(exp.destType)
8350                   exp.destType.refCount++;
8351             }
8352             else if(!assign)
8353             {
8354                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8355                exp.op.exp1.destType = dummy;
8356                dummy.refCount++;
8357             }
8358
8359             // TESTING THIS HERE...
8360             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8361                ProcessExpressionType(exp.op.exp1);
8362             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8363
8364             exp.op.exp1.opDestType = false;
8365
8366             // Fix for unit and ++ / --
8367             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8368                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8369             {
8370                exp.op.exp2 = MkExpConstant("1");
8371                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8372                assign = true;
8373             }
8374
8375             if(exp.op.exp1.destType == dummy)
8376             {
8377                FreeType(dummy);
8378                exp.op.exp1.destType = null;
8379             }
8380             type1 = exp.op.exp1.expType;
8381          }
8382
8383          if(exp.op.exp2)
8384          {
8385             char expString[10240];
8386             expString[0] = '\0';
8387             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8388             {
8389                if(exp.op.exp1)
8390                {
8391                   exp.op.exp2.destType = exp.op.exp1.expType;
8392                   if(exp.op.exp1.expType)
8393                      exp.op.exp1.expType.refCount++;
8394                }
8395                else
8396                {
8397                   exp.op.exp2.destType = exp.destType;
8398                   if(!exp.op.exp1 || exp.op.op != '&')
8399                      exp.op.exp2.opDestType = true;
8400                   if(exp.destType)
8401                      exp.destType.refCount++;
8402                }
8403
8404                if(type1) type1.refCount++;
8405                exp.expType = type1;
8406             }
8407             else if(assign)
8408             {
8409                if(inCompiler)
8410                   PrintExpression(exp.op.exp2, expString);
8411
8412                if(type1 && type1.kind == pointerType)
8413                {
8414                   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 ||
8415                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8416                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8417                   else if(exp.op.op == '=')
8418                   {
8419                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8420                      exp.op.exp2.destType = type1;
8421                      if(type1)
8422                         type1.refCount++;
8423                   }
8424                }
8425                else
8426                {
8427                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8428                   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/* ||
8429                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8430                   else
8431                   {
8432                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8433                      exp.op.exp2.destType = type1;
8434                      if(type1)
8435                         type1.refCount++;
8436                   }
8437                }
8438                if(type1) type1.refCount++;
8439                exp.expType = type1;
8440             }
8441             else if(destClass &&
8442                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8443                   (destClass.type == enumClass && useDestType)))
8444             {
8445                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8446                exp.op.exp2.destType = exp.destType;
8447                if(exp.op.op != '&')
8448                   exp.op.exp2.opDestType = true;
8449                if(exp.destType)
8450                   exp.destType.refCount++;
8451             }
8452             else
8453             {
8454                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8455                exp.op.exp2.destType = dummy;
8456                dummy.refCount++;
8457             }
8458
8459             // TESTING THIS HERE... (DANGEROUS)
8460             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8461                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8462             {
8463                FreeType(exp.op.exp2.destType);
8464                exp.op.exp2.destType = type1;
8465                type1.refCount++;
8466             }
8467             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8468             // Cannot lose the cast on a sizeof
8469             if(exp.op.op == SIZEOF)
8470             {
8471                Expression e = exp.op.exp2;
8472                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8473                {
8474                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8475                   {
8476                      if(e.type == extensionCompoundExp)
8477                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8478                      else
8479                         e = e.list->last;
8480                   }
8481                }
8482                if(e.type == castExp && e.cast.exp)
8483                   e.cast.exp.needCast = true;
8484             }
8485             ProcessExpressionType(exp.op.exp2);
8486             exp.op.exp2.opDestType = false;
8487             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8488
8489             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8490             {
8491                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)
8492                {
8493                   if(exp.op.op != '=' && type1.type.kind == voidType)
8494                      Compiler_Error($"void *: unknown size\n");
8495                }
8496                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||
8497                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8498                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8499                               exp.op.exp2.expType._class.registered.type == structClass ||
8500                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8501                {
8502                   if(exp.op.op == ADD_ASSIGN)
8503                      Compiler_Error($"cannot add two pointers\n");
8504                }
8505                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8506                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8507                {
8508                   if(exp.op.op == ADD_ASSIGN)
8509                      Compiler_Error($"cannot add two pointers\n");
8510                }
8511                else if(inCompiler)
8512                {
8513                   char type1String[1024];
8514                   char type2String[1024];
8515                   type1String[0] = '\0';
8516                   type2String[0] = '\0';
8517
8518                   PrintType(exp.op.exp2.expType, type1String, false, true);
8519                   PrintType(type1, type2String, false, true);
8520                   ChangeCh(expString, '\n', ' ');
8521                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8522                }
8523             }
8524
8525             if(exp.op.exp2.destType == dummy)
8526             {
8527                FreeType(dummy);
8528                exp.op.exp2.destType = null;
8529             }
8530
8531             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8532             {
8533                type2 = { };
8534                type2.refCount = 1;
8535                CopyTypeInto(type2, exp.op.exp2.expType);
8536                type2.isSigned = true;
8537             }
8538             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8539             {
8540                type2 = { kind = intType };
8541                type2.refCount = 1;
8542                type2.isSigned = true;
8543             }
8544             else
8545             {
8546                type2 = exp.op.exp2.expType;
8547                if(type2) type2.refCount++;
8548             }
8549          }
8550
8551          dummy.kind = voidType;
8552
8553          if(exp.op.op == SIZEOF)
8554          {
8555             exp.expType = Type
8556             {
8557                refCount = 1;
8558                kind = intSizeType;
8559             };
8560             exp.isConstant = true;
8561          }
8562          // Get type of dereferenced pointer
8563          else if(exp.op.op == '*' && !exp.op.exp1)
8564          {
8565             exp.expType = Dereference(type2);
8566             if(type2 && type2.kind == classType)
8567                notByReference = true;
8568          }
8569          else if(exp.op.op == '&' && !exp.op.exp1)
8570             exp.expType = Reference(type2);
8571          else if(!assign)
8572          {
8573             if(boolOps)
8574             {
8575                if(exp.op.exp1)
8576                {
8577                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8578                   exp.op.exp1.destType = MkClassType("bool");
8579                   exp.op.exp1.destType.truth = true;
8580                   if(!exp.op.exp1.expType)
8581                      ProcessExpressionType(exp.op.exp1);
8582                   else
8583                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8584                   FreeType(exp.op.exp1.expType);
8585                   exp.op.exp1.expType = MkClassType("bool");
8586                   exp.op.exp1.expType.truth = true;
8587                }
8588                if(exp.op.exp2)
8589                {
8590                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8591                   exp.op.exp2.destType = MkClassType("bool");
8592                   exp.op.exp2.destType.truth = true;
8593                   if(!exp.op.exp2.expType)
8594                      ProcessExpressionType(exp.op.exp2);
8595                   else
8596                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8597                   FreeType(exp.op.exp2.expType);
8598                   exp.op.exp2.expType = MkClassType("bool");
8599                   exp.op.exp2.expType.truth = true;
8600                }
8601             }
8602             else if(exp.op.exp1 && exp.op.exp2 &&
8603                ((useSideType /*&&
8604                      (useSideUnit ||
8605                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8606                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8607                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8608                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8609             {
8610                if(type1 && type2 &&
8611                   // If either both are class or both are not class
8612                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8613                {
8614                   // Added this check for enum subtraction to result in an int type:
8615                   if(exp.op.op == '-' &&
8616                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8617                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8618                   {
8619                      Type intType;
8620                      if(!type1._class.registered.dataType)
8621                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8622                      if(!type2._class.registered.dataType)
8623                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8624
8625                      intType = ProcessTypeString(
8626                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8627
8628                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8629                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8630                      exp.op.exp1.destType = intType;
8631                      exp.op.exp2.destType = intType;
8632                      intType.refCount++;
8633                   }
8634                   else
8635                   {
8636                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8637                      exp.op.exp2.destType = type1;
8638                      type1.refCount++;
8639                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8640                      exp.op.exp1.destType = type2;
8641                      type2.refCount++;
8642                   }
8643
8644                   // Warning here for adding Radians + Degrees with no destination type
8645                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8646                      type1._class.registered && type1._class.registered.type == unitClass &&
8647                      type2._class.registered && type2._class.registered.type == unitClass &&
8648                      type1._class.registered != type2._class.registered)
8649                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8650                         type1._class.string, type2._class.string, type1._class.string);
8651
8652                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8653                   {
8654                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8655                      if(argExp)
8656                      {
8657                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8658
8659                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8660                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8661                            exp.op.exp1)));
8662
8663                         ProcessExpressionType(exp.op.exp1);
8664
8665                         if(type2.kind != pointerType)
8666                         {
8667                            ProcessExpressionType(classExp);
8668
8669                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8670
8671                            if(!exp.op.exp2.expType)
8672                            {
8673                               if(type2)
8674                                  FreeType(type2);
8675                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8676                               type2.refCount++;
8677                            }
8678
8679                            ProcessExpressionType(exp.op.exp2);
8680                         }
8681                      }
8682                   }
8683
8684                   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)))
8685                   {
8686                      if(type1.kind != classType && type1.type.kind == voidType)
8687                         Compiler_Error($"void *: unknown size\n");
8688                      exp.expType = type1;
8689                      if(type1) type1.refCount++;
8690                   }
8691                   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)))
8692                   {
8693                      if(type2.kind != classType && type2.type.kind == voidType)
8694                         Compiler_Error($"void *: unknown size\n");
8695                      exp.expType = type2;
8696                      if(type2) type2.refCount++;
8697                   }
8698                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8699                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8700                   {
8701                      Compiler_Warning($"different levels of indirection\n");
8702                   }
8703                   else
8704                   {
8705                      bool success = false;
8706                      if(type1.kind == pointerType && type2.kind == pointerType)
8707                      {
8708                         if(exp.op.op == '+')
8709                            Compiler_Error($"cannot add two pointers\n");
8710                         else if(exp.op.op == '-')
8711                         {
8712                            // Pointer Subtraction gives integer
8713                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8714                            {
8715                               exp.expType = Type
8716                               {
8717                                  kind = intType;
8718                                  refCount = 1;
8719                               };
8720                               success = true;
8721
8722                               if(type1.type.kind == templateType)
8723                               {
8724                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8725                                  if(argExp)
8726                                  {
8727                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8728
8729                                     ProcessExpressionType(classExp);
8730
8731                                     exp.type = bracketsExp;
8732                                     exp.list = MkListOne(MkExpOp(
8733                                        MkExpBrackets(MkListOne(MkExpOp(
8734                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8735                                              , exp.op.op,
8736                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8737                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8738
8739                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8740                                     FreeType(dummy);
8741                                     return;
8742                                  }
8743                               }
8744                            }
8745                         }
8746                      }
8747
8748                      if(!success && exp.op.exp1.type == constantExp)
8749                      {
8750                         // If first expression is constant, try to match that first
8751                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8752                         {
8753                            if(exp.expType) FreeType(exp.expType);
8754                            exp.expType = exp.op.exp1.destType;
8755                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8756                            success = true;
8757                         }
8758                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8759                         {
8760                            if(exp.expType) FreeType(exp.expType);
8761                            exp.expType = exp.op.exp2.destType;
8762                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8763                            success = true;
8764                         }
8765                      }
8766                      else if(!success)
8767                      {
8768                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8769                         {
8770                            if(exp.expType) FreeType(exp.expType);
8771                            exp.expType = exp.op.exp2.destType;
8772                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8773                            success = true;
8774                         }
8775                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8776                         {
8777                            if(exp.expType) FreeType(exp.expType);
8778                            exp.expType = exp.op.exp1.destType;
8779                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8780                            success = true;
8781                         }
8782                      }
8783                      if(!success)
8784                      {
8785                         char expString1[10240];
8786                         char expString2[10240];
8787                         char type1[1024];
8788                         char type2[1024];
8789                         expString1[0] = '\0';
8790                         expString2[0] = '\0';
8791                         type1[0] = '\0';
8792                         type2[0] = '\0';
8793                         if(inCompiler)
8794                         {
8795                            PrintExpression(exp.op.exp1, expString1);
8796                            ChangeCh(expString1, '\n', ' ');
8797                            PrintExpression(exp.op.exp2, expString2);
8798                            ChangeCh(expString2, '\n', ' ');
8799                            PrintType(exp.op.exp1.expType, type1, false, true);
8800                            PrintType(exp.op.exp2.expType, type2, false, true);
8801                         }
8802
8803                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8804                      }
8805                   }
8806                }
8807                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8808                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8809                {
8810                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8811                   // Convert e.g. / 4 into / 4.0
8812                   exp.op.exp1.destType = type2._class.registered.dataType;
8813                   if(type2._class.registered.dataType)
8814                      type2._class.registered.dataType.refCount++;
8815                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8816                   exp.expType = type2;
8817                   if(type2) type2.refCount++;
8818                }
8819                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8820                {
8821                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8822                   // Convert e.g. / 4 into / 4.0
8823                   exp.op.exp2.destType = type1._class.registered.dataType;
8824                   if(type1._class.registered.dataType)
8825                      type1._class.registered.dataType.refCount++;
8826                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8827                   exp.expType = type1;
8828                   if(type1) type1.refCount++;
8829                }
8830                else if(type1)
8831                {
8832                   bool valid = false;
8833
8834                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8835                   {
8836                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8837
8838                      if(!type1._class.registered.dataType)
8839                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8840                      exp.op.exp2.destType = type1._class.registered.dataType;
8841                      exp.op.exp2.destType.refCount++;
8842
8843                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8844                      if(type2)
8845                         FreeType(type2);
8846                      type2 = exp.op.exp2.destType;
8847                      if(type2) type2.refCount++;
8848
8849                      exp.expType = type2;
8850                      type2.refCount++;
8851                   }
8852
8853                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8854                   {
8855                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8856
8857                      if(!type2._class.registered.dataType)
8858                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8859                      exp.op.exp1.destType = type2._class.registered.dataType;
8860                      exp.op.exp1.destType.refCount++;
8861
8862                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8863                      type1 = exp.op.exp1.destType;
8864                      exp.expType = type1;
8865                      type1.refCount++;
8866                   }
8867
8868                   // TESTING THIS NEW CODE
8869                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8870                   {
8871                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8872                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8873                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8874                      {
8875                         // Convert the enum to an int instead for these operators
8876                         if(op1IsEnum && exp.op.exp2.expType)
8877                         {
8878                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8879                            {
8880                               if(exp.expType) FreeType(exp.expType);
8881                               exp.expType = exp.op.exp2.expType;
8882                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8883                               valid = true;
8884                            }
8885                         }
8886                         else if(op2IsEnum && exp.op.exp1.expType)
8887                         {
8888                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8889                            {
8890                               if(exp.expType) FreeType(exp.expType);
8891                               exp.expType = exp.op.exp1.expType;
8892                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8893                               valid = true;
8894                            }
8895                         }
8896                      }
8897                      else
8898                      {
8899                         if(op1IsEnum && exp.op.exp2.expType)
8900                         {
8901                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8902                            {
8903                               if(exp.expType) FreeType(exp.expType);
8904                               exp.expType = exp.op.exp1.expType;
8905                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8906                               valid = true;
8907                            }
8908                         }
8909                         else if(op2IsEnum && exp.op.exp1.expType)
8910                         {
8911                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8912                            {
8913                               if(exp.expType) FreeType(exp.expType);
8914                               exp.expType = exp.op.exp2.expType;
8915                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8916                               valid = true;
8917                            }
8918                         }
8919                      }
8920                   }
8921
8922                   if(!valid)
8923                   {
8924                      // 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
8925                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
8926                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
8927                      {
8928                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8929                         exp.op.exp1.destType = type2;
8930                         type2.refCount++;
8931
8932                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8933                         {
8934                            if(exp.expType) FreeType(exp.expType);
8935                            exp.expType = exp.op.exp1.destType;
8936                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8937                         }
8938                      }
8939                      else
8940                      {
8941                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8942                         exp.op.exp2.destType = type1;
8943                         type1.refCount++;
8944
8945                      /*
8946                      // Maybe this was meant to be an enum...
8947                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8948                      {
8949                         Type oldType = exp.op.exp2.expType;
8950                         exp.op.exp2.expType = null;
8951                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8952                            FreeType(oldType);
8953                         else
8954                            exp.op.exp2.expType = oldType;
8955                      }
8956                      */
8957
8958                      /*
8959                      // TESTING THIS HERE... LATEST ADDITION
8960                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8961                      {
8962                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8963                         exp.op.exp2.destType = type2._class.registered.dataType;
8964                         if(type2._class.registered.dataType)
8965                            type2._class.registered.dataType.refCount++;
8966                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8967
8968                         //exp.expType = type2._class.registered.dataType; //type2;
8969                         //if(type2) type2.refCount++;
8970                      }
8971
8972                      // TESTING THIS HERE... LATEST ADDITION
8973                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8974                      {
8975                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8976                         exp.op.exp1.destType = type1._class.registered.dataType;
8977                         if(type1._class.registered.dataType)
8978                            type1._class.registered.dataType.refCount++;
8979                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8980                         exp.expType = type1._class.registered.dataType; //type1;
8981                         if(type1) type1.refCount++;
8982                      }
8983                      */
8984
8985                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8986                         {
8987                            if(exp.expType) FreeType(exp.expType);
8988                            exp.expType = exp.op.exp2.destType;
8989                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8990                         }
8991                         else if(type1 && type2)
8992                         {
8993                            char expString1[10240];
8994                            char expString2[10240];
8995                            char type1String[1024];
8996                            char type2String[1024];
8997                            expString1[0] = '\0';
8998                            expString2[0] = '\0';
8999                            type1String[0] = '\0';
9000                            type2String[0] = '\0';
9001                            if(inCompiler)
9002                            {
9003                               PrintExpression(exp.op.exp1, expString1);
9004                               ChangeCh(expString1, '\n', ' ');
9005                               PrintExpression(exp.op.exp2, expString2);
9006                               ChangeCh(expString2, '\n', ' ');
9007                               PrintType(exp.op.exp1.expType, type1String, false, true);
9008                               PrintType(exp.op.exp2.expType, type2String, false, true);
9009                            }
9010
9011                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
9012
9013                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9014                            {
9015                               exp.expType = exp.op.exp1.expType;
9016                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9017                            }
9018                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9019                            {
9020                               exp.expType = exp.op.exp2.expType;
9021                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9022                            }
9023                         }
9024                      }
9025                   }
9026                }
9027                else if(type2)
9028                {
9029                   // Maybe this was meant to be an enum...
9030                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9031                   {
9032                      Type oldType = exp.op.exp1.expType;
9033                      exp.op.exp1.expType = null;
9034                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9035                         FreeType(oldType);
9036                      else
9037                         exp.op.exp1.expType = oldType;
9038                   }
9039
9040                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9041                   exp.op.exp1.destType = type2;
9042                   type2.refCount++;
9043                   /*
9044                   // TESTING THIS HERE... LATEST ADDITION
9045                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9046                   {
9047                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9048                      exp.op.exp1.destType = type1._class.registered.dataType;
9049                      if(type1._class.registered.dataType)
9050                         type1._class.registered.dataType.refCount++;
9051                   }
9052
9053                   // TESTING THIS HERE... LATEST ADDITION
9054                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9055                   {
9056                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9057                      exp.op.exp2.destType = type2._class.registered.dataType;
9058                      if(type2._class.registered.dataType)
9059                         type2._class.registered.dataType.refCount++;
9060                   }
9061                   */
9062
9063                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9064                   {
9065                      if(exp.expType) FreeType(exp.expType);
9066                      exp.expType = exp.op.exp1.destType;
9067                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9068                   }
9069                }
9070             }
9071             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9072             {
9073                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9074                {
9075                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9076                   // Convert e.g. / 4 into / 4.0
9077                   exp.op.exp1.destType = type2._class.registered.dataType;
9078                   if(type2._class.registered.dataType)
9079                      type2._class.registered.dataType.refCount++;
9080                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9081                }
9082                if(exp.op.op == '!')
9083                {
9084                   exp.expType = MkClassType("bool");
9085                   exp.expType.truth = true;
9086                }
9087                else
9088                {
9089                   exp.expType = type2;
9090                   if(type2) type2.refCount++;
9091                }
9092             }
9093             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9094             {
9095                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9096                {
9097                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9098                   // Convert e.g. / 4 into / 4.0
9099                   exp.op.exp2.destType = type1._class.registered.dataType;
9100                   if(type1._class.registered.dataType)
9101                      type1._class.registered.dataType.refCount++;
9102                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9103                }
9104                exp.expType = type1;
9105                if(type1) type1.refCount++;
9106             }
9107          }
9108
9109          yylloc = exp.loc;
9110          if(exp.op.exp1 && !exp.op.exp1.expType)
9111          {
9112             char expString[10000];
9113             expString[0] = '\0';
9114             if(inCompiler)
9115             {
9116                PrintExpression(exp.op.exp1, expString);
9117                ChangeCh(expString, '\n', ' ');
9118             }
9119             if(expString[0])
9120                Compiler_Error($"couldn't determine type of %s\n", expString);
9121          }
9122          if(exp.op.exp2 && !exp.op.exp2.expType)
9123          {
9124             char expString[10240];
9125             expString[0] = '\0';
9126             if(inCompiler)
9127             {
9128                PrintExpression(exp.op.exp2, expString);
9129                ChangeCh(expString, '\n', ' ');
9130             }
9131             if(expString[0])
9132                Compiler_Error($"couldn't determine type of %s\n", expString);
9133          }
9134
9135          if(boolResult)
9136          {
9137             FreeType(exp.expType);
9138             exp.expType = MkClassType("bool");
9139             exp.expType.truth = true;
9140          }
9141
9142          if(exp.op.op != SIZEOF)
9143             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9144                (!exp.op.exp2 || exp.op.exp2.isConstant);
9145
9146          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9147          {
9148             DeclareType(exp.op.exp2.expType, false, false);
9149          }
9150
9151          if(exp.op.op == DELETE && exp.op.exp2 && exp.op.exp2.expType && exp.op.exp2.expType.specConst)
9152             Compiler_Warning($"deleting const qualified object\n");
9153
9154          yylloc = oldyylloc;
9155
9156          FreeType(dummy);
9157          if(type2)
9158             FreeType(type2);
9159          break;
9160       }
9161       case bracketsExp:
9162       case extensionExpressionExp:
9163       {
9164          Expression e;
9165          exp.isConstant = true;
9166          for(e = exp.list->first; e; e = e.next)
9167          {
9168             bool inced = false;
9169             if(!e.next)
9170             {
9171                FreeType(e.destType);
9172                e.opDestType = exp.opDestType;
9173                e.destType = exp.destType;
9174                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
9175             }
9176             ProcessExpressionType(e);
9177             if(inced)
9178                exp.destType.count--;
9179             if(!exp.expType && !e.next)
9180             {
9181                exp.expType = e.expType;
9182                if(e.expType) e.expType.refCount++;
9183             }
9184             if(!e.isConstant)
9185                exp.isConstant = false;
9186          }
9187
9188          // In case a cast became a member...
9189          e = exp.list->first;
9190          if(!e.next && e.type == memberExp)
9191          {
9192             // Preserve prev, next
9193             Expression next = exp.next, prev = exp.prev;
9194
9195
9196             FreeType(exp.expType);
9197             FreeType(exp.destType);
9198             delete exp.list;
9199
9200             *exp = *e;
9201
9202             exp.prev = prev;
9203             exp.next = next;
9204
9205             delete e;
9206
9207             ProcessExpressionType(exp);
9208          }
9209          break;
9210       }
9211       case indexExp:
9212       {
9213          Expression e;
9214          exp.isConstant = true;
9215
9216          ProcessExpressionType(exp.index.exp);
9217          if(!exp.index.exp.isConstant)
9218             exp.isConstant = false;
9219
9220          if(exp.index.exp.expType)
9221          {
9222             Type source = exp.index.exp.expType;
9223             if(source.kind == classType && source._class && source._class.registered)
9224             {
9225                Class _class = source._class.registered;
9226                Class c = _class.templateClass ? _class.templateClass : _class;
9227                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9228                {
9229                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9230
9231                   if(exp.index.index && exp.index.index->last)
9232                   {
9233                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9234
9235                      if(type.kind == classType) type.constant = true;
9236                      else if(type.kind == pointerType)
9237                      {
9238                         Type t = type;
9239                         while(t.kind == pointerType) t = t.type;
9240                         t.constant = true;
9241                      }
9242
9243                      ((Expression)exp.index.index->last).destType = type;
9244                   }
9245                }
9246             }
9247          }
9248
9249          for(e = exp.index.index->first; e; e = e.next)
9250          {
9251             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9252             {
9253                if(e.destType) FreeType(e.destType);
9254                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9255             }
9256             ProcessExpressionType(e);
9257             if(!e.next)
9258             {
9259                // Check if this type is int
9260             }
9261             if(!e.isConstant)
9262                exp.isConstant = false;
9263          }
9264
9265          if(!exp.expType)
9266             exp.expType = Dereference(exp.index.exp.expType);
9267          if(exp.expType)
9268             DeclareType(exp.expType, false, false);
9269          break;
9270       }
9271       case callExp:
9272       {
9273          Expression e;
9274          Type functionType;
9275          Type methodType = null;
9276          char name[1024];
9277          name[0] = '\0';
9278
9279          if(inCompiler)
9280          {
9281             PrintExpression(exp.call.exp,  name);
9282             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9283             {
9284                //exp.call.exp.expType = null;
9285                PrintExpression(exp.call.exp,  name);
9286             }
9287          }
9288          if(exp.call.exp.type == identifierExp)
9289          {
9290             Expression idExp = exp.call.exp;
9291             Identifier id = idExp.identifier;
9292             if(!strcmp(id.string, "__builtin_frame_address"))
9293             {
9294                exp.expType = ProcessTypeString("void *", true);
9295                if(exp.call.arguments && exp.call.arguments->first)
9296                   ProcessExpressionType(exp.call.arguments->first);
9297                break;
9298             }
9299             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9300             {
9301                exp.expType = ProcessTypeString("int", true);
9302                if(exp.call.arguments && exp.call.arguments->first)
9303                   ProcessExpressionType(exp.call.arguments->first);
9304                break;
9305             }
9306             else if(!strcmp(id.string, "Max") ||
9307                !strcmp(id.string, "Min") ||
9308                !strcmp(id.string, "Sgn") ||
9309                !strcmp(id.string, "Abs"))
9310             {
9311                Expression a = null;
9312                Expression b = null;
9313                Expression tempExp1 = null, tempExp2 = null;
9314                if((!strcmp(id.string, "Max") ||
9315                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9316                {
9317                   a = exp.call.arguments->first;
9318                   b = exp.call.arguments->last;
9319                   tempExp1 = a;
9320                   tempExp2 = b;
9321                }
9322                else if(exp.call.arguments->count == 1)
9323                {
9324                   a = exp.call.arguments->first;
9325                   tempExp1 = a;
9326                }
9327
9328                if(a)
9329                {
9330                   exp.call.arguments->Clear();
9331                   idExp.identifier = null;
9332
9333                   FreeExpContents(exp);
9334
9335                   ProcessExpressionType(a);
9336                   if(b)
9337                      ProcessExpressionType(b);
9338
9339                   exp.type = bracketsExp;
9340                   exp.list = MkList();
9341
9342                   if(a.expType && (!b || b.expType))
9343                   {
9344                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9345                      {
9346                         // Use the simpleStruct name/ids for now...
9347                         if(inCompiler)
9348                         {
9349                            OldList * specs = MkList();
9350                            OldList * decls = MkList();
9351                            Declaration decl;
9352                            char temp1[1024], temp2[1024];
9353
9354                            GetTypeSpecs(a.expType, specs);
9355
9356                            if(a && !a.isConstant && a.type != identifierExp)
9357                            {
9358                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9359                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9360                               tempExp1 = QMkExpId(temp1);
9361                               tempExp1.expType = a.expType;
9362                               if(a.expType)
9363                                  a.expType.refCount++;
9364                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9365                            }
9366                            if(b && !b.isConstant && b.type != identifierExp)
9367                            {
9368                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9369                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9370                               tempExp2 = QMkExpId(temp2);
9371                               tempExp2.expType = b.expType;
9372                               if(b.expType)
9373                                  b.expType.refCount++;
9374                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9375                            }
9376
9377                            decl = MkDeclaration(specs, decls);
9378                            if(!curCompound.compound.declarations)
9379                               curCompound.compound.declarations = MkList();
9380                            curCompound.compound.declarations->Insert(null, decl);
9381                         }
9382                      }
9383                   }
9384
9385                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9386                   {
9387                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9388                      ListAdd(exp.list,
9389                         MkExpCondition(MkExpBrackets(MkListOne(
9390                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9391                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9392                      exp.expType = a.expType;
9393                      if(a.expType)
9394                         a.expType.refCount++;
9395                   }
9396                   else if(!strcmp(id.string, "Abs"))
9397                   {
9398                      ListAdd(exp.list,
9399                         MkExpCondition(MkExpBrackets(MkListOne(
9400                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9401                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9402                      exp.expType = a.expType;
9403                      if(a.expType)
9404                         a.expType.refCount++;
9405                   }
9406                   else if(!strcmp(id.string, "Sgn"))
9407                   {
9408                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9409                      ListAdd(exp.list,
9410                         MkExpCondition(MkExpBrackets(MkListOne(
9411                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9412                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9413                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9414                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9415                      exp.expType = ProcessTypeString("int", false);
9416                   }
9417
9418                   FreeExpression(tempExp1);
9419                   if(tempExp2) FreeExpression(tempExp2);
9420
9421                   FreeIdentifier(id);
9422                   break;
9423                }
9424             }
9425          }
9426
9427          {
9428             Type dummy
9429             {
9430                count = 1;
9431                refCount = 1;
9432             };
9433             if(!exp.call.exp.destType)
9434             {
9435                exp.call.exp.destType = dummy;
9436                dummy.refCount++;
9437             }
9438             ProcessExpressionType(exp.call.exp);
9439             if(exp.call.exp.destType == dummy)
9440             {
9441                FreeType(dummy);
9442                exp.call.exp.destType = null;
9443             }
9444             FreeType(dummy);
9445          }
9446
9447          // Check argument types against parameter types
9448          functionType = exp.call.exp.expType;
9449
9450          if(functionType && functionType.kind == TypeKind::methodType)
9451          {
9452             methodType = functionType;
9453             functionType = methodType.method.dataType;
9454
9455             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9456             // TOCHECK: Instead of doing this here could this be done per param?
9457             if(exp.call.exp.expType.usedClass)
9458             {
9459                char typeString[1024];
9460                typeString[0] = '\0';
9461                {
9462                   Symbol back = functionType.thisClass;
9463                   // Do not output class specifier here (thisclass was added to this)
9464                   functionType.thisClass = null;
9465                   PrintType(functionType, typeString, true, true);
9466                   functionType.thisClass = back;
9467                }
9468                if(strstr(typeString, "thisclass"))
9469                {
9470                   OldList * specs = MkList();
9471                   Declarator decl;
9472                   {
9473                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9474
9475                      decl = SpecDeclFromString(typeString, specs, null);
9476
9477                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9478                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9479                         exp.call.exp.expType.usedClass))
9480                         thisClassParams = false;
9481
9482                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9483                      {
9484                         Class backupThisClass = thisClass;
9485                         thisClass = exp.call.exp.expType.usedClass;
9486                         ProcessDeclarator(decl);
9487                         thisClass = backupThisClass;
9488                      }
9489
9490                      thisClassParams = true;
9491
9492                      functionType = ProcessType(specs, decl);
9493                      functionType.refCount = 0;
9494                      FinishTemplatesContext(context);
9495                   }
9496
9497                   FreeList(specs, FreeSpecifier);
9498                   FreeDeclarator(decl);
9499                 }
9500             }
9501          }
9502          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9503          {
9504             Type type = functionType.type;
9505             if(!functionType.refCount)
9506             {
9507                functionType.type = null;
9508                FreeType(functionType);
9509             }
9510             //methodType = functionType;
9511             functionType = type;
9512          }
9513          if(functionType && functionType.kind != TypeKind::functionType)
9514          {
9515             Compiler_Error($"called object %s is not a function\n", name);
9516          }
9517          else if(functionType)
9518          {
9519             bool emptyParams = false, noParams = false;
9520             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9521             Type type = functionType.params.first;
9522             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9523             int extra = 0;
9524             Location oldyylloc = yylloc;
9525
9526             if(!type) emptyParams = true;
9527
9528             // WORKING ON THIS:
9529             if(functionType.extraParam && e && functionType.thisClass)
9530             {
9531                e.destType = MkClassType(functionType.thisClass.string);
9532                e = e.next;
9533             }
9534
9535             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9536             // Fixed #141 by adding '&& !functionType.extraParam'
9537             if(!functionType.staticMethod && !functionType.extraParam)
9538             {
9539                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9540                   memberExp.member.exp.expType._class)
9541                {
9542                   type = MkClassType(memberExp.member.exp.expType._class.string);
9543                   if(e)
9544                   {
9545                      e.destType = type;
9546                      e = e.next;
9547                      type = functionType.params.first;
9548                   }
9549                   else
9550                      type.refCount = 0;
9551                }
9552                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9553                {
9554                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9555                   type.byReference = functionType.byReference;
9556                   type.typedByReference = functionType.typedByReference;
9557                   if(e)
9558                   {
9559                      // Allow manually passing a class for typed object
9560                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9561                         e = e.next;
9562                      e.destType = type;
9563                      e = e.next;
9564                      type = functionType.params.first;
9565                   }
9566                   else
9567                      type.refCount = 0;
9568                   //extra = 1;
9569                }
9570             }
9571
9572             if(type && type.kind == voidType)
9573             {
9574                noParams = true;
9575                if(!type.refCount) FreeType(type);
9576                type = null;
9577             }
9578
9579             for( ; e; e = e.next)
9580             {
9581                if(!type && !emptyParams)
9582                {
9583                   yylloc = e.loc;
9584                   if(methodType && methodType.methodClass)
9585                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9586                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9587                         noParams ? 0 : functionType.params.count);
9588                   else
9589                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9590                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9591                         noParams ? 0 : functionType.params.count);
9592                   break;
9593                }
9594
9595                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9596                {
9597                   Type templatedType = null;
9598                   Class _class = methodType.usedClass;
9599                   ClassTemplateParameter curParam = null;
9600                   int id = 0;
9601                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9602                   {
9603                      Class sClass;
9604                      for(sClass = _class; sClass; sClass = sClass.base)
9605                      {
9606                         if(sClass.templateClass) sClass = sClass.templateClass;
9607                         id = 0;
9608                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9609                         {
9610                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9611                            {
9612                               Class nextClass;
9613                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9614                               {
9615                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9616                                  id += nextClass.templateParams.count;
9617                               }
9618                               break;
9619                            }
9620                            id++;
9621                         }
9622                         if(curParam) break;
9623                      }
9624                   }
9625                   if(curParam && _class.templateArgs[id].dataTypeString)
9626                   {
9627                      bool constant = type.constant;
9628                      ClassTemplateArgument arg = _class.templateArgs[id];
9629                      {
9630                         Context context = SetupTemplatesContext(_class);
9631
9632                         /*if(!arg.dataType)
9633                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9634                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9635                         FinishTemplatesContext(context);
9636                      }
9637
9638                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9639                      else if(templatedType.kind == pointerType)
9640                      {
9641                         Type t = templatedType.type;
9642                         while(t.kind == pointerType) t = t.type;
9643                         if(constant) t.constant = constant;
9644                      }
9645
9646                      e.destType = templatedType;
9647                      if(templatedType)
9648                      {
9649                         templatedType.passAsTemplate = true;
9650                         // templatedType.refCount++;
9651                      }
9652                   }
9653                   else
9654                   {
9655                      e.destType = type;
9656                      if(type) type.refCount++;
9657                   }
9658                }
9659                else
9660                {
9661                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9662                   {
9663                      e.destType = type.prev;
9664                      e.destType.refCount++;
9665                   }
9666                   else
9667                   {
9668                      e.destType = type;
9669                      if(type) type.refCount++;
9670                   }
9671                }
9672                // Don't reach the end for the ellipsis
9673                if(type && type.kind != ellipsisType)
9674                {
9675                   Type next = type.next;
9676                   if(!type.refCount) FreeType(type);
9677                   type = next;
9678                }
9679             }
9680
9681             if(type && type.kind != ellipsisType)
9682             {
9683                if(methodType && methodType.methodClass)
9684                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9685                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9686                      functionType.params.count + extra);
9687                else
9688                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9689                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9690                      functionType.params.count + extra);
9691             }
9692             yylloc = oldyylloc;
9693             if(type && !type.refCount) FreeType(type);
9694          }
9695          else
9696          {
9697             functionType = Type
9698             {
9699                refCount = 0;
9700                kind = TypeKind::functionType;
9701             };
9702
9703             if(exp.call.exp.type == identifierExp)
9704             {
9705                char * string = exp.call.exp.identifier.string;
9706                if(inCompiler)
9707                {
9708                   Symbol symbol;
9709                   Location oldyylloc = yylloc;
9710
9711                   yylloc = exp.call.exp.identifier.loc;
9712                   if(strstr(string, "__builtin_") == string)
9713                   {
9714                      if(exp.destType)
9715                      {
9716                         functionType.returnType = exp.destType;
9717                         exp.destType.refCount++;
9718                      }
9719                   }
9720                   else
9721                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9722                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9723                   globalContext.symbols.Add((BTNode)symbol);
9724                   if(strstr(symbol.string, "::"))
9725                      globalContext.hasNameSpace = true;
9726
9727                   yylloc = oldyylloc;
9728                }
9729             }
9730             else if(exp.call.exp.type == memberExp)
9731             {
9732                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9733                   exp.call.exp.member.member.string);*/
9734             }
9735             else
9736                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9737
9738             if(!functionType.returnType)
9739             {
9740                functionType.returnType = Type
9741                {
9742                   refCount = 1;
9743                   kind = intType;
9744                };
9745             }
9746          }
9747          if(functionType && functionType.kind == TypeKind::functionType)
9748          {
9749             exp.expType = functionType.returnType;
9750
9751             if(functionType.returnType)
9752                functionType.returnType.refCount++;
9753
9754             if(!functionType.refCount)
9755                FreeType(functionType);
9756          }
9757
9758          if(exp.call.arguments)
9759          {
9760             for(e = exp.call.arguments->first; e; e = e.next)
9761                ProcessExpressionType(e);
9762          }
9763          break;
9764       }
9765       case memberExp:
9766       {
9767          Type type;
9768          Location oldyylloc = yylloc;
9769          bool thisPtr;
9770          Expression checkExp = exp.member.exp;
9771          while(checkExp)
9772          {
9773             if(checkExp.type == castExp)
9774                checkExp = checkExp.cast.exp;
9775             else if(checkExp.type == bracketsExp)
9776                checkExp = checkExp.list ? checkExp.list->first : null;
9777             else
9778                break;
9779          }
9780
9781          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9782          exp.thisPtr = thisPtr;
9783
9784          // DOING THIS LATER NOW...
9785          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9786          {
9787             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9788             /* TODO: Name Space Fix ups
9789             if(!exp.member.member.classSym)
9790                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9791             */
9792          }
9793
9794          ProcessExpressionType(exp.member.exp);
9795          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9796             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9797          {
9798             exp.isConstant = false;
9799          }
9800          else
9801             exp.isConstant = exp.member.exp.isConstant;
9802          type = exp.member.exp.expType;
9803
9804          yylloc = exp.loc;
9805
9806          if(type && (type.kind == templateType))
9807          {
9808             Class _class = thisClass ? thisClass : currentClass;
9809             ClassTemplateParameter param = null;
9810             if(_class)
9811             {
9812                for(param = _class.templateParams.first; param; param = param.next)
9813                {
9814                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9815                      break;
9816                }
9817             }
9818             if(param && param.defaultArg.member)
9819             {
9820                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9821                if(argExp)
9822                {
9823                   Expression expMember = exp.member.exp;
9824                   Declarator decl;
9825                   OldList * specs = MkList();
9826                   char thisClassTypeString[1024];
9827
9828                   FreeIdentifier(exp.member.member);
9829
9830                   ProcessExpressionType(argExp);
9831
9832                   {
9833                      char * colon = strstr(param.defaultArg.memberString, "::");
9834                      if(colon)
9835                      {
9836                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9837                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9838                      }
9839                      else
9840                         strcpy(thisClassTypeString, _class.fullName);
9841                   }
9842
9843                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9844
9845                   exp.expType = ProcessType(specs, decl);
9846                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9847                   {
9848                      Class expClass = exp.expType._class.registered;
9849                      Class cClass = null;
9850                      int paramCount = 0;
9851                      int lastParam = -1;
9852
9853                      char templateString[1024];
9854                      ClassTemplateParameter param;
9855                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9856                      for(cClass = expClass; cClass; cClass = cClass.base)
9857                      {
9858                         int p = 0;
9859                         for(param = cClass.templateParams.first; param; param = param.next)
9860                         {
9861                            int id = p;
9862                            Class sClass;
9863                            ClassTemplateArgument arg;
9864                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9865                            arg = expClass.templateArgs[id];
9866
9867                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9868                            {
9869                               ClassTemplateParameter cParam;
9870                               //int p = numParams - sClass.templateParams.count;
9871                               int p = 0;
9872                               Class nextClass;
9873                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9874
9875                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9876                               {
9877                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9878                                  {
9879                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9880                                     {
9881                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9882                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9883                                        break;
9884                                     }
9885                                  }
9886                               }
9887                            }
9888
9889                            {
9890                               char argument[256];
9891                               argument[0] = '\0';
9892                               /*if(arg.name)
9893                               {
9894                                  strcat(argument, arg.name.string);
9895                                  strcat(argument, " = ");
9896                               }*/
9897                               switch(param.type)
9898                               {
9899                                  case expression:
9900                                  {
9901                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9902                                     char expString[1024];
9903                                     OldList * specs = MkList();
9904                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9905                                     Expression exp;
9906                                     char * string = PrintHexUInt64(arg.expression.ui64);
9907                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9908                                     delete string;
9909
9910                                     ProcessExpressionType(exp);
9911                                     ComputeExpression(exp);
9912                                     expString[0] = '\0';
9913                                     PrintExpression(exp, expString);
9914                                     strcat(argument, expString);
9915                                     // delete exp;
9916                                     FreeExpression(exp);
9917                                     break;
9918                                  }
9919                                  case identifier:
9920                                  {
9921                                     strcat(argument, arg.member.name);
9922                                     break;
9923                                  }
9924                                  case TemplateParameterType::type:
9925                                  {
9926                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9927                                     {
9928                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9929                                           strcat(argument, thisClassTypeString);
9930                                        else
9931                                           strcat(argument, arg.dataTypeString);
9932                                     }
9933                                     break;
9934                                  }
9935                               }
9936                               if(argument[0])
9937                               {
9938                                  if(paramCount) strcat(templateString, ", ");
9939                                  if(lastParam != p - 1)
9940                                  {
9941                                     strcat(templateString, param.name);
9942                                     strcat(templateString, " = ");
9943                                  }
9944                                  strcat(templateString, argument);
9945                                  paramCount++;
9946                                  lastParam = p;
9947                               }
9948                               p++;
9949                            }
9950                         }
9951                      }
9952                      {
9953                         int len = strlen(templateString);
9954                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9955                         templateString[len++] = '>';
9956                         templateString[len++] = '\0';
9957                      }
9958                      {
9959                         Context context = SetupTemplatesContext(_class);
9960                         FreeType(exp.expType);
9961                         exp.expType = ProcessTypeString(templateString, false);
9962                         FinishTemplatesContext(context);
9963                      }
9964                   }
9965
9966                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9967                   exp.type = bracketsExp;
9968                   exp.list = MkListOne(MkExpOp(null, '*',
9969                   /*opExp;
9970                   exp.op.op = '*';
9971                   exp.op.exp1 = null;
9972                   exp.op.exp2 = */
9973                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9974                      MkExpBrackets(MkListOne(
9975                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9976                            '+',
9977                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9978                            '+',
9979                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9980
9981                            ));
9982                }
9983             }
9984             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9985                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9986             {
9987                type = ProcessTemplateParameterType(type.templateParameter);
9988             }
9989          }
9990          // TODO: *** This seems to be where we should add method support for all basic types ***
9991          if(type && (type.kind == templateType));
9992          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9993                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9994                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9995                           (type.kind == pointerType && type.type.kind == charType)))
9996          {
9997             Identifier id = exp.member.member;
9998             TypeKind typeKind = type.kind;
9999             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10000             if(typeKind == subClassType && exp.member.exp.type == classExp)
10001             {
10002                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
10003                typeKind = classType;
10004             }
10005
10006             if(id)
10007             {
10008                if(typeKind == intType || typeKind == enumType)
10009                   _class = eSystem_FindClass(privateModule, "int");
10010                else if(!_class)
10011                {
10012                   if(type.kind == classType && type._class && type._class.registered)
10013                   {
10014                      _class = type._class.registered;
10015                   }
10016                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10017                   {
10018                      _class = FindClass("char *").registered;
10019                   }
10020                   else if(type.kind == pointerType)
10021                   {
10022                      _class = eSystem_FindClass(privateModule, "uintptr");
10023                      FreeType(exp.expType);
10024                      exp.expType = ProcessTypeString("uintptr", false);
10025                      exp.byReference = true;
10026                   }
10027                   else
10028                   {
10029                      char string[1024] = "";
10030                      Symbol classSym;
10031                      PrintTypeNoConst(type, string, false, true);
10032                      classSym = FindClass(string);
10033                      if(classSym) _class = classSym.registered;
10034                   }
10035                }
10036             }
10037
10038             if(_class && id)
10039             {
10040                /*bool thisPtr =
10041                   (exp.member.exp.type == identifierExp &&
10042                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10043                Property prop = null;
10044                Method method = null;
10045                DataMember member = null;
10046                Property revConvert = null;
10047                ClassProperty classProp = null;
10048
10049                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10050                   exp.member.memberType = propertyMember;
10051
10052                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10053                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10054
10055                if(typeKind != subClassType)
10056                {
10057                   // Prioritize data members over properties for "this"
10058                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10059                   {
10060                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10061                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10062                      {
10063                         prop = eClass_FindProperty(_class, id.string, privateModule);
10064                         if(prop)
10065                            member = null;
10066                      }
10067                      if(!member && !prop)
10068                         prop = eClass_FindProperty(_class, id.string, privateModule);
10069                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10070                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10071                         exp.member.thisPtr = true;
10072                   }
10073                   // Prioritize properties over data members otherwise
10074                   else
10075                   {
10076                      bool useMemberForNonConst = false;
10077                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10078                      if(!id.classSym)
10079                      {
10080                         prop = eClass_FindProperty(_class, id.string, null);
10081
10082                         useMemberForNonConst = prop && exp.destType &&
10083                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10084                               !strncmp(prop.dataTypeString, "const ", 6);
10085
10086                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10087                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10088                      }
10089
10090                      if((!prop || useMemberForNonConst) && !member)
10091                      {
10092                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10093                         if(!method)
10094                         {
10095                            prop = eClass_FindProperty(_class, id.string, privateModule);
10096
10097                            useMemberForNonConst |= prop && exp.destType &&
10098                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10099                                  !strncmp(prop.dataTypeString, "const ", 6);
10100
10101                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10102                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10103                         }
10104                      }
10105
10106                      if(member && prop)
10107                      {
10108                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10109                            prop = null;
10110                         else
10111                            member = null;
10112                      }
10113                   }
10114                }
10115                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10116                   method = eClass_FindMethod(_class, id.string, privateModule);
10117                if(!prop && !member && !method)
10118                {
10119                   if(typeKind == subClassType)
10120                   {
10121                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10122                      if(classProp)
10123                      {
10124                         exp.member.memberType = classPropertyMember;
10125                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10126                      }
10127                      else
10128                      {
10129                         // Assume this is a class_data member
10130                         char structName[1024];
10131                         Identifier id = exp.member.member;
10132                         Expression classExp = exp.member.exp;
10133                         type.refCount++;
10134
10135                         FreeType(classExp.expType);
10136                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10137
10138                         strcpy(structName, "__ecereClassData_");
10139                         FullClassNameCat(structName, type._class.string, false);
10140                         exp.type = pointerExp;
10141                         exp.member.member = id;
10142
10143                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10144                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10145                               MkExpBrackets(MkListOne(MkExpOp(
10146                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10147                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10148                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10149                                  )));
10150
10151                         FreeType(type);
10152
10153                         ProcessExpressionType(exp);
10154                         return;
10155                      }
10156                   }
10157                   else
10158                   {
10159                      // Check for reverse conversion
10160                      // (Convert in an instantiation later, so that we can use
10161                      //  deep properties system)
10162                      Symbol classSym = FindClass(id.string);
10163                      if(classSym)
10164                      {
10165                         Class convertClass = classSym.registered;
10166                         if(convertClass)
10167                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10168                      }
10169                   }
10170                }
10171
10172                if(!exp.member.exp.destType)
10173                {
10174                   exp.member.exp.destType = Type
10175                   {
10176                      refCount = 1;
10177                      kind = classType;
10178                      _class = _class.symbol;
10179                   };
10180                }
10181
10182                if(prop)
10183                {
10184                   exp.member.memberType = propertyMember;
10185                   if(!prop.dataType)
10186                      ProcessPropertyType(prop);
10187                   exp.expType = prop.dataType;
10188                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10189                   {
10190                      Type type { };
10191                      CopyTypeInto(type, exp.expType);
10192                      type.refCount = 1;
10193                      type.constant = true;
10194                      exp.expType = type;
10195                   }
10196                   else if(prop.dataType)
10197                      prop.dataType.refCount++;
10198                }
10199                else if(member)
10200                {
10201                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10202                   {
10203                      FreeExpContents(exp);
10204                      exp.type = identifierExp;
10205                      exp.identifier = MkIdentifier("class");
10206                      ProcessExpressionType(exp);
10207                      return;
10208                   }
10209
10210                   exp.member.memberType = dataMember;
10211                   DeclareStruct(_class.fullName, false);
10212                   if(!member.dataType)
10213                   {
10214                      Context context = SetupTemplatesContext(_class);
10215                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10216                      FinishTemplatesContext(context);
10217                   }
10218                   exp.expType = member.dataType;
10219                   if(member.dataType) member.dataType.refCount++;
10220                }
10221                else if(revConvert)
10222                {
10223                   exp.member.memberType = reverseConversionMember;
10224                   exp.expType = MkClassType(revConvert._class.fullName);
10225                }
10226                else if(method)
10227                {
10228                   //if(inCompiler)
10229                   {
10230                      /*if(id._class)
10231                      {
10232                         exp.type = identifierExp;
10233                         exp.identifier = exp.member.member;
10234                      }
10235                      else*/
10236                         exp.member.memberType = methodMember;
10237                   }
10238                   if(!method.dataType)
10239                      ProcessMethodType(method);
10240                   exp.expType = Type
10241                   {
10242                      refCount = 1;
10243                      kind = methodType;
10244                      method = method;
10245                   };
10246
10247                   // Tricky spot here... To use instance versus class virtual table
10248                   // Put it back to what it was... What did we break?
10249
10250                   // Had to put it back for overriding Main of Thread global instance
10251
10252                   //exp.expType.methodClass = _class;
10253                   exp.expType.methodClass = (id && id._class) ? _class : null;
10254
10255                   // Need the actual class used for templated classes
10256                   exp.expType.usedClass = _class;
10257                }
10258                else if(!classProp)
10259                {
10260                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10261                   {
10262                      FreeExpContents(exp);
10263                      exp.type = identifierExp;
10264                      exp.identifier = MkIdentifier("class");
10265                      FreeType(exp.expType);
10266                      exp.expType = MkClassType("ecere::com::Class");
10267                      return;
10268                   }
10269                   yylloc = exp.member.member.loc;
10270                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10271                   if(inCompiler)
10272                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10273                }
10274
10275                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10276                {
10277                   Class tClass;
10278
10279                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10280                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10281
10282                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10283                   {
10284                      int id = 0;
10285                      ClassTemplateParameter curParam = null;
10286                      Class sClass;
10287
10288                      for(sClass = tClass; sClass; sClass = sClass.base)
10289                      {
10290                         id = 0;
10291                         if(sClass.templateClass) sClass = sClass.templateClass;
10292                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10293                         {
10294                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10295                            {
10296                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10297                                  id += sClass.templateParams.count;
10298                               break;
10299                            }
10300                            id++;
10301                         }
10302                         if(curParam) break;
10303                      }
10304
10305                      if(curParam && tClass.templateArgs[id].dataTypeString)
10306                      {
10307                         ClassTemplateArgument arg = tClass.templateArgs[id];
10308                         Context context = SetupTemplatesContext(tClass);
10309                         bool constant = exp.expType.constant;
10310                         /*if(!arg.dataType)
10311                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10312                         FreeType(exp.expType);
10313
10314                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10315                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10316                         else if(exp.expType.kind == pointerType)
10317                         {
10318                            Type t = exp.expType.type;
10319                            while(t.kind == pointerType) t = t.type;
10320                            if(constant) t.constant = constant;
10321                         }
10322                         if(exp.expType)
10323                         {
10324                            if(exp.expType.kind == thisClassType)
10325                            {
10326                               FreeType(exp.expType);
10327                               exp.expType = ReplaceThisClassType(_class);
10328                            }
10329
10330                            if(tClass.templateClass && (exp.expType.kind != templateType || (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType))))
10331                               exp.expType.passAsTemplate = true;
10332                            //exp.expType.refCount++;
10333                            if(!exp.destType)
10334                            {
10335                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10336                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10337                               else if(exp.destType.kind == pointerType)
10338                               {
10339                                  Type t = exp.destType.type;
10340                                  while(t.kind == pointerType) t = t.type;
10341                                  if(constant) t.constant = constant;
10342                               }
10343
10344                               //exp.destType.refCount++;
10345
10346                               if(exp.destType.kind == thisClassType)
10347                               {
10348                                  FreeType(exp.destType);
10349                                  exp.destType = ReplaceThisClassType(_class);
10350                               }
10351                            }
10352                         }
10353                         FinishTemplatesContext(context);
10354                      }
10355                   }
10356                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10357                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10358                   {
10359                      int id = 0;
10360                      ClassTemplateParameter curParam = null;
10361                      Class sClass;
10362
10363                      for(sClass = tClass; sClass; sClass = sClass.base)
10364                      {
10365                         id = 0;
10366                         if(sClass.templateClass) sClass = sClass.templateClass;
10367                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10368                         {
10369                            if(curParam.type == TemplateParameterType::type &&
10370                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10371                            {
10372                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10373                                  id += sClass.templateParams.count;
10374                               break;
10375                            }
10376                            id++;
10377                         }
10378                         if(curParam) break;
10379                      }
10380
10381                      if(curParam)
10382                      {
10383                         ClassTemplateArgument arg = tClass.templateArgs[id];
10384                         Context context = SetupTemplatesContext(tClass);
10385                         Type basicType;
10386                         /*if(!arg.dataType)
10387                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10388
10389                         basicType = ProcessTypeString(arg.dataTypeString, false);
10390                         if(basicType)
10391                         {
10392                            if(basicType.kind == thisClassType)
10393                            {
10394                               FreeType(basicType);
10395                               basicType = ReplaceThisClassType(_class);
10396                            }
10397
10398                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10399                            if(tClass.templateClass)
10400                               basicType.passAsTemplate = true;
10401                            */
10402
10403                            FreeType(exp.expType);
10404
10405                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10406                            //exp.expType.refCount++;
10407                            if(!exp.destType)
10408                            {
10409                               exp.destType = exp.expType;
10410                               exp.destType.refCount++;
10411                            }
10412
10413                            {
10414                               Expression newExp { };
10415                               OldList * specs = MkList();
10416                               Declarator decl;
10417                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10418                               *newExp = *exp;
10419                               if(exp.destType) exp.destType.refCount++;
10420                               if(exp.expType)  exp.expType.refCount++;
10421                               exp.type = castExp;
10422                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10423                               exp.cast.exp = newExp;
10424                               //FreeType(exp.expType);
10425                               //exp.expType = null;
10426                               //ProcessExpressionType(sourceExp);
10427                            }
10428                         }
10429                         FinishTemplatesContext(context);
10430                      }
10431                   }
10432                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10433                   {
10434                      Class expClass = exp.expType._class.registered;
10435                      if(expClass)
10436                      {
10437                         Class cClass = null;
10438                         int p = 0;
10439                         int paramCount = 0;
10440                         int lastParam = -1;
10441                         char templateString[1024];
10442                         ClassTemplateParameter param;
10443                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10444                         while(cClass != expClass)
10445                         {
10446                            Class sClass;
10447                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10448                            cClass = sClass;
10449
10450                            for(param = cClass.templateParams.first; param; param = param.next)
10451                            {
10452                               Class cClassCur = null;
10453                               int cp = 0;
10454                               ClassTemplateParameter paramCur = null;
10455                               ClassTemplateArgument arg;
10456                               while(cClassCur != tClass && !paramCur)
10457                               {
10458                                  Class sClassCur;
10459                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10460                                  cClassCur = sClassCur;
10461
10462                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10463                                  {
10464                                     if(!strcmp(paramCur.name, param.name))
10465                                     {
10466
10467                                        break;
10468                                     }
10469                                     cp++;
10470                                  }
10471                               }
10472                               if(paramCur && paramCur.type == TemplateParameterType::type)
10473                                  arg = tClass.templateArgs[cp];
10474                               else
10475                                  arg = expClass.templateArgs[p];
10476
10477                               {
10478                                  char argument[256];
10479                                  argument[0] = '\0';
10480                                  /*if(arg.name)
10481                                  {
10482                                     strcat(argument, arg.name.string);
10483                                     strcat(argument, " = ");
10484                                  }*/
10485                                  switch(param.type)
10486                                  {
10487                                     case expression:
10488                                     {
10489                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10490                                        char expString[1024];
10491                                        OldList * specs = MkList();
10492                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10493                                        Expression exp;
10494                                        char * string = PrintHexUInt64(arg.expression.ui64);
10495                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10496                                        delete string;
10497
10498                                        ProcessExpressionType(exp);
10499                                        ComputeExpression(exp);
10500                                        expString[0] = '\0';
10501                                        PrintExpression(exp, expString);
10502                                        strcat(argument, expString);
10503                                        // delete exp;
10504                                        FreeExpression(exp);
10505                                        break;
10506                                     }
10507                                     case identifier:
10508                                     {
10509                                        strcat(argument, arg.member.name);
10510                                        break;
10511                                     }
10512                                     case TemplateParameterType::type:
10513                                     {
10514                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10515                                           strcat(argument, arg.dataTypeString);
10516                                        break;
10517                                     }
10518                                  }
10519                                  if(argument[0])
10520                                  {
10521                                     if(paramCount) strcat(templateString, ", ");
10522                                     if(lastParam != p - 1)
10523                                     {
10524                                        strcat(templateString, param.name);
10525                                        strcat(templateString, " = ");
10526                                     }
10527                                     strcat(templateString, argument);
10528                                     paramCount++;
10529                                     lastParam = p;
10530                                  }
10531                               }
10532                               p++;
10533                            }
10534                         }
10535                         {
10536                            int len = strlen(templateString);
10537                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10538                            templateString[len++] = '>';
10539                            templateString[len++] = '\0';
10540                         }
10541
10542                         FreeType(exp.expType);
10543                         {
10544                            Context context = SetupTemplatesContext(tClass);
10545                            exp.expType = ProcessTypeString(templateString, false);
10546                            FinishTemplatesContext(context);
10547                         }
10548                      }
10549                   }
10550                }
10551             }
10552             else
10553                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10554          }
10555          else if(type && (type.kind == structType || type.kind == unionType))
10556          {
10557             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10558             if(memberType)
10559             {
10560                exp.expType = memberType;
10561                if(memberType)
10562                   memberType.refCount++;
10563             }
10564          }
10565          else
10566          {
10567             char expString[10240];
10568             expString[0] = '\0';
10569             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10570             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10571          }
10572
10573          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10574          {
10575             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10576             {
10577                Identifier id = exp.member.member;
10578                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10579                if(_class)
10580                {
10581                   FreeType(exp.expType);
10582                   exp.expType = ReplaceThisClassType(_class);
10583                }
10584             }
10585          }
10586          yylloc = oldyylloc;
10587          break;
10588       }
10589       // Convert x->y into (*x).y
10590       case pointerExp:
10591       {
10592          Type destType = exp.destType;
10593
10594          // DOING THIS LATER NOW...
10595          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10596          {
10597             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10598             /* TODO: Name Space Fix ups
10599             if(!exp.member.member.classSym)
10600                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10601             */
10602          }
10603
10604          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10605          exp.type = memberExp;
10606          if(destType)
10607             destType.count++;
10608          ProcessExpressionType(exp);
10609          if(destType)
10610             destType.count--;
10611          break;
10612       }
10613       case classSizeExp:
10614       {
10615          //ComputeExpression(exp);
10616
10617          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10618          if(classSym && classSym.registered)
10619          {
10620             if(classSym.registered.type == noHeadClass)
10621             {
10622                char name[1024];
10623                name[0] = '\0';
10624                DeclareStruct(classSym.string, false);
10625                FreeSpecifier(exp._class);
10626                exp.type = typeSizeExp;
10627                FullClassNameCat(name, classSym.string, false);
10628                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10629             }
10630             else
10631             {
10632                if(classSym.registered.fixed)
10633                {
10634                   FreeSpecifier(exp._class);
10635                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10636                   exp.type = constantExp;
10637                }
10638                else
10639                {
10640                   char className[1024];
10641                   strcpy(className, "__ecereClass_");
10642                   FullClassNameCat(className, classSym.string, true);
10643                   //MangleClassName(className);
10644
10645                   DeclareClass(classSym, className);
10646
10647                   FreeExpContents(exp);
10648                   exp.type = pointerExp;
10649                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10650                   exp.member.member = MkIdentifier("structSize");
10651                }
10652             }
10653          }
10654
10655          exp.expType = Type
10656          {
10657             refCount = 1;
10658             kind = intSizeType;
10659          };
10660          // exp.isConstant = true;
10661          break;
10662       }
10663       case typeSizeExp:
10664       {
10665          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10666
10667          exp.expType = Type
10668          {
10669             refCount = 1;
10670             kind = intSizeType;
10671          };
10672          exp.isConstant = true;
10673
10674          DeclareType(type, false, false);
10675          FreeType(type);
10676          break;
10677       }
10678       case castExp:
10679       {
10680          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10681          type.count = 1;
10682          FreeType(exp.cast.exp.destType);
10683          exp.cast.exp.destType = type;
10684          type.refCount++;
10685          type.casted = true;
10686          ProcessExpressionType(exp.cast.exp);
10687          type.casted = false;
10688          type.count = 0;
10689          exp.expType = type;
10690          //type.refCount++;
10691
10692          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10693          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10694          {
10695             void * prev = exp.prev, * next = exp.next;
10696             Type expType = exp.cast.exp.destType;
10697             Expression castExp = exp.cast.exp;
10698             Type destType = exp.destType;
10699
10700             if(expType) expType.refCount++;
10701
10702             //FreeType(exp.destType);
10703             FreeType(exp.expType);
10704             FreeTypeName(exp.cast.typeName);
10705
10706             *exp = *castExp;
10707             FreeType(exp.expType);
10708             FreeType(exp.destType);
10709
10710             exp.expType = expType;
10711             exp.destType = destType;
10712
10713             delete castExp;
10714
10715             exp.prev = prev;
10716             exp.next = next;
10717
10718          }
10719          else
10720          {
10721             exp.isConstant = exp.cast.exp.isConstant;
10722          }
10723          //FreeType(type);
10724          break;
10725       }
10726       case extensionInitializerExp:
10727       {
10728          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10729          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10730          // ProcessInitializer(exp.initializer.initializer, type);
10731          exp.expType = type;
10732          break;
10733       }
10734       case vaArgExp:
10735       {
10736          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10737          ProcessExpressionType(exp.vaArg.exp);
10738          exp.expType = type;
10739          break;
10740       }
10741       case conditionExp:
10742       {
10743          Expression e;
10744          exp.isConstant = true;
10745
10746          FreeType(exp.cond.cond.destType);
10747          exp.cond.cond.destType = MkClassType("bool");
10748          exp.cond.cond.destType.truth = true;
10749          ProcessExpressionType(exp.cond.cond);
10750          if(!exp.cond.cond.isConstant)
10751             exp.isConstant = false;
10752          for(e = exp.cond.exp->first; e; e = e.next)
10753          {
10754             if(!e.next)
10755             {
10756                FreeType(e.destType);
10757                e.destType = exp.destType;
10758                if(e.destType) e.destType.refCount++;
10759             }
10760             ProcessExpressionType(e);
10761             if(!e.next)
10762             {
10763                exp.expType = e.expType;
10764                if(e.expType) e.expType.refCount++;
10765             }
10766             if(!e.isConstant)
10767                exp.isConstant = false;
10768          }
10769
10770          FreeType(exp.cond.elseExp.destType);
10771          // Added this check if we failed to find an expType
10772          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10773
10774          // Reversed it...
10775          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10776
10777          if(exp.cond.elseExp.destType)
10778             exp.cond.elseExp.destType.refCount++;
10779          ProcessExpressionType(exp.cond.elseExp);
10780
10781          // FIXED THIS: Was done before calling process on elseExp
10782          if(!exp.cond.elseExp.isConstant)
10783             exp.isConstant = false;
10784          break;
10785       }
10786       case extensionCompoundExp:
10787       {
10788          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10789          {
10790             Statement last = exp.compound.compound.statements->last;
10791             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10792             {
10793                ((Expression)last.expressions->last).destType = exp.destType;
10794                if(exp.destType)
10795                   exp.destType.refCount++;
10796             }
10797             ProcessStatement(exp.compound);
10798             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10799             if(exp.expType)
10800                exp.expType.refCount++;
10801          }
10802          break;
10803       }
10804       case classExp:
10805       {
10806          Specifier spec = exp._classExp.specifiers->first;
10807          if(spec && spec.type == nameSpecifier)
10808          {
10809             exp.expType = MkClassType(spec.name);
10810             exp.expType.kind = subClassType;
10811             exp.byReference = true;
10812          }
10813          else
10814          {
10815             exp.expType = MkClassType("ecere::com::Class");
10816             exp.byReference = true;
10817          }
10818          break;
10819       }
10820       case classDataExp:
10821       {
10822          Class _class = thisClass ? thisClass : currentClass;
10823          if(_class)
10824          {
10825             Identifier id = exp.classData.id;
10826             char structName[1024];
10827             Expression classExp;
10828             strcpy(structName, "__ecereClassData_");
10829             FullClassNameCat(structName, _class.fullName, false);
10830             exp.type = pointerExp;
10831             exp.member.member = id;
10832             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10833                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10834             else
10835                classExp = MkExpIdentifier(MkIdentifier("class"));
10836
10837             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10838                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10839                   MkExpBrackets(MkListOne(MkExpOp(
10840                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10841                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10842                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10843                      )));
10844
10845             ProcessExpressionType(exp);
10846             return;
10847          }
10848          break;
10849       }
10850       case arrayExp:
10851       {
10852          Type type = null;
10853          const char * typeString = null;
10854          char typeStringBuf[1024];
10855          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10856             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10857          {
10858             Class templateClass = exp.destType._class.registered;
10859             typeString = templateClass.templateArgs[2].dataTypeString;
10860          }
10861          else if(exp.list)
10862          {
10863             // Guess type from expressions in the array
10864             Expression e;
10865             for(e = exp.list->first; e; e = e.next)
10866             {
10867                ProcessExpressionType(e);
10868                if(e.expType)
10869                {
10870                   if(!type) { type = e.expType; type.refCount++; }
10871                   else
10872                   {
10873                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10874                      if(!MatchTypeExpression(e, type, null, false, true))
10875                      {
10876                         FreeType(type);
10877                         type = e.expType;
10878                         e.expType = null;
10879
10880                         e = exp.list->first;
10881                         ProcessExpressionType(e);
10882                         if(e.expType)
10883                         {
10884                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10885                            if(!MatchTypeExpression(e, type, null, false, true))
10886                            {
10887                               FreeType(e.expType);
10888                               e.expType = null;
10889                               FreeType(type);
10890                               type = null;
10891                               break;
10892                            }
10893                         }
10894                      }
10895                   }
10896                   if(e.expType)
10897                   {
10898                      FreeType(e.expType);
10899                      e.expType = null;
10900                   }
10901                }
10902             }
10903             if(type)
10904             {
10905                typeStringBuf[0] = '\0';
10906                PrintTypeNoConst(type, typeStringBuf, false, true);
10907                typeString = typeStringBuf;
10908                FreeType(type);
10909                type = null;
10910             }
10911          }
10912          if(typeString)
10913          {
10914             /*
10915             (Container)& (struct BuiltInContainer)
10916             {
10917                ._vTbl = class(BuiltInContainer)._vTbl,
10918                ._class = class(BuiltInContainer),
10919                .refCount = 0,
10920                .data = (int[]){ 1, 7, 3, 4, 5 },
10921                .count = 5,
10922                .type = class(int),
10923             }
10924             */
10925             char templateString[1024];
10926             OldList * initializers = MkList();
10927             OldList * structInitializers = MkList();
10928             OldList * specs = MkList();
10929             Expression expExt;
10930             Declarator decl = SpecDeclFromString(typeString, specs, null);
10931             sprintf(templateString, "Container<%s>", typeString);
10932
10933             if(exp.list)
10934             {
10935                Expression e;
10936                type = ProcessTypeString(typeString, false);
10937                while((e = exp.list->first))
10938                {
10939                   exp.list->Remove(e);
10940                   e.destType = type;
10941                   type.refCount++;
10942                   ProcessExpressionType(e);
10943                   ListAdd(initializers, MkInitializerAssignment(e));
10944                }
10945                FreeType(type);
10946                delete exp.list;
10947             }
10948
10949             DeclareStruct("ecere::com::BuiltInContainer", false);
10950
10951             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10952                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10953             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10954                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10955             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10956                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10957             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10958                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10959                MkInitializerList(initializers))));
10960                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10961             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10962                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10963             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10964                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10965             exp.expType = ProcessTypeString(templateString, false);
10966             exp.type = bracketsExp;
10967             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10968                MkExpOp(null, '&',
10969                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10970                   MkInitializerList(structInitializers)))));
10971             ProcessExpressionType(expExt);
10972          }
10973          else
10974          {
10975             exp.expType = ProcessTypeString("Container", false);
10976             Compiler_Error($"Couldn't determine type of array elements\n");
10977          }
10978          break;
10979       }
10980    }
10981
10982    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10983    {
10984       FreeType(exp.expType);
10985       exp.expType = ReplaceThisClassType(thisClass);
10986    }
10987
10988    // Resolve structures here
10989    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10990    {
10991       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10992       // TODO: Fix members reference...
10993       if(symbol)
10994       {
10995          if(exp.expType.kind != enumType)
10996          {
10997             Type member;
10998             String enumName = CopyString(exp.expType.enumName);
10999
11000             // Fixed a memory leak on self-referencing C structs typedefs
11001             // by instantiating a new type rather than simply copying members
11002             // into exp.expType
11003             FreeType(exp.expType);
11004             exp.expType = Type { };
11005             exp.expType.kind = symbol.type.kind;
11006             exp.expType.refCount++;
11007             exp.expType.enumName = enumName;
11008
11009             exp.expType.members = symbol.type.members;
11010             for(member = symbol.type.members.first; member; member = member.next)
11011                member.refCount++;
11012          }
11013          else
11014          {
11015             NamedLink member;
11016             for(member = symbol.type.members.first; member; member = member.next)
11017             {
11018                NamedLink value { name = CopyString(member.name) };
11019                exp.expType.members.Add(value);
11020             }
11021          }
11022       }
11023    }
11024
11025    yylloc = exp.loc;
11026    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11027    else if(exp.destType && !exp.destType.keepCast)
11028    {
11029       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11030          exp.needTemplateCast = 1;
11031
11032       if(exp.destType.kind == voidType);
11033       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11034       {
11035          if(!exp.destType.count || unresolved)
11036          {
11037             if(!exp.expType)
11038             {
11039                yylloc = exp.loc;
11040                if(exp.destType.kind != ellipsisType)
11041                {
11042                   char type2[1024];
11043                   type2[0] = '\0';
11044                   if(inCompiler)
11045                   {
11046                      char expString[10240];
11047                      expString[0] = '\0';
11048
11049                      PrintType(exp.destType, type2, false, true);
11050
11051                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11052                      if(unresolved)
11053                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11054                      else if(exp.type != dummyExp)
11055                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11056                   }
11057                }
11058                else
11059                {
11060                   char expString[10240] ;
11061                   expString[0] = '\0';
11062                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11063
11064                   if(unresolved)
11065                      Compiler_Error($"unresolved identifier %s\n", expString);
11066                   else if(exp.type != dummyExp)
11067                      Compiler_Error($"couldn't determine type of %s\n", expString);
11068                }
11069             }
11070             else
11071             {
11072                char type1[1024];
11073                char type2[1024];
11074                type1[0] = '\0';
11075                type2[0] = '\0';
11076                if(inCompiler)
11077                {
11078                   PrintType(exp.expType, type1, false, true);
11079                   PrintType(exp.destType, type2, false, true);
11080                }
11081
11082                //CheckExpressionType(exp, exp.destType, false);
11083
11084                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11085                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11086                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11087                else
11088                {
11089                   char expString[10240];
11090                   expString[0] = '\0';
11091                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11092
11093 #ifdef _DEBUG
11094                   CheckExpressionType(exp, exp.destType, false, true);
11095 #endif
11096                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11097                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
11098                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11099
11100                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11101                   FreeType(exp.expType);
11102                   exp.destType.refCount++;
11103                   exp.expType = exp.destType;
11104                }
11105             }
11106          }
11107       }
11108       /*else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
11109       {
11110          Expression newExp { };
11111          char typeString[1024];
11112          OldList * specs = MkList();
11113          Declarator decl;
11114
11115          typeString[0] = '\0';
11116
11117          *newExp = *exp;
11118
11119          if(exp.expType)  exp.expType.refCount++;
11120          if(exp.expType)  exp.expType.refCount++;
11121          exp.type = castExp;
11122          newExp.destType = exp.expType;
11123
11124          PrintType(exp.expType, typeString, false, false);
11125          decl = SpecDeclFromString(typeString, specs, null);
11126
11127          exp.cast.typeName = MkTypeName(specs, decl);
11128          exp.cast.exp = newExp;
11129       }*/
11130    }
11131    else if(unresolved)
11132    {
11133       if(exp.identifier._class && exp.identifier._class.name)
11134          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11135       else if(exp.identifier.string && exp.identifier.string[0])
11136          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11137    }
11138    else if(!exp.expType && exp.type != dummyExp)
11139    {
11140       char expString[10240];
11141       expString[0] = '\0';
11142       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11143       Compiler_Error($"couldn't determine type of %s\n", expString);
11144    }
11145
11146    // Let's try to support any_object & typed_object here:
11147    if(inCompiler)
11148       ApplyAnyObjectLogic(exp);
11149
11150    // Mark nohead classes as by reference, unless we're casting them to an integral type
11151    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11152       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11153          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11154           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11155    {
11156       exp.byReference = true;
11157    }
11158    yylloc = oldyylloc;
11159 }
11160
11161 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11162 {
11163    // THIS CODE WILL FIND NEXT MEMBER...
11164    if(*curMember)
11165    {
11166       *curMember = (*curMember).next;
11167
11168       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11169       {
11170          *curMember = subMemberStack[--(*subMemberStackPos)];
11171          *curMember = (*curMember).next;
11172       }
11173
11174       // SKIP ALL PROPERTIES HERE...
11175       while((*curMember) && (*curMember).isProperty)
11176          *curMember = (*curMember).next;
11177
11178       if(subMemberStackPos)
11179       {
11180          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11181          {
11182             subMemberStack[(*subMemberStackPos)++] = *curMember;
11183
11184             *curMember = (*curMember).members.first;
11185             while(*curMember && (*curMember).isProperty)
11186                *curMember = (*curMember).next;
11187          }
11188       }
11189    }
11190    while(!*curMember)
11191    {
11192       if(!*curMember)
11193       {
11194          if(subMemberStackPos && *subMemberStackPos)
11195          {
11196             *curMember = subMemberStack[--(*subMemberStackPos)];
11197             *curMember = (*curMember).next;
11198          }
11199          else
11200          {
11201             Class lastCurClass = *curClass;
11202
11203             if(*curClass == _class) break;     // REACHED THE END
11204
11205             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11206             *curMember = (*curClass).membersAndProperties.first;
11207          }
11208
11209          while((*curMember) && (*curMember).isProperty)
11210             *curMember = (*curMember).next;
11211          if(subMemberStackPos)
11212          {
11213             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11214             {
11215                subMemberStack[(*subMemberStackPos)++] = *curMember;
11216
11217                *curMember = (*curMember).members.first;
11218                while(*curMember && (*curMember).isProperty)
11219                   *curMember = (*curMember).next;
11220             }
11221          }
11222       }
11223    }
11224 }
11225
11226
11227 static void ProcessInitializer(Initializer init, Type type)
11228 {
11229    switch(init.type)
11230    {
11231       case expInitializer:
11232          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11233          {
11234             // TESTING THIS FOR SHUTTING = 0 WARNING
11235             if(init.exp && !init.exp.destType)
11236             {
11237                FreeType(init.exp.destType);
11238                init.exp.destType = type;
11239                if(type) type.refCount++;
11240             }
11241             if(init.exp)
11242             {
11243                ProcessExpressionType(init.exp);
11244                init.isConstant = init.exp.isConstant;
11245             }
11246             break;
11247          }
11248          else
11249          {
11250             Expression exp = init.exp;
11251             Instantiation inst = exp.instance;
11252             MembersInit members;
11253
11254             init.type = listInitializer;
11255             init.list = MkList();
11256
11257             if(inst.members)
11258             {
11259                for(members = inst.members->first; members; members = members.next)
11260                {
11261                   if(members.type == dataMembersInit)
11262                   {
11263                      MemberInit member;
11264                      for(member = members.dataMembers->first; member; member = member.next)
11265                      {
11266                         ListAdd(init.list, member.initializer);
11267                         member.initializer = null;
11268                      }
11269                   }
11270                   // Discard all MembersInitMethod
11271                }
11272             }
11273             FreeExpression(exp);
11274          }
11275       case listInitializer:
11276       {
11277          Initializer i;
11278          Type initializerType = null;
11279          Class curClass = null;
11280          DataMember curMember = null;
11281          DataMember subMemberStack[256];
11282          int subMemberStackPos = 0;
11283
11284          if(type && type.kind == arrayType)
11285             initializerType = Dereference(type);
11286          else if(type && (type.kind == structType || type.kind == unionType))
11287             initializerType = type.members.first;
11288
11289          for(i = init.list->first; i; i = i.next)
11290          {
11291             if(type && type.kind == classType && type._class && type._class.registered)
11292             {
11293                // 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)
11294                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11295                // TODO: Generate error on initializing a private data member this way from another module...
11296                if(curMember)
11297                {
11298                   if(!curMember.dataType)
11299                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11300                   initializerType = curMember.dataType;
11301                }
11302             }
11303             ProcessInitializer(i, initializerType);
11304             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11305                initializerType = initializerType.next;
11306             if(!i.isConstant)
11307                init.isConstant = false;
11308          }
11309
11310          if(type && type.kind == arrayType)
11311             FreeType(initializerType);
11312
11313          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11314          {
11315             Compiler_Error($"Assigning list initializer to non list\n");
11316          }
11317          break;
11318       }
11319    }
11320 }
11321
11322 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11323 {
11324    switch(spec.type)
11325    {
11326       case baseSpecifier:
11327       {
11328          if(spec.specifier == THISCLASS)
11329          {
11330             if(thisClass)
11331             {
11332                spec.type = nameSpecifier;
11333                spec.name = ReplaceThisClass(thisClass);
11334                spec.symbol = FindClass(spec.name);
11335                ProcessSpecifier(spec, declareStruct);
11336             }
11337          }
11338          break;
11339       }
11340       case nameSpecifier:
11341       {
11342          Symbol symbol = FindType(curContext, spec.name);
11343          if(symbol)
11344             DeclareType(symbol.type, true, true);
11345          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11346             DeclareStruct(spec.name, false);
11347          break;
11348       }
11349       case enumSpecifier:
11350       {
11351          Enumerator e;
11352          if(spec.list)
11353          {
11354             for(e = spec.list->first; e; e = e.next)
11355             {
11356                if(e.exp)
11357                   ProcessExpressionType(e.exp);
11358             }
11359          }
11360          break;
11361       }
11362       case structSpecifier:
11363       case unionSpecifier:
11364       {
11365          if(spec.definitions)
11366          {
11367             //ClassDef def;
11368             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11369             //if(symbol)
11370                ProcessClass(spec.definitions, symbol);
11371             /*else
11372             {
11373                for(def = spec.definitions->first; def; def = def.next)
11374                {
11375                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11376                      ProcessDeclaration(def.decl);
11377                }
11378             }*/
11379          }
11380          break;
11381       }
11382       /*
11383       case classSpecifier:
11384       {
11385          Symbol classSym = FindClass(spec.name);
11386          if(classSym && classSym.registered && classSym.registered.type == structClass)
11387             DeclareStruct(spec.name, false);
11388          break;
11389       }
11390       */
11391    }
11392 }
11393
11394
11395 static void ProcessDeclarator(Declarator decl)
11396 {
11397    switch(decl.type)
11398    {
11399       case identifierDeclarator:
11400          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11401          {
11402             FreeSpecifier(decl.identifier._class);
11403             decl.identifier._class = null;
11404          }
11405          break;
11406       case arrayDeclarator:
11407          if(decl.array.exp)
11408             ProcessExpressionType(decl.array.exp);
11409       case structDeclarator:
11410       case bracketsDeclarator:
11411       case functionDeclarator:
11412       case pointerDeclarator:
11413       case extendedDeclarator:
11414       case extendedDeclaratorEnd:
11415          if(decl.declarator)
11416             ProcessDeclarator(decl.declarator);
11417          if(decl.type == functionDeclarator)
11418          {
11419             Identifier id = GetDeclId(decl);
11420             if(id && id._class)
11421             {
11422                TypeName param
11423                {
11424                   qualifiers = MkListOne(id._class);
11425                   declarator = null;
11426                };
11427                if(!decl.function.parameters)
11428                   decl.function.parameters = MkList();
11429                decl.function.parameters->Insert(null, param);
11430                id._class = null;
11431             }
11432             if(decl.function.parameters)
11433             {
11434                TypeName param;
11435
11436                for(param = decl.function.parameters->first; param; param = param.next)
11437                {
11438                   if(param.qualifiers && param.qualifiers->first)
11439                   {
11440                      Specifier spec = param.qualifiers->first;
11441                      if(spec && spec.specifier == TYPED_OBJECT)
11442                      {
11443                         Declarator d = param.declarator;
11444                         TypeName newParam
11445                         {
11446                            qualifiers = MkListOne(MkSpecifier(VOID));
11447                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11448                         };
11449                         if(d.type != pointerDeclarator)
11450                            newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11451
11452                         FreeList(param.qualifiers, FreeSpecifier);
11453
11454                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11455                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11456
11457                         decl.function.parameters->Insert(param, newParam);
11458                         param = newParam;
11459                      }
11460                      else if(spec && spec.specifier == ANY_OBJECT)
11461                      {
11462                         Declarator d = param.declarator;
11463
11464                         FreeList(param.qualifiers, FreeSpecifier);
11465
11466                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11467                         if(d.type != pointerDeclarator)
11468                            param.qualifiers->Insert(null, MkSpecifier(CONST));
11469                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11470                      }
11471                      else if(spec.specifier == THISCLASS)
11472                      {
11473                         if(thisClass)
11474                         {
11475                            spec.type = nameSpecifier;
11476                            spec.name = ReplaceThisClass(thisClass);
11477                            spec.symbol = FindClass(spec.name);
11478                            ProcessSpecifier(spec, false);
11479                         }
11480                      }
11481                   }
11482
11483                   if(param.declarator)
11484                      ProcessDeclarator(param.declarator);
11485                }
11486             }
11487          }
11488          break;
11489    }
11490 }
11491
11492 static void ProcessDeclaration(Declaration decl)
11493 {
11494    yylloc = decl.loc;
11495    switch(decl.type)
11496    {
11497       case initDeclaration:
11498       {
11499          bool declareStruct = false;
11500          /*
11501          lineNum = decl.pos.line;
11502          column = decl.pos.col;
11503          */
11504
11505          if(decl.declarators)
11506          {
11507             InitDeclarator d;
11508
11509             for(d = decl.declarators->first; d; d = d.next)
11510             {
11511                Type type, subType;
11512                ProcessDeclarator(d.declarator);
11513
11514                type = ProcessType(decl.specifiers, d.declarator);
11515
11516                if(d.initializer)
11517                {
11518                   ProcessInitializer(d.initializer, type);
11519
11520                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11521
11522                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11523                      d.initializer.exp.type == instanceExp)
11524                   {
11525                      if(type.kind == classType && type._class ==
11526                         d.initializer.exp.expType._class)
11527                      {
11528                         Instantiation inst = d.initializer.exp.instance;
11529                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11530
11531                         d.initializer.exp.instance = null;
11532                         if(decl.specifiers)
11533                            FreeList(decl.specifiers, FreeSpecifier);
11534                         FreeList(decl.declarators, FreeInitDeclarator);
11535
11536                         d = null;
11537
11538                         decl.type = instDeclaration;
11539                         decl.inst = inst;
11540                      }
11541                   }
11542                }
11543                for(subType = type; subType;)
11544                {
11545                   if(subType.kind == classType)
11546                   {
11547                      declareStruct = true;
11548                      break;
11549                   }
11550                   else if(subType.kind == pointerType)
11551                      break;
11552                   else if(subType.kind == arrayType)
11553                      subType = subType.arrayType;
11554                   else
11555                      break;
11556                }
11557
11558                FreeType(type);
11559                if(!d) break;
11560             }
11561          }
11562
11563          if(decl.specifiers)
11564          {
11565             Specifier s;
11566             for(s = decl.specifiers->first; s; s = s.next)
11567             {
11568                ProcessSpecifier(s, declareStruct);
11569             }
11570          }
11571          break;
11572       }
11573       case instDeclaration:
11574       {
11575          ProcessInstantiationType(decl.inst);
11576          break;
11577       }
11578       case structDeclaration:
11579       {
11580          Specifier spec;
11581          Declarator d;
11582          bool declareStruct = false;
11583
11584          if(decl.declarators)
11585          {
11586             for(d = decl.declarators->first; d; d = d.next)
11587             {
11588                Type type = ProcessType(decl.specifiers, d.declarator);
11589                Type subType;
11590                ProcessDeclarator(d);
11591                for(subType = type; subType;)
11592                {
11593                   if(subType.kind == classType)
11594                   {
11595                      declareStruct = true;
11596                      break;
11597                   }
11598                   else if(subType.kind == pointerType)
11599                      break;
11600                   else if(subType.kind == arrayType)
11601                      subType = subType.arrayType;
11602                   else
11603                      break;
11604                }
11605                FreeType(type);
11606             }
11607          }
11608          if(decl.specifiers)
11609          {
11610             for(spec = decl.specifiers->first; spec; spec = spec.next)
11611                ProcessSpecifier(spec, declareStruct);
11612          }
11613          break;
11614       }
11615    }
11616 }
11617
11618 static FunctionDefinition curFunction;
11619
11620 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11621 {
11622    char propName[1024], propNameM[1024];
11623    char getName[1024], setName[1024];
11624    OldList * args;
11625
11626    DeclareProperty(prop, setName, getName);
11627
11628    // eInstance_FireWatchers(object, prop);
11629    strcpy(propName, "__ecereProp_");
11630    FullClassNameCat(propName, prop._class.fullName, false);
11631    strcat(propName, "_");
11632    // strcat(propName, prop.name);
11633    FullClassNameCat(propName, prop.name, true);
11634    //MangleClassName(propName);
11635
11636    strcpy(propNameM, "__ecerePropM_");
11637    FullClassNameCat(propNameM, prop._class.fullName, false);
11638    strcat(propNameM, "_");
11639    // strcat(propNameM, prop.name);
11640    FullClassNameCat(propNameM, prop.name, true);
11641    //MangleClassName(propNameM);
11642
11643    if(prop.isWatchable)
11644    {
11645       args = MkList();
11646       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11647       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11648       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11649
11650       args = MkList();
11651       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11652       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11653       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11654    }
11655
11656
11657    {
11658       args = MkList();
11659       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11660       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11661       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11662
11663       args = MkList();
11664       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11665       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11666       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11667    }
11668
11669    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11670       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11671       curFunction.propSet.fireWatchersDone = true;
11672 }
11673
11674 static void ProcessStatement(Statement stmt)
11675 {
11676    yylloc = stmt.loc;
11677    /*
11678    lineNum = stmt.pos.line;
11679    column = stmt.pos.col;
11680    */
11681    switch(stmt.type)
11682    {
11683       case labeledStmt:
11684          ProcessStatement(stmt.labeled.stmt);
11685          break;
11686       case caseStmt:
11687          // This expression should be constant...
11688          if(stmt.caseStmt.exp)
11689          {
11690             FreeType(stmt.caseStmt.exp.destType);
11691             stmt.caseStmt.exp.destType = curSwitchType;
11692             if(curSwitchType) curSwitchType.refCount++;
11693             ProcessExpressionType(stmt.caseStmt.exp);
11694             ComputeExpression(stmt.caseStmt.exp);
11695          }
11696          if(stmt.caseStmt.stmt)
11697             ProcessStatement(stmt.caseStmt.stmt);
11698          break;
11699       case compoundStmt:
11700       {
11701          if(stmt.compound.context)
11702          {
11703             Declaration decl;
11704             Statement s;
11705
11706             Statement prevCompound = curCompound;
11707             Context prevContext = curContext;
11708
11709             if(!stmt.compound.isSwitch)
11710                curCompound = stmt;
11711             curContext = stmt.compound.context;
11712
11713             if(stmt.compound.declarations)
11714             {
11715                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11716                   ProcessDeclaration(decl);
11717             }
11718             if(stmt.compound.statements)
11719             {
11720                for(s = stmt.compound.statements->first; s; s = s.next)
11721                   ProcessStatement(s);
11722             }
11723
11724             curContext = prevContext;
11725             curCompound = prevCompound;
11726          }
11727          break;
11728       }
11729       case expressionStmt:
11730       {
11731          Expression exp;
11732          if(stmt.expressions)
11733          {
11734             for(exp = stmt.expressions->first; exp; exp = exp.next)
11735                ProcessExpressionType(exp);
11736          }
11737          break;
11738       }
11739       case ifStmt:
11740       {
11741          Expression exp;
11742
11743          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11744          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11745          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11746          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11747          {
11748             ProcessExpressionType(exp);
11749          }
11750          if(stmt.ifStmt.stmt)
11751             ProcessStatement(stmt.ifStmt.stmt);
11752          if(stmt.ifStmt.elseStmt)
11753             ProcessStatement(stmt.ifStmt.elseStmt);
11754          break;
11755       }
11756       case switchStmt:
11757       {
11758          Type oldSwitchType = curSwitchType;
11759          if(stmt.switchStmt.exp)
11760          {
11761             Expression exp;
11762             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11763             {
11764                if(!exp.next)
11765                {
11766                   /*
11767                   Type destType
11768                   {
11769                      kind = intType;
11770                      refCount = 1;
11771                   };
11772                   e.exp.destType = destType;
11773                   */
11774
11775                   ProcessExpressionType(exp);
11776                }
11777                if(!exp.next)
11778                   curSwitchType = exp.expType;
11779             }
11780          }
11781          ProcessStatement(stmt.switchStmt.stmt);
11782          curSwitchType = oldSwitchType;
11783          break;
11784       }
11785       case whileStmt:
11786       {
11787          if(stmt.whileStmt.exp)
11788          {
11789             Expression exp;
11790
11791             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11792             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11793             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11794             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11795             {
11796                ProcessExpressionType(exp);
11797             }
11798          }
11799          if(stmt.whileStmt.stmt)
11800             ProcessStatement(stmt.whileStmt.stmt);
11801          break;
11802       }
11803       case doWhileStmt:
11804       {
11805          if(stmt.doWhile.exp)
11806          {
11807             Expression exp;
11808
11809             if(stmt.doWhile.exp->last)
11810             {
11811                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11812                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11813                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11814             }
11815             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11816             {
11817                ProcessExpressionType(exp);
11818             }
11819          }
11820          if(stmt.doWhile.stmt)
11821             ProcessStatement(stmt.doWhile.stmt);
11822          break;
11823       }
11824       case forStmt:
11825       {
11826          Expression exp;
11827          if(stmt.forStmt.init)
11828             ProcessStatement(stmt.forStmt.init);
11829
11830          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11831          {
11832             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11833             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11834             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11835          }
11836
11837          if(stmt.forStmt.check)
11838             ProcessStatement(stmt.forStmt.check);
11839          if(stmt.forStmt.increment)
11840          {
11841             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11842                ProcessExpressionType(exp);
11843          }
11844
11845          if(stmt.forStmt.stmt)
11846             ProcessStatement(stmt.forStmt.stmt);
11847          break;
11848       }
11849       case forEachStmt:
11850       {
11851          Identifier id = stmt.forEachStmt.id;
11852          OldList * exp = stmt.forEachStmt.exp;
11853          OldList * filter = stmt.forEachStmt.filter;
11854          Statement block = stmt.forEachStmt.stmt;
11855          char iteratorType[1024];
11856          Type source;
11857          Expression e;
11858          bool isBuiltin = exp && exp->last &&
11859             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11860               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11861          Expression arrayExp;
11862          const char * typeString = null;
11863          int builtinCount = 0;
11864
11865          for(e = exp ? exp->first : null; e; e = e.next)
11866          {
11867             if(!e.next)
11868             {
11869                FreeType(e.destType);
11870                e.destType = ProcessTypeString("Container", false);
11871             }
11872             if(!isBuiltin || e.next)
11873                ProcessExpressionType(e);
11874          }
11875
11876          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11877          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11878             eClass_IsDerived(source._class.registered, containerClass)))
11879          {
11880             Class _class = source ? source._class.registered : null;
11881             Symbol symbol;
11882             Expression expIt = null;
11883             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
11884             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11885             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11886             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11887             stmt.type = compoundStmt;
11888
11889             stmt.compound.context = Context { };
11890             stmt.compound.context.parent = curContext;
11891             curContext = stmt.compound.context;
11892
11893             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11894             {
11895                Class mapClass = eSystem_FindClass(privateModule, "Map");
11896                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11897                isCustomAVLTree = true;
11898                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
11899                   isAVLTree = true;
11900                else */if(eClass_IsDerived(source._class.registered, mapClass))
11901                   isMap = true;
11902             }
11903             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11904             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11905             {
11906                Class listClass = eSystem_FindClass(privateModule, "List");
11907                isLinkList = true;
11908                isList = eClass_IsDerived(source._class.registered, listClass);
11909             }
11910
11911             if(isArray)
11912             {
11913                Declarator decl;
11914                OldList * specs = MkList();
11915                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11916                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11917                stmt.compound.declarations = MkListOne(
11918                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11919                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11920                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11921                      MkInitializerAssignment(MkExpBrackets(exp))))));
11922             }
11923             else if(isBuiltin)
11924             {
11925                Type type = null;
11926                char typeStringBuf[1024];
11927
11928                // TODO: Merge this code?
11929                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11930                if(((Expression)exp->last).type == castExp)
11931                {
11932                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11933                   if(typeName)
11934                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11935                }
11936
11937                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11938                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11939                   arrayExp.destType._class.registered.templateArgs)
11940                {
11941                   Class templateClass = arrayExp.destType._class.registered;
11942                   typeString = templateClass.templateArgs[2].dataTypeString;
11943                }
11944                else if(arrayExp.list)
11945                {
11946                   // Guess type from expressions in the array
11947                   Expression e;
11948                   for(e = arrayExp.list->first; e; e = e.next)
11949                   {
11950                      ProcessExpressionType(e);
11951                      if(e.expType)
11952                      {
11953                         if(!type) { type = e.expType; type.refCount++; }
11954                         else
11955                         {
11956                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11957                            if(!MatchTypeExpression(e, type, null, false, true))
11958                            {
11959                               FreeType(type);
11960                               type = e.expType;
11961                               e.expType = null;
11962
11963                               e = arrayExp.list->first;
11964                               ProcessExpressionType(e);
11965                               if(e.expType)
11966                               {
11967                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11968                                  if(!MatchTypeExpression(e, type, null, false, true))
11969                                  {
11970                                     FreeType(e.expType);
11971                                     e.expType = null;
11972                                     FreeType(type);
11973                                     type = null;
11974                                     break;
11975                                  }
11976                               }
11977                            }
11978                         }
11979                         if(e.expType)
11980                         {
11981                            FreeType(e.expType);
11982                            e.expType = null;
11983                         }
11984                      }
11985                   }
11986                   if(type)
11987                   {
11988                      typeStringBuf[0] = '\0';
11989                      PrintType(type, typeStringBuf, false, true);
11990                      typeString = typeStringBuf;
11991                      FreeType(type);
11992                   }
11993                }
11994                if(typeString)
11995                {
11996                   OldList * initializers = MkList();
11997                   Declarator decl;
11998                   OldList * specs = MkList();
11999                   if(arrayExp.list)
12000                   {
12001                      Expression e;
12002
12003                      builtinCount = arrayExp.list->count;
12004                      type = ProcessTypeString(typeString, false);
12005                      while((e = arrayExp.list->first))
12006                      {
12007                         arrayExp.list->Remove(e);
12008                         e.destType = type;
12009                         type.refCount++;
12010                         ProcessExpressionType(e);
12011                         ListAdd(initializers, MkInitializerAssignment(e));
12012                      }
12013                      FreeType(type);
12014                      delete arrayExp.list;
12015                   }
12016                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12017                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12018                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12019
12020                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12021                      PlugDeclarator(
12022                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12023                         ), MkInitializerList(initializers)))));
12024                   FreeList(exp, FreeExpression);
12025                }
12026                else
12027                {
12028                   arrayExp.expType = ProcessTypeString("Container", false);
12029                   Compiler_Error($"Couldn't determine type of array elements\n");
12030                }
12031
12032                /*
12033                Declarator decl;
12034                OldList * specs = MkList();
12035
12036                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12037                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12038                stmt.compound.declarations = MkListOne(
12039                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12040                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12041                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12042                      MkInitializerAssignment(MkExpBrackets(exp))))));
12043                */
12044             }
12045             else if(isLinkList && !isList)
12046             {
12047                Declarator decl;
12048                OldList * specs = MkList();
12049                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12050                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12051                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12052                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12053                      MkInitializerAssignment(MkExpBrackets(exp))))));
12054             }
12055             /*else if(isCustomAVLTree)
12056             {
12057                Declarator decl;
12058                OldList * specs = MkList();
12059                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12060                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12061                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12062                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12063                      MkInitializerAssignment(MkExpBrackets(exp))))));
12064             }*/
12065             else if(_class.templateArgs)
12066             {
12067                if(isMap)
12068                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12069                else
12070                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12071
12072                stmt.compound.declarations = MkListOne(
12073                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12074                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12075                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12076             }
12077             symbol = FindSymbol(id.string, curContext, curContext, false, false);
12078
12079             if(block)
12080             {
12081                // Reparent sub-contexts in this statement
12082                switch(block.type)
12083                {
12084                   case compoundStmt:
12085                      if(block.compound.context)
12086                         block.compound.context.parent = stmt.compound.context;
12087                      break;
12088                   case ifStmt:
12089                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12090                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12091                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12092                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12093                      break;
12094                   case switchStmt:
12095                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12096                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12097                      break;
12098                   case whileStmt:
12099                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12100                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12101                      break;
12102                   case doWhileStmt:
12103                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12104                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12105                      break;
12106                   case forStmt:
12107                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12108                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12109                      break;
12110                   case forEachStmt:
12111                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12112                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12113                      break;
12114                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12115                   case labeledStmt:
12116                   case caseStmt
12117                   case expressionStmt:
12118                   case gotoStmt:
12119                   case continueStmt:
12120                   case breakStmt
12121                   case returnStmt:
12122                   case asmStmt:
12123                   case badDeclarationStmt:
12124                   case fireWatchersStmt:
12125                   case stopWatchingStmt:
12126                   case watchStmt:
12127                   */
12128                }
12129             }
12130             if(filter)
12131             {
12132                block = MkIfStmt(filter, block, null);
12133             }
12134             if(isArray)
12135             {
12136                stmt.compound.statements = MkListOne(MkForStmt(
12137                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12138                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12139                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12140                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12141                   block));
12142               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12143               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12144               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12145             }
12146             else if(isBuiltin)
12147             {
12148                char count[128];
12149                //OldList * specs = MkList();
12150                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12151
12152                sprintf(count, "%d", builtinCount);
12153
12154                stmt.compound.statements = MkListOne(MkForStmt(
12155                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12156                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12157                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12158                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12159                   block));
12160
12161                /*
12162                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12163                stmt.compound.statements = MkListOne(MkForStmt(
12164                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12165                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12166                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12167                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12168                   block));
12169               */
12170               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12171               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12172               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12173             }
12174             else if(isLinkList && !isList)
12175             {
12176                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12177                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12178                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12179                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12180                {
12181                   stmt.compound.statements = MkListOne(MkForStmt(
12182                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12183                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12184                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12185                      block));
12186                }
12187                else
12188                {
12189                   OldList * specs = MkList();
12190                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12191                   stmt.compound.statements = MkListOne(MkForStmt(
12192                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12193                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12194                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12195                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12196                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12197                      block));
12198                }
12199                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12200                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12201                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12202             }
12203             /*else if(isCustomAVLTree)
12204             {
12205                stmt.compound.statements = MkListOne(MkForStmt(
12206                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12207                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12208                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12209                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12210                   block));
12211
12212                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12213                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12214                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12215             }*/
12216             else
12217             {
12218                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12219                   MkIdentifier("Next")), null)), block));
12220             }
12221             ProcessExpressionType(expIt);
12222             if(stmt.compound.declarations->first)
12223                ProcessDeclaration(stmt.compound.declarations->first);
12224
12225             if(symbol)
12226                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12227
12228             ProcessStatement(stmt);
12229             curContext = stmt.compound.context.parent;
12230             break;
12231          }
12232          else
12233          {
12234             Compiler_Error($"Expression is not a container\n");
12235          }
12236          break;
12237       }
12238       case gotoStmt:
12239          break;
12240       case continueStmt:
12241          break;
12242       case breakStmt:
12243          break;
12244       case returnStmt:
12245       {
12246          Expression exp;
12247          if(stmt.expressions)
12248          {
12249             for(exp = stmt.expressions->first; exp; exp = exp.next)
12250             {
12251                if(!exp.next)
12252                {
12253                   if(curFunction && !curFunction.type)
12254                      curFunction.type = ProcessType(
12255                         curFunction.specifiers, curFunction.declarator);
12256                   FreeType(exp.destType);
12257                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12258                   if(exp.destType) exp.destType.refCount++;
12259                }
12260                ProcessExpressionType(exp);
12261             }
12262          }
12263          break;
12264       }
12265       case badDeclarationStmt:
12266       {
12267          ProcessDeclaration(stmt.decl);
12268          break;
12269       }
12270       case asmStmt:
12271       {
12272          AsmField field;
12273          if(stmt.asmStmt.inputFields)
12274          {
12275             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12276                if(field.expression)
12277                   ProcessExpressionType(field.expression);
12278          }
12279          if(stmt.asmStmt.outputFields)
12280          {
12281             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12282                if(field.expression)
12283                   ProcessExpressionType(field.expression);
12284          }
12285          if(stmt.asmStmt.clobberedFields)
12286          {
12287             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12288             {
12289                if(field.expression)
12290                   ProcessExpressionType(field.expression);
12291             }
12292          }
12293          break;
12294       }
12295       case watchStmt:
12296       {
12297          PropertyWatch propWatch;
12298          OldList * watches = stmt._watch.watches;
12299          Expression object = stmt._watch.object;
12300          Expression watcher = stmt._watch.watcher;
12301          if(watcher)
12302             ProcessExpressionType(watcher);
12303          if(object)
12304             ProcessExpressionType(object);
12305
12306          if(inCompiler)
12307          {
12308             if(watcher || thisClass)
12309             {
12310                External external = curExternal;
12311                Context context = curContext;
12312
12313                stmt.type = expressionStmt;
12314                stmt.expressions = MkList();
12315
12316                curExternal = external.prev;
12317
12318                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12319                {
12320                   ClassFunction func;
12321                   char watcherName[1024];
12322                   Class watcherClass = watcher ?
12323                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12324                   External createdExternal;
12325
12326                   // Create a declaration above
12327                   External externalDecl = MkExternalDeclaration(null);
12328                   ast->Insert(curExternal.prev, externalDecl);
12329
12330                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12331                   if(propWatch.deleteWatch)
12332                      strcat(watcherName, "_delete");
12333                   else
12334                   {
12335                      Identifier propID;
12336                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12337                      {
12338                         strcat(watcherName, "_");
12339                         strcat(watcherName, propID.string);
12340                      }
12341                   }
12342
12343                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12344                   {
12345                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12346                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12347                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12348                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12349                      ProcessClassFunctionBody(func, propWatch.compound);
12350                      propWatch.compound = null;
12351
12352                      //afterExternal = afterExternal ? afterExternal : curExternal;
12353
12354                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12355                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12356                      // TESTING THIS...
12357                      createdExternal.symbol.idCode = external.symbol.idCode;
12358
12359                      curExternal = createdExternal;
12360                      ProcessFunction(createdExternal.function);
12361
12362
12363                      // Create a declaration above
12364                      {
12365                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12366                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12367                         externalDecl.declaration = decl;
12368                         if(decl.symbol && !decl.symbol.pointerExternal)
12369                            decl.symbol.pointerExternal = externalDecl;
12370                      }
12371
12372                      if(propWatch.deleteWatch)
12373                      {
12374                         OldList * args = MkList();
12375                         ListAdd(args, CopyExpression(object));
12376                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12377                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12378                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12379                      }
12380                      else
12381                      {
12382                         Class _class = object.expType._class.registered;
12383                         Identifier propID;
12384
12385                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12386                         {
12387                            char propName[1024];
12388                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12389                            if(prop)
12390                            {
12391                               char getName[1024], setName[1024];
12392                               OldList * args = MkList();
12393
12394                               DeclareProperty(prop, setName, getName);
12395
12396                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12397                               strcpy(propName, "__ecereProp_");
12398                               FullClassNameCat(propName, prop._class.fullName, false);
12399                               strcat(propName, "_");
12400                               // strcat(propName, prop.name);
12401                               FullClassNameCat(propName, prop.name, true);
12402
12403                               ListAdd(args, CopyExpression(object));
12404                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12405                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12406                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12407
12408                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12409                            }
12410                            else
12411                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12412                         }
12413                      }
12414                   }
12415                   else
12416                      Compiler_Error($"Invalid watched object\n");
12417                }
12418
12419                curExternal = external;
12420                curContext = context;
12421
12422                if(watcher)
12423                   FreeExpression(watcher);
12424                if(object)
12425                   FreeExpression(object);
12426                FreeList(watches, FreePropertyWatch);
12427             }
12428             else
12429                Compiler_Error($"No observer specified and not inside a _class\n");
12430          }
12431          else
12432          {
12433             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12434             {
12435                ProcessStatement(propWatch.compound);
12436             }
12437
12438          }
12439          break;
12440       }
12441       case fireWatchersStmt:
12442       {
12443          OldList * watches = stmt._watch.watches;
12444          Expression object = stmt._watch.object;
12445          Class _class;
12446          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12447          // printf("%X\n", watches);
12448          // printf("%X\n", stmt._watch.watches);
12449          if(object)
12450             ProcessExpressionType(object);
12451
12452          if(inCompiler)
12453          {
12454             _class = object ?
12455                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12456
12457             if(_class)
12458             {
12459                Identifier propID;
12460
12461                stmt.type = expressionStmt;
12462                stmt.expressions = MkList();
12463
12464                // Check if we're inside a property set
12465                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12466                {
12467                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12468                }
12469                else if(!watches)
12470                {
12471                   //Compiler_Error($"No property specified and not inside a property set\n");
12472                }
12473                if(watches)
12474                {
12475                   for(propID = watches->first; propID; propID = propID.next)
12476                   {
12477                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12478                      if(prop)
12479                      {
12480                         CreateFireWatcher(prop, object, stmt);
12481                      }
12482                      else
12483                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12484                   }
12485                }
12486                else
12487                {
12488                   // Fire all properties!
12489                   Property prop;
12490                   Class base;
12491                   for(base = _class; base; base = base.base)
12492                   {
12493                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12494                      {
12495                         if(prop.isProperty && prop.isWatchable)
12496                         {
12497                            CreateFireWatcher(prop, object, stmt);
12498                         }
12499                      }
12500                   }
12501                }
12502
12503                if(object)
12504                   FreeExpression(object);
12505                FreeList(watches, FreeIdentifier);
12506             }
12507             else
12508                Compiler_Error($"Invalid object specified and not inside a class\n");
12509          }
12510          break;
12511       }
12512       case stopWatchingStmt:
12513       {
12514          OldList * watches = stmt._watch.watches;
12515          Expression object = stmt._watch.object;
12516          Expression watcher = stmt._watch.watcher;
12517          Class _class;
12518          if(object)
12519             ProcessExpressionType(object);
12520          if(watcher)
12521             ProcessExpressionType(watcher);
12522          if(inCompiler)
12523          {
12524             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12525
12526             if(watcher || thisClass)
12527             {
12528                if(_class)
12529                {
12530                   Identifier propID;
12531
12532                   stmt.type = expressionStmt;
12533                   stmt.expressions = MkList();
12534
12535                   if(!watches)
12536                   {
12537                      OldList * args;
12538                      // eInstance_StopWatching(object, null, watcher);
12539                      args = MkList();
12540                      ListAdd(args, CopyExpression(object));
12541                      ListAdd(args, MkExpConstant("0"));
12542                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12543                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12544                   }
12545                   else
12546                   {
12547                      for(propID = watches->first; propID; propID = propID.next)
12548                      {
12549                         char propName[1024];
12550                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12551                         if(prop)
12552                         {
12553                            char getName[1024], setName[1024];
12554                            OldList * args = MkList();
12555
12556                            DeclareProperty(prop, setName, getName);
12557
12558                            // eInstance_StopWatching(object, prop, watcher);
12559                            strcpy(propName, "__ecereProp_");
12560                            FullClassNameCat(propName, prop._class.fullName, false);
12561                            strcat(propName, "_");
12562                            // strcat(propName, prop.name);
12563                            FullClassNameCat(propName, prop.name, true);
12564                            //MangleClassName(propName);
12565
12566                            ListAdd(args, CopyExpression(object));
12567                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12568                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12569                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12570                         }
12571                         else
12572                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12573                      }
12574                   }
12575
12576                   if(object)
12577                      FreeExpression(object);
12578                   if(watcher)
12579                      FreeExpression(watcher);
12580                   FreeList(watches, FreeIdentifier);
12581                }
12582                else
12583                   Compiler_Error($"Invalid object specified and not inside a class\n");
12584             }
12585             else
12586                Compiler_Error($"No observer specified and not inside a class\n");
12587          }
12588          break;
12589       }
12590    }
12591 }
12592
12593 static void ProcessFunction(FunctionDefinition function)
12594 {
12595    Identifier id = GetDeclId(function.declarator);
12596    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12597    Type type = symbol ? symbol.type : null;
12598    Class oldThisClass = thisClass;
12599    Context oldTopContext = topContext;
12600
12601    yylloc = function.loc;
12602    // Process thisClass
12603
12604    if(type && type.thisClass)
12605    {
12606       Symbol classSym = type.thisClass;
12607       Class _class = type.thisClass.registered;
12608       char className[1024];
12609       char structName[1024];
12610       Declarator funcDecl;
12611       Symbol thisSymbol;
12612
12613       bool typedObject = false;
12614
12615       if(_class && !_class.base)
12616       {
12617          _class = currentClass;
12618          if(_class && !_class.symbol)
12619             _class.symbol = FindClass(_class.fullName);
12620          classSym = _class ? _class.symbol : null;
12621          typedObject = true;
12622       }
12623
12624       thisClass = _class;
12625
12626       if(inCompiler && _class)
12627       {
12628          if(type.kind == functionType)
12629          {
12630             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12631             {
12632                //TypeName param = symbol.type.params.first;
12633                Type param = symbol.type.params.first;
12634                symbol.type.params.Remove(param);
12635                //FreeTypeName(param);
12636                FreeType(param);
12637             }
12638             if(type.classObjectType != classPointer)
12639             {
12640                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12641                symbol.type.staticMethod = true;
12642                symbol.type.thisClass = null;
12643
12644                // HIGH DANGER: VERIFYING THIS...
12645                symbol.type.extraParam = false;
12646             }
12647          }
12648
12649          strcpy(className, "__ecereClass_");
12650          FullClassNameCat(className, _class.fullName, true);
12651
12652          //MangleClassName(className);
12653
12654          structName[0] = 0;
12655          FullClassNameCat(structName, _class.fullName, false);
12656
12657          // [class] this
12658
12659
12660          funcDecl = GetFuncDecl(function.declarator);
12661          if(funcDecl)
12662          {
12663             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12664             {
12665                TypeName param = funcDecl.function.parameters->first;
12666                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12667                {
12668                   funcDecl.function.parameters->Remove(param);
12669                   FreeTypeName(param);
12670                }
12671             }
12672
12673             // DANGER: Watch for this... Check if it's a Conversion?
12674             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12675
12676             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12677             if(!function.propertyNoThis)
12678             {
12679                TypeName thisParam;
12680
12681                if(type.classObjectType != classPointer)
12682                {
12683                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12684                   if(!funcDecl.function.parameters)
12685                      funcDecl.function.parameters = MkList();
12686                   funcDecl.function.parameters->Insert(null, thisParam);
12687                }
12688
12689                if(typedObject)
12690                {
12691                   if(type.classObjectType != classPointer)
12692                   {
12693                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12694                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12695                   }
12696
12697                   thisParam = TypeName
12698                   {
12699                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12700                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12701                   };
12702                   funcDecl.function.parameters->Insert(null, thisParam);
12703                }
12704             }
12705          }
12706
12707          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12708          {
12709             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12710             funcDecl = GetFuncDecl(initDecl.declarator);
12711             if(funcDecl)
12712             {
12713                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12714                {
12715                   TypeName param = funcDecl.function.parameters->first;
12716                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12717                   {
12718                      funcDecl.function.parameters->Remove(param);
12719                      FreeTypeName(param);
12720                   }
12721                }
12722
12723                if(type.classObjectType != classPointer)
12724                {
12725                   // DANGER: Watch for this... Check if it's a Conversion?
12726                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12727                   {
12728                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12729
12730                      if(!funcDecl.function.parameters)
12731                         funcDecl.function.parameters = MkList();
12732                      funcDecl.function.parameters->Insert(null, thisParam);
12733                   }
12734                }
12735             }
12736          }
12737       }
12738
12739       // Add this to the context
12740       if(function.body)
12741       {
12742          if(type.classObjectType != classPointer)
12743          {
12744             thisSymbol = Symbol
12745             {
12746                string = CopyString("this");
12747                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12748             };
12749             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12750
12751             if(typedObject && thisSymbol.type)
12752             {
12753                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12754                thisSymbol.type.byReference = type.byReference;
12755                thisSymbol.type.typedByReference = type.byReference;
12756                /*
12757                thisSymbol = Symbol { string = CopyString("class") };
12758                function.body.compound.context.symbols.Add(thisSymbol);
12759                */
12760             }
12761          }
12762       }
12763
12764       // Pointer to class data
12765
12766       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12767       {
12768          DataMember member = null;
12769          {
12770             Class base;
12771             for(base = _class; base && base.type != systemClass; base = base.next)
12772             {
12773                for(member = base.membersAndProperties.first; member; member = member.next)
12774                   if(!member.isProperty)
12775                      break;
12776                if(member)
12777                   break;
12778             }
12779          }
12780          for(member = _class.membersAndProperties.first; member; member = member.next)
12781             if(!member.isProperty)
12782                break;
12783          if(member)
12784          {
12785             char pointerName[1024];
12786
12787             Declaration decl;
12788             Initializer initializer;
12789             Expression exp, bytePtr;
12790
12791             strcpy(pointerName, "__ecerePointer_");
12792             FullClassNameCat(pointerName, _class.fullName, false);
12793             {
12794                char className[1024];
12795                strcpy(className, "__ecereClass_");
12796                FullClassNameCat(className, classSym.string, true);
12797                //MangleClassName(className);
12798
12799                // Testing This
12800                DeclareClass(classSym, className);
12801             }
12802
12803             // ((byte *) this)
12804             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12805
12806             if(_class.fixed)
12807             {
12808                char string[256];
12809                sprintf(string, "%d", _class.offset);
12810                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12811             }
12812             else
12813             {
12814                // ([bytePtr] + [className]->offset)
12815                exp = QBrackets(MkExpOp(bytePtr, '+',
12816                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12817             }
12818
12819             // (this ? [exp] : 0)
12820             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12821             exp.expType = Type
12822             {
12823                refCount = 1;
12824                kind = pointerType;
12825                type = Type { refCount = 1, kind = voidType };
12826             };
12827
12828             if(function.body)
12829             {
12830                yylloc = function.body.loc;
12831                // ([structName] *) [exp]
12832                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12833                initializer = MkInitializerAssignment(
12834                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12835
12836                // [structName] * [pointerName] = [initializer];
12837                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12838
12839                {
12840                   Context prevContext = curContext;
12841                   curContext = function.body.compound.context;
12842
12843                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12844                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12845
12846                   curContext = prevContext;
12847                }
12848
12849                // WHY?
12850                decl.symbol = null;
12851
12852                if(!function.body.compound.declarations)
12853                   function.body.compound.declarations = MkList();
12854                function.body.compound.declarations->Insert(null, decl);
12855             }
12856          }
12857       }
12858
12859
12860       // Loop through the function and replace undeclared identifiers
12861       // which are a member of the class (methods, properties or data)
12862       // by "this.[member]"
12863    }
12864    else
12865       thisClass = null;
12866
12867    if(id)
12868    {
12869       FreeSpecifier(id._class);
12870       id._class = null;
12871
12872       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12873       {
12874          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12875          id = GetDeclId(initDecl.declarator);
12876
12877          FreeSpecifier(id._class);
12878          id._class = null;
12879       }
12880    }
12881    if(function.body)
12882       topContext = function.body.compound.context;
12883    {
12884       FunctionDefinition oldFunction = curFunction;
12885       curFunction = function;
12886       if(function.body)
12887          ProcessStatement(function.body);
12888
12889       // If this is a property set and no firewatchers has been done yet, add one here
12890       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12891       {
12892          Statement prevCompound = curCompound;
12893          Context prevContext = curContext;
12894
12895          Statement fireWatchers = MkFireWatchersStmt(null, null);
12896          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12897          ListAdd(function.body.compound.statements, fireWatchers);
12898
12899          curCompound = function.body;
12900          curContext = function.body.compound.context;
12901
12902          ProcessStatement(fireWatchers);
12903
12904          curContext = prevContext;
12905          curCompound = prevCompound;
12906
12907       }
12908
12909       curFunction = oldFunction;
12910    }
12911
12912    if(function.declarator)
12913    {
12914       ProcessDeclarator(function.declarator);
12915    }
12916
12917    topContext = oldTopContext;
12918    thisClass = oldThisClass;
12919 }
12920
12921 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12922 static void ProcessClass(OldList definitions, Symbol symbol)
12923 {
12924    ClassDef def;
12925    External external = curExternal;
12926    Class regClass = symbol ? symbol.registered : null;
12927
12928    // Process all functions
12929    for(def = definitions.first; def; def = def.next)
12930    {
12931       if(def.type == functionClassDef)
12932       {
12933          if(def.function.declarator)
12934             curExternal = def.function.declarator.symbol.pointerExternal;
12935          else
12936             curExternal = external;
12937
12938          ProcessFunction((FunctionDefinition)def.function);
12939       }
12940       else if(def.type == declarationClassDef)
12941       {
12942          if(def.decl.type == instDeclaration)
12943          {
12944             thisClass = regClass;
12945             ProcessInstantiationType(def.decl.inst);
12946             thisClass = null;
12947          }
12948          // Testing this
12949          else
12950          {
12951             Class backThisClass = thisClass;
12952             if(regClass) thisClass = regClass;
12953             ProcessDeclaration(def.decl);
12954             thisClass = backThisClass;
12955          }
12956       }
12957       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12958       {
12959          MemberInit defProperty;
12960
12961          // Add this to the context
12962          Symbol thisSymbol = Symbol
12963          {
12964             string = CopyString("this");
12965             type = regClass ? MkClassType(regClass.fullName) : null;
12966          };
12967          globalContext.symbols.Add((BTNode)thisSymbol);
12968
12969          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12970          {
12971             thisClass = regClass;
12972             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12973             thisClass = null;
12974          }
12975
12976          globalContext.symbols.Remove((BTNode)thisSymbol);
12977          FreeSymbol(thisSymbol);
12978       }
12979       else if(def.type == propertyClassDef && def.propertyDef)
12980       {
12981          PropertyDef prop = def.propertyDef;
12982
12983          // Add this to the context
12984          /*
12985          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12986          globalContext.symbols.Add(thisSymbol);
12987          */
12988
12989          thisClass = regClass;
12990          if(prop.setStmt)
12991          {
12992             if(regClass)
12993             {
12994                Symbol thisSymbol
12995                {
12996                   string = CopyString("this");
12997                   type = MkClassType(regClass.fullName);
12998                };
12999                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13000             }
13001
13002             curExternal = prop.symbol ? prop.symbol.externalSet : null;
13003             ProcessStatement(prop.setStmt);
13004          }
13005          if(prop.getStmt)
13006          {
13007             if(regClass)
13008             {
13009                Symbol thisSymbol
13010                {
13011                   string = CopyString("this");
13012                   type = MkClassType(regClass.fullName);
13013                };
13014                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13015             }
13016
13017             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13018             ProcessStatement(prop.getStmt);
13019          }
13020          if(prop.issetStmt)
13021          {
13022             if(regClass)
13023             {
13024                Symbol thisSymbol
13025                {
13026                   string = CopyString("this");
13027                   type = MkClassType(regClass.fullName);
13028                };
13029                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13030             }
13031
13032             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13033             ProcessStatement(prop.issetStmt);
13034          }
13035
13036          thisClass = null;
13037
13038          /*
13039          globalContext.symbols.Remove(thisSymbol);
13040          FreeSymbol(thisSymbol);
13041          */
13042       }
13043       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13044       {
13045          PropertyWatch propertyWatch = def.propertyWatch;
13046
13047          thisClass = regClass;
13048          if(propertyWatch.compound)
13049          {
13050             Symbol thisSymbol
13051             {
13052                string = CopyString("this");
13053                type = regClass ? MkClassType(regClass.fullName) : null;
13054             };
13055
13056             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13057
13058             curExternal = null;
13059             ProcessStatement(propertyWatch.compound);
13060          }
13061          thisClass = null;
13062       }
13063    }
13064 }
13065
13066 void DeclareFunctionUtil(const String s)
13067 {
13068    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13069    if(function)
13070    {
13071       char name[1024];
13072       name[0] = 0;
13073       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13074          strcpy(name, "__ecereFunction_");
13075       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13076       DeclareFunction(function, name);
13077    }
13078 }
13079
13080 void ComputeDataTypes()
13081 {
13082    External external;
13083    External temp { };
13084    External after = null;
13085
13086    currentClass = null;
13087
13088    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13089
13090    for(external = ast->first; external; external = external.next)
13091    {
13092       if(external.type == declarationExternal)
13093       {
13094          Declaration decl = external.declaration;
13095          if(decl)
13096          {
13097             OldList * decls = decl.declarators;
13098             if(decls)
13099             {
13100                InitDeclarator initDecl = decls->first;
13101                if(initDecl)
13102                {
13103                   Declarator declarator = initDecl.declarator;
13104                   if(declarator && declarator.type == identifierDeclarator)
13105                   {
13106                      Identifier id = declarator.identifier;
13107                      if(id && id.string)
13108                      {
13109                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
13110                         {
13111                            external.symbol.id = -1001, external.symbol.idCode = -1001;
13112                            after = external;
13113                         }
13114                      }
13115                   }
13116                }
13117             }
13118          }
13119        }
13120    }
13121
13122    {
13123       // Workaround until we have proper toposort for declarations reordering
13124       External e = MkExternalDeclaration(MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Instance"), null)), null));
13125       ast->Insert(after, e);
13126       after = e;
13127    }
13128
13129    temp.symbol = Symbol { id = -1000, idCode = -1000 };
13130    ast->Insert(after, temp);
13131    curExternal = temp;
13132
13133    DeclareFunctionUtil("eSystem_New");
13134    DeclareFunctionUtil("eSystem_New0");
13135    DeclareFunctionUtil("eSystem_Renew");
13136    DeclareFunctionUtil("eSystem_Renew0");
13137    DeclareFunctionUtil("eSystem_Delete");
13138    DeclareFunctionUtil("eClass_GetProperty");
13139    DeclareFunctionUtil("eClass_SetProperty");
13140    DeclareFunctionUtil("eInstance_FireSelfWatchers");
13141    DeclareFunctionUtil("eInstance_SetMethod");
13142    DeclareFunctionUtil("eInstance_IncRef");
13143    DeclareFunctionUtil("eInstance_StopWatching");
13144    DeclareFunctionUtil("eInstance_Watch");
13145    DeclareFunctionUtil("eInstance_FireWatchers");
13146
13147    DeclareStruct("ecere::com::Class", false);
13148    DeclareStruct("ecere::com::Instance", false);
13149    DeclareStruct("ecere::com::Property", false);
13150    DeclareStruct("ecere::com::DataMember", false);
13151    DeclareStruct("ecere::com::Method", false);
13152    DeclareStruct("ecere::com::SerialBuffer", false);
13153    DeclareStruct("ecere::com::ClassTemplateArgument", false);
13154
13155    ast->Remove(temp);
13156
13157    for(external = ast->first; external; external = external.next)
13158    {
13159       afterExternal = curExternal = external;
13160       if(external.type == functionExternal)
13161       {
13162          currentClass = external.function._class;
13163          ProcessFunction(external.function);
13164       }
13165       // There shouldn't be any _class member access here anyways...
13166       else if(external.type == declarationExternal)
13167       {
13168          currentClass = null;
13169          if(external.declaration)
13170             ProcessDeclaration(external.declaration);
13171       }
13172       else if(external.type == classExternal)
13173       {
13174          ClassDefinition _class = external._class;
13175          currentClass = external.symbol.registered;
13176          if(_class.definitions)
13177          {
13178             ProcessClass(_class.definitions, _class.symbol);
13179          }
13180          if(inCompiler)
13181          {
13182             // Free class data...
13183             ast->Remove(external);
13184             delete external;
13185          }
13186       }
13187       else if(external.type == nameSpaceExternal)
13188       {
13189          thisNameSpace = external.id.string;
13190       }
13191    }
13192    currentClass = null;
13193    thisNameSpace = null;
13194    curExternal = null;
13195
13196    delete temp.symbol;
13197    delete temp;
13198 }