sdk: Fixed many warnings and related problems
[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(prop)
10173                {
10174                   exp.member.memberType = propertyMember;
10175                   if(!prop.dataType)
10176                      ProcessPropertyType(prop);
10177                   exp.expType = prop.dataType;
10178                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10179                   {
10180                      Type type { };
10181                      CopyTypeInto(type, exp.expType);
10182                      type.refCount = 1;
10183                      type.constant = true;
10184                      exp.expType = type;
10185                   }
10186                   else if(prop.dataType)
10187                      prop.dataType.refCount++;
10188                }
10189                else if(member)
10190                {
10191                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10192                   {
10193                      FreeExpContents(exp);
10194                      exp.type = identifierExp;
10195                      exp.identifier = MkIdentifier("class");
10196                      ProcessExpressionType(exp);
10197                      return;
10198                   }
10199
10200                   exp.member.memberType = dataMember;
10201                   DeclareStruct(_class.fullName, false);
10202                   if(!member.dataType)
10203                   {
10204                      Context context = SetupTemplatesContext(_class);
10205                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10206                      FinishTemplatesContext(context);
10207                   }
10208                   exp.expType = member.dataType;
10209                   if(member.dataType) member.dataType.refCount++;
10210                }
10211                else if(revConvert)
10212                {
10213                   exp.member.memberType = reverseConversionMember;
10214                   exp.expType = MkClassType(revConvert._class.fullName);
10215                }
10216                else if(method)
10217                {
10218                   //if(inCompiler)
10219                   {
10220                      /*if(id._class)
10221                      {
10222                         exp.type = identifierExp;
10223                         exp.identifier = exp.member.member;
10224                      }
10225                      else*/
10226                         exp.member.memberType = methodMember;
10227                   }
10228                   if(!method.dataType)
10229                      ProcessMethodType(method);
10230                   exp.expType = Type
10231                   {
10232                      refCount = 1;
10233                      kind = methodType;
10234                      method = method;
10235                   };
10236
10237                   // Tricky spot here... To use instance versus class virtual table
10238                   // Put it back to what it was... What did we break?
10239
10240                   // Had to put it back for overriding Main of Thread global instance
10241
10242                   //exp.expType.methodClass = _class;
10243                   exp.expType.methodClass = (id && id._class) ? _class : null;
10244
10245                   // Need the actual class used for templated classes
10246                   exp.expType.usedClass = _class;
10247                }
10248                else if(!classProp)
10249                {
10250                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10251                   {
10252                      FreeExpContents(exp);
10253                      exp.type = identifierExp;
10254                      exp.identifier = MkIdentifier("class");
10255                      FreeType(exp.expType);
10256                      exp.expType = MkClassType("ecere::com::Class");
10257                      return;
10258                   }
10259                   yylloc = exp.member.member.loc;
10260                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10261                   if(inCompiler)
10262                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10263                }
10264
10265                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10266                {
10267                   Class tClass;
10268
10269                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10270                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10271
10272                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10273                   {
10274                      int id = 0;
10275                      ClassTemplateParameter curParam = null;
10276                      Class sClass;
10277
10278                      for(sClass = tClass; sClass; sClass = sClass.base)
10279                      {
10280                         id = 0;
10281                         if(sClass.templateClass) sClass = sClass.templateClass;
10282                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10283                         {
10284                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10285                            {
10286                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10287                                  id += sClass.templateParams.count;
10288                               break;
10289                            }
10290                            id++;
10291                         }
10292                         if(curParam) break;
10293                      }
10294
10295                      if(curParam && tClass.templateArgs[id].dataTypeString)
10296                      {
10297                         ClassTemplateArgument arg = tClass.templateArgs[id];
10298                         Context context = SetupTemplatesContext(tClass);
10299                         bool constant = exp.expType.constant;
10300                         /*if(!arg.dataType)
10301                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10302                         FreeType(exp.expType);
10303
10304                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10305                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10306                         else if(exp.expType.kind == pointerType)
10307                         {
10308                            Type t = exp.expType.type;
10309                            while(t.kind == pointerType) t = t.type;
10310                            if(constant) t.constant = constant;
10311                         }
10312                         if(exp.expType)
10313                         {
10314                            if(exp.expType.kind == thisClassType)
10315                            {
10316                               FreeType(exp.expType);
10317                               exp.expType = ReplaceThisClassType(_class);
10318                            }
10319
10320                            if(tClass.templateClass && (exp.expType.kind != templateType || (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType))))
10321                               exp.expType.passAsTemplate = true;
10322                            //exp.expType.refCount++;
10323                            if(!exp.destType)
10324                            {
10325                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10326                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10327                               else if(exp.destType.kind == pointerType)
10328                               {
10329                                  Type t = exp.destType.type;
10330                                  while(t.kind == pointerType) t = t.type;
10331                                  if(constant) t.constant = constant;
10332                               }
10333
10334                               //exp.destType.refCount++;
10335
10336                               if(exp.destType.kind == thisClassType)
10337                               {
10338                                  FreeType(exp.destType);
10339                                  exp.destType = ReplaceThisClassType(_class);
10340                               }
10341                            }
10342                         }
10343                         FinishTemplatesContext(context);
10344                      }
10345                   }
10346                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10347                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10348                   {
10349                      int id = 0;
10350                      ClassTemplateParameter curParam = null;
10351                      Class sClass;
10352
10353                      for(sClass = tClass; sClass; sClass = sClass.base)
10354                      {
10355                         id = 0;
10356                         if(sClass.templateClass) sClass = sClass.templateClass;
10357                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10358                         {
10359                            if(curParam.type == TemplateParameterType::type &&
10360                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10361                            {
10362                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10363                                  id += sClass.templateParams.count;
10364                               break;
10365                            }
10366                            id++;
10367                         }
10368                         if(curParam) break;
10369                      }
10370
10371                      if(curParam)
10372                      {
10373                         ClassTemplateArgument arg = tClass.templateArgs[id];
10374                         Context context = SetupTemplatesContext(tClass);
10375                         Type basicType;
10376                         /*if(!arg.dataType)
10377                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10378
10379                         basicType = ProcessTypeString(arg.dataTypeString, false);
10380                         if(basicType)
10381                         {
10382                            if(basicType.kind == thisClassType)
10383                            {
10384                               FreeType(basicType);
10385                               basicType = ReplaceThisClassType(_class);
10386                            }
10387
10388                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10389                            if(tClass.templateClass)
10390                               basicType.passAsTemplate = true;
10391                            */
10392
10393                            FreeType(exp.expType);
10394
10395                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10396                            //exp.expType.refCount++;
10397                            if(!exp.destType)
10398                            {
10399                               exp.destType = exp.expType;
10400                               exp.destType.refCount++;
10401                            }
10402
10403                            {
10404                               Expression newExp { };
10405                               OldList * specs = MkList();
10406                               Declarator decl;
10407                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10408                               *newExp = *exp;
10409                               if(exp.destType) exp.destType.refCount++;
10410                               if(exp.expType)  exp.expType.refCount++;
10411                               exp.type = castExp;
10412                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10413                               exp.cast.exp = newExp;
10414                               //FreeType(exp.expType);
10415                               //exp.expType = null;
10416                               //ProcessExpressionType(sourceExp);
10417                            }
10418                         }
10419                         FinishTemplatesContext(context);
10420                      }
10421                   }
10422                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10423                   {
10424                      Class expClass = exp.expType._class.registered;
10425                      if(expClass)
10426                      {
10427                         Class cClass = null;
10428                         int p = 0;
10429                         int paramCount = 0;
10430                         int lastParam = -1;
10431                         char templateString[1024];
10432                         ClassTemplateParameter param;
10433                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10434                         while(cClass != expClass)
10435                         {
10436                            Class sClass;
10437                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10438                            cClass = sClass;
10439
10440                            for(param = cClass.templateParams.first; param; param = param.next)
10441                            {
10442                               Class cClassCur = null;
10443                               int cp = 0;
10444                               ClassTemplateParameter paramCur = null;
10445                               ClassTemplateArgument arg;
10446                               while(cClassCur != tClass && !paramCur)
10447                               {
10448                                  Class sClassCur;
10449                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10450                                  cClassCur = sClassCur;
10451
10452                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10453                                  {
10454                                     if(!strcmp(paramCur.name, param.name))
10455                                     {
10456
10457                                        break;
10458                                     }
10459                                     cp++;
10460                                  }
10461                               }
10462                               if(paramCur && paramCur.type == TemplateParameterType::type)
10463                                  arg = tClass.templateArgs[cp];
10464                               else
10465                                  arg = expClass.templateArgs[p];
10466
10467                               {
10468                                  char argument[256];
10469                                  argument[0] = '\0';
10470                                  /*if(arg.name)
10471                                  {
10472                                     strcat(argument, arg.name.string);
10473                                     strcat(argument, " = ");
10474                                  }*/
10475                                  switch(param.type)
10476                                  {
10477                                     case expression:
10478                                     {
10479                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10480                                        char expString[1024];
10481                                        OldList * specs = MkList();
10482                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10483                                        Expression exp;
10484                                        char * string = PrintHexUInt64(arg.expression.ui64);
10485                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10486                                        delete string;
10487
10488                                        ProcessExpressionType(exp);
10489                                        ComputeExpression(exp);
10490                                        expString[0] = '\0';
10491                                        PrintExpression(exp, expString);
10492                                        strcat(argument, expString);
10493                                        // delete exp;
10494                                        FreeExpression(exp);
10495                                        break;
10496                                     }
10497                                     case identifier:
10498                                     {
10499                                        strcat(argument, arg.member.name);
10500                                        break;
10501                                     }
10502                                     case TemplateParameterType::type:
10503                                     {
10504                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10505                                           strcat(argument, arg.dataTypeString);
10506                                        break;
10507                                     }
10508                                  }
10509                                  if(argument[0])
10510                                  {
10511                                     if(paramCount) strcat(templateString, ", ");
10512                                     if(lastParam != p - 1)
10513                                     {
10514                                        strcat(templateString, param.name);
10515                                        strcat(templateString, " = ");
10516                                     }
10517                                     strcat(templateString, argument);
10518                                     paramCount++;
10519                                     lastParam = p;
10520                                  }
10521                               }
10522                               p++;
10523                            }
10524                         }
10525                         {
10526                            int len = strlen(templateString);
10527                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10528                            templateString[len++] = '>';
10529                            templateString[len++] = '\0';
10530                         }
10531
10532                         FreeType(exp.expType);
10533                         {
10534                            Context context = SetupTemplatesContext(tClass);
10535                            exp.expType = ProcessTypeString(templateString, false);
10536                            FinishTemplatesContext(context);
10537                         }
10538                      }
10539                   }
10540                }
10541             }
10542             else
10543                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10544          }
10545          else if(type && (type.kind == structType || type.kind == unionType))
10546          {
10547             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10548             if(memberType)
10549             {
10550                exp.expType = memberType;
10551                if(memberType)
10552                   memberType.refCount++;
10553             }
10554          }
10555          else
10556          {
10557             char expString[10240];
10558             expString[0] = '\0';
10559             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10560             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10561          }
10562
10563          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10564          {
10565             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10566             {
10567                Identifier id = exp.member.member;
10568                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10569                if(_class)
10570                {
10571                   FreeType(exp.expType);
10572                   exp.expType = ReplaceThisClassType(_class);
10573                }
10574             }
10575          }
10576          yylloc = oldyylloc;
10577          break;
10578       }
10579       // Convert x->y into (*x).y
10580       case pointerExp:
10581       {
10582          Type destType = exp.destType;
10583
10584          // DOING THIS LATER NOW...
10585          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10586          {
10587             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10588             /* TODO: Name Space Fix ups
10589             if(!exp.member.member.classSym)
10590                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10591             */
10592          }
10593
10594          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10595          exp.type = memberExp;
10596          if(destType)
10597             destType.count++;
10598          ProcessExpressionType(exp);
10599          if(destType)
10600             destType.count--;
10601          break;
10602       }
10603       case classSizeExp:
10604       {
10605          //ComputeExpression(exp);
10606
10607          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10608          if(classSym && classSym.registered)
10609          {
10610             if(classSym.registered.type == noHeadClass)
10611             {
10612                char name[1024];
10613                name[0] = '\0';
10614                DeclareStruct(classSym.string, false);
10615                FreeSpecifier(exp._class);
10616                exp.type = typeSizeExp;
10617                FullClassNameCat(name, classSym.string, false);
10618                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10619             }
10620             else
10621             {
10622                if(classSym.registered.fixed)
10623                {
10624                   FreeSpecifier(exp._class);
10625                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10626                   exp.type = constantExp;
10627                }
10628                else
10629                {
10630                   char className[1024];
10631                   strcpy(className, "__ecereClass_");
10632                   FullClassNameCat(className, classSym.string, true);
10633                   //MangleClassName(className);
10634
10635                   DeclareClass(classSym, className);
10636
10637                   FreeExpContents(exp);
10638                   exp.type = pointerExp;
10639                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10640                   exp.member.member = MkIdentifier("structSize");
10641                }
10642             }
10643          }
10644
10645          exp.expType = Type
10646          {
10647             refCount = 1;
10648             kind = intSizeType;
10649          };
10650          // exp.isConstant = true;
10651          break;
10652       }
10653       case typeSizeExp:
10654       {
10655          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10656
10657          exp.expType = Type
10658          {
10659             refCount = 1;
10660             kind = intSizeType;
10661          };
10662          exp.isConstant = true;
10663
10664          DeclareType(type, false, false);
10665          FreeType(type);
10666          break;
10667       }
10668       case castExp:
10669       {
10670          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10671          type.count = 1;
10672          FreeType(exp.cast.exp.destType);
10673          exp.cast.exp.destType = type;
10674          type.refCount++;
10675          type.casted = true;
10676          ProcessExpressionType(exp.cast.exp);
10677          type.casted = false;
10678          type.count = 0;
10679          exp.expType = type;
10680          //type.refCount++;
10681
10682          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10683          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10684          {
10685             void * prev = exp.prev, * next = exp.next;
10686             Type expType = exp.cast.exp.destType;
10687             Expression castExp = exp.cast.exp;
10688             Type destType = exp.destType;
10689
10690             if(expType) expType.refCount++;
10691
10692             //FreeType(exp.destType);
10693             FreeType(exp.expType);
10694             FreeTypeName(exp.cast.typeName);
10695
10696             *exp = *castExp;
10697             FreeType(exp.expType);
10698             FreeType(exp.destType);
10699
10700             exp.expType = expType;
10701             exp.destType = destType;
10702
10703             delete castExp;
10704
10705             exp.prev = prev;
10706             exp.next = next;
10707
10708          }
10709          else
10710          {
10711             exp.isConstant = exp.cast.exp.isConstant;
10712          }
10713          //FreeType(type);
10714          break;
10715       }
10716       case extensionInitializerExp:
10717       {
10718          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10719          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10720          // ProcessInitializer(exp.initializer.initializer, type);
10721          exp.expType = type;
10722          break;
10723       }
10724       case vaArgExp:
10725       {
10726          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10727          ProcessExpressionType(exp.vaArg.exp);
10728          exp.expType = type;
10729          break;
10730       }
10731       case conditionExp:
10732       {
10733          Expression e;
10734          exp.isConstant = true;
10735
10736          FreeType(exp.cond.cond.destType);
10737          exp.cond.cond.destType = MkClassType("bool");
10738          exp.cond.cond.destType.truth = true;
10739          ProcessExpressionType(exp.cond.cond);
10740          if(!exp.cond.cond.isConstant)
10741             exp.isConstant = false;
10742          for(e = exp.cond.exp->first; e; e = e.next)
10743          {
10744             if(!e.next)
10745             {
10746                FreeType(e.destType);
10747                e.destType = exp.destType;
10748                if(e.destType) e.destType.refCount++;
10749             }
10750             ProcessExpressionType(e);
10751             if(!e.next)
10752             {
10753                exp.expType = e.expType;
10754                if(e.expType) e.expType.refCount++;
10755             }
10756             if(!e.isConstant)
10757                exp.isConstant = false;
10758          }
10759
10760          FreeType(exp.cond.elseExp.destType);
10761          // Added this check if we failed to find an expType
10762          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10763
10764          // Reversed it...
10765          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10766
10767          if(exp.cond.elseExp.destType)
10768             exp.cond.elseExp.destType.refCount++;
10769          ProcessExpressionType(exp.cond.elseExp);
10770
10771          // FIXED THIS: Was done before calling process on elseExp
10772          if(!exp.cond.elseExp.isConstant)
10773             exp.isConstant = false;
10774          break;
10775       }
10776       case extensionCompoundExp:
10777       {
10778          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10779          {
10780             Statement last = exp.compound.compound.statements->last;
10781             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10782             {
10783                ((Expression)last.expressions->last).destType = exp.destType;
10784                if(exp.destType)
10785                   exp.destType.refCount++;
10786             }
10787             ProcessStatement(exp.compound);
10788             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10789             if(exp.expType)
10790                exp.expType.refCount++;
10791          }
10792          break;
10793       }
10794       case classExp:
10795       {
10796          Specifier spec = exp._classExp.specifiers->first;
10797          if(spec && spec.type == nameSpecifier)
10798          {
10799             exp.expType = MkClassType(spec.name);
10800             exp.expType.kind = subClassType;
10801             exp.byReference = true;
10802          }
10803          else
10804          {
10805             exp.expType = MkClassType("ecere::com::Class");
10806             exp.byReference = true;
10807          }
10808          break;
10809       }
10810       case classDataExp:
10811       {
10812          Class _class = thisClass ? thisClass : currentClass;
10813          if(_class)
10814          {
10815             Identifier id = exp.classData.id;
10816             char structName[1024];
10817             Expression classExp;
10818             strcpy(structName, "__ecereClassData_");
10819             FullClassNameCat(structName, _class.fullName, false);
10820             exp.type = pointerExp;
10821             exp.member.member = id;
10822             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10823                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10824             else
10825                classExp = MkExpIdentifier(MkIdentifier("class"));
10826
10827             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10828                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10829                   MkExpBrackets(MkListOne(MkExpOp(
10830                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10831                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10832                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10833                      )));
10834
10835             ProcessExpressionType(exp);
10836             return;
10837          }
10838          break;
10839       }
10840       case arrayExp:
10841       {
10842          Type type = null;
10843          const char * typeString = null;
10844          char typeStringBuf[1024];
10845          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10846             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10847          {
10848             Class templateClass = exp.destType._class.registered;
10849             typeString = templateClass.templateArgs[2].dataTypeString;
10850          }
10851          else if(exp.list)
10852          {
10853             // Guess type from expressions in the array
10854             Expression e;
10855             for(e = exp.list->first; e; e = e.next)
10856             {
10857                ProcessExpressionType(e);
10858                if(e.expType)
10859                {
10860                   if(!type) { type = e.expType; type.refCount++; }
10861                   else
10862                   {
10863                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10864                      if(!MatchTypeExpression(e, type, null, false, true))
10865                      {
10866                         FreeType(type);
10867                         type = e.expType;
10868                         e.expType = null;
10869
10870                         e = exp.list->first;
10871                         ProcessExpressionType(e);
10872                         if(e.expType)
10873                         {
10874                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10875                            if(!MatchTypeExpression(e, type, null, false, true))
10876                            {
10877                               FreeType(e.expType);
10878                               e.expType = null;
10879                               FreeType(type);
10880                               type = null;
10881                               break;
10882                            }
10883                         }
10884                      }
10885                   }
10886                   if(e.expType)
10887                   {
10888                      FreeType(e.expType);
10889                      e.expType = null;
10890                   }
10891                }
10892             }
10893             if(type)
10894             {
10895                typeStringBuf[0] = '\0';
10896                PrintTypeNoConst(type, typeStringBuf, false, true);
10897                typeString = typeStringBuf;
10898                FreeType(type);
10899                type = null;
10900             }
10901          }
10902          if(typeString)
10903          {
10904             /*
10905             (Container)& (struct BuiltInContainer)
10906             {
10907                ._vTbl = class(BuiltInContainer)._vTbl,
10908                ._class = class(BuiltInContainer),
10909                .refCount = 0,
10910                .data = (int[]){ 1, 7, 3, 4, 5 },
10911                .count = 5,
10912                .type = class(int),
10913             }
10914             */
10915             char templateString[1024];
10916             OldList * initializers = MkList();
10917             OldList * structInitializers = MkList();
10918             OldList * specs = MkList();
10919             Expression expExt;
10920             Declarator decl = SpecDeclFromString(typeString, specs, null);
10921             sprintf(templateString, "Container<%s>", typeString);
10922
10923             if(exp.list)
10924             {
10925                Expression e;
10926                type = ProcessTypeString(typeString, false);
10927                while((e = exp.list->first))
10928                {
10929                   exp.list->Remove(e);
10930                   e.destType = type;
10931                   type.refCount++;
10932                   ProcessExpressionType(e);
10933                   ListAdd(initializers, MkInitializerAssignment(e));
10934                }
10935                FreeType(type);
10936                delete exp.list;
10937             }
10938
10939             DeclareStruct("ecere::com::BuiltInContainer", false);
10940
10941             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10942                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10943             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10944                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10945             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10946                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10947             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10948                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10949                MkInitializerList(initializers))));
10950                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10951             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10952                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10953             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10954                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10955             exp.expType = ProcessTypeString(templateString, false);
10956             exp.type = bracketsExp;
10957             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10958                MkExpOp(null, '&',
10959                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10960                   MkInitializerList(structInitializers)))));
10961             ProcessExpressionType(expExt);
10962          }
10963          else
10964          {
10965             exp.expType = ProcessTypeString("Container", false);
10966             Compiler_Error($"Couldn't determine type of array elements\n");
10967          }
10968          break;
10969       }
10970    }
10971
10972    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10973    {
10974       FreeType(exp.expType);
10975       exp.expType = ReplaceThisClassType(thisClass);
10976    }
10977
10978    // Resolve structures here
10979    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10980    {
10981       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10982       // TODO: Fix members reference...
10983       if(symbol)
10984       {
10985          if(exp.expType.kind != enumType)
10986          {
10987             Type member;
10988             String enumName = CopyString(exp.expType.enumName);
10989
10990             // Fixed a memory leak on self-referencing C structs typedefs
10991             // by instantiating a new type rather than simply copying members
10992             // into exp.expType
10993             FreeType(exp.expType);
10994             exp.expType = Type { };
10995             exp.expType.kind = symbol.type.kind;
10996             exp.expType.refCount++;
10997             exp.expType.enumName = enumName;
10998
10999             exp.expType.members = symbol.type.members;
11000             for(member = symbol.type.members.first; member; member = member.next)
11001                member.refCount++;
11002          }
11003          else
11004          {
11005             NamedLink member;
11006             for(member = symbol.type.members.first; member; member = member.next)
11007             {
11008                NamedLink value { name = CopyString(member.name) };
11009                exp.expType.members.Add(value);
11010             }
11011          }
11012       }
11013    }
11014
11015    yylloc = exp.loc;
11016    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11017    else if(exp.destType && !exp.destType.keepCast)
11018    {
11019       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11020          exp.needTemplateCast = 1;
11021
11022       if(exp.destType.kind == voidType);
11023       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11024       {
11025          if(!exp.destType.count || unresolved)
11026          {
11027             if(!exp.expType)
11028             {
11029                yylloc = exp.loc;
11030                if(exp.destType.kind != ellipsisType)
11031                {
11032                   char type2[1024];
11033                   type2[0] = '\0';
11034                   if(inCompiler)
11035                   {
11036                      char expString[10240];
11037                      expString[0] = '\0';
11038
11039                      PrintType(exp.destType, type2, false, true);
11040
11041                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11042                      if(unresolved)
11043                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11044                      else if(exp.type != dummyExp)
11045                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11046                   }
11047                }
11048                else
11049                {
11050                   char expString[10240] ;
11051                   expString[0] = '\0';
11052                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11053
11054                   if(unresolved)
11055                      Compiler_Error($"unresolved identifier %s\n", expString);
11056                   else if(exp.type != dummyExp)
11057                      Compiler_Error($"couldn't determine type of %s\n", expString);
11058                }
11059             }
11060             else
11061             {
11062                char type1[1024];
11063                char type2[1024];
11064                type1[0] = '\0';
11065                type2[0] = '\0';
11066                if(inCompiler)
11067                {
11068                   PrintType(exp.expType, type1, false, true);
11069                   PrintType(exp.destType, type2, false, true);
11070                }
11071
11072                //CheckExpressionType(exp, exp.destType, false);
11073
11074                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11075                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11076                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11077                else
11078                {
11079                   char expString[10240];
11080                   expString[0] = '\0';
11081                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11082
11083 #ifdef _DEBUG
11084                   CheckExpressionType(exp, exp.destType, false, true);
11085 #endif
11086                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11087                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
11088                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11089
11090                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11091                   FreeType(exp.expType);
11092                   exp.destType.refCount++;
11093                   exp.expType = exp.destType;
11094                }
11095             }
11096          }
11097       }
11098       /*else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
11099       {
11100          Expression newExp { };
11101          char typeString[1024];
11102          OldList * specs = MkList();
11103          Declarator decl;
11104
11105          typeString[0] = '\0';
11106
11107          *newExp = *exp;
11108
11109          if(exp.expType)  exp.expType.refCount++;
11110          if(exp.expType)  exp.expType.refCount++;
11111          exp.type = castExp;
11112          newExp.destType = exp.expType;
11113
11114          PrintType(exp.expType, typeString, false, false);
11115          decl = SpecDeclFromString(typeString, specs, null);
11116
11117          exp.cast.typeName = MkTypeName(specs, decl);
11118          exp.cast.exp = newExp;
11119       }*/
11120    }
11121    else if(unresolved)
11122    {
11123       if(exp.identifier._class && exp.identifier._class.name)
11124          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11125       else if(exp.identifier.string && exp.identifier.string[0])
11126          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11127    }
11128    else if(!exp.expType && exp.type != dummyExp)
11129    {
11130       char expString[10240];
11131       expString[0] = '\0';
11132       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11133       Compiler_Error($"couldn't determine type of %s\n", expString);
11134    }
11135
11136    // Let's try to support any_object & typed_object here:
11137    if(inCompiler)
11138       ApplyAnyObjectLogic(exp);
11139
11140    // Mark nohead classes as by reference, unless we're casting them to an integral type
11141    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11142       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11143          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11144           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11145    {
11146       exp.byReference = true;
11147    }
11148    yylloc = oldyylloc;
11149 }
11150
11151 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11152 {
11153    // THIS CODE WILL FIND NEXT MEMBER...
11154    if(*curMember)
11155    {
11156       *curMember = (*curMember).next;
11157
11158       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11159       {
11160          *curMember = subMemberStack[--(*subMemberStackPos)];
11161          *curMember = (*curMember).next;
11162       }
11163
11164       // SKIP ALL PROPERTIES HERE...
11165       while((*curMember) && (*curMember).isProperty)
11166          *curMember = (*curMember).next;
11167
11168       if(subMemberStackPos)
11169       {
11170          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11171          {
11172             subMemberStack[(*subMemberStackPos)++] = *curMember;
11173
11174             *curMember = (*curMember).members.first;
11175             while(*curMember && (*curMember).isProperty)
11176                *curMember = (*curMember).next;
11177          }
11178       }
11179    }
11180    while(!*curMember)
11181    {
11182       if(!*curMember)
11183       {
11184          if(subMemberStackPos && *subMemberStackPos)
11185          {
11186             *curMember = subMemberStack[--(*subMemberStackPos)];
11187             *curMember = (*curMember).next;
11188          }
11189          else
11190          {
11191             Class lastCurClass = *curClass;
11192
11193             if(*curClass == _class) break;     // REACHED THE END
11194
11195             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11196             *curMember = (*curClass).membersAndProperties.first;
11197          }
11198
11199          while((*curMember) && (*curMember).isProperty)
11200             *curMember = (*curMember).next;
11201          if(subMemberStackPos)
11202          {
11203             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11204             {
11205                subMemberStack[(*subMemberStackPos)++] = *curMember;
11206
11207                *curMember = (*curMember).members.first;
11208                while(*curMember && (*curMember).isProperty)
11209                   *curMember = (*curMember).next;
11210             }
11211          }
11212       }
11213    }
11214 }
11215
11216
11217 static void ProcessInitializer(Initializer init, Type type)
11218 {
11219    switch(init.type)
11220    {
11221       case expInitializer:
11222          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11223          {
11224             // TESTING THIS FOR SHUTTING = 0 WARNING
11225             if(init.exp && !init.exp.destType)
11226             {
11227                FreeType(init.exp.destType);
11228                init.exp.destType = type;
11229                if(type) type.refCount++;
11230             }
11231             if(init.exp)
11232             {
11233                ProcessExpressionType(init.exp);
11234                init.isConstant = init.exp.isConstant;
11235             }
11236             break;
11237          }
11238          else
11239          {
11240             Expression exp = init.exp;
11241             Instantiation inst = exp.instance;
11242             MembersInit members;
11243
11244             init.type = listInitializer;
11245             init.list = MkList();
11246
11247             if(inst.members)
11248             {
11249                for(members = inst.members->first; members; members = members.next)
11250                {
11251                   if(members.type == dataMembersInit)
11252                   {
11253                      MemberInit member;
11254                      for(member = members.dataMembers->first; member; member = member.next)
11255                      {
11256                         ListAdd(init.list, member.initializer);
11257                         member.initializer = null;
11258                      }
11259                   }
11260                   // Discard all MembersInitMethod
11261                }
11262             }
11263             FreeExpression(exp);
11264          }
11265       case listInitializer:
11266       {
11267          Initializer i;
11268          Type initializerType = null;
11269          Class curClass = null;
11270          DataMember curMember = null;
11271          DataMember subMemberStack[256];
11272          int subMemberStackPos = 0;
11273
11274          if(type && type.kind == arrayType)
11275             initializerType = Dereference(type);
11276          else if(type && (type.kind == structType || type.kind == unionType))
11277             initializerType = type.members.first;
11278
11279          for(i = init.list->first; i; i = i.next)
11280          {
11281             if(type && type.kind == classType && type._class && type._class.registered)
11282             {
11283                // 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)
11284                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11285                // TODO: Generate error on initializing a private data member this way from another module...
11286                if(curMember)
11287                {
11288                   if(!curMember.dataType)
11289                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11290                   initializerType = curMember.dataType;
11291                }
11292             }
11293             ProcessInitializer(i, initializerType);
11294             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11295                initializerType = initializerType.next;
11296             if(!i.isConstant)
11297                init.isConstant = false;
11298          }
11299
11300          if(type && type.kind == arrayType)
11301             FreeType(initializerType);
11302
11303          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11304          {
11305             Compiler_Error($"Assigning list initializer to non list\n");
11306          }
11307          break;
11308       }
11309    }
11310 }
11311
11312 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11313 {
11314    switch(spec.type)
11315    {
11316       case baseSpecifier:
11317       {
11318          if(spec.specifier == THISCLASS)
11319          {
11320             if(thisClass)
11321             {
11322                spec.type = nameSpecifier;
11323                spec.name = ReplaceThisClass(thisClass);
11324                spec.symbol = FindClass(spec.name);
11325                ProcessSpecifier(spec, declareStruct);
11326             }
11327          }
11328          break;
11329       }
11330       case nameSpecifier:
11331       {
11332          Symbol symbol = FindType(curContext, spec.name);
11333          if(symbol)
11334             DeclareType(symbol.type, true, true);
11335          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11336             DeclareStruct(spec.name, false);
11337          break;
11338       }
11339       case enumSpecifier:
11340       {
11341          Enumerator e;
11342          if(spec.list)
11343          {
11344             for(e = spec.list->first; e; e = e.next)
11345             {
11346                if(e.exp)
11347                   ProcessExpressionType(e.exp);
11348             }
11349          }
11350          break;
11351       }
11352       case structSpecifier:
11353       case unionSpecifier:
11354       {
11355          if(spec.definitions)
11356          {
11357             //ClassDef def;
11358             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11359             //if(symbol)
11360                ProcessClass(spec.definitions, symbol);
11361             /*else
11362             {
11363                for(def = spec.definitions->first; def; def = def.next)
11364                {
11365                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11366                      ProcessDeclaration(def.decl);
11367                }
11368             }*/
11369          }
11370          break;
11371       }
11372       /*
11373       case classSpecifier:
11374       {
11375          Symbol classSym = FindClass(spec.name);
11376          if(classSym && classSym.registered && classSym.registered.type == structClass)
11377             DeclareStruct(spec.name, false);
11378          break;
11379       }
11380       */
11381    }
11382 }
11383
11384
11385 static void ProcessDeclarator(Declarator decl)
11386 {
11387    switch(decl.type)
11388    {
11389       case identifierDeclarator:
11390          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11391          {
11392             FreeSpecifier(decl.identifier._class);
11393             decl.identifier._class = null;
11394          }
11395          break;
11396       case arrayDeclarator:
11397          if(decl.array.exp)
11398             ProcessExpressionType(decl.array.exp);
11399       case structDeclarator:
11400       case bracketsDeclarator:
11401       case functionDeclarator:
11402       case pointerDeclarator:
11403       case extendedDeclarator:
11404       case extendedDeclaratorEnd:
11405          if(decl.declarator)
11406             ProcessDeclarator(decl.declarator);
11407          if(decl.type == functionDeclarator)
11408          {
11409             Identifier id = GetDeclId(decl);
11410             if(id && id._class)
11411             {
11412                TypeName param
11413                {
11414                   qualifiers = MkListOne(id._class);
11415                   declarator = null;
11416                };
11417                if(!decl.function.parameters)
11418                   decl.function.parameters = MkList();
11419                decl.function.parameters->Insert(null, param);
11420                id._class = null;
11421             }
11422             if(decl.function.parameters)
11423             {
11424                TypeName param;
11425
11426                for(param = decl.function.parameters->first; param; param = param.next)
11427                {
11428                   if(param.qualifiers && param.qualifiers->first)
11429                   {
11430                      Specifier spec = param.qualifiers->first;
11431                      if(spec && spec.specifier == TYPED_OBJECT)
11432                      {
11433                         Declarator d = param.declarator;
11434                         TypeName newParam
11435                         {
11436                            qualifiers = MkListOne(MkSpecifier(VOID));
11437                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11438                         };
11439                         if(d.type != pointerDeclarator)
11440                            newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11441
11442                         FreeList(param.qualifiers, FreeSpecifier);
11443
11444                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11445                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11446
11447                         decl.function.parameters->Insert(param, newParam);
11448                         param = newParam;
11449                      }
11450                      else if(spec && spec.specifier == ANY_OBJECT)
11451                      {
11452                         Declarator d = param.declarator;
11453
11454                         FreeList(param.qualifiers, FreeSpecifier);
11455
11456                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11457                         if(d.type != pointerDeclarator)
11458                            param.qualifiers->Insert(null, MkSpecifier(CONST));
11459                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11460                      }
11461                      else if(spec.specifier == THISCLASS)
11462                      {
11463                         if(thisClass)
11464                         {
11465                            spec.type = nameSpecifier;
11466                            spec.name = ReplaceThisClass(thisClass);
11467                            spec.symbol = FindClass(spec.name);
11468                            ProcessSpecifier(spec, false);
11469                         }
11470                      }
11471                   }
11472
11473                   if(param.declarator)
11474                      ProcessDeclarator(param.declarator);
11475                }
11476             }
11477          }
11478          break;
11479    }
11480 }
11481
11482 static void ProcessDeclaration(Declaration decl)
11483 {
11484    yylloc = decl.loc;
11485    switch(decl.type)
11486    {
11487       case initDeclaration:
11488       {
11489          bool declareStruct = false;
11490          /*
11491          lineNum = decl.pos.line;
11492          column = decl.pos.col;
11493          */
11494
11495          if(decl.declarators)
11496          {
11497             InitDeclarator d;
11498
11499             for(d = decl.declarators->first; d; d = d.next)
11500             {
11501                Type type, subType;
11502                ProcessDeclarator(d.declarator);
11503
11504                type = ProcessType(decl.specifiers, d.declarator);
11505
11506                if(d.initializer)
11507                {
11508                   ProcessInitializer(d.initializer, type);
11509
11510                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11511
11512                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11513                      d.initializer.exp.type == instanceExp)
11514                   {
11515                      if(type.kind == classType && type._class ==
11516                         d.initializer.exp.expType._class)
11517                      {
11518                         Instantiation inst = d.initializer.exp.instance;
11519                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11520
11521                         d.initializer.exp.instance = null;
11522                         if(decl.specifiers)
11523                            FreeList(decl.specifiers, FreeSpecifier);
11524                         FreeList(decl.declarators, FreeInitDeclarator);
11525
11526                         d = null;
11527
11528                         decl.type = instDeclaration;
11529                         decl.inst = inst;
11530                      }
11531                   }
11532                }
11533                for(subType = type; subType;)
11534                {
11535                   if(subType.kind == classType)
11536                   {
11537                      declareStruct = true;
11538                      break;
11539                   }
11540                   else if(subType.kind == pointerType)
11541                      break;
11542                   else if(subType.kind == arrayType)
11543                      subType = subType.arrayType;
11544                   else
11545                      break;
11546                }
11547
11548                FreeType(type);
11549                if(!d) break;
11550             }
11551          }
11552
11553          if(decl.specifiers)
11554          {
11555             Specifier s;
11556             for(s = decl.specifiers->first; s; s = s.next)
11557             {
11558                ProcessSpecifier(s, declareStruct);
11559             }
11560          }
11561          break;
11562       }
11563       case instDeclaration:
11564       {
11565          ProcessInstantiationType(decl.inst);
11566          break;
11567       }
11568       case structDeclaration:
11569       {
11570          Specifier spec;
11571          Declarator d;
11572          bool declareStruct = false;
11573
11574          if(decl.declarators)
11575          {
11576             for(d = decl.declarators->first; d; d = d.next)
11577             {
11578                Type type = ProcessType(decl.specifiers, d.declarator);
11579                Type subType;
11580                ProcessDeclarator(d);
11581                for(subType = type; subType;)
11582                {
11583                   if(subType.kind == classType)
11584                   {
11585                      declareStruct = true;
11586                      break;
11587                   }
11588                   else if(subType.kind == pointerType)
11589                      break;
11590                   else if(subType.kind == arrayType)
11591                      subType = subType.arrayType;
11592                   else
11593                      break;
11594                }
11595                FreeType(type);
11596             }
11597          }
11598          if(decl.specifiers)
11599          {
11600             for(spec = decl.specifiers->first; spec; spec = spec.next)
11601                ProcessSpecifier(spec, declareStruct);
11602          }
11603          break;
11604       }
11605    }
11606 }
11607
11608 static FunctionDefinition curFunction;
11609
11610 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11611 {
11612    char propName[1024], propNameM[1024];
11613    char getName[1024], setName[1024];
11614    OldList * args;
11615
11616    DeclareProperty(prop, setName, getName);
11617
11618    // eInstance_FireWatchers(object, prop);
11619    strcpy(propName, "__ecereProp_");
11620    FullClassNameCat(propName, prop._class.fullName, false);
11621    strcat(propName, "_");
11622    // strcat(propName, prop.name);
11623    FullClassNameCat(propName, prop.name, true);
11624    //MangleClassName(propName);
11625
11626    strcpy(propNameM, "__ecerePropM_");
11627    FullClassNameCat(propNameM, prop._class.fullName, false);
11628    strcat(propNameM, "_");
11629    // strcat(propNameM, prop.name);
11630    FullClassNameCat(propNameM, prop.name, true);
11631    //MangleClassName(propNameM);
11632
11633    if(prop.isWatchable)
11634    {
11635       args = MkList();
11636       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11637       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11638       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11639
11640       args = MkList();
11641       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11642       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11643       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11644    }
11645
11646
11647    {
11648       args = MkList();
11649       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11650       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11651       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11652
11653       args = MkList();
11654       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11655       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11656       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11657    }
11658
11659    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11660       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11661       curFunction.propSet.fireWatchersDone = true;
11662 }
11663
11664 static void ProcessStatement(Statement stmt)
11665 {
11666    yylloc = stmt.loc;
11667    /*
11668    lineNum = stmt.pos.line;
11669    column = stmt.pos.col;
11670    */
11671    switch(stmt.type)
11672    {
11673       case labeledStmt:
11674          ProcessStatement(stmt.labeled.stmt);
11675          break;
11676       case caseStmt:
11677          // This expression should be constant...
11678          if(stmt.caseStmt.exp)
11679          {
11680             FreeType(stmt.caseStmt.exp.destType);
11681             stmt.caseStmt.exp.destType = curSwitchType;
11682             if(curSwitchType) curSwitchType.refCount++;
11683             ProcessExpressionType(stmt.caseStmt.exp);
11684             ComputeExpression(stmt.caseStmt.exp);
11685          }
11686          if(stmt.caseStmt.stmt)
11687             ProcessStatement(stmt.caseStmt.stmt);
11688          break;
11689       case compoundStmt:
11690       {
11691          if(stmt.compound.context)
11692          {
11693             Declaration decl;
11694             Statement s;
11695
11696             Statement prevCompound = curCompound;
11697             Context prevContext = curContext;
11698
11699             if(!stmt.compound.isSwitch)
11700                curCompound = stmt;
11701             curContext = stmt.compound.context;
11702
11703             if(stmt.compound.declarations)
11704             {
11705                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11706                   ProcessDeclaration(decl);
11707             }
11708             if(stmt.compound.statements)
11709             {
11710                for(s = stmt.compound.statements->first; s; s = s.next)
11711                   ProcessStatement(s);
11712             }
11713
11714             curContext = prevContext;
11715             curCompound = prevCompound;
11716          }
11717          break;
11718       }
11719       case expressionStmt:
11720       {
11721          Expression exp;
11722          if(stmt.expressions)
11723          {
11724             for(exp = stmt.expressions->first; exp; exp = exp.next)
11725                ProcessExpressionType(exp);
11726          }
11727          break;
11728       }
11729       case ifStmt:
11730       {
11731          Expression exp;
11732
11733          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11734          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11735          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11736          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11737          {
11738             ProcessExpressionType(exp);
11739          }
11740          if(stmt.ifStmt.stmt)
11741             ProcessStatement(stmt.ifStmt.stmt);
11742          if(stmt.ifStmt.elseStmt)
11743             ProcessStatement(stmt.ifStmt.elseStmt);
11744          break;
11745       }
11746       case switchStmt:
11747       {
11748          Type oldSwitchType = curSwitchType;
11749          if(stmt.switchStmt.exp)
11750          {
11751             Expression exp;
11752             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11753             {
11754                if(!exp.next)
11755                {
11756                   /*
11757                   Type destType
11758                   {
11759                      kind = intType;
11760                      refCount = 1;
11761                   };
11762                   e.exp.destType = destType;
11763                   */
11764
11765                   ProcessExpressionType(exp);
11766                }
11767                if(!exp.next)
11768                   curSwitchType = exp.expType;
11769             }
11770          }
11771          ProcessStatement(stmt.switchStmt.stmt);
11772          curSwitchType = oldSwitchType;
11773          break;
11774       }
11775       case whileStmt:
11776       {
11777          if(stmt.whileStmt.exp)
11778          {
11779             Expression exp;
11780
11781             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11782             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11783             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11784             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11785             {
11786                ProcessExpressionType(exp);
11787             }
11788          }
11789          if(stmt.whileStmt.stmt)
11790             ProcessStatement(stmt.whileStmt.stmt);
11791          break;
11792       }
11793       case doWhileStmt:
11794       {
11795          if(stmt.doWhile.exp)
11796          {
11797             Expression exp;
11798
11799             if(stmt.doWhile.exp->last)
11800             {
11801                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11802                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11803                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11804             }
11805             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11806             {
11807                ProcessExpressionType(exp);
11808             }
11809          }
11810          if(stmt.doWhile.stmt)
11811             ProcessStatement(stmt.doWhile.stmt);
11812          break;
11813       }
11814       case forStmt:
11815       {
11816          Expression exp;
11817          if(stmt.forStmt.init)
11818             ProcessStatement(stmt.forStmt.init);
11819
11820          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11821          {
11822             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11823             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11824             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11825          }
11826
11827          if(stmt.forStmt.check)
11828             ProcessStatement(stmt.forStmt.check);
11829          if(stmt.forStmt.increment)
11830          {
11831             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11832                ProcessExpressionType(exp);
11833          }
11834
11835          if(stmt.forStmt.stmt)
11836             ProcessStatement(stmt.forStmt.stmt);
11837          break;
11838       }
11839       case forEachStmt:
11840       {
11841          Identifier id = stmt.forEachStmt.id;
11842          OldList * exp = stmt.forEachStmt.exp;
11843          OldList * filter = stmt.forEachStmt.filter;
11844          Statement block = stmt.forEachStmt.stmt;
11845          char iteratorType[1024];
11846          Type source;
11847          Expression e;
11848          bool isBuiltin = exp && exp->last &&
11849             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11850               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11851          Expression arrayExp;
11852          const char * typeString = null;
11853          int builtinCount = 0;
11854
11855          for(e = exp ? exp->first : null; e; e = e.next)
11856          {
11857             if(!e.next)
11858             {
11859                FreeType(e.destType);
11860                e.destType = ProcessTypeString("Container", false);
11861             }
11862             if(!isBuiltin || e.next)
11863                ProcessExpressionType(e);
11864          }
11865
11866          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11867          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11868             eClass_IsDerived(source._class.registered, containerClass)))
11869          {
11870             Class _class = source ? source._class.registered : null;
11871             Symbol symbol;
11872             Expression expIt = null;
11873             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
11874             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11875             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11876             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11877             stmt.type = compoundStmt;
11878
11879             stmt.compound.context = Context { };
11880             stmt.compound.context.parent = curContext;
11881             curContext = stmt.compound.context;
11882
11883             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11884             {
11885                Class mapClass = eSystem_FindClass(privateModule, "Map");
11886                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11887                isCustomAVLTree = true;
11888                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
11889                   isAVLTree = true;
11890                else */if(eClass_IsDerived(source._class.registered, mapClass))
11891                   isMap = true;
11892             }
11893             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11894             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11895             {
11896                Class listClass = eSystem_FindClass(privateModule, "List");
11897                isLinkList = true;
11898                isList = eClass_IsDerived(source._class.registered, listClass);
11899             }
11900
11901             if(isArray)
11902             {
11903                Declarator decl;
11904                OldList * specs = MkList();
11905                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11906                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11907                stmt.compound.declarations = MkListOne(
11908                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11909                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11910                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11911                      MkInitializerAssignment(MkExpBrackets(exp))))));
11912             }
11913             else if(isBuiltin)
11914             {
11915                Type type = null;
11916                char typeStringBuf[1024];
11917
11918                // TODO: Merge this code?
11919                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11920                if(((Expression)exp->last).type == castExp)
11921                {
11922                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11923                   if(typeName)
11924                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11925                }
11926
11927                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11928                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11929                   arrayExp.destType._class.registered.templateArgs)
11930                {
11931                   Class templateClass = arrayExp.destType._class.registered;
11932                   typeString = templateClass.templateArgs[2].dataTypeString;
11933                }
11934                else if(arrayExp.list)
11935                {
11936                   // Guess type from expressions in the array
11937                   Expression e;
11938                   for(e = arrayExp.list->first; e; e = e.next)
11939                   {
11940                      ProcessExpressionType(e);
11941                      if(e.expType)
11942                      {
11943                         if(!type) { type = e.expType; type.refCount++; }
11944                         else
11945                         {
11946                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11947                            if(!MatchTypeExpression(e, type, null, false, true))
11948                            {
11949                               FreeType(type);
11950                               type = e.expType;
11951                               e.expType = null;
11952
11953                               e = arrayExp.list->first;
11954                               ProcessExpressionType(e);
11955                               if(e.expType)
11956                               {
11957                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11958                                  if(!MatchTypeExpression(e, type, null, false, true))
11959                                  {
11960                                     FreeType(e.expType);
11961                                     e.expType = null;
11962                                     FreeType(type);
11963                                     type = null;
11964                                     break;
11965                                  }
11966                               }
11967                            }
11968                         }
11969                         if(e.expType)
11970                         {
11971                            FreeType(e.expType);
11972                            e.expType = null;
11973                         }
11974                      }
11975                   }
11976                   if(type)
11977                   {
11978                      typeStringBuf[0] = '\0';
11979                      PrintType(type, typeStringBuf, false, true);
11980                      typeString = typeStringBuf;
11981                      FreeType(type);
11982                   }
11983                }
11984                if(typeString)
11985                {
11986                   OldList * initializers = MkList();
11987                   Declarator decl;
11988                   OldList * specs = MkList();
11989                   if(arrayExp.list)
11990                   {
11991                      Expression e;
11992
11993                      builtinCount = arrayExp.list->count;
11994                      type = ProcessTypeString(typeString, false);
11995                      while((e = arrayExp.list->first))
11996                      {
11997                         arrayExp.list->Remove(e);
11998                         e.destType = type;
11999                         type.refCount++;
12000                         ProcessExpressionType(e);
12001                         ListAdd(initializers, MkInitializerAssignment(e));
12002                      }
12003                      FreeType(type);
12004                      delete arrayExp.list;
12005                   }
12006                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12007                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12008                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12009
12010                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12011                      PlugDeclarator(
12012                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12013                         ), MkInitializerList(initializers)))));
12014                   FreeList(exp, FreeExpression);
12015                }
12016                else
12017                {
12018                   arrayExp.expType = ProcessTypeString("Container", false);
12019                   Compiler_Error($"Couldn't determine type of array elements\n");
12020                }
12021
12022                /*
12023                Declarator decl;
12024                OldList * specs = MkList();
12025
12026                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12027                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12028                stmt.compound.declarations = MkListOne(
12029                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12030                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12031                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12032                      MkInitializerAssignment(MkExpBrackets(exp))))));
12033                */
12034             }
12035             else if(isLinkList && !isList)
12036             {
12037                Declarator decl;
12038                OldList * specs = MkList();
12039                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12040                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12041                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12042                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12043                      MkInitializerAssignment(MkExpBrackets(exp))))));
12044             }
12045             /*else if(isCustomAVLTree)
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("__internalTree")),
12053                      MkInitializerAssignment(MkExpBrackets(exp))))));
12054             }*/
12055             else if(_class.templateArgs)
12056             {
12057                if(isMap)
12058                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12059                else
12060                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12061
12062                stmt.compound.declarations = MkListOne(
12063                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12064                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12065                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12066             }
12067             symbol = FindSymbol(id.string, curContext, curContext, false, false);
12068
12069             if(block)
12070             {
12071                // Reparent sub-contexts in this statement
12072                switch(block.type)
12073                {
12074                   case compoundStmt:
12075                      if(block.compound.context)
12076                         block.compound.context.parent = stmt.compound.context;
12077                      break;
12078                   case ifStmt:
12079                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12080                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12081                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12082                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12083                      break;
12084                   case switchStmt:
12085                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12086                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12087                      break;
12088                   case whileStmt:
12089                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12090                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12091                      break;
12092                   case doWhileStmt:
12093                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12094                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12095                      break;
12096                   case forStmt:
12097                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12098                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12099                      break;
12100                   case forEachStmt:
12101                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12102                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12103                      break;
12104                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12105                   case labeledStmt:
12106                   case caseStmt
12107                   case expressionStmt:
12108                   case gotoStmt:
12109                   case continueStmt:
12110                   case breakStmt
12111                   case returnStmt:
12112                   case asmStmt:
12113                   case badDeclarationStmt:
12114                   case fireWatchersStmt:
12115                   case stopWatchingStmt:
12116                   case watchStmt:
12117                   */
12118                }
12119             }
12120             if(filter)
12121             {
12122                block = MkIfStmt(filter, block, null);
12123             }
12124             if(isArray)
12125             {
12126                stmt.compound.statements = MkListOne(MkForStmt(
12127                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12128                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12129                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12130                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12131                   block));
12132               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12133               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12134               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12135             }
12136             else if(isBuiltin)
12137             {
12138                char count[128];
12139                //OldList * specs = MkList();
12140                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12141
12142                sprintf(count, "%d", builtinCount);
12143
12144                stmt.compound.statements = MkListOne(MkForStmt(
12145                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12146                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12147                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12148                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12149                   block));
12150
12151                /*
12152                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12153                stmt.compound.statements = MkListOne(MkForStmt(
12154                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12155                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12156                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12157                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12158                   block));
12159               */
12160               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12161               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12162               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12163             }
12164             else if(isLinkList && !isList)
12165             {
12166                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12167                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12168                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12169                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12170                {
12171                   stmt.compound.statements = MkListOne(MkForStmt(
12172                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12173                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12174                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12175                      block));
12176                }
12177                else
12178                {
12179                   OldList * specs = MkList();
12180                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
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)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12185                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12186                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12187                      block));
12188                }
12189                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12190                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12191                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12192             }
12193             /*else if(isCustomAVLTree)
12194             {
12195                stmt.compound.statements = MkListOne(MkForStmt(
12196                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12197                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12198                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12199                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12200                   block));
12201
12202                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12203                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12204                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12205             }*/
12206             else
12207             {
12208                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12209                   MkIdentifier("Next")), null)), block));
12210             }
12211             ProcessExpressionType(expIt);
12212             if(stmt.compound.declarations->first)
12213                ProcessDeclaration(stmt.compound.declarations->first);
12214
12215             if(symbol)
12216                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12217
12218             ProcessStatement(stmt);
12219             curContext = stmt.compound.context.parent;
12220             break;
12221          }
12222          else
12223          {
12224             Compiler_Error($"Expression is not a container\n");
12225          }
12226          break;
12227       }
12228       case gotoStmt:
12229          break;
12230       case continueStmt:
12231          break;
12232       case breakStmt:
12233          break;
12234       case returnStmt:
12235       {
12236          Expression exp;
12237          if(stmt.expressions)
12238          {
12239             for(exp = stmt.expressions->first; exp; exp = exp.next)
12240             {
12241                if(!exp.next)
12242                {
12243                   if(curFunction && !curFunction.type)
12244                      curFunction.type = ProcessType(
12245                         curFunction.specifiers, curFunction.declarator);
12246                   FreeType(exp.destType);
12247                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12248                   if(exp.destType) exp.destType.refCount++;
12249                }
12250                ProcessExpressionType(exp);
12251             }
12252          }
12253          break;
12254       }
12255       case badDeclarationStmt:
12256       {
12257          ProcessDeclaration(stmt.decl);
12258          break;
12259       }
12260       case asmStmt:
12261       {
12262          AsmField field;
12263          if(stmt.asmStmt.inputFields)
12264          {
12265             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12266                if(field.expression)
12267                   ProcessExpressionType(field.expression);
12268          }
12269          if(stmt.asmStmt.outputFields)
12270          {
12271             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12272                if(field.expression)
12273                   ProcessExpressionType(field.expression);
12274          }
12275          if(stmt.asmStmt.clobberedFields)
12276          {
12277             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12278             {
12279                if(field.expression)
12280                   ProcessExpressionType(field.expression);
12281             }
12282          }
12283          break;
12284       }
12285       case watchStmt:
12286       {
12287          PropertyWatch propWatch;
12288          OldList * watches = stmt._watch.watches;
12289          Expression object = stmt._watch.object;
12290          Expression watcher = stmt._watch.watcher;
12291          if(watcher)
12292             ProcessExpressionType(watcher);
12293          if(object)
12294             ProcessExpressionType(object);
12295
12296          if(inCompiler)
12297          {
12298             if(watcher || thisClass)
12299             {
12300                External external = curExternal;
12301                Context context = curContext;
12302
12303                stmt.type = expressionStmt;
12304                stmt.expressions = MkList();
12305
12306                curExternal = external.prev;
12307
12308                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12309                {
12310                   ClassFunction func;
12311                   char watcherName[1024];
12312                   Class watcherClass = watcher ?
12313                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12314                   External createdExternal;
12315
12316                   // Create a declaration above
12317                   External externalDecl = MkExternalDeclaration(null);
12318                   ast->Insert(curExternal.prev, externalDecl);
12319
12320                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12321                   if(propWatch.deleteWatch)
12322                      strcat(watcherName, "_delete");
12323                   else
12324                   {
12325                      Identifier propID;
12326                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12327                      {
12328                         strcat(watcherName, "_");
12329                         strcat(watcherName, propID.string);
12330                      }
12331                   }
12332
12333                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12334                   {
12335                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12336                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12337                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12338                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12339                      ProcessClassFunctionBody(func, propWatch.compound);
12340                      propWatch.compound = null;
12341
12342                      //afterExternal = afterExternal ? afterExternal : curExternal;
12343
12344                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12345                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12346                      // TESTING THIS...
12347                      createdExternal.symbol.idCode = external.symbol.idCode;
12348
12349                      curExternal = createdExternal;
12350                      ProcessFunction(createdExternal.function);
12351
12352
12353                      // Create a declaration above
12354                      {
12355                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12356                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12357                         externalDecl.declaration = decl;
12358                         if(decl.symbol && !decl.symbol.pointerExternal)
12359                            decl.symbol.pointerExternal = externalDecl;
12360                      }
12361
12362                      if(propWatch.deleteWatch)
12363                      {
12364                         OldList * args = MkList();
12365                         ListAdd(args, CopyExpression(object));
12366                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12367                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12368                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12369                      }
12370                      else
12371                      {
12372                         Class _class = object.expType._class.registered;
12373                         Identifier propID;
12374
12375                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12376                         {
12377                            char propName[1024];
12378                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12379                            if(prop)
12380                            {
12381                               char getName[1024], setName[1024];
12382                               OldList * args = MkList();
12383
12384                               DeclareProperty(prop, setName, getName);
12385
12386                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12387                               strcpy(propName, "__ecereProp_");
12388                               FullClassNameCat(propName, prop._class.fullName, false);
12389                               strcat(propName, "_");
12390                               // strcat(propName, prop.name);
12391                               FullClassNameCat(propName, prop.name, true);
12392
12393                               ListAdd(args, CopyExpression(object));
12394                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12395                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12396                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12397
12398                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12399                            }
12400                            else
12401                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12402                         }
12403                      }
12404                   }
12405                   else
12406                      Compiler_Error($"Invalid watched object\n");
12407                }
12408
12409                curExternal = external;
12410                curContext = context;
12411
12412                if(watcher)
12413                   FreeExpression(watcher);
12414                if(object)
12415                   FreeExpression(object);
12416                FreeList(watches, FreePropertyWatch);
12417             }
12418             else
12419                Compiler_Error($"No observer specified and not inside a _class\n");
12420          }
12421          else
12422          {
12423             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12424             {
12425                ProcessStatement(propWatch.compound);
12426             }
12427
12428          }
12429          break;
12430       }
12431       case fireWatchersStmt:
12432       {
12433          OldList * watches = stmt._watch.watches;
12434          Expression object = stmt._watch.object;
12435          Class _class;
12436          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12437          // printf("%X\n", watches);
12438          // printf("%X\n", stmt._watch.watches);
12439          if(object)
12440             ProcessExpressionType(object);
12441
12442          if(inCompiler)
12443          {
12444             _class = object ?
12445                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12446
12447             if(_class)
12448             {
12449                Identifier propID;
12450
12451                stmt.type = expressionStmt;
12452                stmt.expressions = MkList();
12453
12454                // Check if we're inside a property set
12455                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12456                {
12457                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12458                }
12459                else if(!watches)
12460                {
12461                   //Compiler_Error($"No property specified and not inside a property set\n");
12462                }
12463                if(watches)
12464                {
12465                   for(propID = watches->first; propID; propID = propID.next)
12466                   {
12467                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12468                      if(prop)
12469                      {
12470                         CreateFireWatcher(prop, object, stmt);
12471                      }
12472                      else
12473                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12474                   }
12475                }
12476                else
12477                {
12478                   // Fire all properties!
12479                   Property prop;
12480                   Class base;
12481                   for(base = _class; base; base = base.base)
12482                   {
12483                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12484                      {
12485                         if(prop.isProperty && prop.isWatchable)
12486                         {
12487                            CreateFireWatcher(prop, object, stmt);
12488                         }
12489                      }
12490                   }
12491                }
12492
12493                if(object)
12494                   FreeExpression(object);
12495                FreeList(watches, FreeIdentifier);
12496             }
12497             else
12498                Compiler_Error($"Invalid object specified and not inside a class\n");
12499          }
12500          break;
12501       }
12502       case stopWatchingStmt:
12503       {
12504          OldList * watches = stmt._watch.watches;
12505          Expression object = stmt._watch.object;
12506          Expression watcher = stmt._watch.watcher;
12507          Class _class;
12508          if(object)
12509             ProcessExpressionType(object);
12510          if(watcher)
12511             ProcessExpressionType(watcher);
12512          if(inCompiler)
12513          {
12514             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12515
12516             if(watcher || thisClass)
12517             {
12518                if(_class)
12519                {
12520                   Identifier propID;
12521
12522                   stmt.type = expressionStmt;
12523                   stmt.expressions = MkList();
12524
12525                   if(!watches)
12526                   {
12527                      OldList * args;
12528                      // eInstance_StopWatching(object, null, watcher);
12529                      args = MkList();
12530                      ListAdd(args, CopyExpression(object));
12531                      ListAdd(args, MkExpConstant("0"));
12532                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12533                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12534                   }
12535                   else
12536                   {
12537                      for(propID = watches->first; propID; propID = propID.next)
12538                      {
12539                         char propName[1024];
12540                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12541                         if(prop)
12542                         {
12543                            char getName[1024], setName[1024];
12544                            OldList * args = MkList();
12545
12546                            DeclareProperty(prop, setName, getName);
12547
12548                            // eInstance_StopWatching(object, prop, watcher);
12549                            strcpy(propName, "__ecereProp_");
12550                            FullClassNameCat(propName, prop._class.fullName, false);
12551                            strcat(propName, "_");
12552                            // strcat(propName, prop.name);
12553                            FullClassNameCat(propName, prop.name, true);
12554                            //MangleClassName(propName);
12555
12556                            ListAdd(args, CopyExpression(object));
12557                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12558                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12559                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12560                         }
12561                         else
12562                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12563                      }
12564                   }
12565
12566                   if(object)
12567                      FreeExpression(object);
12568                   if(watcher)
12569                      FreeExpression(watcher);
12570                   FreeList(watches, FreeIdentifier);
12571                }
12572                else
12573                   Compiler_Error($"Invalid object specified and not inside a class\n");
12574             }
12575             else
12576                Compiler_Error($"No observer specified and not inside a class\n");
12577          }
12578          break;
12579       }
12580    }
12581 }
12582
12583 static void ProcessFunction(FunctionDefinition function)
12584 {
12585    Identifier id = GetDeclId(function.declarator);
12586    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12587    Type type = symbol ? symbol.type : null;
12588    Class oldThisClass = thisClass;
12589    Context oldTopContext = topContext;
12590
12591    yylloc = function.loc;
12592    // Process thisClass
12593
12594    if(type && type.thisClass)
12595    {
12596       Symbol classSym = type.thisClass;
12597       Class _class = type.thisClass.registered;
12598       char className[1024];
12599       char structName[1024];
12600       Declarator funcDecl;
12601       Symbol thisSymbol;
12602
12603       bool typedObject = false;
12604
12605       if(_class && !_class.base)
12606       {
12607          _class = currentClass;
12608          if(_class && !_class.symbol)
12609             _class.symbol = FindClass(_class.fullName);
12610          classSym = _class ? _class.symbol : null;
12611          typedObject = true;
12612       }
12613
12614       thisClass = _class;
12615
12616       if(inCompiler && _class)
12617       {
12618          if(type.kind == functionType)
12619          {
12620             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12621             {
12622                //TypeName param = symbol.type.params.first;
12623                Type param = symbol.type.params.first;
12624                symbol.type.params.Remove(param);
12625                //FreeTypeName(param);
12626                FreeType(param);
12627             }
12628             if(type.classObjectType != classPointer)
12629             {
12630                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12631                symbol.type.staticMethod = true;
12632                symbol.type.thisClass = null;
12633
12634                // HIGH DANGER: VERIFYING THIS...
12635                symbol.type.extraParam = false;
12636             }
12637          }
12638
12639          strcpy(className, "__ecereClass_");
12640          FullClassNameCat(className, _class.fullName, true);
12641
12642          //MangleClassName(className);
12643
12644          structName[0] = 0;
12645          FullClassNameCat(structName, _class.fullName, false);
12646
12647          // [class] this
12648
12649
12650          funcDecl = GetFuncDecl(function.declarator);
12651          if(funcDecl)
12652          {
12653             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12654             {
12655                TypeName param = funcDecl.function.parameters->first;
12656                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12657                {
12658                   funcDecl.function.parameters->Remove(param);
12659                   FreeTypeName(param);
12660                }
12661             }
12662
12663             // DANGER: Watch for this... Check if it's a Conversion?
12664             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12665
12666             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12667             if(!function.propertyNoThis)
12668             {
12669                TypeName thisParam;
12670
12671                if(type.classObjectType != classPointer)
12672                {
12673                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12674                   if(!funcDecl.function.parameters)
12675                      funcDecl.function.parameters = MkList();
12676                   funcDecl.function.parameters->Insert(null, thisParam);
12677                }
12678
12679                if(typedObject)
12680                {
12681                   if(type.classObjectType != classPointer)
12682                   {
12683                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12684                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12685                   }
12686
12687                   thisParam = TypeName
12688                   {
12689                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12690                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12691                   };
12692                   funcDecl.function.parameters->Insert(null, thisParam);
12693                }
12694             }
12695          }
12696
12697          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12698          {
12699             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12700             funcDecl = GetFuncDecl(initDecl.declarator);
12701             if(funcDecl)
12702             {
12703                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12704                {
12705                   TypeName param = funcDecl.function.parameters->first;
12706                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12707                   {
12708                      funcDecl.function.parameters->Remove(param);
12709                      FreeTypeName(param);
12710                   }
12711                }
12712
12713                if(type.classObjectType != classPointer)
12714                {
12715                   // DANGER: Watch for this... Check if it's a Conversion?
12716                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12717                   {
12718                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12719
12720                      if(!funcDecl.function.parameters)
12721                         funcDecl.function.parameters = MkList();
12722                      funcDecl.function.parameters->Insert(null, thisParam);
12723                   }
12724                }
12725             }
12726          }
12727       }
12728
12729       // Add this to the context
12730       if(function.body)
12731       {
12732          if(type.classObjectType != classPointer)
12733          {
12734             thisSymbol = Symbol
12735             {
12736                string = CopyString("this");
12737                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12738             };
12739             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12740
12741             if(typedObject && thisSymbol.type)
12742             {
12743                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12744                thisSymbol.type.byReference = type.byReference;
12745                thisSymbol.type.typedByReference = type.byReference;
12746                /*
12747                thisSymbol = Symbol { string = CopyString("class") };
12748                function.body.compound.context.symbols.Add(thisSymbol);
12749                */
12750             }
12751          }
12752       }
12753
12754       // Pointer to class data
12755
12756       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12757       {
12758          DataMember member = null;
12759          {
12760             Class base;
12761             for(base = _class; base && base.type != systemClass; base = base.next)
12762             {
12763                for(member = base.membersAndProperties.first; member; member = member.next)
12764                   if(!member.isProperty)
12765                      break;
12766                if(member)
12767                   break;
12768             }
12769          }
12770          for(member = _class.membersAndProperties.first; member; member = member.next)
12771             if(!member.isProperty)
12772                break;
12773          if(member)
12774          {
12775             char pointerName[1024];
12776
12777             Declaration decl;
12778             Initializer initializer;
12779             Expression exp, bytePtr;
12780
12781             strcpy(pointerName, "__ecerePointer_");
12782             FullClassNameCat(pointerName, _class.fullName, false);
12783             {
12784                char className[1024];
12785                strcpy(className, "__ecereClass_");
12786                FullClassNameCat(className, classSym.string, true);
12787                //MangleClassName(className);
12788
12789                // Testing This
12790                DeclareClass(classSym, className);
12791             }
12792
12793             // ((byte *) this)
12794             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12795
12796             if(_class.fixed)
12797             {
12798                char string[256];
12799                sprintf(string, "%d", _class.offset);
12800                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12801             }
12802             else
12803             {
12804                // ([bytePtr] + [className]->offset)
12805                exp = QBrackets(MkExpOp(bytePtr, '+',
12806                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12807             }
12808
12809             // (this ? [exp] : 0)
12810             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12811             exp.expType = Type
12812             {
12813                refCount = 1;
12814                kind = pointerType;
12815                type = Type { refCount = 1, kind = voidType };
12816             };
12817
12818             if(function.body)
12819             {
12820                yylloc = function.body.loc;
12821                // ([structName] *) [exp]
12822                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12823                initializer = MkInitializerAssignment(
12824                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12825
12826                // [structName] * [pointerName] = [initializer];
12827                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12828
12829                {
12830                   Context prevContext = curContext;
12831                   curContext = function.body.compound.context;
12832
12833                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12834                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12835
12836                   curContext = prevContext;
12837                }
12838
12839                // WHY?
12840                decl.symbol = null;
12841
12842                if(!function.body.compound.declarations)
12843                   function.body.compound.declarations = MkList();
12844                function.body.compound.declarations->Insert(null, decl);
12845             }
12846          }
12847       }
12848
12849
12850       // Loop through the function and replace undeclared identifiers
12851       // which are a member of the class (methods, properties or data)
12852       // by "this.[member]"
12853    }
12854    else
12855       thisClass = null;
12856
12857    if(id)
12858    {
12859       FreeSpecifier(id._class);
12860       id._class = null;
12861
12862       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12863       {
12864          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12865          id = GetDeclId(initDecl.declarator);
12866
12867          FreeSpecifier(id._class);
12868          id._class = null;
12869       }
12870    }
12871    if(function.body)
12872       topContext = function.body.compound.context;
12873    {
12874       FunctionDefinition oldFunction = curFunction;
12875       curFunction = function;
12876       if(function.body)
12877          ProcessStatement(function.body);
12878
12879       // If this is a property set and no firewatchers has been done yet, add one here
12880       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12881       {
12882          Statement prevCompound = curCompound;
12883          Context prevContext = curContext;
12884
12885          Statement fireWatchers = MkFireWatchersStmt(null, null);
12886          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12887          ListAdd(function.body.compound.statements, fireWatchers);
12888
12889          curCompound = function.body;
12890          curContext = function.body.compound.context;
12891
12892          ProcessStatement(fireWatchers);
12893
12894          curContext = prevContext;
12895          curCompound = prevCompound;
12896
12897       }
12898
12899       curFunction = oldFunction;
12900    }
12901
12902    if(function.declarator)
12903    {
12904       ProcessDeclarator(function.declarator);
12905    }
12906
12907    topContext = oldTopContext;
12908    thisClass = oldThisClass;
12909 }
12910
12911 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12912 static void ProcessClass(OldList definitions, Symbol symbol)
12913 {
12914    ClassDef def;
12915    External external = curExternal;
12916    Class regClass = symbol ? symbol.registered : null;
12917
12918    // Process all functions
12919    for(def = definitions.first; def; def = def.next)
12920    {
12921       if(def.type == functionClassDef)
12922       {
12923          if(def.function.declarator)
12924             curExternal = def.function.declarator.symbol.pointerExternal;
12925          else
12926             curExternal = external;
12927
12928          ProcessFunction((FunctionDefinition)def.function);
12929       }
12930       else if(def.type == declarationClassDef)
12931       {
12932          if(def.decl.type == instDeclaration)
12933          {
12934             thisClass = regClass;
12935             ProcessInstantiationType(def.decl.inst);
12936             thisClass = null;
12937          }
12938          // Testing this
12939          else
12940          {
12941             Class backThisClass = thisClass;
12942             if(regClass) thisClass = regClass;
12943             ProcessDeclaration(def.decl);
12944             thisClass = backThisClass;
12945          }
12946       }
12947       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12948       {
12949          MemberInit defProperty;
12950
12951          // Add this to the context
12952          Symbol thisSymbol = Symbol
12953          {
12954             string = CopyString("this");
12955             type = regClass ? MkClassType(regClass.fullName) : null;
12956          };
12957          globalContext.symbols.Add((BTNode)thisSymbol);
12958
12959          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12960          {
12961             thisClass = regClass;
12962             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12963             thisClass = null;
12964          }
12965
12966          globalContext.symbols.Remove((BTNode)thisSymbol);
12967          FreeSymbol(thisSymbol);
12968       }
12969       else if(def.type == propertyClassDef && def.propertyDef)
12970       {
12971          PropertyDef prop = def.propertyDef;
12972
12973          // Add this to the context
12974          /*
12975          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12976          globalContext.symbols.Add(thisSymbol);
12977          */
12978
12979          thisClass = regClass;
12980          if(prop.setStmt)
12981          {
12982             if(regClass)
12983             {
12984                Symbol thisSymbol
12985                {
12986                   string = CopyString("this");
12987                   type = MkClassType(regClass.fullName);
12988                };
12989                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12990             }
12991
12992             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12993             ProcessStatement(prop.setStmt);
12994          }
12995          if(prop.getStmt)
12996          {
12997             if(regClass)
12998             {
12999                Symbol thisSymbol
13000                {
13001                   string = CopyString("this");
13002                   type = MkClassType(regClass.fullName);
13003                };
13004                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13005             }
13006
13007             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13008             ProcessStatement(prop.getStmt);
13009          }
13010          if(prop.issetStmt)
13011          {
13012             if(regClass)
13013             {
13014                Symbol thisSymbol
13015                {
13016                   string = CopyString("this");
13017                   type = MkClassType(regClass.fullName);
13018                };
13019                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13020             }
13021
13022             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13023             ProcessStatement(prop.issetStmt);
13024          }
13025
13026          thisClass = null;
13027
13028          /*
13029          globalContext.symbols.Remove(thisSymbol);
13030          FreeSymbol(thisSymbol);
13031          */
13032       }
13033       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13034       {
13035          PropertyWatch propertyWatch = def.propertyWatch;
13036
13037          thisClass = regClass;
13038          if(propertyWatch.compound)
13039          {
13040             Symbol thisSymbol
13041             {
13042                string = CopyString("this");
13043                type = regClass ? MkClassType(regClass.fullName) : null;
13044             };
13045
13046             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13047
13048             curExternal = null;
13049             ProcessStatement(propertyWatch.compound);
13050          }
13051          thisClass = null;
13052       }
13053    }
13054 }
13055
13056 void DeclareFunctionUtil(const String s)
13057 {
13058    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13059    if(function)
13060    {
13061       char name[1024];
13062       name[0] = 0;
13063       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13064          strcpy(name, "__ecereFunction_");
13065       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13066       DeclareFunction(function, name);
13067    }
13068 }
13069
13070 void ComputeDataTypes()
13071 {
13072    External external;
13073    External temp { };
13074    External after = null;
13075
13076    currentClass = null;
13077
13078    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13079
13080    for(external = ast->first; external; external = external.next)
13081    {
13082       if(external.type == declarationExternal)
13083       {
13084          Declaration decl = external.declaration;
13085          if(decl)
13086          {
13087             OldList * decls = decl.declarators;
13088             if(decls)
13089             {
13090                InitDeclarator initDecl = decls->first;
13091                if(initDecl)
13092                {
13093                   Declarator declarator = initDecl.declarator;
13094                   if(declarator && declarator.type == identifierDeclarator)
13095                   {
13096                      Identifier id = declarator.identifier;
13097                      if(id && id.string)
13098                      {
13099                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
13100                         {
13101                            external.symbol.id = -1001, external.symbol.idCode = -1001;
13102                            after = external;
13103                         }
13104                      }
13105                   }
13106                }
13107             }
13108          }
13109        }
13110    }
13111
13112    {
13113       // Workaround until we have proper toposort for declarations reordering
13114       External e = MkExternalDeclaration(MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Instance"), null)), null));
13115       ast->Insert(after, e);
13116       after = e;
13117    }
13118
13119    temp.symbol = Symbol { id = -1000, idCode = -1000 };
13120    ast->Insert(after, temp);
13121    curExternal = temp;
13122
13123    DeclareFunctionUtil("eSystem_New");
13124    DeclareFunctionUtil("eSystem_New0");
13125    DeclareFunctionUtil("eSystem_Renew");
13126    DeclareFunctionUtil("eSystem_Renew0");
13127    DeclareFunctionUtil("eSystem_Delete");
13128    DeclareFunctionUtil("eClass_GetProperty");
13129    DeclareFunctionUtil("eClass_SetProperty");
13130    DeclareFunctionUtil("eInstance_FireSelfWatchers");
13131    DeclareFunctionUtil("eInstance_SetMethod");
13132    DeclareFunctionUtil("eInstance_IncRef");
13133    DeclareFunctionUtil("eInstance_StopWatching");
13134    DeclareFunctionUtil("eInstance_Watch");
13135    DeclareFunctionUtil("eInstance_FireWatchers");
13136
13137    DeclareStruct("ecere::com::Class", false);
13138    DeclareStruct("ecere::com::Instance", false);
13139    DeclareStruct("ecere::com::Property", false);
13140    DeclareStruct("ecere::com::DataMember", false);
13141    DeclareStruct("ecere::com::Method", false);
13142    DeclareStruct("ecere::com::SerialBuffer", false);
13143    DeclareStruct("ecere::com::ClassTemplateArgument", false);
13144
13145    ast->Remove(temp);
13146
13147    for(external = ast->first; external; external = external.next)
13148    {
13149       afterExternal = curExternal = external;
13150       if(external.type == functionExternal)
13151       {
13152          currentClass = external.function._class;
13153          ProcessFunction(external.function);
13154       }
13155       // There shouldn't be any _class member access here anyways...
13156       else if(external.type == declarationExternal)
13157       {
13158          currentClass = null;
13159          if(external.declaration)
13160             ProcessDeclaration(external.declaration);
13161       }
13162       else if(external.type == classExternal)
13163       {
13164          ClassDefinition _class = external._class;
13165          currentClass = external.symbol.registered;
13166          if(_class.definitions)
13167          {
13168             ProcessClass(_class.definitions, _class.symbol);
13169          }
13170          if(inCompiler)
13171          {
13172             // Free class data...
13173             ast->Remove(external);
13174             delete external;
13175          }
13176       }
13177       else if(external.type == nameSpaceExternal)
13178       {
13179          thisNameSpace = external.id.string;
13180       }
13181    }
13182    currentClass = null;
13183    thisNameSpace = null;
13184    curExternal = null;
13185
13186    delete temp.symbol;
13187    delete temp;
13188 }