compiler/libec; ecere: (#158, #305) Taking advantage of new DataType Size vs Struct...
[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 c;
1834          int paramCount = 0;
1835          int lastParam = -1;
1836
1837          char templateString[1024];
1838          ClassTemplateParameter param;
1839          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1840          for(cClass = expClass; cClass; cClass = cClass.base)
1841          {
1842             int p = 0;
1843             if(cClass.templateClass) cClass = cClass.templateClass;
1844             for(param = cClass.templateParams.first; param; param = param.next)
1845             {
1846                int id = p;
1847                Class sClass;
1848                ClassTemplateArgument arg;
1849                for(sClass = cClass.base; sClass; sClass = sClass.base)
1850                {
1851                   if(sClass.templateClass) sClass = sClass.templateClass;
1852                   id += sClass.templateParams.count;
1853                }
1854                arg = expClass.templateArgs[id];
1855
1856                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1857                {
1858                   ClassTemplateParameter cParam;
1859                   //int p = numParams - sClass.templateParams.count;
1860                   int p = 0;
1861                   Class nextClass;
1862                   if(sClass.templateClass) sClass = sClass.templateClass;
1863
1864                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1865                   {
1866                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1867                      p += nextClass.templateParams.count;
1868                   }
1869
1870                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1871                   {
1872                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1873                      {
1874                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1875                         {
1876                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1877                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1878                            break;
1879                         }
1880                      }
1881                   }
1882                }
1883
1884                {
1885                   char argument[256];
1886                   argument[0] = '\0';
1887                   /*if(arg.name)
1888                   {
1889                      strcat(argument, arg.name.string);
1890                      strcat(argument, " = ");
1891                   }*/
1892                   switch(param.type)
1893                   {
1894                      case expression:
1895                      {
1896                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1897                         char expString[1024];
1898                         OldList * specs = MkList();
1899                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1900                         Expression exp;
1901                         char * string = PrintHexUInt64(arg.expression.ui64);
1902                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1903                         delete string;
1904
1905                         ProcessExpressionType(exp);
1906                         ComputeExpression(exp);
1907                         expString[0] = '\0';
1908                         PrintExpression(exp, expString);
1909                         strcat(argument, expString);
1910                         //delete exp;
1911                         FreeExpression(exp);
1912                         break;
1913                      }
1914                      case identifier:
1915                      {
1916                         strcat(argument, arg.member.name);
1917                         break;
1918                      }
1919                      case TemplateParameterType::type:
1920                      {
1921                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1922                            strcat(argument, arg.dataTypeString);
1923                         break;
1924                      }
1925                   }
1926                   if(argument[0])
1927                   {
1928                      if(paramCount) strcat(templateString, ", ");
1929                      if(lastParam != p - 1)
1930                      {
1931                         strcat(templateString, param.name);
1932                         strcat(templateString, " = ");
1933                      }
1934                      strcat(templateString, argument);
1935                      paramCount++;
1936                      lastParam = p;
1937                   }
1938                   p++;
1939                }
1940             }
1941          }
1942          {
1943             int len = strlen(templateString);
1944             if(templateString[len-1] == '<')
1945                len--;
1946             else
1947             {
1948                if(templateString[len-1] == '>')
1949                   templateString[len++] = ' ';
1950                templateString[len++] = '>';
1951             }
1952             templateString[len++] = '\0';
1953          }
1954          {
1955             Context context = SetupTemplatesContext(_class);
1956             if(freeType) FreeType(type);
1957             type = ProcessTypeString(templateString, false);
1958             freeType = true;
1959             FinishTemplatesContext(context);
1960          }
1961       }
1962
1963       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1964       {
1965          ProcessExpressionType(member.initializer.exp);
1966          if(!member.initializer.exp.expType)
1967          {
1968             if(inCompiler)
1969             {
1970                char expString[10240];
1971                expString[0] = '\0';
1972                PrintExpression(member.initializer.exp, expString);
1973                ChangeCh(expString, '\n', ' ');
1974                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1975             }
1976          }
1977          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1978          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false, true))
1979          {
1980             Compiler_Error($"incompatible instance method %s\n", ident.string);
1981          }
1982       }
1983       else if(member.initializer)
1984       {
1985          /*
1986          FreeType(member.exp.destType);
1987          member.exp.destType = type;
1988          if(member.exp.destType)
1989             member.exp.destType.refCount++;
1990          ProcessExpressionType(member.exp);
1991          */
1992
1993          ProcessInitializer(member.initializer, type);
1994       }
1995       if(freeType) FreeType(type);
1996    }
1997    else
1998    {
1999       if(_class && _class.type == unitClass)
2000       {
2001          if(member.initializer)
2002          {
2003             /*
2004             FreeType(member.exp.destType);
2005             member.exp.destType = MkClassType(_class.fullName);
2006             ProcessExpressionType(member.initializer, type);
2007             */
2008             Type type = MkClassType(_class.fullName);
2009             ProcessInitializer(member.initializer, type);
2010             FreeType(type);
2011          }
2012       }
2013       else
2014       {
2015          if(member.initializer)
2016          {
2017             //ProcessExpressionType(member.exp);
2018             ProcessInitializer(member.initializer, null);
2019          }
2020          if(ident)
2021          {
2022             if(method)
2023             {
2024                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
2025             }
2026             else if(_class)
2027             {
2028                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
2029                if(inCompiler)
2030                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
2031             }
2032          }
2033          else if(_class)
2034             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
2035       }
2036    }
2037 }
2038
2039 void ProcessInstantiationType(Instantiation inst)
2040 {
2041    yylloc = inst.loc;
2042    if(inst._class)
2043    {
2044       MembersInit members;
2045       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
2046       Class _class;
2047
2048       /*if(!inst._class.symbol)
2049          inst._class.symbol = FindClass(inst._class.name);*/
2050       classSym = inst._class.symbol;
2051       _class = classSym ? classSym.registered : null;
2052
2053       // DANGER: Patch for mutex not declaring its struct when not needed
2054       if(!_class || _class.type != noHeadClass)
2055          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
2056
2057       afterExternal = afterExternal ? afterExternal : curExternal;
2058
2059       if(inst.exp)
2060          ProcessExpressionType(inst.exp);
2061
2062       inst.isConstant = true;
2063       if(inst.members)
2064       {
2065          DataMember curMember = null;
2066          Class curClass = null;
2067          DataMember subMemberStack[256];
2068          int subMemberStackPos = 0;
2069
2070          for(members = inst.members->first; members; members = members.next)
2071          {
2072             switch(members.type)
2073             {
2074                case methodMembersInit:
2075                {
2076                   char name[1024];
2077                   static uint instMethodID = 0;
2078                   External external = curExternal;
2079                   Context context = curContext;
2080                   Declarator declarator = members.function.declarator;
2081                   Identifier nameID = GetDeclId(declarator);
2082                   char * unmangled = nameID ? nameID.string : null;
2083                   Expression exp;
2084                   External createdExternal = null;
2085
2086                   if(inCompiler)
2087                   {
2088                      char number[16];
2089                      //members.function.dontMangle = true;
2090                      strcpy(name, "__ecereInstMeth_");
2091                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2092                      strcat(name, "_");
2093                      strcat(name, nameID.string);
2094                      strcat(name, "_");
2095                      sprintf(number, "_%08d", instMethodID++);
2096                      strcat(name, number);
2097                      nameID.string = CopyString(name);
2098                   }
2099
2100                   // Do modifications here...
2101                   if(declarator)
2102                   {
2103                      Symbol symbol = declarator.symbol;
2104                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2105
2106                      if(method && method.type == virtualMethod)
2107                      {
2108                         symbol.method = method;
2109                         ProcessMethodType(method);
2110
2111                         if(!symbol.type.thisClass)
2112                         {
2113                            if(method.dataType.thisClass && currentClass &&
2114                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2115                            {
2116                               if(!currentClass.symbol)
2117                                  currentClass.symbol = FindClass(currentClass.fullName);
2118                               symbol.type.thisClass = currentClass.symbol;
2119                            }
2120                            else
2121                            {
2122                               if(!_class.symbol)
2123                                  _class.symbol = FindClass(_class.fullName);
2124                               symbol.type.thisClass = _class.symbol;
2125                            }
2126                         }
2127                         // TESTING THIS HERE:
2128                         DeclareType(symbol.type, true, true);
2129
2130                      }
2131                      else if(classSym)
2132                      {
2133                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2134                            unmangled, classSym.string);
2135                      }
2136                   }
2137
2138                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2139                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2140
2141                   if(nameID)
2142                   {
2143                      FreeSpecifier(nameID._class);
2144                      nameID._class = null;
2145                   }
2146
2147                   if(inCompiler)
2148                   {
2149                      //Type type = declarator.symbol.type;
2150                      External oldExternal = curExternal;
2151
2152                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2153                      // *** It was commented out for problems such as
2154                      /*
2155                            class VirtualDesktop : Window
2156                            {
2157                               clientSize = Size { };
2158                               Timer timer
2159                               {
2160                                  bool DelayExpired()
2161                                  {
2162                                     clientSize.w;
2163                                     return true;
2164                                  }
2165                               };
2166                            }
2167                      */
2168                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2169
2170                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2171
2172                      /*
2173                      if(strcmp(declarator.symbol.string, name))
2174                      {
2175                         printf("TOCHECK: Look out for this\n");
2176                         delete declarator.symbol.string;
2177                         declarator.symbol.string = CopyString(name);
2178                      }
2179
2180                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2181                      {
2182                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2183                         excludedSymbols->Remove(declarator.symbol);
2184                         globalContext.symbols.Add((BTNode)declarator.symbol);
2185                         if(strstr(declarator.symbol.string), "::")
2186                            globalContext.hasNameSpace = true;
2187
2188                      }
2189                      */
2190
2191                      //curExternal = curExternal.prev;
2192                      //afterExternal = afterExternal->next;
2193
2194                      //ProcessFunction(afterExternal->function);
2195
2196                      //curExternal = afterExternal;
2197                      {
2198                         External externalDecl;
2199                         externalDecl = MkExternalDeclaration(null);
2200                         ast->Insert(oldExternal.prev, externalDecl);
2201
2202                         // Which function does this process?
2203                         if(createdExternal.function)
2204                         {
2205                            ProcessFunction(createdExternal.function);
2206
2207                            //curExternal = oldExternal;
2208
2209                            {
2210                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2211
2212                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2213                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2214
2215                               //externalDecl = MkExternalDeclaration(decl);
2216
2217                               //***** ast->Insert(external.prev, externalDecl);
2218                               //ast->Insert(curExternal.prev, externalDecl);
2219                               externalDecl.declaration = decl;
2220                               if(decl.symbol && !decl.symbol.pointerExternal)
2221                                  decl.symbol.pointerExternal = externalDecl;
2222
2223                               // Trying this out...
2224                               declarator.symbol.pointerExternal = externalDecl;
2225                            }
2226                         }
2227                      }
2228                   }
2229                   else if(declarator)
2230                   {
2231                      curExternal = declarator.symbol.pointerExternal;
2232                      ProcessFunction((FunctionDefinition)members.function);
2233                   }
2234                   curExternal = external;
2235                   curContext = context;
2236
2237                   if(inCompiler)
2238                   {
2239                      FreeClassFunction(members.function);
2240
2241                      // In this pass, turn this into a MemberInitData
2242                      exp = QMkExpId(name);
2243                      members.type = dataMembersInit;
2244                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2245
2246                      delete unmangled;
2247                   }
2248                   break;
2249                }
2250                case dataMembersInit:
2251                {
2252                   if(members.dataMembers && classSym)
2253                   {
2254                      MemberInit member;
2255                      Location oldyyloc = yylloc;
2256                      for(member = members.dataMembers->first; member; member = member.next)
2257                      {
2258                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2259                         if(member.initializer && !member.initializer.isConstant)
2260                            inst.isConstant = false;
2261                      }
2262                      yylloc = oldyyloc;
2263                   }
2264                   break;
2265                }
2266             }
2267          }
2268       }
2269    }
2270 }
2271
2272 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2273 {
2274    // OPTIMIZATIONS: TESTING THIS...
2275    if(inCompiler)
2276    {
2277       if(type.kind == functionType)
2278       {
2279          Type param;
2280          if(declareParams)
2281          {
2282             for(param = type.params.first; param; param = param.next)
2283                DeclareType(param, declarePointers, true);
2284          }
2285          DeclareType(type.returnType, declarePointers, true);
2286       }
2287       else if(type.kind == pointerType && declarePointers)
2288          DeclareType(type.type, declarePointers, false);
2289       else if(type.kind == classType)
2290       {
2291          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2292             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2293       }
2294       else if(type.kind == structType || type.kind == unionType)
2295       {
2296          Type member;
2297          for(member = type.members.first; member; member = member.next)
2298             DeclareType(member, false, false);
2299       }
2300       else if(type.kind == arrayType)
2301          DeclareType(type.arrayType, declarePointers, false);
2302    }
2303 }
2304
2305 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2306 {
2307    ClassTemplateArgument * arg = null;
2308    int id = 0;
2309    ClassTemplateParameter curParam = null;
2310    Class sClass;
2311    for(sClass = _class; sClass; sClass = sClass.base)
2312    {
2313       id = 0;
2314       if(sClass.templateClass) sClass = sClass.templateClass;
2315       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2316       {
2317          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2318          {
2319             for(sClass = sClass.base; sClass; sClass = sClass.base)
2320             {
2321                if(sClass.templateClass) sClass = sClass.templateClass;
2322                id += sClass.templateParams.count;
2323             }
2324             break;
2325          }
2326          id++;
2327       }
2328       if(curParam) break;
2329    }
2330    if(curParam)
2331    {
2332       arg = &_class.templateArgs[id];
2333       if(arg && param.type == type)
2334          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2335    }
2336    return arg;
2337 }
2338
2339 public Context SetupTemplatesContext(Class _class)
2340 {
2341    Context context = PushContext();
2342    context.templateTypesOnly = true;
2343    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2344    {
2345       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2346       for(; param; param = param.next)
2347       {
2348          if(param.type == type && param.identifier)
2349          {
2350             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2351             curContext.templateTypes.Add((BTNode)type);
2352          }
2353       }
2354    }
2355    else if(_class)
2356    {
2357       Class sClass;
2358       for(sClass = _class; sClass; sClass = sClass.base)
2359       {
2360          ClassTemplateParameter p;
2361          for(p = sClass.templateParams.first; p; p = p.next)
2362          {
2363             //OldList * specs = MkList();
2364             //Declarator decl = null;
2365             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2366             if(p.type == type)
2367             {
2368                TemplateParameter param = p.param;
2369                TemplatedType type;
2370                if(!param)
2371                {
2372                   // ADD DATA TYPE HERE...
2373                   p.param = param = TemplateParameter
2374                   {
2375                      identifier = MkIdentifier(p.name), type = p.type,
2376                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2377                   };
2378                }
2379                type = TemplatedType { key = (uintptr)p.name, param = param };
2380                curContext.templateTypes.Add((BTNode)type);
2381             }
2382          }
2383       }
2384    }
2385    return context;
2386 }
2387
2388 public void FinishTemplatesContext(Context context)
2389 {
2390    PopContext(context);
2391    FreeContext(context);
2392    delete context;
2393 }
2394
2395 public void ProcessMethodType(Method method)
2396 {
2397    if(!method.dataType)
2398    {
2399       Context context = SetupTemplatesContext(method._class);
2400
2401       method.dataType = ProcessTypeString(method.dataTypeString, false);
2402
2403       FinishTemplatesContext(context);
2404
2405       if(method.type != virtualMethod && method.dataType)
2406       {
2407          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2408          {
2409             if(!method._class.symbol)
2410                method._class.symbol = FindClass(method._class.fullName);
2411             method.dataType.thisClass = method._class.symbol;
2412          }
2413       }
2414
2415       // Why was this commented out? Working fine without now...
2416
2417       /*
2418       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2419          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2420          */
2421    }
2422
2423    /*
2424    if(type)
2425    {
2426       char * par = strstr(type, "(");
2427       char * classOp = null;
2428       int classOpLen = 0;
2429       if(par)
2430       {
2431          int c;
2432          for(c = par-type-1; c >= 0; c++)
2433          {
2434             if(type[c] == ':' && type[c+1] == ':')
2435             {
2436                classOp = type + c - 1;
2437                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2438                {
2439                   classOp--;
2440                   classOpLen++;
2441                }
2442                break;
2443             }
2444             else if(!isspace(type[c]))
2445                break;
2446          }
2447       }
2448       if(classOp)
2449       {
2450          char temp[1024];
2451          int typeLen = strlen(type);
2452          memcpy(temp, classOp, classOpLen);
2453          temp[classOpLen] = '\0';
2454          if(temp[0])
2455             _class = eSystem_FindClass(module, temp);
2456          else
2457             _class = null;
2458          method.dataTypeString = new char[typeLen - classOpLen + 1];
2459          memcpy(method.dataTypeString, type, classOp - type);
2460          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2461       }
2462       else
2463          method.dataTypeString = type;
2464    }
2465    */
2466 }
2467
2468
2469 public void ProcessPropertyType(Property prop)
2470 {
2471    if(!prop.dataType)
2472    {
2473       Context context = SetupTemplatesContext(prop._class);
2474       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2475       FinishTemplatesContext(context);
2476    }
2477 }
2478
2479 public void DeclareMethod(Method method, const char * name)
2480 {
2481    Symbol symbol = method.symbol;
2482    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2483    {
2484       bool imported = false;
2485       bool dllImport = false;
2486
2487       if(!method.dataType)
2488          method.dataType = ProcessTypeString(method.dataTypeString, false);
2489
2490       if(!symbol || symbol._import || method.type == virtualMethod)
2491       {
2492          if(!symbol || method.type == virtualMethod)
2493          {
2494             Symbol classSym;
2495             if(!method._class.symbol)
2496                method._class.symbol = FindClass(method._class.fullName);
2497             classSym = method._class.symbol;
2498             if(!classSym._import)
2499             {
2500                ModuleImport module;
2501
2502                if(method._class.module && method._class.module.name)
2503                   module = FindModule(method._class.module);
2504                else
2505                   module = mainModule;
2506                classSym._import = ClassImport
2507                {
2508                   name = CopyString(method._class.fullName);
2509                   isRemote = method._class.isRemote;
2510                };
2511                module.classes.Add(classSym._import);
2512             }
2513             if(!symbol)
2514             {
2515                symbol = method.symbol = Symbol { };
2516             }
2517             if(!symbol._import)
2518             {
2519                symbol._import = (ClassImport)MethodImport
2520                {
2521                   name = CopyString(method.name);
2522                   isVirtual = method.type == virtualMethod;
2523                };
2524                classSym._import.methods.Add(symbol._import);
2525             }
2526             if(!symbol)
2527             {
2528                // Set the symbol type
2529                /*
2530                if(!type.thisClass)
2531                {
2532                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2533                }
2534                else if(type.thisClass == (void *)-1)
2535                {
2536                   type.thisClass = null;
2537                }
2538                */
2539                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2540                symbol.type = method.dataType;
2541                if(symbol.type) symbol.type.refCount++;
2542             }
2543             /*
2544             if(!method.thisClass || strcmp(method.thisClass, "void"))
2545                symbol.type.params.Insert(null,
2546                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2547             */
2548          }
2549          if(!method.dataType.dllExport)
2550          {
2551             imported = true;
2552             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2553                dllImport = true;
2554          }
2555       }
2556
2557       /* MOVING THIS UP
2558       if(!method.dataType)
2559          method.dataType = ((Symbol)method.symbol).type;
2560          //ProcessMethodType(method);
2561       */
2562
2563       if(method.type != virtualMethod && method.dataType)
2564          DeclareType(method.dataType, true, true);
2565
2566       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2567       {
2568          // We need a declaration here :)
2569          Declaration decl;
2570          OldList * specifiers, * declarators;
2571          Declarator d;
2572          Declarator funcDecl;
2573          External external;
2574
2575          specifiers = MkList();
2576          declarators = MkList();
2577
2578          //if(imported)
2579          if(dllImport)
2580             ListAdd(specifiers, MkSpecifier(EXTERN));
2581          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2582             ListAdd(specifiers, MkSpecifier(STATIC));
2583
2584          if(method.type == virtualMethod)
2585          {
2586             ListAdd(specifiers, MkSpecifier(INT));
2587             d = MkDeclaratorIdentifier(MkIdentifier(name));
2588          }
2589          else
2590          {
2591             d = MkDeclaratorIdentifier(MkIdentifier(name));
2592             //if(imported)
2593             if(dllImport)
2594                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2595             {
2596                Context context = SetupTemplatesContext(method._class);
2597                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2598                FinishTemplatesContext(context);
2599             }
2600             funcDecl = GetFuncDecl(d);
2601
2602             if(dllImport)
2603             {
2604                Specifier spec, next;
2605                for(spec = specifiers->first; spec; spec = next)
2606                {
2607                   next = spec.next;
2608                   if(spec.type == extendedSpecifier)
2609                   {
2610                      specifiers->Remove(spec);
2611                      FreeSpecifier(spec);
2612                   }
2613                }
2614             }
2615
2616             // Add this parameter if not a static method
2617             if(method.dataType && !method.dataType.staticMethod)
2618             {
2619                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2620                {
2621                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2622                   TypeName thisParam = MkTypeName(MkListOne(
2623                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2624                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2625                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2626                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2627
2628                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2629                   {
2630                      TypeName param = funcDecl.function.parameters->first;
2631                      funcDecl.function.parameters->Remove(param);
2632                      FreeTypeName(param);
2633                   }
2634
2635                   if(!funcDecl.function.parameters)
2636                      funcDecl.function.parameters = MkList();
2637                   funcDecl.function.parameters->Insert(null, thisParam);
2638                }
2639             }
2640             // Make sure we don't have empty parameter declarations for static methods...
2641             /*
2642             else if(!funcDecl.function.parameters)
2643             {
2644                funcDecl.function.parameters = MkList();
2645                funcDecl.function.parameters->Insert(null,
2646                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2647             }*/
2648          }
2649          // TESTING THIS:
2650          ProcessDeclarator(d);
2651
2652          ListAdd(declarators, MkInitDeclarator(d, null));
2653
2654          decl = MkDeclaration(specifiers, declarators);
2655
2656          ReplaceThisClassSpecifiers(specifiers, method._class);
2657
2658          // Keep a different symbol for the function definition than the declaration...
2659          if(symbol.pointerExternal)
2660          {
2661             Symbol functionSymbol { };
2662
2663             // Copy symbol
2664             {
2665                *functionSymbol = *symbol;
2666                functionSymbol.string = CopyString(symbol.string);
2667                if(functionSymbol.type)
2668                   functionSymbol.type.refCount++;
2669             }
2670
2671             excludedSymbols->Add(functionSymbol);
2672             symbol.pointerExternal.symbol = functionSymbol;
2673          }
2674          external = MkExternalDeclaration(decl);
2675          if(curExternal)
2676             ast->Insert(curExternal ? curExternal.prev : null, external);
2677          external.symbol = symbol;
2678          symbol.pointerExternal = external;
2679       }
2680       else if(ast)
2681       {
2682          // Move declaration higher...
2683          ast->Move(symbol.pointerExternal, curExternal.prev);
2684       }
2685
2686       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2687    }
2688 }
2689
2690 char * ReplaceThisClass(Class _class)
2691 {
2692    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2693    {
2694       bool first = true;
2695       int p = 0;
2696       ClassTemplateParameter param;
2697       int lastParam = -1;
2698
2699       char className[1024];
2700       strcpy(className, _class.fullName);
2701       for(param = _class.templateParams.first; param; param = param.next)
2702       {
2703          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2704          {
2705             if(first) strcat(className, "<");
2706             if(!first) strcat(className, ", ");
2707             if(lastParam + 1 != p)
2708             {
2709                strcat(className, param.name);
2710                strcat(className, " = ");
2711             }
2712             strcat(className, param.name);
2713             first = false;
2714             lastParam = p;
2715          }
2716          p++;
2717       }
2718       if(!first)
2719       {
2720          int len = strlen(className);
2721          if(className[len-1] == '>') className[len++] = ' ';
2722          className[len++] = '>';
2723          className[len++] = '\0';
2724       }
2725       return CopyString(className);
2726    }
2727    else
2728       return CopyString(_class.fullName);
2729 }
2730
2731 Type ReplaceThisClassType(Class _class)
2732 {
2733    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2734    {
2735       bool first = true;
2736       int p = 0;
2737       ClassTemplateParameter param;
2738       int lastParam = -1;
2739       char className[1024];
2740       strcpy(className, _class.fullName);
2741
2742       for(param = _class.templateParams.first; param; param = param.next)
2743       {
2744          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2745          {
2746             if(first) strcat(className, "<");
2747             if(!first) strcat(className, ", ");
2748             if(lastParam + 1 != p)
2749             {
2750                strcat(className, param.name);
2751                strcat(className, " = ");
2752             }
2753             strcat(className, param.name);
2754             first = false;
2755             lastParam = p;
2756          }
2757          p++;
2758       }
2759       if(!first)
2760       {
2761          int len = strlen(className);
2762          if(className[len-1] == '>') className[len++] = ' ';
2763          className[len++] = '>';
2764          className[len++] = '\0';
2765       }
2766       return MkClassType(className);
2767       //return ProcessTypeString(className, false);
2768    }
2769    else
2770    {
2771       return MkClassType(_class.fullName);
2772       //return ProcessTypeString(_class.fullName, false);
2773    }
2774 }
2775
2776 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2777 {
2778    if(specs != null && _class)
2779    {
2780       Specifier spec;
2781       for(spec = specs.first; spec; spec = spec.next)
2782       {
2783          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2784          {
2785             spec.type = nameSpecifier;
2786             spec.name = ReplaceThisClass(_class);
2787             spec.symbol = FindClass(spec.name); //_class.symbol;
2788          }
2789       }
2790    }
2791 }
2792
2793 // Returns imported or not
2794 bool DeclareFunction(GlobalFunction function, char * name)
2795 {
2796    Symbol symbol = function.symbol;
2797    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2798    {
2799       bool imported = false;
2800       bool dllImport = false;
2801
2802       if(!function.dataType)
2803       {
2804          function.dataType = ProcessTypeString(function.dataTypeString, false);
2805          if(!function.dataType.thisClass)
2806             function.dataType.staticMethod = true;
2807       }
2808
2809       if(inCompiler)
2810       {
2811          if(!symbol)
2812          {
2813             ModuleImport module = FindModule(function.module);
2814             // WARNING: This is not added anywhere...
2815             symbol = function.symbol = Symbol {  };
2816
2817             if(module.name)
2818             {
2819                if(!function.dataType.dllExport)
2820                {
2821                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2822                   module.functions.Add(symbol._import);
2823                }
2824             }
2825             // Set the symbol type
2826             {
2827                symbol.type = ProcessTypeString(function.dataTypeString, false);
2828                if(!symbol.type.thisClass)
2829                   symbol.type.staticMethod = true;
2830             }
2831          }
2832          imported = symbol._import ? true : false;
2833          if(imported && function.module != privateModule && function.module.importType != staticImport)
2834             dllImport = true;
2835       }
2836
2837       DeclareType(function.dataType, true, true);
2838
2839       if(inCompiler)
2840       {
2841          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2842          {
2843             // We need a declaration here :)
2844             Declaration decl;
2845             OldList * specifiers, * declarators;
2846             Declarator d;
2847             Declarator funcDecl;
2848             External external;
2849
2850             specifiers = MkList();
2851             declarators = MkList();
2852
2853             //if(imported)
2854                ListAdd(specifiers, MkSpecifier(EXTERN));
2855             /*
2856             else
2857                ListAdd(specifiers, MkSpecifier(STATIC));
2858             */
2859
2860             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2861             //if(imported)
2862             if(dllImport)
2863                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2864
2865             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2866             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2867             if(function.module.importType == staticImport)
2868             {
2869                Specifier spec;
2870                for(spec = specifiers->first; spec; spec = spec.next)
2871                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2872                   {
2873                      specifiers->Remove(spec);
2874                      FreeSpecifier(spec);
2875                      break;
2876                   }
2877             }
2878
2879             funcDecl = GetFuncDecl(d);
2880
2881             // Make sure we don't have empty parameter declarations for static methods...
2882             if(funcDecl && !funcDecl.function.parameters)
2883             {
2884                funcDecl.function.parameters = MkList();
2885                funcDecl.function.parameters->Insert(null,
2886                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2887             }
2888
2889             ListAdd(declarators, MkInitDeclarator(d, null));
2890
2891             {
2892                Context oldCtx = curContext;
2893                curContext = globalContext;
2894                decl = MkDeclaration(specifiers, declarators);
2895                curContext = oldCtx;
2896             }
2897
2898             // Keep a different symbol for the function definition than the declaration...
2899             if(symbol.pointerExternal)
2900             {
2901                Symbol functionSymbol { };
2902                // Copy symbol
2903                {
2904                   *functionSymbol = *symbol;
2905                   functionSymbol.string = CopyString(symbol.string);
2906                   if(functionSymbol.type)
2907                      functionSymbol.type.refCount++;
2908                }
2909
2910                excludedSymbols->Add(functionSymbol);
2911
2912                symbol.pointerExternal.symbol = functionSymbol;
2913             }
2914             external = MkExternalDeclaration(decl);
2915             if(curExternal)
2916                ast->Insert(curExternal.prev, external);
2917             external.symbol = symbol;
2918             symbol.pointerExternal = external;
2919          }
2920          else
2921          {
2922             // Move declaration higher...
2923             ast->Move(symbol.pointerExternal, curExternal.prev);
2924          }
2925
2926          if(curExternal)
2927             symbol.id = curExternal.symbol.idCode;
2928       }
2929    }
2930    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2931 }
2932
2933 void DeclareGlobalData(GlobalData data)
2934 {
2935    Symbol symbol = data.symbol;
2936    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2937    {
2938       if(inCompiler)
2939       {
2940          if(!symbol)
2941             symbol = data.symbol = Symbol { };
2942       }
2943       if(!data.dataType)
2944          data.dataType = ProcessTypeString(data.dataTypeString, false);
2945       DeclareType(data.dataType, true, true);
2946       if(inCompiler)
2947       {
2948          if(!symbol.pointerExternal)
2949          {
2950             // We need a declaration here :)
2951             Declaration decl;
2952             OldList * specifiers, * declarators;
2953             Declarator d;
2954             External external;
2955
2956             specifiers = MkList();
2957             declarators = MkList();
2958
2959             ListAdd(specifiers, MkSpecifier(EXTERN));
2960             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2961             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2962
2963             ListAdd(declarators, MkInitDeclarator(d, null));
2964
2965             decl = MkDeclaration(specifiers, declarators);
2966             external = MkExternalDeclaration(decl);
2967             if(curExternal)
2968                ast->Insert(curExternal.prev, external);
2969             external.symbol = symbol;
2970             symbol.pointerExternal = external;
2971          }
2972          else
2973          {
2974             // Move declaration higher...
2975             ast->Move(symbol.pointerExternal, curExternal.prev);
2976          }
2977
2978          if(curExternal)
2979             symbol.id = curExternal.symbol.idCode;
2980       }
2981    }
2982 }
2983
2984 class Conversion : struct
2985 {
2986    Conversion prev, next;
2987    Property convert;
2988    bool isGet;
2989    Type resultType;
2990 };
2991
2992 static bool CheckConstCompatibility(Type source, Type dest, bool warn)
2993 {
2994    bool status = true;
2995    if(((source.kind == classType && source._class && source._class.registered) || source.kind == arrayType || source.kind == pointerType) &&
2996       ((dest.kind == classType && dest._class && dest._class.registered) || /*dest.kind == arrayType || */dest.kind == pointerType))
2997    {
2998       Class sourceClass = source.kind == classType ? source._class.registered : null;
2999       Class destClass = dest.kind == classType ? dest._class.registered : null;
3000       if((!sourceClass || (sourceClass && sourceClass.type == normalClass && !sourceClass.structSize)) &&
3001          (!destClass || (destClass && destClass.type == normalClass && !destClass.structSize)))
3002       {
3003          Type sourceType = source, destType = dest;
3004          while((sourceType.kind == pointerType || sourceType.kind == arrayType) && sourceType.type) sourceType = sourceType.type;
3005          while((destType.kind == pointerType || destType.kind == arrayType) && destType.type) destType = destType.type;
3006          if(!destType.constant && sourceType.constant)
3007          {
3008             status = false;
3009             if(warn)
3010                Compiler_Warning($"discarding const qualifier\n");
3011          }
3012       }
3013    }
3014    return status;
3015 }
3016
3017 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams,
3018                        bool isConversionExploration, bool warnConst)
3019 {
3020    if(source && dest)
3021    {
3022       if(warnConst)
3023          CheckConstCompatibility(source, dest, true);
3024       // Property convert;
3025
3026       if(source.kind == templateType && dest.kind != templateType)
3027       {
3028          Type type = ProcessTemplateParameterType(source.templateParameter);
3029          if(type) source = type;
3030       }
3031
3032       if(dest.kind == templateType && source.kind != templateType)
3033       {
3034          Type type = ProcessTemplateParameterType(dest.templateParameter);
3035          if(type) dest = type;
3036       }
3037
3038       if(dest.classObjectType == typedObject && dest.kind != functionType)
3039       {
3040          if(source.classObjectType != anyObject)
3041             return true;
3042          else
3043          {
3044             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
3045             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
3046             {
3047                return true;
3048             }
3049          }
3050       }
3051       else
3052       {
3053          if(source.kind != functionType && source.classObjectType == anyObject)
3054             return true;
3055          if(dest.kind != functionType && dest.classObjectType == anyObject && source.classObjectType != typedObject)
3056             return true;
3057       }
3058
3059       if((dest.kind == structType && source.kind == structType) ||
3060          (dest.kind == unionType && source.kind == unionType))
3061       {
3062          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
3063              (source.members.first && source.members.first == dest.members.first))
3064             return true;
3065       }
3066
3067       if(dest.kind == ellipsisType && source.kind != voidType)
3068          return true;
3069
3070       if(dest.kind == pointerType && dest.type.kind == voidType &&
3071          ((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))
3072          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
3073
3074          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
3075
3076          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
3077          return true;
3078       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
3079          ((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))
3080          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
3081          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
3082
3083          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
3084          return true;
3085
3086       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
3087       {
3088          if(source._class.registered && source._class.registered.type == unitClass)
3089          {
3090             if(conversions != null)
3091             {
3092                if(source._class.registered == dest._class.registered)
3093                   return true;
3094             }
3095             else
3096             {
3097                Class sourceBase, destBase;
3098                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
3099                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
3100                if(sourceBase == destBase)
3101                   return true;
3102             }
3103          }
3104          // Don't match enum inheriting from other enum if resolving enumeration values
3105          // TESTING: !dest.classObjectType
3106          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
3107             (enumBaseType ||
3108                (!source._class.registered || source._class.registered.type != enumClass) ||
3109                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
3110             return true;
3111          else
3112          {
3113             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
3114             if(enumBaseType &&
3115                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
3116                ((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)
3117             {
3118                if(eClass_IsDerived(dest._class.registered, source._class.registered))
3119                {
3120                   return true;
3121                }
3122             }
3123          }
3124       }
3125
3126       // JUST ADDED THIS...
3127       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3128          return true;
3129
3130       if(doConversion)
3131       {
3132          // Just added this for Straight conversion of ColorAlpha => Color
3133          if(source.kind == classType)
3134          {
3135             Class _class;
3136             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3137             {
3138                Property convert;
3139                for(convert = _class.conversions.first; convert; convert = convert.next)
3140                {
3141                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3142                   {
3143                      Conversion after = (conversions != null) ? conversions.last : null;
3144
3145                      if(!convert.dataType)
3146                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3147                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3148                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3149                         MatchTypes(convert.dataType, dest, conversions, null, null,
3150                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3151                               convert.dataType.kind == classType, false, true, warnConst))
3152                      {
3153                         if(!conversions && !convert.Get)
3154                            return true;
3155                         else if(conversions != null)
3156                         {
3157                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3158                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3159                               (dest.kind != classType || dest._class.registered != _class.base))
3160                               return true;
3161                            else
3162                            {
3163                               Conversion conv { convert = convert, isGet = true };
3164                               // conversions.Add(conv);
3165                               conversions.Insert(after, conv);
3166
3167                               return true;
3168                            }
3169                         }
3170                      }
3171                   }
3172                }
3173             }
3174          }
3175
3176          // MOVING THIS??
3177
3178          if(dest.kind == classType)
3179          {
3180             Class _class;
3181             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3182             {
3183                Property convert;
3184                for(convert = _class.conversions.first; convert; convert = convert.next)
3185                {
3186                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3187                   {
3188                      Type constType = null;
3189                      bool success = false;
3190                      // Conversion after = (conversions != null) ? conversions.last : null;
3191
3192                      if(!convert.dataType)
3193                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3194
3195                      if(warnConst && convert.dataType.kind == pointerType && convert.dataType.type && dest.constant)
3196                      {
3197                         Type ptrType { };
3198                         constType = { kind = pointerType, refCount = 1, type = ptrType };
3199                         CopyTypeInto(ptrType, convert.dataType.type);
3200                         ptrType.constant = true;
3201                      }
3202
3203                      // Just added this equality check to prevent recursion.... Make it safer?
3204                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3205                      if((constType || convert.dataType != dest) && MatchTypes(source, constType ? constType : convert.dataType, conversions, null, null, true, false /*true*/, false, true, warnConst))
3206                      {
3207                         if(!conversions && !convert.Set)
3208                            success = true;
3209                         else if(conversions != null)
3210                         {
3211                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3212                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3213                               (source.kind != classType || source._class.registered != _class.base))
3214                               success = true;
3215                            else
3216                            {
3217                               // *** Testing this! ***
3218                               Conversion conv { convert = convert };
3219                               conversions.Add(conv);
3220                               //conversions.Insert(after, conv);
3221                               success = true;
3222                            }
3223                         }
3224                      }
3225                      if(constType)
3226                         FreeType(constType);
3227                      if(success)
3228                         return true;
3229                   }
3230                }
3231             }
3232             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3233             {
3234                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3235                   (source.kind != classType || source._class.registered.type != structClass))
3236                   return true;
3237             }*/
3238
3239             // TESTING THIS... IS THIS OK??
3240             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3241             {
3242                if(!dest._class.registered.dataType)
3243                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3244                // Only support this for classes...
3245                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3246                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3247                {
3248                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false, warnConst))
3249                   {
3250                      return true;
3251                   }
3252                }
3253             }
3254          }
3255
3256          // Moved this lower
3257          if(source.kind == classType)
3258          {
3259             Class _class;
3260             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3261             {
3262                Property convert;
3263                for(convert = _class.conversions.first; convert; convert = convert.next)
3264                {
3265                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3266                   {
3267                      Conversion after = (conversions != null) ? conversions.last : null;
3268
3269                      if(!convert.dataType)
3270                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3271                      if(convert.dataType != source &&
3272                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3273                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true, warnConst))
3274                      {
3275                         if(!conversions && !convert.Get)
3276                            return true;
3277                         else if(conversions != null)
3278                         {
3279                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3280                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3281                               (dest.kind != classType || dest._class.registered != _class.base))
3282                               return true;
3283                            else
3284                            {
3285                               Conversion conv { convert = convert, isGet = true };
3286
3287                               // conversions.Add(conv);
3288                               conversions.Insert(after, conv);
3289                               return true;
3290                            }
3291                         }
3292                      }
3293                   }
3294                }
3295             }
3296
3297             // TESTING THIS... IS THIS OK??
3298             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3299             {
3300                if(!source._class.registered.dataType)
3301                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3302                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3303                {
3304                   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))
3305                      return true;
3306                   // For bool to be accepted by byte, short, etc.
3307                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false, warnConst))
3308                      return true;
3309                }
3310             }
3311          }
3312       }
3313
3314       if(source.kind == classType || source.kind == subClassType)
3315          ;
3316       else if(dest.kind == source.kind &&
3317          (dest.kind != structType && dest.kind != unionType &&
3318           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3319           return true;
3320       // RECENTLY ADDED THESE
3321       else if(dest.kind == doubleType && source.kind == floatType)
3322          return true;
3323       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3324          return true;
3325       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3326          return true;
3327       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3328          return true;
3329       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3330          return true;
3331       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3332          return true;
3333       else if(source.kind == enumType &&
3334          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3335           return true;
3336       else if(dest.kind == enumType && !isConversionExploration &&
3337          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3338           return true;
3339       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3340               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3341       {
3342          Type paramSource, paramDest;
3343
3344          if(dest.kind == methodType)
3345             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3346          if(source.kind == methodType)
3347             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3348
3349          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3350          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3351          if(dest.kind == methodType)
3352             dest = dest.method.dataType;
3353          if(source.kind == methodType)
3354             source = source.method.dataType;
3355
3356          paramSource = source.params.first;
3357          if(paramSource && paramSource.kind == voidType) paramSource = null;
3358          paramDest = dest.params.first;
3359          if(paramDest && paramDest.kind == voidType) paramDest = null;
3360
3361
3362          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3363             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3364          {
3365             // Source thisClass must be derived from destination thisClass
3366             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3367                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3368             {
3369                if(paramDest && paramDest.kind == classType)
3370                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3371                else
3372                   Compiler_Error($"method class should not take an object\n");
3373                return false;
3374             }
3375             paramDest = paramDest.next;
3376          }
3377          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3378          {
3379             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3380             {
3381                if(dest.thisClass)
3382                {
3383                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3384                   {
3385                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3386                      return false;
3387                   }
3388                }
3389                else
3390                {
3391                   // THIS WAS BACKWARDS:
3392                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3393                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3394                   {
3395                      if(owningClassDest)
3396                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3397                      else
3398                         Compiler_Error($"overriding class expected to be derived from method class\n");
3399                      return false;
3400                   }
3401                }
3402                paramSource = paramSource.next;
3403             }
3404             else
3405             {
3406                if(dest.thisClass)
3407                {
3408                   // Source thisClass must be derived from destination thisClass
3409                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3410                   {
3411                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3412                      return false;
3413                   }
3414                }
3415                else
3416                {
3417                   // THIS WAS BACKWARDS TOO??
3418                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3419                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3420                   {
3421                      //if(owningClass)
3422                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3423                      //else
3424                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3425                      return false;
3426                   }
3427                }
3428             }
3429          }
3430
3431
3432          // Source return type must be derived from destination return type
3433          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false, warnConst))
3434          {
3435             Compiler_Warning($"incompatible return type for function\n");
3436             return false;
3437          }
3438          // The const check is backwards from the MatchTypes above (for derivative classes checks)
3439          else
3440             CheckConstCompatibility(dest.returnType, source.returnType, true);
3441
3442          // Check parameters
3443
3444          for(; paramDest; paramDest = paramDest.next)
3445          {
3446             if(!paramSource)
3447             {
3448                //Compiler_Warning($"not enough parameters\n");
3449                Compiler_Error($"not enough parameters\n");
3450                return false;
3451             }
3452             {
3453                Type paramDestType = paramDest;
3454                Type paramSourceType = paramSource;
3455                Type type = paramDestType;
3456
3457                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3458                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3459                   paramSource.kind != templateType)
3460                {
3461                   int id = 0;
3462                   ClassTemplateParameter curParam = null;
3463                   Class sClass;
3464                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3465                   {
3466                      id = 0;
3467                      if(sClass.templateClass) sClass = sClass.templateClass;
3468                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3469                      {
3470                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3471                         {
3472                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3473                            {
3474                               if(sClass.templateClass) sClass = sClass.templateClass;
3475                               id += sClass.templateParams.count;
3476                            }
3477                            break;
3478                         }
3479                         id++;
3480                      }
3481                      if(curParam) break;
3482                   }
3483
3484                   if(curParam)
3485                   {
3486                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3487                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3488                   }
3489                }
3490
3491                // paramDest must be derived from paramSource
3492                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false, warnConst) &&
3493                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false, warnConst)))
3494                {
3495                   char type[1024];
3496                   type[0] = 0;
3497                   PrintType(paramDest, type, false, true);
3498                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3499
3500                   if(paramDestType != paramDest)
3501                      FreeType(paramDestType);
3502                   return false;
3503                }
3504                if(paramDestType != paramDest)
3505                   FreeType(paramDestType);
3506             }
3507
3508             paramSource = paramSource.next;
3509          }
3510          if(paramSource)
3511          {
3512             Compiler_Error($"too many parameters\n");
3513             return false;
3514          }
3515          return true;
3516       }
3517       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3518       {
3519          return true;
3520       }
3521       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3522          (source.kind == arrayType || source.kind == pointerType))
3523       {
3524          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false, warnConst))
3525             return true;
3526       }
3527    }
3528    return false;
3529 }
3530
3531 static void FreeConvert(Conversion convert)
3532 {
3533    if(convert.resultType)
3534       FreeType(convert.resultType);
3535 }
3536
3537 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3538                               char * string, OldList conversions)
3539 {
3540    BTNamedLink link;
3541
3542    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3543    {
3544       Class _class = link.data;
3545       if(_class.type == enumClass)
3546       {
3547          OldList converts { };
3548          Type type { };
3549          type.kind = classType;
3550
3551          if(!_class.symbol)
3552             _class.symbol = FindClass(_class.fullName);
3553          type._class = _class.symbol;
3554
3555          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false, false))
3556          {
3557             NamedLink value;
3558             Class enumClass = eSystem_FindClass(privateModule, "enum");
3559             if(enumClass)
3560             {
3561                Class baseClass;
3562                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3563                {
3564                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3565                   for(value = e.values.first; value; value = value.next)
3566                   {
3567                      if(!strcmp(value.name, string))
3568                         break;
3569                   }
3570                   if(value)
3571                   {
3572                      FreeExpContents(sourceExp);
3573                      FreeType(sourceExp.expType);
3574
3575                      sourceExp.isConstant = true;
3576                      sourceExp.expType = MkClassType(baseClass.fullName);
3577                      //if(inCompiler)
3578                      {
3579                         char constant[256];
3580                         sourceExp.type = constantExp;
3581                         if(!strcmp(baseClass.dataTypeString, "int"))
3582                            sprintf(constant, "%d",(int)value.data);
3583                         else
3584                            sprintf(constant, "0x%X",(int)value.data);
3585                         sourceExp.constant = CopyString(constant);
3586                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3587                      }
3588
3589                      while(converts.first)
3590                      {
3591                         Conversion convert = converts.first;
3592                         converts.Remove(convert);
3593                         conversions.Add(convert);
3594                      }
3595                      delete type;
3596                      return true;
3597                   }
3598                }
3599             }
3600          }
3601          if(converts.first)
3602             converts.Free(FreeConvert);
3603          delete type;
3604       }
3605    }
3606    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3607       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3608          return true;
3609    return false;
3610 }
3611
3612 public bool ModuleVisibility(Module searchIn, Module searchFor)
3613 {
3614    SubModule subModule;
3615
3616    if(searchFor == searchIn)
3617       return true;
3618
3619    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3620    {
3621       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3622       {
3623          if(ModuleVisibility(subModule.module, searchFor))
3624             return true;
3625       }
3626    }
3627    return false;
3628 }
3629
3630 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3631 {
3632    Module module;
3633
3634    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3635       return true;
3636    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3637       return true;
3638    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3639       return true;
3640
3641    for(module = mainModule.application.allModules.first; module; module = module.next)
3642    {
3643       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3644          return true;
3645    }
3646    return false;
3647 }
3648
3649 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla, bool warnConst)
3650 {
3651    Type source;
3652    Type realDest = dest;
3653    Type backupSourceExpType = null;
3654    Expression computedExp = sourceExp;
3655    dest.refCount++;
3656
3657    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3658       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3659    {
3660       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3661       ComputeExpression(computedExp /*sourceExp*/);
3662    }
3663
3664    source = sourceExp.expType;
3665
3666    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3667    {
3668       if(computedExp != sourceExp)
3669       {
3670          FreeExpression(computedExp);
3671          computedExp = sourceExp;
3672       }
3673       FreeType(dest);
3674       return true;
3675    }
3676
3677    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3678    {
3679        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3680        {
3681           Class sourceBase, destBase;
3682           for(sourceBase = source._class.registered;
3683               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3684               sourceBase = sourceBase.base);
3685           for(destBase = dest._class.registered;
3686               destBase && destBase.base && destBase.base.type != systemClass;
3687               destBase = destBase.base);
3688           //if(source._class.registered == dest._class.registered)
3689           if(sourceBase == destBase)
3690           {
3691             if(computedExp != sourceExp)
3692             {
3693                FreeExpression(computedExp);
3694                computedExp = sourceExp;
3695             }
3696             FreeType(dest);
3697             return true;
3698          }
3699       }
3700    }
3701
3702    if(source)
3703    {
3704       OldList * specs;
3705       bool flag = false;
3706       int64 value = MAXINT;
3707
3708       source.refCount++;
3709
3710       if(computedExp.type == constantExp)
3711       {
3712          if(source.isSigned)
3713             value = strtoll(computedExp.constant, null, 0);
3714          else
3715             value = strtoull(computedExp.constant, null, 0);
3716       }
3717       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3718       {
3719          if(source.isSigned)
3720             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3721          else
3722             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3723       }
3724       if(computedExp != sourceExp)
3725       {
3726          FreeExpression(computedExp);
3727          computedExp = sourceExp;
3728       }
3729
3730       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3731          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3732       {
3733          FreeType(source);
3734          source = Type { kind = intType, isSigned = false, refCount = 1 };
3735       }
3736
3737       if(dest.kind == classType)
3738       {
3739          Class _class = dest._class ? dest._class.registered : null;
3740
3741          if(_class && _class.type == unitClass)
3742          {
3743             if(source.kind != classType)
3744             {
3745                Type tempType { };
3746                Type tempDest, tempSource;
3747
3748                for(; _class.base.type != systemClass; _class = _class.base);
3749                tempSource = dest;
3750                tempDest = tempType;
3751
3752                tempType.kind = classType;
3753                if(!_class.symbol)
3754                   _class.symbol = FindClass(_class.fullName);
3755
3756                tempType._class = _class.symbol;
3757                tempType.truth = dest.truth;
3758                if(tempType._class)
3759                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3760
3761                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3762                backupSourceExpType = sourceExp.expType;
3763                sourceExp.expType = dest; dest.refCount++;
3764                //sourceExp.expType = MkClassType(_class.fullName);
3765                flag = true;
3766
3767                delete tempType;
3768             }
3769          }
3770
3771
3772          // Why wasn't there something like this?
3773          if(_class && _class.type == bitClass && source.kind != classType)
3774          {
3775             if(!dest._class.registered.dataType)
3776                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3777             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false, warnConst))
3778             {
3779                FreeType(source);
3780                FreeType(sourceExp.expType);
3781                source = sourceExp.expType = MkClassType(dest._class.string);
3782                source.refCount++;
3783
3784                //source.kind = classType;
3785                //source._class = dest._class;
3786             }
3787          }
3788
3789          // Adding two enumerations
3790          /*
3791          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3792          {
3793             if(!source._class.registered.dataType)
3794                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3795             if(!dest._class.registered.dataType)
3796                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3797
3798             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3799             {
3800                FreeType(source);
3801                source = sourceExp.expType = MkClassType(dest._class.string);
3802                source.refCount++;
3803
3804                //source.kind = classType;
3805                //source._class = dest._class;
3806             }
3807          }*/
3808
3809          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3810          {
3811             OldList * specs = MkList();
3812             Declarator decl;
3813             char string[1024];
3814
3815             ReadString(string, sourceExp.string);
3816             decl = SpecDeclFromString(string, specs, null);
3817
3818             FreeExpContents(sourceExp);
3819             FreeType(sourceExp.expType);
3820
3821             sourceExp.type = classExp;
3822             sourceExp._classExp.specifiers = specs;
3823             sourceExp._classExp.decl = decl;
3824             sourceExp.expType = dest;
3825             dest.refCount++;
3826
3827             FreeType(source);
3828             FreeType(dest);
3829             if(backupSourceExpType) FreeType(backupSourceExpType);
3830             return true;
3831          }
3832       }
3833       else if(source.kind == classType)
3834       {
3835          Class _class = source._class ? source._class.registered : null;
3836
3837          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3838          {
3839             /*
3840             if(dest.kind != classType)
3841             {
3842                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3843                if(!source._class.registered.dataType)
3844                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3845
3846                FreeType(dest);
3847                dest = MkClassType(source._class.string);
3848                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3849                //   dest = MkClassType(source._class.string);
3850             }
3851             */
3852
3853             if(dest.kind != classType)
3854             {
3855                Type tempType { };
3856                Type tempDest, tempSource;
3857
3858                if(!source._class.registered.dataType)
3859                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3860
3861                for(; _class.base.type != systemClass; _class = _class.base);
3862                tempDest = source;
3863                tempSource = tempType;
3864                tempType.kind = classType;
3865                tempType._class = FindClass(_class.fullName);
3866                tempType.truth = source.truth;
3867                tempType.classObjectType = source.classObjectType;
3868
3869                if(tempType._class)
3870                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3871
3872                // PUT THIS BACK TESTING UNITS?
3873                if(conversions.last)
3874                {
3875                   ((Conversion)(conversions.last)).resultType = dest;
3876                   dest.refCount++;
3877                }
3878
3879                FreeType(sourceExp.expType);
3880                sourceExp.expType = MkClassType(_class.fullName);
3881                sourceExp.expType.truth = source.truth;
3882                sourceExp.expType.classObjectType = source.classObjectType;
3883
3884                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3885
3886                if(!sourceExp.destType)
3887                {
3888                   FreeType(sourceExp.destType);
3889                   sourceExp.destType = sourceExp.expType;
3890                   if(sourceExp.expType)
3891                      sourceExp.expType.refCount++;
3892                }
3893                //flag = true;
3894                //source = _class.dataType;
3895
3896
3897                // TOCHECK: TESTING THIS NEW CODE
3898                if(!_class.dataType)
3899                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3900                FreeType(dest);
3901                dest = MkClassType(source._class.string);
3902                dest.truth = source.truth;
3903                dest.classObjectType = source.classObjectType;
3904
3905                FreeType(source);
3906                source = _class.dataType;
3907                source.refCount++;
3908
3909                delete tempType;
3910             }
3911          }
3912       }
3913
3914       if(!flag)
3915       {
3916          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false, warnConst))
3917          {
3918             FreeType(source);
3919             FreeType(dest);
3920             return true;
3921          }
3922       }
3923
3924       // Implicit Casts
3925       /*
3926       if(source.kind == classType)
3927       {
3928          Class _class = source._class.registered;
3929          if(_class.type == unitClass)
3930          {
3931             if(!_class.dataType)
3932                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3933             source = _class.dataType;
3934          }
3935       }*/
3936
3937       if(dest.kind == classType)
3938       {
3939          Class _class = dest._class ? dest._class.registered : null;
3940          bool fittingValue = false;
3941          if(_class && _class.type == enumClass)
3942          {
3943             Class enumClass = eSystem_FindClass(privateModule, "enum");
3944             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3945             if(c && value >= 0 && value <= c.largest)
3946                fittingValue = true;
3947          }
3948
3949          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3950             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3951          {
3952             if(_class.type == normalClass || _class.type == noHeadClass)
3953             {
3954                Expression newExp { };
3955                *newExp = *sourceExp;
3956                if(sourceExp.destType) sourceExp.destType.refCount++;
3957                if(sourceExp.expType)  sourceExp.expType.refCount++;
3958                sourceExp.type = castExp;
3959                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3960                sourceExp.cast.exp = newExp;
3961                FreeType(sourceExp.expType);
3962                sourceExp.expType = null;
3963                ProcessExpressionType(sourceExp);
3964
3965                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3966                if(!inCompiler)
3967                {
3968                   FreeType(sourceExp.expType);
3969                   sourceExp.expType = dest;
3970                }
3971
3972                FreeType(source);
3973                if(inCompiler) FreeType(dest);
3974
3975                if(backupSourceExpType) FreeType(backupSourceExpType);
3976                return true;
3977             }
3978
3979             if(!_class.dataType)
3980                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3981             FreeType(dest);
3982             dest = _class.dataType;
3983             dest.refCount++;
3984          }
3985
3986          // Accept lower precision types for units, since we want to keep the unit type
3987          if(dest.kind == doubleType &&
3988             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3989              source.kind == charType || source.kind == _BoolType))
3990          {
3991             specs = MkListOne(MkSpecifier(DOUBLE));
3992          }
3993          else if(dest.kind == floatType &&
3994             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3995             source.kind == _BoolType || source.kind == doubleType))
3996          {
3997             specs = MkListOne(MkSpecifier(FLOAT));
3998          }
3999          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
4000             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4001          {
4002             specs = MkList();
4003             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4004             ListAdd(specs, MkSpecifier(INT64));
4005          }
4006          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
4007             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4008          {
4009             specs = MkList();
4010             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4011             ListAdd(specs, MkSpecifier(INT));
4012          }
4013          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
4014             source.kind == floatType || source.kind == doubleType))
4015          {
4016             specs = MkList();
4017             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4018             ListAdd(specs, MkSpecifier(SHORT));
4019          }
4020          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
4021             source.kind == floatType || source.kind == doubleType))
4022          {
4023             specs = MkList();
4024             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4025             ListAdd(specs, MkSpecifier(CHAR));
4026          }
4027          else
4028          {
4029             FreeType(source);
4030             FreeType(dest);
4031             if(backupSourceExpType)
4032             {
4033                // Failed to convert: revert previous exp type
4034                if(sourceExp.expType) FreeType(sourceExp.expType);
4035                sourceExp.expType = backupSourceExpType;
4036             }
4037             return false;
4038          }
4039       }
4040       else if(dest.kind == doubleType &&
4041          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
4042           source.kind == _BoolType || source.kind == charType))
4043       {
4044          specs = MkListOne(MkSpecifier(DOUBLE));
4045       }
4046       else if(dest.kind == floatType &&
4047          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4048       {
4049          specs = MkListOne(MkSpecifier(FLOAT));
4050       }
4051       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4052          (value == 1 || value == 0))
4053       {
4054          specs = MkList();
4055          ListAdd(specs, MkSpecifier(BOOL));
4056       }
4057       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4058          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
4059       {
4060          specs = MkList();
4061          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4062          ListAdd(specs, MkSpecifier(CHAR));
4063       }
4064       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
4065          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
4066       {
4067          specs = MkList();
4068          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4069          ListAdd(specs, MkSpecifier(SHORT));
4070       }
4071       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
4072       {
4073          specs = MkList();
4074          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4075          ListAdd(specs, MkSpecifier(INT));
4076       }
4077       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
4078       {
4079          specs = MkList();
4080          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4081          ListAdd(specs, MkSpecifier(INT64));
4082       }
4083       else if(dest.kind == enumType &&
4084          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4085       {
4086          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
4087       }
4088       else
4089       {
4090          FreeType(source);
4091          FreeType(dest);
4092          if(backupSourceExpType)
4093          {
4094             // Failed to convert: revert previous exp type
4095             if(sourceExp.expType) FreeType(sourceExp.expType);
4096             sourceExp.expType = backupSourceExpType;
4097          }
4098          return false;
4099       }
4100
4101       if(!flag && !sourceExp.opDestType)
4102       {
4103          Expression newExp { };
4104          *newExp = *sourceExp;
4105          newExp.prev = null;
4106          newExp.next = null;
4107          if(sourceExp.destType) sourceExp.destType.refCount++;
4108          if(sourceExp.expType)  sourceExp.expType.refCount++;
4109
4110          sourceExp.type = castExp;
4111          if(realDest.kind == classType)
4112          {
4113             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
4114             FreeList(specs, FreeSpecifier);
4115          }
4116          else
4117             sourceExp.cast.typeName = MkTypeName(specs, null);
4118          if(newExp.type == opExp)
4119          {
4120             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4121          }
4122          else
4123             sourceExp.cast.exp = newExp;
4124
4125          FreeType(sourceExp.expType);
4126          sourceExp.expType = null;
4127          ProcessExpressionType(sourceExp);
4128       }
4129       else
4130          FreeList(specs, FreeSpecifier);
4131
4132       FreeType(dest);
4133       FreeType(source);
4134       if(backupSourceExpType) FreeType(backupSourceExpType);
4135
4136       return true;
4137    }
4138    else
4139    {
4140       if(computedExp != sourceExp)
4141       {
4142          FreeExpression(computedExp);
4143          computedExp = sourceExp;
4144       }
4145
4146       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4147       if(sourceExp.type == identifierExp)
4148       {
4149          Identifier id = sourceExp.identifier;
4150          if(dest.kind == classType)
4151          {
4152             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4153             {
4154                Class _class = dest._class.registered;
4155                Class enumClass = eSystem_FindClass(privateModule, "enum");
4156                if(enumClass)
4157                {
4158                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4159                   {
4160                      NamedLink value;
4161                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4162                      for(value = e.values.first; value; value = value.next)
4163                      {
4164                         if(!strcmp(value.name, id.string))
4165                            break;
4166                      }
4167                      if(value)
4168                      {
4169                         FreeExpContents(sourceExp);
4170                         FreeType(sourceExp.expType);
4171
4172                         sourceExp.isConstant = true;
4173                         sourceExp.expType = MkClassType(_class.fullName);
4174                         //if(inCompiler)
4175                         {
4176                            char constant[256];
4177                            sourceExp.type = constantExp;
4178                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
4179                               sprintf(constant, "%d", (int) value.data);
4180                            else
4181                               sprintf(constant, "0x%X", (int) value.data);
4182                            sourceExp.constant = CopyString(constant);
4183                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4184                         }
4185                         FreeType(dest);
4186                         return true;
4187                      }
4188                   }
4189                }
4190             }
4191          }
4192
4193          // Loop through all enum classes
4194          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4195          {
4196             FreeType(dest);
4197             return true;
4198          }
4199       }
4200       FreeType(dest);
4201    }
4202    return false;
4203 }
4204
4205 #define TERTIARY(o, name, m, t, p) \
4206    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4207    {                                                              \
4208       exp.type = constantExp;                                    \
4209       exp.string = p(op1.m ? op2.m : op3.m);                     \
4210       if(!exp.expType) \
4211          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4212       return true;                                                \
4213    }
4214
4215 #define BINARY(o, name, m, t, p) \
4216    static bool name(Expression exp, Operand op1, Operand op2)   \
4217    {                                                              \
4218       t value2 = op2.m;                                           \
4219       exp.type = constantExp;                                    \
4220       exp.string = p((t)(op1.m o value2));                     \
4221       if(!exp.expType) \
4222          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4223       return true;                                                \
4224    }
4225
4226 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4227    static bool name(Expression exp, Operand op1, Operand op2)   \
4228    {                                                              \
4229       t value2 = op2.m;                                           \
4230       exp.type = constantExp;                                    \
4231       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4232       if(!exp.expType) \
4233          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4234       return true;                                                \
4235    }
4236
4237 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4238    static bool name(Expression exp, Operand op1, Operand op2)   \
4239    {                                                              \
4240       t value2 = op2.m;                                           \
4241       exp.type = constantExp;                                    \
4242       exp.string = p(op1.m o value2);             \
4243       if(!exp.expType) \
4244          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4245       return true;                                                \
4246    }
4247
4248 #define UNARY(o, name, m, t, p) \
4249    static bool name(Expression exp, Operand op1)                \
4250    {                                                              \
4251       exp.type = constantExp;                                    \
4252       exp.string = p((t)(o op1.m));                                   \
4253       if(!exp.expType) \
4254          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4255       return true;                                                \
4256    }
4257
4258 #define OPERATOR_ALL(macro, o, name) \
4259    macro(o, Int##name, i, int, PrintInt) \
4260    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4261    macro(o, Int64##name, i64, int64, PrintInt64) \
4262    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4263    macro(o, Short##name, s, short, PrintShort) \
4264    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4265    macro(o, Char##name, c, char, PrintChar) \
4266    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4267    macro(o, Float##name, f, float, PrintFloat) \
4268    macro(o, Double##name, d, double, PrintDouble)
4269
4270 #define OPERATOR_INTTYPES(macro, o, name) \
4271    macro(o, Int##name, i, int, PrintInt) \
4272    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4273    macro(o, Int64##name, i64, int64, PrintInt64) \
4274    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4275    macro(o, Short##name, s, short, PrintShort) \
4276    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4277    macro(o, Char##name, c, char, PrintChar) \
4278    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4279
4280 #define OPERATOR_REALTYPES(macro, o, name) \
4281    macro(o, Float##name, f, float, PrintFloat) \
4282    macro(o, Double##name, d, double, PrintDouble)
4283
4284 // binary arithmetic
4285 OPERATOR_ALL(BINARY, +, Add)
4286 OPERATOR_ALL(BINARY, -, Sub)
4287 OPERATOR_ALL(BINARY, *, Mul)
4288 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4289 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4290 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4291
4292 // unary arithmetic
4293 OPERATOR_ALL(UNARY, -, Neg)
4294
4295 // unary arithmetic increment and decrement
4296 OPERATOR_ALL(UNARY, ++, Inc)
4297 OPERATOR_ALL(UNARY, --, Dec)
4298
4299 // binary arithmetic assignment
4300 OPERATOR_ALL(BINARY, =, Asign)
4301 OPERATOR_ALL(BINARY, +=, AddAsign)
4302 OPERATOR_ALL(BINARY, -=, SubAsign)
4303 OPERATOR_ALL(BINARY, *=, MulAsign)
4304 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4305 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4306 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4307
4308 // binary bitwise
4309 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4310 OPERATOR_INTTYPES(BINARY, |, BitOr)
4311 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4312 OPERATOR_INTTYPES(BINARY, <<, LShift)
4313 OPERATOR_INTTYPES(BINARY, >>, RShift)
4314
4315 // unary bitwise
4316 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4317
4318 // binary bitwise assignment
4319 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4320 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4321 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4322 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4323 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4324
4325 // unary logical negation
4326 OPERATOR_INTTYPES(UNARY, !, Not)
4327
4328 // binary logical equality
4329 OPERATOR_ALL(BINARY, ==, Equ)
4330 OPERATOR_ALL(BINARY, !=, Nqu)
4331
4332 // binary logical
4333 OPERATOR_ALL(BINARY, &&, And)
4334 OPERATOR_ALL(BINARY, ||, Or)
4335
4336 // binary logical relational
4337 OPERATOR_ALL(BINARY, >, Grt)
4338 OPERATOR_ALL(BINARY, <, Sma)
4339 OPERATOR_ALL(BINARY, >=, GrtEqu)
4340 OPERATOR_ALL(BINARY, <=, SmaEqu)
4341
4342 // tertiary condition operator
4343 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4344
4345 //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
4346 #define OPERATOR_TABLE_ALL(name, type) \
4347     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4348                           type##Neg, \
4349                           type##Inc, type##Dec, \
4350                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4351                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4352                           type##BitNot, \
4353                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4354                           type##Not, \
4355                           type##Equ, type##Nqu, \
4356                           type##And, type##Or, \
4357                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4358                         }; \
4359
4360 #define OPERATOR_TABLE_INTTYPES(name, type) \
4361     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4362                           type##Neg, \
4363                           type##Inc, type##Dec, \
4364                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4365                           null, null, null, null, null, \
4366                           null, \
4367                           null, null, null, null, null, \
4368                           null, \
4369                           type##Equ, type##Nqu, \
4370                           type##And, type##Or, \
4371                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4372                         }; \
4373
4374 OPERATOR_TABLE_ALL(int, Int)
4375 OPERATOR_TABLE_ALL(uint, UInt)
4376 OPERATOR_TABLE_ALL(int64, Int64)
4377 OPERATOR_TABLE_ALL(uint64, UInt64)
4378 OPERATOR_TABLE_ALL(short, Short)
4379 OPERATOR_TABLE_ALL(ushort, UShort)
4380 OPERATOR_TABLE_INTTYPES(float, Float)
4381 OPERATOR_TABLE_INTTYPES(double, Double)
4382 OPERATOR_TABLE_ALL(char, Char)
4383 OPERATOR_TABLE_ALL(uchar, UChar)
4384
4385 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4386 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4387 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4388 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4389 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4390 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4391 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4392 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4393
4394 public void ReadString(char * output,  char * string)
4395 {
4396    int len = strlen(string);
4397    int c,d = 0;
4398    bool quoted = false, escaped = false;
4399    for(c = 0; c<len; c++)
4400    {
4401       char ch = string[c];
4402       if(escaped)
4403       {
4404          switch(ch)
4405          {
4406             case 'n': output[d] = '\n'; break;
4407             case 't': output[d] = '\t'; break;
4408             case 'a': output[d] = '\a'; break;
4409             case 'b': output[d] = '\b'; break;
4410             case 'f': output[d] = '\f'; break;
4411             case 'r': output[d] = '\r'; break;
4412             case 'v': output[d] = '\v'; break;
4413             case '\\': output[d] = '\\'; break;
4414             case '\"': output[d] = '\"'; break;
4415             case '\'': output[d] = '\''; break;
4416             default: output[d] = ch;
4417          }
4418          d++;
4419          escaped = false;
4420       }
4421       else
4422       {
4423          if(ch == '\"')
4424             quoted ^= true;
4425          else if(quoted)
4426          {
4427             if(ch == '\\')
4428                escaped = true;
4429             else
4430                output[d++] = ch;
4431          }
4432       }
4433    }
4434    output[d] = '\0';
4435 }
4436
4437 // String Unescape Copy
4438
4439 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4440 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4441 public int UnescapeString(char * d, char * s, int len)
4442 {
4443    int j = 0, k = 0;
4444    char ch;
4445    while(j < len && (ch = s[j]))
4446    {
4447       switch(ch)
4448       {
4449          case '\\':
4450             switch((ch = s[++j]))
4451             {
4452                case 'n': d[k] = '\n'; break;
4453                case 't': d[k] = '\t'; break;
4454                case 'a': d[k] = '\a'; break;
4455                case 'b': d[k] = '\b'; break;
4456                case 'f': d[k] = '\f'; break;
4457                case 'r': d[k] = '\r'; break;
4458                case 'v': d[k] = '\v'; break;
4459                case '\\': d[k] = '\\'; break;
4460                case '\"': d[k] = '\"'; break;
4461                case '\'': d[k] = '\''; break;
4462                default: d[k] = '\\'; d[k] = ch;
4463             }
4464             break;
4465          default:
4466             d[k] = ch;
4467       }
4468       j++, k++;
4469    }
4470    d[k] = '\0';
4471    return k;
4472 }
4473
4474 public char * OffsetEscapedString(char * s, int len, int offset)
4475 {
4476    char ch;
4477    int j = 0, k = 0;
4478    while(j < len && k < offset && (ch = s[j]))
4479    {
4480       if(ch == '\\') ++j;
4481       j++, k++;
4482    }
4483    return (k == offset) ? s + j : null;
4484 }
4485
4486 public Operand GetOperand(Expression exp)
4487 {
4488    Operand op { };
4489    Type type = exp.expType;
4490    if(type)
4491    {
4492       while(type.kind == classType &&
4493          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4494       {
4495          if(!type._class.registered.dataType)
4496             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4497          type = type._class.registered.dataType;
4498
4499       }
4500       if(exp.type == stringExp && op.kind == pointerType)
4501       {
4502          op.ui64 = (uint64)exp.string;
4503          op.kind = pointerType;
4504          op.ops = uint64Ops;
4505       }
4506       else if(exp.isConstant && exp.type == constantExp)
4507       {
4508          op.kind = type.kind;
4509          op.type = exp.expType;
4510
4511          switch(op.kind)
4512          {
4513             case _BoolType:
4514             case charType:
4515             {
4516                if(exp.constant[0] == '\'')
4517                {
4518                   op.c = exp.constant[1];
4519                   op.ops = charOps;
4520                }
4521                else if(type.isSigned)
4522                {
4523                   op.c = (char)strtol(exp.constant, null, 0);
4524                   op.ops = charOps;
4525                }
4526                else
4527                {
4528                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4529                   op.ops = ucharOps;
4530                }
4531                break;
4532             }
4533             case shortType:
4534                if(type.isSigned)
4535                {
4536                   op.s = (short)strtol(exp.constant, null, 0);
4537                   op.ops = shortOps;
4538                }
4539                else
4540                {
4541                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4542                   op.ops = ushortOps;
4543                }
4544                break;
4545             case intType:
4546             case longType:
4547                if(type.isSigned)
4548                {
4549                   op.i = (int)strtol(exp.constant, null, 0);
4550                   op.ops = intOps;
4551                }
4552                else
4553                {
4554                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4555                   op.ops = uintOps;
4556                }
4557                op.kind = intType;
4558                break;
4559             case int64Type:
4560                if(type.isSigned)
4561                {
4562                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4563                   op.ops = int64Ops;
4564                }
4565                else
4566                {
4567                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4568                   op.ops = uint64Ops;
4569                }
4570                op.kind = int64Type;
4571                break;
4572             case intPtrType:
4573                if(type.isSigned)
4574                {
4575                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4576                   op.ops = int64Ops;
4577                }
4578                else
4579                {
4580                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4581                   op.ops = uint64Ops;
4582                }
4583                op.kind = int64Type;
4584                break;
4585             case intSizeType:
4586                if(type.isSigned)
4587                {
4588                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4589                   op.ops = int64Ops;
4590                }
4591                else
4592                {
4593                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4594                   op.ops = uint64Ops;
4595                }
4596                op.kind = int64Type;
4597                break;
4598             case floatType:
4599                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4600                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4601                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4602                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4603                else
4604                   op.f = (float)strtod(exp.constant, null);
4605                op.ops = floatOps;
4606                break;
4607             case doubleType:
4608                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4609                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4610                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4611                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4612                else
4613                   op.d = (double)strtod(exp.constant, null);
4614                op.ops = doubleOps;
4615                break;
4616             //case classType:    For when we have operator overloading...
4617             // Pointer additions
4618             //case functionType:
4619             case arrayType:
4620             case pointerType:
4621             case classType:
4622                op.ui64 = _strtoui64(exp.constant, null, 0);
4623                op.kind = pointerType;
4624                op.ops = uint64Ops;
4625                // op.ptrSize =
4626                break;
4627          }
4628       }
4629    }
4630    return op;
4631 }
4632
4633 static __attribute__((unused)) void UnusedFunction()
4634 {
4635    int a;
4636    a.OnGetString(0,0,0);
4637 }
4638 default:
4639 extern int __ecereVMethodID_class_OnGetString;
4640 public:
4641
4642 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4643 {
4644    DataMember dataMember;
4645    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4646    {
4647       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4648          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4649       else
4650       {
4651          Expression exp { };
4652          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4653          Type type;
4654          void * ptr = inst.data + dataMember.offset + offset;
4655          char * result = null;
4656          exp.loc = member.loc = inst.loc;
4657          ((Identifier)member.identifiers->first).loc = inst.loc;
4658
4659          if(!dataMember.dataType)
4660             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4661          type = dataMember.dataType;
4662          if(type.kind == classType)
4663          {
4664             Class _class = type._class.registered;
4665             if(_class.type == enumClass)
4666             {
4667                Class enumClass = eSystem_FindClass(privateModule, "enum");
4668                if(enumClass)
4669                {
4670                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4671                   NamedLink item;
4672                   for(item = e.values.first; item; item = item.next)
4673                   {
4674                      if((int)item.data == *(int *)ptr)
4675                      {
4676                         result = item.name;
4677                         break;
4678                      }
4679                   }
4680                   if(result)
4681                   {
4682                      exp.identifier = MkIdentifier(result);
4683                      exp.type = identifierExp;
4684                      exp.destType = MkClassType(_class.fullName);
4685                      ProcessExpressionType(exp);
4686                   }
4687                }
4688             }
4689             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4690             {
4691                if(!_class.dataType)
4692                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4693                type = _class.dataType;
4694             }
4695          }
4696          if(!result)
4697          {
4698             switch(type.kind)
4699             {
4700                case floatType:
4701                {
4702                   FreeExpContents(exp);
4703
4704                   exp.constant = PrintFloat(*(float*)ptr);
4705                   exp.type = constantExp;
4706                   break;
4707                }
4708                case doubleType:
4709                {
4710                   FreeExpContents(exp);
4711
4712                   exp.constant = PrintDouble(*(double*)ptr);
4713                   exp.type = constantExp;
4714                   break;
4715                }
4716                case intType:
4717                {
4718                   FreeExpContents(exp);
4719
4720                   exp.constant = PrintInt(*(int*)ptr);
4721                   exp.type = constantExp;
4722                   break;
4723                }
4724                case int64Type:
4725                {
4726                   FreeExpContents(exp);
4727
4728                   exp.constant = PrintInt64(*(int64*)ptr);
4729                   exp.type = constantExp;
4730                   break;
4731                }
4732                case intPtrType:
4733                {
4734                   FreeExpContents(exp);
4735                   // TODO: This should probably use proper type
4736                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4737                   exp.type = constantExp;
4738                   break;
4739                }
4740                case intSizeType:
4741                {
4742                   FreeExpContents(exp);
4743                   // TODO: This should probably use proper type
4744                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4745                   exp.type = constantExp;
4746                   break;
4747                }
4748                default:
4749                   Compiler_Error($"Unhandled type populating instance\n");
4750             }
4751          }
4752          ListAdd(memberList, member);
4753       }
4754
4755       if(parentDataMember.type == unionMember)
4756          break;
4757    }
4758 }
4759
4760 void PopulateInstance(Instantiation inst)
4761 {
4762    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4763    Class _class = classSym.registered;
4764    DataMember dataMember;
4765    OldList * memberList = MkList();
4766    // Added this check and ->Add to prevent memory leaks on bad code
4767    if(!inst.members)
4768       inst.members = MkListOne(MkMembersInitList(memberList));
4769    else
4770       inst.members->Add(MkMembersInitList(memberList));
4771    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4772    {
4773       if(!dataMember.isProperty)
4774       {
4775          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4776             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4777          else
4778          {
4779             Expression exp { };
4780             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4781             Type type;
4782             void * ptr = inst.data + dataMember.offset;
4783             char * result = null;
4784
4785             exp.loc = member.loc = inst.loc;
4786             ((Identifier)member.identifiers->first).loc = inst.loc;
4787
4788             if(!dataMember.dataType)
4789                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4790             type = dataMember.dataType;
4791             if(type.kind == classType)
4792             {
4793                Class _class = type._class.registered;
4794                if(_class.type == enumClass)
4795                {
4796                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4797                   if(enumClass)
4798                   {
4799                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4800                      NamedLink item;
4801                      for(item = e.values.first; item; item = item.next)
4802                      {
4803                         if((int)item.data == *(int *)ptr)
4804                         {
4805                            result = item.name;
4806                            break;
4807                         }
4808                      }
4809                   }
4810                   if(result)
4811                   {
4812                      exp.identifier = MkIdentifier(result);
4813                      exp.type = identifierExp;
4814                      exp.destType = MkClassType(_class.fullName);
4815                      ProcessExpressionType(exp);
4816                   }
4817                }
4818                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4819                {
4820                   if(!_class.dataType)
4821                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4822                   type = _class.dataType;
4823                }
4824             }
4825             if(!result)
4826             {
4827                switch(type.kind)
4828                {
4829                   case floatType:
4830                   {
4831                      exp.constant = PrintFloat(*(float*)ptr);
4832                      exp.type = constantExp;
4833                      break;
4834                   }
4835                   case doubleType:
4836                   {
4837                      exp.constant = PrintDouble(*(double*)ptr);
4838                      exp.type = constantExp;
4839                      break;
4840                   }
4841                   case intType:
4842                   {
4843                      exp.constant = PrintInt(*(int*)ptr);
4844                      exp.type = constantExp;
4845                      break;
4846                   }
4847                   case int64Type:
4848                   {
4849                      exp.constant = PrintInt64(*(int64*)ptr);
4850                      exp.type = constantExp;
4851                      break;
4852                   }
4853                   case intPtrType:
4854                   {
4855                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4856                      exp.type = constantExp;
4857                      break;
4858                   }
4859                   default:
4860                      Compiler_Error($"Unhandled type populating instance\n");
4861                }
4862             }
4863             ListAdd(memberList, member);
4864          }
4865       }
4866    }
4867 }
4868
4869 void ComputeInstantiation(Expression exp)
4870 {
4871    Instantiation inst = exp.instance;
4872    MembersInit members;
4873    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4874    Class _class = classSym ? classSym.registered : null;
4875    DataMember curMember = null;
4876    Class curClass = null;
4877    DataMember subMemberStack[256];
4878    int subMemberStackPos = 0;
4879    uint64 bits = 0;
4880
4881    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4882    {
4883       // Don't recompute the instantiation...
4884       // Non Simple classes will have become constants by now
4885       if(inst.data)
4886          return;
4887
4888       if(_class.type == normalClass || _class.type == noHeadClass)
4889       {
4890          inst.data = (byte *)eInstance_New(_class);
4891          if(_class.type == normalClass)
4892             ((Instance)inst.data)._refCount++;
4893       }
4894       else
4895          inst.data = new0 byte[_class.structSize];
4896    }
4897
4898    if(inst.members)
4899    {
4900       for(members = inst.members->first; members; members = members.next)
4901       {
4902          switch(members.type)
4903          {
4904             case dataMembersInit:
4905             {
4906                if(members.dataMembers)
4907                {
4908                   MemberInit member;
4909                   for(member = members.dataMembers->first; member; member = member.next)
4910                   {
4911                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4912                      bool found = false;
4913
4914                      Property prop = null;
4915                      DataMember dataMember = null;
4916                      Method method = null;
4917                      uint dataMemberOffset;
4918
4919                      if(!ident)
4920                      {
4921                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4922                         if(curMember)
4923                         {
4924                            if(curMember.isProperty)
4925                               prop = (Property)curMember;
4926                            else
4927                            {
4928                               dataMember = curMember;
4929
4930                               // CHANGED THIS HERE
4931                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4932
4933                               // 2013/17/29 -- It seems that this was missing here!
4934                               if(_class.type == normalClass)
4935                                  dataMemberOffset += _class.base.structSize;
4936                               // dataMemberOffset = dataMember.offset;
4937                            }
4938                            found = true;
4939                         }
4940                      }
4941                      else
4942                      {
4943                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4944                         if(prop)
4945                         {
4946                            found = true;
4947                            if(prop.memberAccess == publicAccess)
4948                            {
4949                               curMember = (DataMember)prop;
4950                               curClass = prop._class;
4951                            }
4952                         }
4953                         else
4954                         {
4955                            DataMember _subMemberStack[256];
4956                            int _subMemberStackPos = 0;
4957
4958                            // FILL MEMBER STACK
4959                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4960
4961                            if(dataMember)
4962                            {
4963                               found = true;
4964                               if(dataMember.memberAccess == publicAccess)
4965                               {
4966                                  curMember = dataMember;
4967                                  curClass = dataMember._class;
4968                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4969                                  subMemberStackPos = _subMemberStackPos;
4970                               }
4971                            }
4972                         }
4973                      }
4974
4975                      if(found && member.initializer && member.initializer.type == expInitializer)
4976                      {
4977                         Expression value = member.initializer.exp;
4978                         Type type = null;
4979                         bool deepMember = false;
4980                         if(prop)
4981                         {
4982                            type = prop.dataType;
4983                         }
4984                         else if(dataMember)
4985                         {
4986                            if(!dataMember.dataType)
4987                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4988
4989                            type = dataMember.dataType;
4990                         }
4991
4992                         if(ident && ident.next)
4993                         {
4994                            deepMember = true;
4995
4996                            // for(; ident && type; ident = ident.next)
4997                            for(ident = ident.next; ident && type; ident = ident.next)
4998                            {
4999                               if(type.kind == classType)
5000                               {
5001                                  prop = eClass_FindProperty(type._class.registered,
5002                                     ident.string, privateModule);
5003                                  if(prop)
5004                                     type = prop.dataType;
5005                                  else
5006                                  {
5007                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
5008                                        ident.string, &dataMemberOffset, privateModule, null, null);
5009                                     if(dataMember)
5010                                        type = dataMember.dataType;
5011                                  }
5012                               }
5013                               else if(type.kind == structType || type.kind == unionType)
5014                               {
5015                                  Type memberType;
5016                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
5017                                  {
5018                                     if(!strcmp(memberType.name, ident.string))
5019                                     {
5020                                        type = memberType;
5021                                        break;
5022                                     }
5023                                  }
5024                               }
5025                            }
5026                         }
5027                         if(value)
5028                         {
5029                            FreeType(value.destType);
5030                            value.destType = type;
5031                            if(type) type.refCount++;
5032                            ComputeExpression(value);
5033                         }
5034                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
5035                         {
5036                            if(type.kind == classType)
5037                            {
5038                               Class _class = type._class.registered;
5039                               if(_class.type == bitClass || _class.type == unitClass ||
5040                                  _class.type == enumClass)
5041                               {
5042                                  if(!_class.dataType)
5043                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5044                                  type = _class.dataType;
5045                               }
5046                            }
5047
5048                            if(dataMember)
5049                            {
5050                               void * ptr = inst.data + dataMemberOffset;
5051
5052                               if(value.type == constantExp)
5053                               {
5054                                  switch(type.kind)
5055                                  {
5056                                     case intType:
5057                                     {
5058                                        GetInt(value, (int*)ptr);
5059                                        break;
5060                                     }
5061                                     case int64Type:
5062                                     {
5063                                        GetInt64(value, (int64*)ptr);
5064                                        break;
5065                                     }
5066                                     case intPtrType:
5067                                     {
5068                                        GetIntPtr(value, (intptr*)ptr);
5069                                        break;
5070                                     }
5071                                     case intSizeType:
5072                                     {
5073                                        GetIntSize(value, (intsize*)ptr);
5074                                        break;
5075                                     }
5076                                     case floatType:
5077                                     {
5078                                        GetFloat(value, (float*)ptr);
5079                                        break;
5080                                     }
5081                                     case doubleType:
5082                                     {
5083                                        GetDouble(value, (double *)ptr);
5084                                        break;
5085                                     }
5086                                  }
5087                               }
5088                               else if(value.type == instanceExp)
5089                               {
5090                                  if(type.kind == classType)
5091                                  {
5092                                     Class _class = type._class.registered;
5093                                     if(_class.type == structClass)
5094                                     {
5095                                        ComputeTypeSize(type);
5096                                        if(value.instance.data)
5097                                           memcpy(ptr, value.instance.data, type.size);
5098                                     }
5099                                  }
5100                               }
5101                            }
5102                            else if(prop)
5103                            {
5104                               if(value.type == instanceExp && value.instance.data)
5105                               {
5106                                  if(type.kind == classType)
5107                                  {
5108                                     Class _class = type._class.registered;
5109                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
5110                                     {
5111                                        void (*Set)(void *, void *) = (void *)prop.Set;
5112                                        Set(inst.data, value.instance.data);
5113                                        PopulateInstance(inst);
5114                                     }
5115                                  }
5116                               }
5117                               else if(value.type == constantExp)
5118                               {
5119                                  switch(type.kind)
5120                                  {
5121                                     case doubleType:
5122                                     {
5123                                        void (*Set)(void *, double) = (void *)prop.Set;
5124                                        Set(inst.data, strtod(value.constant, null) );
5125                                        break;
5126                                     }
5127                                     case floatType:
5128                                     {
5129                                        void (*Set)(void *, float) = (void *)prop.Set;
5130                                        Set(inst.data, (float)(strtod(value.constant, null)));
5131                                        break;
5132                                     }
5133                                     case intType:
5134                                     {
5135                                        void (*Set)(void *, int) = (void *)prop.Set;
5136                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5137                                        break;
5138                                     }
5139                                     case int64Type:
5140                                     {
5141                                        void (*Set)(void *, int64) = (void *)prop.Set;
5142                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5143                                        break;
5144                                     }
5145                                     case intPtrType:
5146                                     {
5147                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5148                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5149                                        break;
5150                                     }
5151                                     case intSizeType:
5152                                     {
5153                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5154                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5155                                        break;
5156                                     }
5157                                  }
5158                               }
5159                               else if(value.type == stringExp)
5160                               {
5161                                  char temp[1024];
5162                                  ReadString(temp, value.string);
5163                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5164                               }
5165                            }
5166                         }
5167                         else if(!deepMember && type && _class.type == unitClass)
5168                         {
5169                            if(prop)
5170                            {
5171                               // Only support converting units to units for now...
5172                               if(value.type == constantExp)
5173                               {
5174                                  if(type.kind == classType)
5175                                  {
5176                                     Class _class = type._class.registered;
5177                                     if(_class.type == unitClass)
5178                                     {
5179                                        if(!_class.dataType)
5180                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5181                                        type = _class.dataType;
5182                                     }
5183                                  }
5184                                  // TODO: Assuming same base type for units...
5185                                  switch(type.kind)
5186                                  {
5187                                     case floatType:
5188                                     {
5189                                        float fValue;
5190                                        float (*Set)(float) = (void *)prop.Set;
5191                                        GetFloat(member.initializer.exp, &fValue);
5192                                        exp.constant = PrintFloat(Set(fValue));
5193                                        exp.type = constantExp;
5194                                        break;
5195                                     }
5196                                     case doubleType:
5197                                     {
5198                                        double dValue;
5199                                        double (*Set)(double) = (void *)prop.Set;
5200                                        GetDouble(member.initializer.exp, &dValue);
5201                                        exp.constant = PrintDouble(Set(dValue));
5202                                        exp.type = constantExp;
5203                                        break;
5204                                     }
5205                                  }
5206                               }
5207                            }
5208                         }
5209                         else if(!deepMember && type && _class.type == bitClass)
5210                         {
5211                            if(prop)
5212                            {
5213                               if(value.type == instanceExp && value.instance.data)
5214                               {
5215                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5216                                  bits = Set(value.instance.data);
5217                               }
5218                               else if(value.type == constantExp)
5219                               {
5220                               }
5221                            }
5222                            else if(dataMember)
5223                            {
5224                               BitMember bitMember = (BitMember) dataMember;
5225                               Type type;
5226                               uint64 part;
5227                               bits = (bits & ~bitMember.mask);
5228                               if(!bitMember.dataType)
5229                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5230                               type = bitMember.dataType;
5231                               if(type.kind == classType && type._class && type._class.registered)
5232                               {
5233                                  if(!type._class.registered.dataType)
5234                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5235                                  type = type._class.registered.dataType;
5236                               }
5237                               switch(type.kind)
5238                               {
5239                                  case _BoolType:
5240                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5241                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (uint16 *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5242                                  case intType:
5243                                  case longType:       { uint v; type.isSigned ? GetInt(value, (uint *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5244                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (uint64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5245                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (uintptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5246                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (uintsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5247                               }
5248                               bits |= part << bitMember.pos;
5249                            }
5250                         }
5251                      }
5252                      else
5253                      {
5254                         if(_class && _class.type == unitClass)
5255                         {
5256                            ComputeExpression(member.initializer.exp);
5257                            exp.constant = member.initializer.exp.constant;
5258                            exp.type = constantExp;
5259
5260                            member.initializer.exp.constant = null;
5261                         }
5262                      }
5263                   }
5264                }
5265                break;
5266             }
5267          }
5268       }
5269    }
5270    if(_class && _class.type == bitClass)
5271    {
5272       exp.constant = PrintHexUInt(bits);
5273       exp.type = constantExp;
5274    }
5275    if(exp.type != instanceExp)
5276    {
5277       FreeInstance(inst);
5278    }
5279 }
5280
5281 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5282 {
5283    bool result = false;
5284    switch(kind)
5285    {
5286       case shortType:
5287          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5288             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5289          break;
5290       case intType:
5291       case longType:
5292          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5293             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5294          break;
5295       case int64Type:
5296          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5297             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5298             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5299          break;
5300       case floatType:
5301          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5302             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5303             result = GetOpFloat(op, &op.f);
5304          break;
5305       case doubleType:
5306          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5307             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5308             result = GetOpDouble(op, &op.d);
5309          break;
5310       case pointerType:
5311          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5312             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5313             result = GetOpUIntPtr(op, &op.ui64);
5314          break;
5315       case enumType:
5316          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5317             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5318             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5319          break;
5320       case intPtrType:
5321          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5322             result = isSigned ? GetOpIntPtr(op, &op.i64) : GetOpUIntPtr(op, &op.i64);
5323          break;
5324       case intSizeType:
5325          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5326             result = isSigned ? GetOpIntSize(op, &op.ui64) : GetOpUIntSize(op, &op.ui64);
5327          break;
5328    }
5329    return result;
5330 }
5331
5332 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5333 {
5334    if(exp.op.op == SIZEOF)
5335    {
5336       FreeExpContents(exp);
5337       exp.type = constantExp;
5338       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5339    }
5340    else
5341    {
5342       if(!exp.op.exp1)
5343       {
5344          switch(exp.op.op)
5345          {
5346             // unary arithmetic
5347             case '+':
5348             {
5349                // Provide default unary +
5350                Expression exp2 = exp.op.exp2;
5351                exp.op.exp2 = null;
5352                FreeExpContents(exp);
5353                FreeType(exp.expType);
5354                FreeType(exp.destType);
5355                *exp = *exp2;
5356                delete exp2;
5357                break;
5358             }
5359             case '-':
5360                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5361                break;
5362             // unary arithmetic increment and decrement
5363                   //OPERATOR_ALL(UNARY, ++, Inc)
5364                   //OPERATOR_ALL(UNARY, --, Dec)
5365             // unary bitwise
5366             case '~':
5367                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5368                break;
5369             // unary logical negation
5370             case '!':
5371                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5372                break;
5373          }
5374       }
5375       else
5376       {
5377          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5378          {
5379             if(Promote(op2, op1.kind, op1.type.isSigned))
5380                op2.kind = op1.kind, op2.ops = op1.ops;
5381             else if(Promote(op1, op2.kind, op2.type.isSigned))
5382                op1.kind = op2.kind, op1.ops = op2.ops;
5383          }
5384          switch(exp.op.op)
5385          {
5386             // binary arithmetic
5387             case '+':
5388                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5389                break;
5390             case '-':
5391                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5392                break;
5393             case '*':
5394                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5395                break;
5396             case '/':
5397                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5398                break;
5399             case '%':
5400                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5401                break;
5402             // binary arithmetic assignment
5403                   //OPERATOR_ALL(BINARY, =, Asign)
5404                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5405                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5406                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5407                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5408                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5409             // binary bitwise
5410             case '&':
5411                if(exp.op.exp2)
5412                {
5413                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5414                }
5415                break;
5416             case '|':
5417                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5418                break;
5419             case '^':
5420                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5421                break;
5422             case LEFT_OP:
5423                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5424                break;
5425             case RIGHT_OP:
5426                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5427                break;
5428             // binary bitwise assignment
5429                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5430                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5431                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5432                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5433                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5434             // binary logical equality
5435             case EQ_OP:
5436                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5437                break;
5438             case NE_OP:
5439                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5440                break;
5441             // binary logical
5442             case AND_OP:
5443                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5444                break;
5445             case OR_OP:
5446                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5447                break;
5448             // binary logical relational
5449             case '>':
5450                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5451                break;
5452             case '<':
5453                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5454                break;
5455             case GE_OP:
5456                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5457                break;
5458             case LE_OP:
5459                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5460                break;
5461          }
5462       }
5463    }
5464 }
5465
5466 void ComputeExpression(Expression exp)
5467 {
5468    char expString[10240];
5469    expString[0] = '\0';
5470 #ifdef _DEBUG
5471    PrintExpression(exp, expString);
5472 #endif
5473
5474    switch(exp.type)
5475    {
5476       case instanceExp:
5477       {
5478          ComputeInstantiation(exp);
5479          break;
5480       }
5481       /*
5482       case constantExp:
5483          break;
5484       */
5485       case opExp:
5486       {
5487          Expression exp1, exp2 = null;
5488          Operand op1 { };
5489          Operand op2 { };
5490
5491          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5492          if(exp.op.exp2)
5493          {
5494             Expression e = exp.op.exp2;
5495
5496             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5497             {
5498                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5499                {
5500                   if(e.type == extensionCompoundExp)
5501                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5502                   else
5503                      e = e.list->last;
5504                }
5505             }
5506             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5507             {
5508                if(e.type == stringExp && e.string)
5509                {
5510                   char * string = e.string;
5511                   int len = strlen(string);
5512                   char * tmp = new char[len-2+1];
5513                   len = UnescapeString(tmp, string + 1, len - 2);
5514                   delete tmp;
5515                   FreeExpContents(exp);
5516                   exp.type = constantExp;
5517                   exp.constant = PrintUInt(len + 1);
5518                }
5519                else
5520                {
5521                   Type type = e.expType;
5522                   type.refCount++;
5523                   FreeExpContents(exp);
5524                   exp.type = constantExp;
5525                   exp.constant = PrintUInt(ComputeTypeSize(type));
5526                   FreeType(type);
5527                }
5528                break;
5529             }
5530             else
5531                ComputeExpression(exp.op.exp2);
5532          }
5533          if(exp.op.exp1)
5534          {
5535             ComputeExpression(exp.op.exp1);
5536             exp1 = exp.op.exp1;
5537             exp2 = exp.op.exp2;
5538             op1 = GetOperand(exp1);
5539             if(op1.type) op1.type.refCount++;
5540             if(exp2)
5541             {
5542                op2 = GetOperand(exp2);
5543                if(op2.type) op2.type.refCount++;
5544             }
5545          }
5546          else
5547          {
5548             exp1 = exp.op.exp2;
5549             op1 = GetOperand(exp1);
5550             if(op1.type) op1.type.refCount++;
5551          }
5552
5553          CallOperator(exp, exp1, exp2, op1, op2);
5554          /*
5555          switch(exp.op.op)
5556          {
5557             // Unary operators
5558             case '&':
5559                // Also binary
5560                if(exp.op.exp1 && exp.op.exp2)
5561                {
5562                   // Binary And
5563                   if(op1.ops.BitAnd)
5564                   {
5565                      FreeExpContents(exp);
5566                      op1.ops.BitAnd(exp, op1, op2);
5567                   }
5568                }
5569                break;
5570             case '*':
5571                if(exp.op.exp1)
5572                {
5573                   if(op1.ops.Mul)
5574                   {
5575                      FreeExpContents(exp);
5576                      op1.ops.Mul(exp, op1, op2);
5577                   }
5578                }
5579                break;
5580             case '+':
5581                if(exp.op.exp1)
5582                {
5583                   if(op1.ops.Add)
5584                   {
5585                      FreeExpContents(exp);
5586                      op1.ops.Add(exp, op1, op2);
5587                   }
5588                }
5589                else
5590                {
5591                   // Provide default unary +
5592                   Expression exp2 = exp.op.exp2;
5593                   exp.op.exp2 = null;
5594                   FreeExpContents(exp);
5595                   FreeType(exp.expType);
5596                   FreeType(exp.destType);
5597
5598                   *exp = *exp2;
5599                   delete exp2;
5600                }
5601                break;
5602             case '-':
5603                if(exp.op.exp1)
5604                {
5605                   if(op1.ops.Sub)
5606                   {
5607                      FreeExpContents(exp);
5608                      op1.ops.Sub(exp, op1, op2);
5609                   }
5610                }
5611                else
5612                {
5613                   if(op1.ops.Neg)
5614                   {
5615                      FreeExpContents(exp);
5616                      op1.ops.Neg(exp, op1);
5617                   }
5618                }
5619                break;
5620             case '~':
5621                if(op1.ops.BitNot)
5622                {
5623                   FreeExpContents(exp);
5624                   op1.ops.BitNot(exp, op1);
5625                }
5626                break;
5627             case '!':
5628                if(op1.ops.Not)
5629                {
5630                   FreeExpContents(exp);
5631                   op1.ops.Not(exp, op1);
5632                }
5633                break;
5634             // Binary only operators
5635             case '/':
5636                if(op1.ops.Div)
5637                {
5638                   FreeExpContents(exp);
5639                   op1.ops.Div(exp, op1, op2);
5640                }
5641                break;
5642             case '%':
5643                if(op1.ops.Mod)
5644                {
5645                   FreeExpContents(exp);
5646                   op1.ops.Mod(exp, op1, op2);
5647                }
5648                break;
5649             case LEFT_OP:
5650                break;
5651             case RIGHT_OP:
5652                break;
5653             case '<':
5654                if(exp.op.exp1)
5655                {
5656                   if(op1.ops.Sma)
5657                   {
5658                      FreeExpContents(exp);
5659                      op1.ops.Sma(exp, op1, op2);
5660                   }
5661                }
5662                break;
5663             case '>':
5664                if(exp.op.exp1)
5665                {
5666                   if(op1.ops.Grt)
5667                   {
5668                      FreeExpContents(exp);
5669                      op1.ops.Grt(exp, op1, op2);
5670                   }
5671                }
5672                break;
5673             case LE_OP:
5674                if(exp.op.exp1)
5675                {
5676                   if(op1.ops.SmaEqu)
5677                   {
5678                      FreeExpContents(exp);
5679                      op1.ops.SmaEqu(exp, op1, op2);
5680                   }
5681                }
5682                break;
5683             case GE_OP:
5684                if(exp.op.exp1)
5685                {
5686                   if(op1.ops.GrtEqu)
5687                   {
5688                      FreeExpContents(exp);
5689                      op1.ops.GrtEqu(exp, op1, op2);
5690                   }
5691                }
5692                break;
5693             case EQ_OP:
5694                if(exp.op.exp1)
5695                {
5696                   if(op1.ops.Equ)
5697                   {
5698                      FreeExpContents(exp);
5699                      op1.ops.Equ(exp, op1, op2);
5700                   }
5701                }
5702                break;
5703             case NE_OP:
5704                if(exp.op.exp1)
5705                {
5706                   if(op1.ops.Nqu)
5707                   {
5708                      FreeExpContents(exp);
5709                      op1.ops.Nqu(exp, op1, op2);
5710                   }
5711                }
5712                break;
5713             case '|':
5714                if(op1.ops.BitOr)
5715                {
5716                   FreeExpContents(exp);
5717                   op1.ops.BitOr(exp, op1, op2);
5718                }
5719                break;
5720             case '^':
5721                if(op1.ops.BitXor)
5722                {
5723                   FreeExpContents(exp);
5724                   op1.ops.BitXor(exp, op1, op2);
5725                }
5726                break;
5727             case AND_OP:
5728                break;
5729             case OR_OP:
5730                break;
5731             case SIZEOF:
5732                FreeExpContents(exp);
5733                exp.type = constantExp;
5734                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5735                break;
5736          }
5737          */
5738          if(op1.type) FreeType(op1.type);
5739          if(op2.type) FreeType(op2.type);
5740          break;
5741       }
5742       case bracketsExp:
5743       case extensionExpressionExp:
5744       {
5745          Expression e, n;
5746          for(e = exp.list->first; e; e = n)
5747          {
5748             n = e.next;
5749             if(!n)
5750             {
5751                OldList * list = exp.list;
5752                Expression prev = exp.prev;
5753                Expression next = exp.next;
5754                ComputeExpression(e);
5755                //FreeExpContents(exp);
5756                FreeType(exp.expType);
5757                FreeType(exp.destType);
5758                *exp = *e;
5759                exp.prev = prev;
5760                exp.next = next;
5761                delete e;
5762                delete list;
5763             }
5764             else
5765             {
5766                FreeExpression(e);
5767             }
5768          }
5769          break;
5770       }
5771       /*
5772
5773       case ExpIndex:
5774       {
5775          Expression e;
5776          exp.isConstant = true;
5777
5778          ComputeExpression(exp.index.exp);
5779          if(!exp.index.exp.isConstant)
5780             exp.isConstant = false;
5781
5782          for(e = exp.index.index->first; e; e = e.next)
5783          {
5784             ComputeExpression(e);
5785             if(!e.next)
5786             {
5787                // Check if this type is int
5788             }
5789             if(!e.isConstant)
5790                exp.isConstant = false;
5791          }
5792          exp.expType = Dereference(exp.index.exp.expType);
5793          break;
5794       }
5795       */
5796       case memberExp:
5797       {
5798          Expression memberExp = exp.member.exp;
5799          Identifier memberID = exp.member.member;
5800
5801          Type type;
5802          ComputeExpression(exp.member.exp);
5803          type = exp.member.exp.expType;
5804          if(type)
5805          {
5806             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);
5807             Property prop = null;
5808             DataMember member = null;
5809             Class convertTo = null;
5810             if(type.kind == subClassType && exp.member.exp.type == classExp)
5811                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5812
5813             if(!_class)
5814             {
5815                char string[256];
5816                Symbol classSym;
5817                string[0] = '\0';
5818                PrintTypeNoConst(type, string, false, true);
5819                classSym = FindClass(string);
5820                _class = classSym ? classSym.registered : null;
5821             }
5822
5823             if(exp.member.member)
5824             {
5825                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5826                if(!prop)
5827                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5828             }
5829             if(!prop && !member && _class && exp.member.member)
5830             {
5831                Symbol classSym = FindClass(exp.member.member.string);
5832                convertTo = _class;
5833                _class = classSym ? classSym.registered : null;
5834                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5835             }
5836
5837             if(prop)
5838             {
5839                if(prop.compiled)
5840                {
5841                   Type type = prop.dataType;
5842                   // TODO: Assuming same base type for units...
5843                   if(_class.type == unitClass)
5844                   {
5845                      if(type.kind == classType)
5846                      {
5847                         Class _class = type._class.registered;
5848                         if(_class.type == unitClass)
5849                         {
5850                            if(!_class.dataType)
5851                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5852                            type = _class.dataType;
5853                         }
5854                      }
5855                      switch(type.kind)
5856                      {
5857                         case floatType:
5858                         {
5859                            float value;
5860                            float (*Get)(float) = (void *)prop.Get;
5861                            GetFloat(exp.member.exp, &value);
5862                            exp.constant = PrintFloat(Get ? Get(value) : value);
5863                            exp.type = constantExp;
5864                            break;
5865                         }
5866                         case doubleType:
5867                         {
5868                            double value;
5869                            double (*Get)(double);
5870                            GetDouble(exp.member.exp, &value);
5871
5872                            if(convertTo)
5873                               Get = (void *)prop.Set;
5874                            else
5875                               Get = (void *)prop.Get;
5876                            exp.constant = PrintDouble(Get ? Get(value) : value);
5877                            exp.type = constantExp;
5878                            break;
5879                         }
5880                      }
5881                   }
5882                   else
5883                   {
5884                      if(convertTo)
5885                      {
5886                         Expression value = exp.member.exp;
5887                         Type type;
5888                         if(!prop.dataType)
5889                            ProcessPropertyType(prop);
5890
5891                         type = prop.dataType;
5892                         if(!type)
5893                         {
5894                             // printf("Investigate this\n");
5895                         }
5896                         else if(_class.type == structClass)
5897                         {
5898                            switch(type.kind)
5899                            {
5900                               case classType:
5901                               {
5902                                  Class propertyClass = type._class.registered;
5903                                  if(propertyClass.type == structClass && value.type == instanceExp)
5904                                  {
5905                                     void (*Set)(void *, void *) = (void *)prop.Set;
5906                                     exp.instance = Instantiation { };
5907                                     exp.instance.data = new0 byte[_class.structSize];
5908                                     exp.instance._class = MkSpecifierName(_class.fullName);
5909                                     exp.instance.loc = exp.loc;
5910                                     exp.type = instanceExp;
5911                                     Set(exp.instance.data, value.instance.data);
5912                                     PopulateInstance(exp.instance);
5913                                  }
5914                                  break;
5915                               }
5916                               case intType:
5917                               {
5918                                  int intValue;
5919                                  void (*Set)(void *, int) = (void *)prop.Set;
5920
5921                                  exp.instance = Instantiation { };
5922                                  exp.instance.data = new0 byte[_class.structSize];
5923                                  exp.instance._class = MkSpecifierName(_class.fullName);
5924                                  exp.instance.loc = exp.loc;
5925                                  exp.type = instanceExp;
5926
5927                                  GetInt(value, &intValue);
5928
5929                                  Set(exp.instance.data, intValue);
5930                                  PopulateInstance(exp.instance);
5931                                  break;
5932                               }
5933                               case int64Type:
5934                               {
5935                                  int64 intValue;
5936                                  void (*Set)(void *, int64) = (void *)prop.Set;
5937
5938                                  exp.instance = Instantiation { };
5939                                  exp.instance.data = new0 byte[_class.structSize];
5940                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5941                                  exp.instance.loc = exp.loc;
5942                                  exp.type = instanceExp;
5943
5944                                  GetInt64(value, &intValue);
5945
5946                                  Set(exp.instance.data, intValue);
5947                                  PopulateInstance(exp.instance);
5948                                  break;
5949                               }
5950                               case intPtrType:
5951                               {
5952                                  // TOFIX:
5953                                  intptr intValue;
5954                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5955
5956                                  exp.instance = Instantiation { };
5957                                  exp.instance.data = new0 byte[_class.structSize];
5958                                  exp.instance._class = MkSpecifierName(_class.fullName);
5959                                  exp.instance.loc = exp.loc;
5960                                  exp.type = instanceExp;
5961
5962                                  GetIntPtr(value, &intValue);
5963
5964                                  Set(exp.instance.data, intValue);
5965                                  PopulateInstance(exp.instance);
5966                                  break;
5967                               }
5968                               case intSizeType:
5969                               {
5970                                  // TOFIX:
5971                                  intsize intValue;
5972                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5973
5974                                  exp.instance = Instantiation { };
5975                                  exp.instance.data = new0 byte[_class.structSize];
5976                                  exp.instance._class = MkSpecifierName(_class.fullName);
5977                                  exp.instance.loc = exp.loc;
5978                                  exp.type = instanceExp;
5979
5980                                  GetIntSize(value, &intValue);
5981
5982                                  Set(exp.instance.data, intValue);
5983                                  PopulateInstance(exp.instance);
5984                                  break;
5985                               }
5986                               case floatType:
5987                               {
5988                                  float floatValue;
5989                                  void (*Set)(void *, float) = (void *)prop.Set;
5990
5991                                  exp.instance = Instantiation { };
5992                                  exp.instance.data = new0 byte[_class.structSize];
5993                                  exp.instance._class = MkSpecifierName(_class.fullName);
5994                                  exp.instance.loc = exp.loc;
5995                                  exp.type = instanceExp;
5996
5997                                  GetFloat(value, &floatValue);
5998
5999                                  Set(exp.instance.data, floatValue);
6000                                  PopulateInstance(exp.instance);
6001                                  break;
6002                               }
6003                               case doubleType:
6004                               {
6005                                  double doubleValue;
6006                                  void (*Set)(void *, double) = (void *)prop.Set;
6007
6008                                  exp.instance = Instantiation { };
6009                                  exp.instance.data = new0 byte[_class.structSize];
6010                                  exp.instance._class = MkSpecifierName(_class.fullName);
6011                                  exp.instance.loc = exp.loc;
6012                                  exp.type = instanceExp;
6013
6014                                  GetDouble(value, &doubleValue);
6015
6016                                  Set(exp.instance.data, doubleValue);
6017                                  PopulateInstance(exp.instance);
6018                                  break;
6019                               }
6020                            }
6021                         }
6022                         else if(_class.type == bitClass)
6023                         {
6024                            switch(type.kind)
6025                            {
6026                               case classType:
6027                               {
6028                                  Class propertyClass = type._class.registered;
6029                                  if(propertyClass.type == structClass && value.instance.data)
6030                                  {
6031                                     unsigned int (*Set)(void *) = (void *)prop.Set;
6032                                     unsigned int bits = Set(value.instance.data);
6033                                     exp.constant = PrintHexUInt(bits);
6034                                     exp.type = constantExp;
6035                                     break;
6036                                  }
6037                                  else if(_class.type == bitClass)
6038                                  {
6039                                     unsigned int value;
6040                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
6041                                     unsigned int bits;
6042
6043                                     GetUInt(exp.member.exp, &value);
6044                                     bits = Set(value);
6045                                     exp.constant = PrintHexUInt(bits);
6046                                     exp.type = constantExp;
6047                                  }
6048                               }
6049                            }
6050                         }
6051                      }
6052                      else
6053                      {
6054                         if(_class.type == bitClass)
6055                         {
6056                            unsigned int value;
6057                            GetUInt(exp.member.exp, &value);
6058
6059                            switch(type.kind)
6060                            {
6061                               case classType:
6062                               {
6063                                  Class _class = type._class.registered;
6064                                  if(_class.type == structClass)
6065                                  {
6066                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
6067
6068                                     exp.instance = Instantiation { };
6069                                     exp.instance.data = new0 byte[_class.structSize];
6070                                     exp.instance._class = MkSpecifierName(_class.fullName);
6071                                     exp.instance.loc = exp.loc;
6072                                     //exp.instance.fullSet = true;
6073                                     exp.type = instanceExp;
6074                                     Get(value, exp.instance.data);
6075                                     PopulateInstance(exp.instance);
6076                                  }
6077                                  else if(_class.type == bitClass)
6078                                  {
6079                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
6080                                     uint64 bits = Get(value);
6081                                     exp.constant = PrintHexUInt64(bits);
6082                                     exp.type = constantExp;
6083                                  }
6084                                  break;
6085                               }
6086                            }
6087                         }
6088                         else if(_class.type == structClass)
6089                         {
6090                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
6091                            switch(type.kind)
6092                            {
6093                               case classType:
6094                               {
6095                                  Class _class = type._class.registered;
6096                                  if(_class.type == structClass && value)
6097                                  {
6098                                     void (*Get)(void *, void *) = (void *)prop.Get;
6099
6100                                     exp.instance = Instantiation { };
6101                                     exp.instance.data = new0 byte[_class.structSize];
6102                                     exp.instance._class = MkSpecifierName(_class.fullName);
6103                                     exp.instance.loc = exp.loc;
6104                                     //exp.instance.fullSet = true;
6105                                     exp.type = instanceExp;
6106                                     Get(value, exp.instance.data);
6107                                     PopulateInstance(exp.instance);
6108                                  }
6109                                  break;
6110                               }
6111                            }
6112                         }
6113                         /*else
6114                         {
6115                            char * value = exp.member.exp.instance.data;
6116                            switch(type.kind)
6117                            {
6118                               case classType:
6119                               {
6120                                  Class _class = type._class.registered;
6121                                  if(_class.type == normalClass)
6122                                  {
6123                                     void *(*Get)(void *) = (void *)prop.Get;
6124
6125                                     exp.instance = Instantiation { };
6126                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6127                                     exp.type = instanceExp;
6128                                     exp.instance.data = Get(value, exp.instance.data);
6129                                  }
6130                                  break;
6131                               }
6132                            }
6133                         }
6134                         */
6135                      }
6136                   }
6137                }
6138                else
6139                {
6140                   exp.isConstant = false;
6141                }
6142             }
6143             else if(member)
6144             {
6145             }
6146          }
6147
6148          if(exp.type != ExpressionType::memberExp)
6149          {
6150             FreeExpression(memberExp);
6151             FreeIdentifier(memberID);
6152          }
6153          break;
6154       }
6155       case typeSizeExp:
6156       {
6157          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6158          FreeExpContents(exp);
6159          exp.constant = PrintUInt(ComputeTypeSize(type));
6160          exp.type = constantExp;
6161          FreeType(type);
6162          break;
6163       }
6164       case classSizeExp:
6165       {
6166          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6167          if(classSym && classSym.registered)
6168          {
6169             if(classSym.registered.fixed)
6170             {
6171                FreeSpecifier(exp._class);
6172                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6173                exp.type = constantExp;
6174             }
6175             else
6176             {
6177                char className[1024];
6178                strcpy(className, "__ecereClass_");
6179                FullClassNameCat(className, classSym.string, true);
6180                MangleClassName(className);
6181
6182                DeclareClass(classSym, className);
6183
6184                FreeExpContents(exp);
6185                exp.type = pointerExp;
6186                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6187                exp.member.member = MkIdentifier("structSize");
6188             }
6189          }
6190          break;
6191       }
6192       case castExp:
6193       //case constantExp:
6194       {
6195          Type type;
6196          Expression e = exp;
6197          if(exp.type == castExp)
6198          {
6199             if(exp.cast.exp)
6200                ComputeExpression(exp.cast.exp);
6201             e = exp.cast.exp;
6202          }
6203          if(e && exp.expType)
6204          {
6205             /*if(exp.destType)
6206                type = exp.destType;
6207             else*/
6208                type = exp.expType;
6209             if(type.kind == classType)
6210             {
6211                Class _class = type._class.registered;
6212                if(_class && (_class.type == unitClass || _class.type == bitClass))
6213                {
6214                   if(!_class.dataType)
6215                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6216                   type = _class.dataType;
6217                }
6218             }
6219
6220             switch(type.kind)
6221             {
6222                case _BoolType:
6223                case charType:
6224                   if(type.isSigned)
6225                   {
6226                      char value = 0;
6227                      if(GetChar(e, &value))
6228                      {
6229                         FreeExpContents(exp);
6230                         exp.constant = PrintChar(value);
6231                         exp.type = constantExp;
6232                      }
6233                   }
6234                   else
6235                   {
6236                      unsigned char value = 0;
6237                      if(GetUChar(e, &value))
6238                      {
6239                         FreeExpContents(exp);
6240                         exp.constant = PrintUChar(value);
6241                         exp.type = constantExp;
6242                      }
6243                   }
6244                   break;
6245                case shortType:
6246                   if(type.isSigned)
6247                   {
6248                      short value = 0;
6249                      if(GetShort(e, &value))
6250                      {
6251                         FreeExpContents(exp);
6252                         exp.constant = PrintShort(value);
6253                         exp.type = constantExp;
6254                      }
6255                   }
6256                   else
6257                   {
6258                      unsigned short value = 0;
6259                      if(GetUShort(e, &value))
6260                      {
6261                         FreeExpContents(exp);
6262                         exp.constant = PrintUShort(value);
6263                         exp.type = constantExp;
6264                      }
6265                   }
6266                   break;
6267                case intType:
6268                   if(type.isSigned)
6269                   {
6270                      int value = 0;
6271                      if(GetInt(e, &value))
6272                      {
6273                         FreeExpContents(exp);
6274                         exp.constant = PrintInt(value);
6275                         exp.type = constantExp;
6276                      }
6277                   }
6278                   else
6279                   {
6280                      unsigned int value = 0;
6281                      if(GetUInt(e, &value))
6282                      {
6283                         FreeExpContents(exp);
6284                         exp.constant = PrintUInt(value);
6285                         exp.type = constantExp;
6286                      }
6287                   }
6288                   break;
6289                case int64Type:
6290                   if(type.isSigned)
6291                   {
6292                      int64 value = 0;
6293                      if(GetInt64(e, &value))
6294                      {
6295                         FreeExpContents(exp);
6296                         exp.constant = PrintInt64(value);
6297                         exp.type = constantExp;
6298                      }
6299                   }
6300                   else
6301                   {
6302                      uint64 value = 0;
6303                      if(GetUInt64(e, &value))
6304                      {
6305                         FreeExpContents(exp);
6306                         exp.constant = PrintUInt64(value);
6307                         exp.type = constantExp;
6308                      }
6309                   }
6310                   break;
6311                case intPtrType:
6312                   if(type.isSigned)
6313                   {
6314                      intptr value = 0;
6315                      if(GetIntPtr(e, &value))
6316                      {
6317                         FreeExpContents(exp);
6318                         exp.constant = PrintInt64((int64)value);
6319                         exp.type = constantExp;
6320                      }
6321                   }
6322                   else
6323                   {
6324                      uintptr value = 0;
6325                      if(GetUIntPtr(e, &value))
6326                      {
6327                         FreeExpContents(exp);
6328                         exp.constant = PrintUInt64((uint64)value);
6329                         exp.type = constantExp;
6330                      }
6331                   }
6332                   break;
6333                case intSizeType:
6334                   if(type.isSigned)
6335                   {
6336                      intsize value = 0;
6337                      if(GetIntSize(e, &value))
6338                      {
6339                         FreeExpContents(exp);
6340                         exp.constant = PrintInt64((int64)value);
6341                         exp.type = constantExp;
6342                      }
6343                   }
6344                   else
6345                   {
6346                      uintsize value = 0;
6347                      if(GetUIntSize(e, &value))
6348                      {
6349                         FreeExpContents(exp);
6350                         exp.constant = PrintUInt64((uint64)value);
6351                         exp.type = constantExp;
6352                      }
6353                   }
6354                   break;
6355                case floatType:
6356                {
6357                   float value = 0;
6358                   if(GetFloat(e, &value))
6359                   {
6360                      FreeExpContents(exp);
6361                      exp.constant = PrintFloat(value);
6362                      exp.type = constantExp;
6363                   }
6364                   break;
6365                }
6366                case doubleType:
6367                {
6368                   double value = 0;
6369                   if(GetDouble(e, &value))
6370                   {
6371                      FreeExpContents(exp);
6372                      exp.constant = PrintDouble(value);
6373                      exp.type = constantExp;
6374                   }
6375                   break;
6376                }
6377             }
6378          }
6379          break;
6380       }
6381       case conditionExp:
6382       {
6383          Operand op1 { };
6384          Operand op2 { };
6385          Operand op3 { };
6386
6387          if(exp.cond.exp)
6388             // Caring only about last expression for now...
6389             ComputeExpression(exp.cond.exp->last);
6390          if(exp.cond.elseExp)
6391             ComputeExpression(exp.cond.elseExp);
6392          if(exp.cond.cond)
6393             ComputeExpression(exp.cond.cond);
6394
6395          op1 = GetOperand(exp.cond.cond);
6396          if(op1.type) op1.type.refCount++;
6397          op2 = GetOperand(exp.cond.exp->last);
6398          if(op2.type) op2.type.refCount++;
6399          op3 = GetOperand(exp.cond.elseExp);
6400          if(op3.type) op3.type.refCount++;
6401
6402          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6403          if(op1.type) FreeType(op1.type);
6404          if(op2.type) FreeType(op2.type);
6405          if(op3.type) FreeType(op3.type);
6406          break;
6407       }
6408    }
6409 }
6410
6411 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla, bool warnConst)
6412 {
6413    bool result = true;
6414    if(destType)
6415    {
6416       OldList converts { };
6417       Conversion convert;
6418
6419       if(destType.kind == voidType)
6420          return false;
6421
6422       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla, warnConst))
6423          result = false;
6424       if(converts.count)
6425       {
6426          // for(convert = converts.last; convert; convert = convert.prev)
6427          for(convert = converts.first; convert; convert = convert.next)
6428          {
6429             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6430             if(!empty)
6431             {
6432                Expression newExp { };
6433                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6434
6435                // TODO: Check this...
6436                *newExp = *exp;
6437                newExp.prev = null;
6438                newExp.next = null;
6439                newExp.destType = null;
6440
6441                if(convert.isGet)
6442                {
6443                   // [exp].ColorRGB
6444                   exp.type = memberExp;
6445                   exp.addedThis = true;
6446                   exp.member.exp = newExp;
6447                   FreeType(exp.member.exp.expType);
6448
6449                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6450                   exp.member.exp.expType.classObjectType = objectType;
6451                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6452                   exp.member.memberType = propertyMember;
6453                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6454                   // TESTING THIS... for (int)degrees
6455                   exp.needCast = true;
6456                   if(exp.expType) exp.expType.refCount++;
6457                   ApplyAnyObjectLogic(exp.member.exp);
6458                }
6459                else
6460                {
6461
6462                   /*if(exp.isConstant)
6463                   {
6464                      // Color { ColorRGB = [exp] };
6465                      exp.type = instanceExp;
6466                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6467                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6468                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6469                   }
6470                   else*/
6471                   {
6472                      // If not constant, don't turn it yet into an instantiation
6473                      // (Go through the deep members system first)
6474                      exp.type = memberExp;
6475                      exp.addedThis = true;
6476                      exp.member.exp = newExp;
6477
6478                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6479                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6480                         newExp.expType._class.registered.type == noHeadClass)
6481                      {
6482                         newExp.byReference = true;
6483                      }
6484
6485                      FreeType(exp.member.exp.expType);
6486                      /*exp.member.exp.expType = convert.convert.dataType;
6487                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6488                      exp.member.exp.expType = null;
6489                      if(convert.convert.dataType)
6490                      {
6491                         exp.member.exp.expType = { };
6492                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6493                         exp.member.exp.expType.refCount = 1;
6494                         exp.member.exp.expType.classObjectType = objectType;
6495                         ApplyAnyObjectLogic(exp.member.exp);
6496                      }
6497
6498                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6499                      exp.member.memberType = reverseConversionMember;
6500                      exp.expType = convert.resultType ? convert.resultType :
6501                         MkClassType(convert.convert._class.fullName);
6502                      exp.needCast = true;
6503                      if(convert.resultType) convert.resultType.refCount++;
6504                   }
6505                }
6506             }
6507             else
6508             {
6509                FreeType(exp.expType);
6510                if(convert.isGet)
6511                {
6512                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6513                   exp.needCast = true;
6514                   if(exp.expType) exp.expType.refCount++;
6515                }
6516                else
6517                {
6518                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
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    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6551    {
6552       Expression newExp { };
6553       Context context;
6554       *newExp = *exp;
6555       if(exp.destType) exp.destType.refCount++;
6556       if(exp.expType)  exp.expType.refCount++;
6557       newExp.prev = null;
6558       newExp.next = null;
6559
6560       switch(exp.expType.kind)
6561       {
6562          case doubleType:
6563             if(exp.destType.classObjectType)
6564             {
6565                // We need to pass the address, just pass it along (Undo what was done above)
6566                if(exp.destType) exp.destType.refCount--;
6567                if(exp.expType)  exp.expType.refCount--;
6568                delete newExp;
6569             }
6570             else
6571             {
6572                // If we're looking for value:
6573                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6574                OldList * specs;
6575                OldList * unionDefs = MkList();
6576                OldList * statements = MkList();
6577                context = PushContext();
6578                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6579                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6580                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6581                exp.type = extensionCompoundExp;
6582                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6583                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6584                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6585                exp.compound.compound.context = context;
6586                PopContext(context);
6587             }
6588             break;
6589          default:
6590             exp.type = castExp;
6591             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6592             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6593             break;
6594       }
6595    }
6596    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6597    {
6598       Expression newExp { };
6599       Statement compound;
6600       Context context;
6601       *newExp = *exp;
6602       if(exp.destType) exp.destType.refCount++;
6603       if(exp.expType)  exp.expType.refCount++;
6604       newExp.prev = null;
6605       newExp.next = null;
6606
6607       switch(exp.expType.kind)
6608       {
6609          case doubleType:
6610             if(exp.destType.classObjectType)
6611             {
6612                // We need to pass the address, just pass it along (Undo what was done above)
6613                if(exp.destType) exp.destType.refCount--;
6614                if(exp.expType)  exp.expType.refCount--;
6615                delete newExp;
6616             }
6617             else
6618             {
6619                // If we're looking for value:
6620                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6621                OldList * specs;
6622                OldList * unionDefs = MkList();
6623                OldList * statements = MkList();
6624                context = PushContext();
6625                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6626                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6627                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6628                exp.type = extensionCompoundExp;
6629                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6630                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6631                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6632                exp.compound.compound.context = context;
6633                PopContext(context);
6634             }
6635             break;
6636          case classType:
6637          {
6638             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6639             {
6640                exp.type = bracketsExp;
6641                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6642                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6643                ProcessExpressionType(exp.list->first);
6644                break;
6645             }
6646             else
6647             {
6648                exp.type = bracketsExp;
6649                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6650                newExp.needCast = true;
6651                ProcessExpressionType(exp.list->first);
6652                break;
6653             }
6654          }
6655          default:
6656          {
6657             if(exp.expType.kind == templateType)
6658             {
6659                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6660                if(type)
6661                {
6662                   FreeType(exp.destType);
6663                   FreeType(exp.expType);
6664                   delete newExp;
6665                   break;
6666                }
6667             }
6668             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6669             {
6670                exp.type = opExp;
6671                exp.op.op = '*';
6672                exp.op.exp1 = null;
6673                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6674                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6675             }
6676             else
6677             {
6678                char typeString[1024];
6679                Declarator decl;
6680                OldList * specs = MkList();
6681                typeString[0] = '\0';
6682                PrintType(exp.expType, typeString, false, false);
6683                decl = SpecDeclFromString(typeString, specs, null);
6684
6685                exp.type = castExp;
6686                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6687                exp.cast.typeName = MkTypeName(specs, decl);
6688                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6689                exp.cast.exp.needCast = true;
6690             }
6691             break;
6692          }
6693       }
6694    }
6695 }
6696 // TODO: The Symbol tree should be reorganized by namespaces
6697 // Name Space:
6698 //    - Tree of all symbols within (stored without namespace)
6699 //    - Tree of sub-namespaces
6700
6701 static Symbol ScanWithNameSpace(BinaryTree tree, const char * nameSpace, const char * name)
6702 {
6703    int nsLen = strlen(nameSpace);
6704    Symbol symbol;
6705    // Start at the name space prefix
6706    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6707    {
6708       char * s = symbol.string;
6709       if(!strncmp(s, nameSpace, nsLen))
6710       {
6711          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6712          int c;
6713          char * namePart;
6714          for(c = strlen(s)-1; c >= 0; c--)
6715             if(s[c] == ':')
6716                break;
6717
6718          namePart = s+c+1;
6719          if(!strcmp(namePart, name))
6720          {
6721             // TODO: Error on ambiguity
6722             return symbol;
6723          }
6724       }
6725       else
6726          break;
6727    }
6728    return null;
6729 }
6730
6731 static Symbol FindWithNameSpace(BinaryTree tree, const char * name)
6732 {
6733    int c;
6734    char nameSpace[1024];
6735    const char * namePart;
6736    bool gotColon = false;
6737
6738    nameSpace[0] = '\0';
6739    for(c = strlen(name)-1; c >= 0; c--)
6740       if(name[c] == ':')
6741       {
6742          gotColon = true;
6743          break;
6744       }
6745
6746    namePart = name+c+1;
6747    while(c >= 0 && name[c] == ':') c--;
6748    if(c >= 0)
6749    {
6750       // Try an exact match first
6751       Symbol symbol = (Symbol)tree.FindString(name);
6752       if(symbol)
6753          return symbol;
6754
6755       // Namespace specified
6756       memcpy(nameSpace, name, c + 1);
6757       nameSpace[c+1] = 0;
6758
6759       return ScanWithNameSpace(tree, nameSpace, namePart);
6760    }
6761    else if(gotColon)
6762    {
6763       // Looking for a global symbol, e.g. ::Sleep()
6764       Symbol symbol = (Symbol)tree.FindString(namePart);
6765       return symbol;
6766    }
6767    else
6768    {
6769       // Name only (no namespace specified)
6770       Symbol symbol = (Symbol)tree.FindString(namePart);
6771       if(symbol)
6772          return symbol;
6773       return ScanWithNameSpace(tree, "", namePart);
6774    }
6775    return null;
6776 }
6777
6778 static void ProcessDeclaration(Declaration decl);
6779
6780 /*static */Symbol FindSymbol(const char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6781 {
6782 #ifdef _DEBUG
6783    //Time startTime = GetTime();
6784 #endif
6785    // Optimize this later? Do this before/less?
6786    Context ctx;
6787    Symbol symbol = null;
6788    // First, check if the identifier is declared inside the function
6789    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6790
6791    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6792    {
6793       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6794       {
6795          symbol = null;
6796          if(thisNameSpace)
6797          {
6798             char curName[1024];
6799             strcpy(curName, thisNameSpace);
6800             strcat(curName, "::");
6801             strcat(curName, name);
6802             // Try to resolve in current namespace first
6803             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6804          }
6805          if(!symbol)
6806             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6807       }
6808       else
6809          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6810
6811       if(symbol || ctx == endContext) break;
6812    }
6813    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6814    {
6815       if(symbol.pointerExternal.type == functionExternal)
6816       {
6817          FunctionDefinition function = symbol.pointerExternal.function;
6818
6819          // Modified this recently...
6820          Context tmpContext = curContext;
6821          curContext = null;
6822          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6823          curContext = tmpContext;
6824
6825          symbol.pointerExternal.symbol = symbol;
6826
6827          // TESTING THIS:
6828          DeclareType(symbol.type, true, true);
6829
6830          ast->Insert(curExternal.prev, symbol.pointerExternal);
6831
6832          symbol.id = curExternal.symbol.idCode;
6833
6834       }
6835       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6836       {
6837          ast->Move(symbol.pointerExternal, curExternal.prev);
6838          symbol.id = curExternal.symbol.idCode;
6839       }
6840    }
6841 #ifdef _DEBUG
6842    //findSymbolTotalTime += GetTime() - startTime;
6843 #endif
6844    return symbol;
6845 }
6846
6847 static void GetTypeSpecs(Type type, OldList * specs)
6848 {
6849    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6850    switch(type.kind)
6851    {
6852       case classType:
6853       {
6854          if(type._class.registered)
6855          {
6856             if(!type._class.registered.dataType)
6857                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6858             GetTypeSpecs(type._class.registered.dataType, specs);
6859          }
6860          break;
6861       }
6862       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6863       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6864       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6865       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6866       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6867       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6868       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6869       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6870       case intType:
6871       default:
6872          ListAdd(specs, MkSpecifier(INT)); break;
6873    }
6874 }
6875
6876 static void PrintArraySize(Type arrayType, char * string)
6877 {
6878    char size[256];
6879    size[0] = '\0';
6880    strcat(size, "[");
6881    if(arrayType.enumClass)
6882       strcat(size, arrayType.enumClass.string);
6883    else if(arrayType.arraySizeExp)
6884       PrintExpression(arrayType.arraySizeExp, size);
6885    strcat(size, "]");
6886    strcat(string, size);
6887 }
6888
6889 // WARNING : This function expects a null terminated string since it recursively concatenate...
6890 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6891 {
6892    if(type)
6893    {
6894       if(printConst && type.constant)
6895          strcat(string, "const ");
6896       switch(type.kind)
6897       {
6898          case classType:
6899          {
6900             Symbol c = type._class;
6901             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6902             //       look into merging with thisclass ?
6903             if(type.classObjectType == typedObject)
6904                strcat(string, "typed_object");
6905             else if(type.classObjectType == anyObject)
6906                strcat(string, "any_object");
6907             else
6908             {
6909                if(c && c.string)
6910                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6911             }
6912             if(type.byReference)
6913                strcat(string, " &");
6914             break;
6915          }
6916          case voidType: strcat(string, "void"); break;
6917          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6918          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6919          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6920          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6921          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6922          case _BoolType: strcat(string, "_Bool"); break;
6923          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6924          case floatType: strcat(string, "float"); break;
6925          case doubleType: strcat(string, "double"); break;
6926          case structType:
6927             if(type.enumName)
6928             {
6929                strcat(string, "struct ");
6930                strcat(string, type.enumName);
6931             }
6932             else if(type.typeName)
6933                strcat(string, type.typeName);
6934             else
6935             {
6936                Type member;
6937                strcat(string, "struct { ");
6938                for(member = type.members.first; member; member = member.next)
6939                {
6940                   PrintType(member, string, true, fullName);
6941                   strcat(string,"; ");
6942                }
6943                strcat(string,"}");
6944             }
6945             break;
6946          case unionType:
6947             if(type.enumName)
6948             {
6949                strcat(string, "union ");
6950                strcat(string, type.enumName);
6951             }
6952             else if(type.typeName)
6953                strcat(string, type.typeName);
6954             else
6955             {
6956                strcat(string, "union ");
6957                strcat(string,"(unnamed)");
6958             }
6959             break;
6960          case enumType:
6961             if(type.enumName)
6962             {
6963                strcat(string, "enum ");
6964                strcat(string, type.enumName);
6965             }
6966             else if(type.typeName)
6967                strcat(string, type.typeName);
6968             else
6969                strcat(string, "int"); // "enum");
6970             break;
6971          case ellipsisType:
6972             strcat(string, "...");
6973             break;
6974          case subClassType:
6975             strcat(string, "subclass(");
6976             strcat(string, type._class ? type._class.string : "int");
6977             strcat(string, ")");
6978             break;
6979          case templateType:
6980             strcat(string, type.templateParameter.identifier.string);
6981             break;
6982          case thisClassType:
6983             strcat(string, "thisclass");
6984             break;
6985          case vaListType:
6986             strcat(string, "__builtin_va_list");
6987             break;
6988       }
6989    }
6990 }
6991
6992 static void PrintName(Type type, char * string, bool fullName)
6993 {
6994    if(type.name && type.name[0])
6995    {
6996       if(fullName)
6997          strcat(string, type.name);
6998       else
6999       {
7000          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
7001          if(name) name += 2; else name = type.name;
7002          strcat(string, name);
7003       }
7004    }
7005 }
7006
7007 static void PrintAttribs(Type type, char * string)
7008 {
7009    if(type)
7010    {
7011       if(type.dllExport)   strcat(string, "dllexport ");
7012       if(type.attrStdcall) strcat(string, "stdcall ");
7013    }
7014 }
7015
7016 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
7017 {
7018    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7019    {
7020       Type attrType = null;
7021       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
7022          PrintAttribs(type, string);
7023       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
7024          strcat(string, " const");
7025       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
7026       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7027          strcat(string, " (");
7028       if(type.kind == pointerType)
7029       {
7030          if(type.type.kind == functionType || type.type.kind == methodType)
7031             PrintAttribs(type.type, string);
7032       }
7033       if(type.kind == pointerType)
7034       {
7035          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
7036             strcat(string, "*");
7037          else
7038             strcat(string, " *");
7039       }
7040       if(printConst && type.constant && type.kind == pointerType)
7041          strcat(string, " const");
7042    }
7043    else
7044       PrintTypeSpecs(type, string, fullName, printConst);
7045 }
7046
7047 static void PostPrintType(Type type, char * string, bool fullName)
7048 {
7049    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7050       strcat(string, ")");
7051    if(type.kind == arrayType)
7052       PrintArraySize(type, string);
7053    else if(type.kind == functionType)
7054    {
7055       Type param;
7056       strcat(string, "(");
7057       for(param = type.params.first; param; param = param.next)
7058       {
7059          PrintType(param, string, true, fullName);
7060          if(param.next) strcat(string, ", ");
7061       }
7062       strcat(string, ")");
7063    }
7064    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7065       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7066 }
7067
7068 // *****
7069 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7070 // *****
7071 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7072 {
7073    PrePrintType(type, string, fullName, null, printConst);
7074
7075    if(type.thisClass || (printName && type.name && type.name[0]))
7076       strcat(string, " ");
7077    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7078    {
7079       Symbol _class = type.thisClass;
7080       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7081       {
7082          if(type.classObjectType == classPointer)
7083             strcat(string, "class");
7084          else
7085             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7086       }
7087       else if(_class && _class.string)
7088       {
7089          String s = _class.string;
7090          if(fullName)
7091             strcat(string, s);
7092          else
7093          {
7094             char * name = RSearchString(s, "::", strlen(s), true, false);
7095             if(name) name += 2; else name = s;
7096             strcat(string, name);
7097          }
7098       }
7099       strcat(string, "::");
7100    }
7101
7102    if(printName && type.name)
7103       PrintName(type, string, fullName);
7104    PostPrintType(type, string, fullName);
7105    if(type.bitFieldCount)
7106    {
7107       char count[100];
7108       sprintf(count, ":%d", type.bitFieldCount);
7109       strcat(string, count);
7110    }
7111 }
7112
7113 void PrintType(Type type, char * string, bool printName, bool fullName)
7114 {
7115    _PrintType(type, string, printName, fullName, true);
7116 }
7117
7118 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7119 {
7120    _PrintType(type, string, printName, fullName, false);
7121 }
7122
7123 static Type FindMember(Type type, char * string)
7124 {
7125    Type memberType;
7126    for(memberType = type.members.first; memberType; memberType = memberType.next)
7127    {
7128       if(!memberType.name)
7129       {
7130          Type subType = FindMember(memberType, string);
7131          if(subType)
7132             return subType;
7133       }
7134       else if(!strcmp(memberType.name, string))
7135          return memberType;
7136    }
7137    return null;
7138 }
7139
7140 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7141 {
7142    Type memberType;
7143    for(memberType = type.members.first; memberType; memberType = memberType.next)
7144    {
7145       if(!memberType.name)
7146       {
7147          Type subType = FindMember(memberType, string);
7148          if(subType)
7149          {
7150             *offset += memberType.offset;
7151             return subType;
7152          }
7153       }
7154       else if(!strcmp(memberType.name, string))
7155       {
7156          *offset += memberType.offset;
7157          return memberType;
7158       }
7159    }
7160    return null;
7161 }
7162
7163 public bool GetParseError() { return parseError; }
7164
7165 Expression ParseExpressionString(char * expression)
7166 {
7167    parseError = false;
7168
7169    fileInput = TempFile { };
7170    fileInput.Write(expression, 1, strlen(expression));
7171    fileInput.Seek(0, start);
7172
7173    echoOn = false;
7174    parsedExpression = null;
7175    resetScanner();
7176    expression_yyparse();
7177    delete fileInput;
7178
7179    return parsedExpression;
7180 }
7181
7182 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7183 {
7184    Identifier id = exp.identifier;
7185    Method method = null;
7186    Property prop = null;
7187    DataMember member = null;
7188    ClassProperty classProp = null;
7189
7190    if(_class && _class.type == enumClass)
7191    {
7192       NamedLink value = null;
7193       Class enumClass = eSystem_FindClass(privateModule, "enum");
7194       if(enumClass)
7195       {
7196          Class baseClass;
7197          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7198          {
7199             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7200             for(value = e.values.first; value; value = value.next)
7201             {
7202                if(!strcmp(value.name, id.string))
7203                   break;
7204             }
7205             if(value)
7206             {
7207                char constant[256];
7208
7209                FreeExpContents(exp);
7210
7211                exp.type = constantExp;
7212                exp.isConstant = true;
7213                if(!strcmp(baseClass.dataTypeString, "int"))
7214                   sprintf(constant, "%d",(int)value.data);
7215                else
7216                   sprintf(constant, "0x%X",(int)value.data);
7217                exp.constant = CopyString(constant);
7218                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7219                exp.expType = MkClassType(baseClass.fullName);
7220                break;
7221             }
7222          }
7223       }
7224       if(value)
7225          return true;
7226    }
7227    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7228    {
7229       ProcessMethodType(method);
7230       exp.expType = Type
7231       {
7232          refCount = 1;
7233          kind = methodType;
7234          method = method;
7235          // Crash here?
7236          // TOCHECK: Put it back to what it was...
7237          // methodClass = _class;
7238          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7239       };
7240       //id._class = null;
7241       return true;
7242    }
7243    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7244    {
7245       if(!prop.dataType)
7246          ProcessPropertyType(prop);
7247       exp.expType = prop.dataType;
7248       if(prop.dataType) prop.dataType.refCount++;
7249       return true;
7250    }
7251    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7252    {
7253       if(!member.dataType)
7254          member.dataType = ProcessTypeString(member.dataTypeString, false);
7255       exp.expType = member.dataType;
7256       if(member.dataType) member.dataType.refCount++;
7257       return true;
7258    }
7259    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7260    {
7261       if(!classProp.dataType)
7262          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7263
7264       if(classProp.constant)
7265       {
7266          FreeExpContents(exp);
7267
7268          exp.isConstant = true;
7269          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7270          {
7271             //char constant[256];
7272             exp.type = stringExp;
7273             exp.constant = QMkString((char *)classProp.Get(_class));
7274          }
7275          else
7276          {
7277             char constant[256];
7278             exp.type = constantExp;
7279             sprintf(constant, "%d", (int)classProp.Get(_class));
7280             exp.constant = CopyString(constant);
7281          }
7282       }
7283       else
7284       {
7285          // TO IMPLEMENT...
7286       }
7287
7288       exp.expType = classProp.dataType;
7289       if(classProp.dataType) classProp.dataType.refCount++;
7290       return true;
7291    }
7292    return false;
7293 }
7294
7295 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7296 {
7297    BinaryTree * tree = &nameSpace.functions;
7298    GlobalData data = (GlobalData)tree->FindString(name);
7299    NameSpace * child;
7300    if(!data)
7301    {
7302       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7303       {
7304          data = ScanGlobalData(child, name);
7305          if(data)
7306             break;
7307       }
7308    }
7309    return data;
7310 }
7311
7312 static GlobalData FindGlobalData(char * name)
7313 {
7314    int start = 0, c;
7315    NameSpace * nameSpace;
7316    nameSpace = globalData;
7317    for(c = 0; name[c]; c++)
7318    {
7319       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7320       {
7321          NameSpace * newSpace;
7322          char * spaceName = new char[c - start + 1];
7323          strncpy(spaceName, name + start, c - start);
7324          spaceName[c-start] = '\0';
7325          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7326          delete spaceName;
7327          if(!newSpace)
7328             return null;
7329          nameSpace = newSpace;
7330          if(name[c] == ':') c++;
7331          start = c+1;
7332       }
7333    }
7334    if(c - start)
7335    {
7336       return ScanGlobalData(nameSpace, name + start);
7337    }
7338    return null;
7339 }
7340
7341 static int definedExpStackPos;
7342 static void * definedExpStack[512];
7343
7344 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7345 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7346 {
7347    Expression prev = checkedExp.prev, next = checkedExp.next;
7348
7349    FreeExpContents(checkedExp);
7350    FreeType(checkedExp.expType);
7351    FreeType(checkedExp.destType);
7352
7353    *checkedExp = *newExp;
7354
7355    delete newExp;
7356
7357    checkedExp.prev = prev;
7358    checkedExp.next = next;
7359 }
7360
7361 void ApplyAnyObjectLogic(Expression e)
7362 {
7363    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7364 #ifdef _DEBUG
7365    char debugExpString[4096];
7366    debugExpString[0] = '\0';
7367    PrintExpression(e, debugExpString);
7368 #endif
7369
7370    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7371    {
7372       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7373       //ellipsisDestType = destType;
7374       if(e && e.expType)
7375       {
7376          Type type = e.expType;
7377          Class _class = null;
7378          //Type destType = e.destType;
7379
7380          if(type.kind == classType && type._class && type._class.registered)
7381          {
7382             _class = type._class.registered;
7383          }
7384          else if(type.kind == subClassType)
7385          {
7386             _class = FindClass("ecere::com::Class").registered;
7387          }
7388          else
7389          {
7390             char string[1024] = "";
7391             Symbol classSym;
7392
7393             PrintTypeNoConst(type, string, false, true);
7394             classSym = FindClass(string);
7395             if(classSym) _class = classSym.registered;
7396          }
7397
7398          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...
7399             (!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))) ||
7400             destType.byReference)))
7401          {
7402             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7403             {
7404                Expression checkedExp = e, newExp;
7405
7406                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7407                {
7408                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7409                   {
7410                      if(checkedExp.type == extensionCompoundExp)
7411                      {
7412                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7413                      }
7414                      else
7415                         checkedExp = checkedExp.list->last;
7416                   }
7417                   else if(checkedExp.type == castExp)
7418                      checkedExp = checkedExp.cast.exp;
7419                }
7420
7421                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7422                {
7423                   newExp = checkedExp.op.exp2;
7424                   checkedExp.op.exp2 = null;
7425                   FreeExpContents(checkedExp);
7426
7427                   if(e.expType && e.expType.passAsTemplate)
7428                   {
7429                      char size[100];
7430                      ComputeTypeSize(e.expType);
7431                      sprintf(size, "%d", e.expType.size);
7432                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7433                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7434                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7435                   }
7436
7437                   ReplaceExpContents(checkedExp, newExp);
7438                   e.byReference = true;
7439                }
7440                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7441                {
7442                   Expression checkedExp; //, newExp;
7443
7444                   {
7445                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7446                      bool hasAddress =
7447                         e.type == identifierExp ||
7448                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7449                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7450                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7451                         e.type == indexExp;
7452
7453                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7454                      {
7455                         Context context = PushContext();
7456                         Declarator decl;
7457                         OldList * specs = MkList();
7458                         char typeString[1024];
7459                         Expression newExp { };
7460
7461                         typeString[0] = '\0';
7462                         *newExp = *e;
7463
7464                         //if(e.destType) e.destType.refCount++;
7465                         // if(exp.expType) exp.expType.refCount++;
7466                         newExp.prev = null;
7467                         newExp.next = null;
7468                         newExp.expType = null;
7469
7470                         PrintTypeNoConst(e.expType, typeString, false, true);
7471                         decl = SpecDeclFromString(typeString, specs, null);
7472                         newExp.destType = ProcessType(specs, decl);
7473
7474                         curContext = context;
7475
7476                         // We need a current compound for this
7477                         if(curCompound)
7478                         {
7479                            char name[100];
7480                            OldList * stmts = MkList();
7481                            e.type = extensionCompoundExp;
7482                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7483                            if(!curCompound.compound.declarations)
7484                               curCompound.compound.declarations = MkList();
7485                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7486                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7487                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7488                            e.compound = MkCompoundStmt(null, stmts);
7489                         }
7490                         else
7491                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7492
7493                         /*
7494                         e.compound = MkCompoundStmt(
7495                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7496                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7497
7498                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7499                         */
7500
7501                         {
7502                            Type type = e.destType;
7503                            e.destType = { };
7504                            CopyTypeInto(e.destType, type);
7505                            e.destType.refCount = 1;
7506                            e.destType.classObjectType = none;
7507                            FreeType(type);
7508                         }
7509
7510                         e.compound.compound.context = context;
7511                         PopContext(context);
7512                         curContext = context.parent;
7513                      }
7514                   }
7515
7516                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7517                   checkedExp = e;
7518                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7519                   {
7520                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7521                      {
7522                         if(checkedExp.type == extensionCompoundExp)
7523                         {
7524                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7525                         }
7526                         else
7527                            checkedExp = checkedExp.list->last;
7528                      }
7529                      else if(checkedExp.type == castExp)
7530                         checkedExp = checkedExp.cast.exp;
7531                   }
7532                   {
7533                      Expression operand { };
7534                      operand = *checkedExp;
7535                      checkedExp.destType = null;
7536                      checkedExp.expType = null;
7537                      checkedExp.Clear();
7538                      checkedExp.type = opExp;
7539                      checkedExp.op.op = '&';
7540                      checkedExp.op.exp1 = null;
7541                      checkedExp.op.exp2 = operand;
7542
7543                      //newExp = MkExpOp(null, '&', checkedExp);
7544                   }
7545                   //ReplaceExpContents(checkedExp, newExp);
7546                }
7547             }
7548          }
7549       }
7550    }
7551    {
7552       // If expression type is a simple class, make it an address
7553       // FixReference(e, true);
7554    }
7555 //#if 0
7556    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7557       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7558          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7559    {
7560       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"))
7561       {
7562          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7563       }
7564       else
7565       {
7566          Expression thisExp { };
7567
7568          *thisExp = *e;
7569          thisExp.prev = null;
7570          thisExp.next = null;
7571          e.Clear();
7572
7573          e.type = bracketsExp;
7574          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7575          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7576             ((Expression)e.list->first).byReference = true;
7577
7578          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7579          {
7580             e.expType = thisExp.expType;
7581             e.expType.refCount++;
7582          }
7583          else*/
7584          {
7585             e.expType = { };
7586             CopyTypeInto(e.expType, thisExp.expType);
7587             e.expType.byReference = false;
7588             e.expType.refCount = 1;
7589
7590             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7591                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7592             {
7593                e.expType.classObjectType = none;
7594             }
7595          }
7596       }
7597    }
7598 // TOFIX: Try this for a nice IDE crash!
7599 //#endif
7600    // The other way around
7601    else
7602 //#endif
7603    if(destType && e.expType &&
7604          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7605          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7606          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7607    {
7608       if(destType.kind == ellipsisType)
7609       {
7610          Compiler_Error($"Unspecified type\n");
7611       }
7612       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7613       {
7614          bool byReference = e.expType.byReference;
7615          Expression thisExp { };
7616          Declarator decl;
7617          OldList * specs = MkList();
7618          char typeString[1024]; // Watch buffer overruns
7619          Type type;
7620          ClassObjectType backupClassObjectType;
7621          bool backupByReference;
7622
7623          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7624             type = e.expType;
7625          else
7626             type = destType;
7627
7628          backupClassObjectType = type.classObjectType;
7629          backupByReference = type.byReference;
7630
7631          type.classObjectType = none;
7632          type.byReference = false;
7633
7634          typeString[0] = '\0';
7635          PrintType(type, typeString, false, true);
7636          decl = SpecDeclFromString(typeString, specs, null);
7637
7638          type.classObjectType = backupClassObjectType;
7639          type.byReference = backupByReference;
7640
7641          *thisExp = *e;
7642          thisExp.prev = null;
7643          thisExp.next = null;
7644          e.Clear();
7645
7646          if( ( type.kind == classType && type._class && type._class.registered &&
7647                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7648                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7649              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7650              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7651          {
7652             e.type = opExp;
7653             e.op.op = '*';
7654             e.op.exp1 = null;
7655             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7656
7657             e.expType = { };
7658             CopyTypeInto(e.expType, type);
7659             e.expType.byReference = false;
7660             e.expType.refCount = 1;
7661          }
7662          else
7663          {
7664             e.type = castExp;
7665             e.cast.typeName = MkTypeName(specs, decl);
7666             e.cast.exp = thisExp;
7667             e.byReference = true;
7668             e.expType = type;
7669             type.refCount++;
7670          }
7671          e.destType = destType;
7672          destType.refCount++;
7673       }
7674    }
7675 }
7676
7677 void ApplyLocation(Expression exp, Location loc)
7678 {
7679    exp.loc = loc;
7680    switch(exp.type)
7681    {
7682       case opExp:
7683          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7684          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7685          break;
7686       case bracketsExp:
7687          if(exp.list)
7688          {
7689             Expression e;
7690             for(e = exp.list->first; e; e = e.next)
7691                ApplyLocation(e, loc);
7692          }
7693          break;
7694       case indexExp:
7695          if(exp.index.index)
7696          {
7697             Expression e;
7698             for(e = exp.index.index->first; e; e = e.next)
7699                ApplyLocation(e, loc);
7700          }
7701          if(exp.index.exp)
7702             ApplyLocation(exp.index.exp, loc);
7703          break;
7704       case callExp:
7705          if(exp.call.arguments)
7706          {
7707             Expression arg;
7708             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7709                ApplyLocation(arg, loc);
7710          }
7711          if(exp.call.exp)
7712             ApplyLocation(exp.call.exp, loc);
7713          break;
7714       case memberExp:
7715       case pointerExp:
7716          if(exp.member.exp)
7717             ApplyLocation(exp.member.exp, loc);
7718          break;
7719       case castExp:
7720          if(exp.cast.exp)
7721             ApplyLocation(exp.cast.exp, loc);
7722          break;
7723       case conditionExp:
7724          if(exp.cond.exp)
7725          {
7726             Expression e;
7727             for(e = exp.cond.exp->first; e; e = e.next)
7728                ApplyLocation(e, loc);
7729          }
7730          if(exp.cond.cond)
7731             ApplyLocation(exp.cond.cond, loc);
7732          if(exp.cond.elseExp)
7733             ApplyLocation(exp.cond.elseExp, loc);
7734          break;
7735       case vaArgExp:
7736          if(exp.vaArg.exp)
7737             ApplyLocation(exp.vaArg.exp, loc);
7738          break;
7739       default:
7740          break;
7741    }
7742 }
7743
7744 void ProcessExpressionType(Expression exp)
7745 {
7746    bool unresolved = false;
7747    Location oldyylloc = yylloc;
7748    bool notByReference = false;
7749 #ifdef _DEBUG
7750    char debugExpString[4096];
7751    debugExpString[0] = '\0';
7752    PrintExpression(exp, debugExpString);
7753 #endif
7754    if(!exp || exp.expType)
7755       return;
7756
7757    //eSystem_Logf("%s\n", expString);
7758
7759    // Testing this here
7760    yylloc = exp.loc;
7761    switch(exp.type)
7762    {
7763       case identifierExp:
7764       {
7765          Identifier id = exp.identifier;
7766          if(!id || !topContext) return;
7767
7768          // DOING THIS LATER NOW...
7769          if(id._class && id._class.name)
7770          {
7771             id.classSym = id._class.symbol; // FindClass(id._class.name);
7772             /* TODO: Name Space Fix ups
7773             if(!id.classSym)
7774                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7775             */
7776          }
7777
7778          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7779          {
7780             exp.expType = ProcessTypeString("Module", true);
7781             break;
7782          }
7783          else */if(strstr(id.string, "__ecereClass") == id.string)
7784          {
7785             exp.expType = ProcessTypeString("ecere::com::Class", true);
7786             break;
7787          }
7788          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7789          {
7790             // Added this here as well
7791             ReplaceClassMembers(exp, thisClass);
7792             if(exp.type != identifierExp)
7793             {
7794                ProcessExpressionType(exp);
7795                break;
7796             }
7797
7798             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7799                break;
7800          }
7801          else
7802          {
7803             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7804             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7805             if(!symbol/* && exp.destType*/)
7806             {
7807                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
7808                   break;
7809                else
7810                {
7811                   if(thisClass)
7812                   {
7813                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7814                      if(exp.type != identifierExp)
7815                      {
7816                         ProcessExpressionType(exp);
7817                         break;
7818                      }
7819                   }
7820                   // Static methods called from inside the _class
7821                   else if(currentClass && !id._class)
7822                   {
7823                      if(ResolveIdWithClass(exp, currentClass, true))
7824                         break;
7825                   }
7826                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7827                }
7828             }
7829
7830             // If we manage to resolve this symbol
7831             if(symbol)
7832             {
7833                Type type = symbol.type;
7834                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7835
7836                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7837                {
7838                   Context context = SetupTemplatesContext(_class);
7839                   type = ReplaceThisClassType(_class);
7840                   FinishTemplatesContext(context);
7841                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7842                }
7843
7844                FreeSpecifier(id._class);
7845                id._class = null;
7846                delete id.string;
7847                id.string = CopyString(symbol.string);
7848
7849                id.classSym = null;
7850                exp.expType = type;
7851                if(type)
7852                   type.refCount++;
7853
7854                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7855                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7856                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7857                   // Add missing cases here... enum Classes...
7858                   exp.isConstant = true;
7859
7860                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7861                if(symbol.isParam || !strcmp(id.string, "this"))
7862                {
7863                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7864                      exp.byReference = true;
7865
7866                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7867                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7868                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7869                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7870                   {
7871                      Identifier id = exp.identifier;
7872                      exp.type = bracketsExp;
7873                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7874                   }*/
7875                }
7876
7877                if(symbol.isIterator)
7878                {
7879                   if(symbol.isIterator == 3)
7880                   {
7881                      exp.type = bracketsExp;
7882                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7883                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7884                      exp.expType = null;
7885                      ProcessExpressionType(exp);
7886                   }
7887                   else if(symbol.isIterator != 4)
7888                   {
7889                      exp.type = memberExp;
7890                      exp.member.exp = MkExpIdentifier(exp.identifier);
7891                      exp.member.exp.expType = exp.expType;
7892                      /*if(symbol.isIterator == 6)
7893                         exp.member.member = MkIdentifier("key");
7894                      else*/
7895                         exp.member.member = MkIdentifier("data");
7896                      exp.expType = null;
7897                      ProcessExpressionType(exp);
7898                   }
7899                }
7900                break;
7901             }
7902             else
7903             {
7904                DefinedExpression definedExp = null;
7905                if(thisNameSpace && !(id._class && !id._class.name))
7906                {
7907                   char name[1024];
7908                   strcpy(name, thisNameSpace);
7909                   strcat(name, "::");
7910                   strcat(name, id.string);
7911                   definedExp = eSystem_FindDefine(privateModule, name);
7912                }
7913                if(!definedExp)
7914                   definedExp = eSystem_FindDefine(privateModule, id.string);
7915                if(definedExp)
7916                {
7917                   int c;
7918                   for(c = 0; c<definedExpStackPos; c++)
7919                      if(definedExpStack[c] == definedExp)
7920                         break;
7921                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7922                   {
7923                      Location backupYylloc = yylloc;
7924                      File backInput = fileInput;
7925                      definedExpStack[definedExpStackPos++] = definedExp;
7926
7927                      fileInput = TempFile { };
7928                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7929                      fileInput.Seek(0, start);
7930
7931                      echoOn = false;
7932                      parsedExpression = null;
7933                      resetScanner();
7934                      expression_yyparse();
7935                      delete fileInput;
7936                      if(backInput)
7937                         fileInput = backInput;
7938
7939                      yylloc = backupYylloc;
7940
7941                      if(parsedExpression)
7942                      {
7943                         FreeIdentifier(id);
7944                         exp.type = bracketsExp;
7945                         exp.list = MkListOne(parsedExpression);
7946                         ApplyLocation(parsedExpression, yylloc);
7947                         ProcessExpressionType(exp);
7948                         definedExpStackPos--;
7949                         return;
7950                      }
7951                      definedExpStackPos--;
7952                   }
7953                   else
7954                   {
7955                      if(inCompiler)
7956                      {
7957                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7958                      }
7959                   }
7960                }
7961                else
7962                {
7963                   GlobalData data = null;
7964                   if(thisNameSpace && !(id._class && !id._class.name))
7965                   {
7966                      char name[1024];
7967                      strcpy(name, thisNameSpace);
7968                      strcat(name, "::");
7969                      strcat(name, id.string);
7970                      data = FindGlobalData(name);
7971                   }
7972                   if(!data)
7973                      data = FindGlobalData(id.string);
7974                   if(data)
7975                   {
7976                      DeclareGlobalData(data);
7977                      exp.expType = data.dataType;
7978                      if(data.dataType) data.dataType.refCount++;
7979
7980                      delete id.string;
7981                      id.string = CopyString(data.fullName);
7982                      FreeSpecifier(id._class);
7983                      id._class = null;
7984
7985                      break;
7986                   }
7987                   else
7988                   {
7989                      GlobalFunction function = null;
7990                      if(thisNameSpace && !(id._class && !id._class.name))
7991                      {
7992                         char name[1024];
7993                         strcpy(name, thisNameSpace);
7994                         strcat(name, "::");
7995                         strcat(name, id.string);
7996                         function = eSystem_FindFunction(privateModule, name);
7997                      }
7998                      if(!function)
7999                         function = eSystem_FindFunction(privateModule, id.string);
8000                      if(function)
8001                      {
8002                         char name[1024];
8003                         delete id.string;
8004                         id.string = CopyString(function.name);
8005                         name[0] = 0;
8006
8007                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
8008                            strcpy(name, "__ecereFunction_");
8009                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
8010                         if(DeclareFunction(function, name))
8011                         {
8012                            delete id.string;
8013                            id.string = CopyString(name);
8014                         }
8015                         exp.expType = function.dataType;
8016                         if(function.dataType) function.dataType.refCount++;
8017
8018                         FreeSpecifier(id._class);
8019                         id._class = null;
8020
8021                         break;
8022                      }
8023                   }
8024                }
8025             }
8026          }
8027          unresolved = true;
8028          break;
8029       }
8030       case instanceExp:
8031       {
8032          Class _class;
8033          // Symbol classSym;
8034
8035          if(!exp.instance._class)
8036          {
8037             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8038             {
8039                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8040             }
8041          }
8042
8043          //classSym = FindClass(exp.instance._class.fullName);
8044          //_class = classSym ? classSym.registered : null;
8045
8046          ProcessInstantiationType(exp.instance);
8047
8048          exp.isConstant = exp.instance.isConstant;
8049
8050          /*
8051          if(_class.type == unitClass && _class.base.type != systemClass)
8052          {
8053             {
8054                Type destType = exp.destType;
8055
8056                exp.destType = MkClassType(_class.base.fullName);
8057                exp.expType = MkClassType(_class.fullName);
8058                CheckExpressionType(exp, exp.destType, true);
8059
8060                exp.destType = destType;
8061             }
8062             exp.expType = MkClassType(_class.fullName);
8063          }
8064          else*/
8065          if(exp.instance._class)
8066          {
8067             exp.expType = MkClassType(exp.instance._class.name);
8068             /*if(exp.expType._class && exp.expType._class.registered &&
8069                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8070                exp.expType.byReference = true;*/
8071          }
8072          break;
8073       }
8074       case constantExp:
8075       {
8076          if(!exp.expType)
8077          {
8078             char * constant = exp.constant;
8079             Type type
8080             {
8081                refCount = 1;
8082                constant = true;
8083             };
8084             exp.expType = type;
8085
8086             if(constant[0] == '\'')
8087             {
8088                if((int)((byte *)constant)[1] > 127)
8089                {
8090                   int nb;
8091                   unichar ch = UTF8GetChar(constant + 1, &nb);
8092                   if(nb < 2) ch = constant[1];
8093                   delete constant;
8094                   exp.constant = PrintUInt(ch);
8095                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8096                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8097                   type._class = FindClass("unichar");
8098
8099                   type.isSigned = false;
8100                }
8101                else
8102                {
8103                   type.kind = charType;
8104                   type.isSigned = true;
8105                }
8106             }
8107             else
8108             {
8109                char * dot = strchr(constant, '.');
8110                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8111                char * exponent;
8112                if(isHex)
8113                {
8114                   exponent = strchr(constant, 'p');
8115                   if(!exponent) exponent = strchr(constant, 'P');
8116                }
8117                else
8118                {
8119                   exponent = strchr(constant, 'e');
8120                   if(!exponent) exponent = strchr(constant, 'E');
8121                }
8122
8123                if(dot || exponent)
8124                {
8125                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8126                      type.kind = floatType;
8127                   else
8128                      type.kind = doubleType;
8129                   type.isSigned = true;
8130                }
8131                else
8132                {
8133                   bool isSigned = constant[0] == '-';
8134                   char * endP = null;
8135                   int64 i64 = strtoll(constant, &endP, 0);
8136                   uint64 ui64 = strtoull(constant, &endP, 0);
8137                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
8138                   if(isSigned)
8139                   {
8140                      if(i64 < MININT)
8141                         is64Bit = true;
8142                   }
8143                   else
8144                   {
8145                      if(ui64 > MAXINT)
8146                      {
8147                         if(ui64 > MAXDWORD)
8148                         {
8149                            is64Bit = true;
8150                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8151                               isSigned = true;
8152                         }
8153                      }
8154                      else if(constant[0] != '0' || !constant[1])
8155                         isSigned = true;
8156                   }
8157                   type.kind = is64Bit ? int64Type : intType;
8158                   type.isSigned = isSigned;
8159                }
8160             }
8161             exp.isConstant = true;
8162             if(exp.destType && exp.destType.kind == doubleType)
8163                type.kind = doubleType;
8164             else if(exp.destType && exp.destType.kind == floatType)
8165                type.kind = floatType;
8166             else if(exp.destType && exp.destType.kind == int64Type)
8167                type.kind = int64Type;
8168          }
8169          break;
8170       }
8171       case stringExp:
8172       {
8173          exp.isConstant = true;      // Why wasn't this constant?
8174          exp.expType = Type
8175          {
8176             refCount = 1;
8177             kind = pointerType;
8178             type = Type
8179             {
8180                refCount = 1;
8181                kind = charType;
8182                constant = true;
8183                isSigned = true;
8184             }
8185          };
8186          break;
8187       }
8188       case newExp:
8189       case new0Exp:
8190          ProcessExpressionType(exp._new.size);
8191          exp.expType = Type
8192          {
8193             refCount = 1;
8194             kind = pointerType;
8195             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8196          };
8197          DeclareType(exp.expType.type, false, false);
8198          break;
8199       case renewExp:
8200       case renew0Exp:
8201          ProcessExpressionType(exp._renew.size);
8202          ProcessExpressionType(exp._renew.exp);
8203          exp.expType = Type
8204          {
8205             refCount = 1;
8206             kind = pointerType;
8207             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8208          };
8209          DeclareType(exp.expType.type, false, false);
8210          break;
8211       case opExp:
8212       {
8213          bool assign = false, boolResult = false, boolOps = false;
8214          Type type1 = null, type2 = null;
8215          bool useDestType = false, useSideType = false;
8216          Location oldyylloc = yylloc;
8217          bool useSideUnit = false;
8218          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8219
8220          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8221          Type dummy
8222          {
8223             count = 1;
8224             refCount = 1;
8225          };
8226
8227          switch(exp.op.op)
8228          {
8229             // Assignment Operators
8230             case '=':
8231             case MUL_ASSIGN:
8232             case DIV_ASSIGN:
8233             case MOD_ASSIGN:
8234             case ADD_ASSIGN:
8235             case SUB_ASSIGN:
8236             case LEFT_ASSIGN:
8237             case RIGHT_ASSIGN:
8238             case AND_ASSIGN:
8239             case XOR_ASSIGN:
8240             case OR_ASSIGN:
8241                assign = true;
8242                break;
8243             // boolean Operators
8244             case '!':
8245                // Expect boolean operators
8246                //boolOps = true;
8247                //boolResult = true;
8248                break;
8249             case AND_OP:
8250             case OR_OP:
8251                // Expect boolean operands
8252                boolOps = true;
8253                boolResult = true;
8254                break;
8255             // Comparisons
8256             case EQ_OP:
8257             case '<':
8258             case '>':
8259             case LE_OP:
8260             case GE_OP:
8261             case NE_OP:
8262                // Gives boolean result
8263                boolResult = true;
8264                useSideType = true;
8265                break;
8266             case '+':
8267             case '-':
8268                useSideUnit = true;
8269                useSideType = true;
8270                useDestType = true;
8271                break;
8272
8273             case LEFT_OP:
8274             case RIGHT_OP:
8275                useSideType = true;
8276                useDestType = true;
8277                break;
8278
8279             case '|':
8280             case '^':
8281                useSideType = true;
8282                useDestType = true;
8283                break;
8284
8285             case '/':
8286             case '%':
8287                useSideType = true;
8288                useDestType = true;
8289                break;
8290             case '&':
8291             case '*':
8292                if(exp.op.exp1)
8293                {
8294                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8295                   useSideType = true;
8296                   useDestType = true;
8297                }
8298                break;
8299
8300             /*// Implement speed etc.
8301             case '*':
8302             case '/':
8303                break;
8304             */
8305          }
8306          if(exp.op.op == '&')
8307          {
8308             // Added this here earlier for Iterator address as key
8309             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8310             {
8311                Identifier id = exp.op.exp2.identifier;
8312                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8313                if(symbol && symbol.isIterator == 2)
8314                {
8315                   exp.type = memberExp;
8316                   exp.member.exp = exp.op.exp2;
8317                   exp.member.member = MkIdentifier("key");
8318                   exp.expType = null;
8319                   exp.op.exp2.expType = symbol.type;
8320                   symbol.type.refCount++;
8321                   ProcessExpressionType(exp);
8322                   FreeType(dummy);
8323                   break;
8324                }
8325                // exp.op.exp2.usage.usageRef = true;
8326             }
8327          }
8328
8329          //dummy.kind = TypeDummy;
8330          if(exp.op.exp1)
8331          {
8332             // Added this check here to use the dest type only for units derived from the base unit
8333             // So that untyped units will use the side unit as opposed to the untyped destination unit
8334             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8335             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8336                useDestType = false;
8337
8338             if(destClass && useDestType &&
8339               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8340
8341               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8342             {
8343                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8344                exp.op.exp1.destType = exp.destType;
8345                exp.op.exp1.opDestType = true;
8346                if(exp.destType)
8347                   exp.destType.refCount++;
8348             }
8349             else if(!assign)
8350             {
8351                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8352                exp.op.exp1.destType = dummy;
8353                dummy.refCount++;
8354             }
8355
8356             // TESTING THIS HERE...
8357             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8358                ProcessExpressionType(exp.op.exp1);
8359             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8360
8361             exp.op.exp1.opDestType = false;
8362
8363             // Fix for unit and ++ / --
8364             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8365                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8366             {
8367                exp.op.exp2 = MkExpConstant("1");
8368                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8369                assign = true;
8370             }
8371
8372             if(exp.op.exp1.destType == dummy)
8373             {
8374                FreeType(dummy);
8375                exp.op.exp1.destType = null;
8376             }
8377             type1 = exp.op.exp1.expType;
8378          }
8379
8380          if(exp.op.exp2)
8381          {
8382             char expString[10240];
8383             expString[0] = '\0';
8384             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8385             {
8386                if(exp.op.exp1)
8387                {
8388                   exp.op.exp2.destType = exp.op.exp1.expType;
8389                   if(exp.op.exp1.expType)
8390                      exp.op.exp1.expType.refCount++;
8391                }
8392                else
8393                {
8394                   exp.op.exp2.destType = exp.destType;
8395                   if(!exp.op.exp1 || exp.op.op != '&')
8396                      exp.op.exp2.opDestType = true;
8397                   if(exp.destType)
8398                      exp.destType.refCount++;
8399                }
8400
8401                if(type1) type1.refCount++;
8402                exp.expType = type1;
8403             }
8404             else if(assign)
8405             {
8406                if(inCompiler)
8407                   PrintExpression(exp.op.exp2, expString);
8408
8409                if(type1 && type1.kind == pointerType)
8410                {
8411                   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 ||
8412                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8413                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8414                   else if(exp.op.op == '=')
8415                   {
8416                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8417                      exp.op.exp2.destType = type1;
8418                      if(type1)
8419                         type1.refCount++;
8420                   }
8421                }
8422                else
8423                {
8424                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8425                   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/* ||
8426                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8427                   else
8428                   {
8429                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8430                      exp.op.exp2.destType = type1;
8431                      if(type1)
8432                         type1.refCount++;
8433                   }
8434                }
8435                if(type1) type1.refCount++;
8436                exp.expType = type1;
8437             }
8438             else if(destClass &&
8439                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8440                   (destClass.type == enumClass && useDestType)))
8441             {
8442                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8443                exp.op.exp2.destType = exp.destType;
8444                if(exp.op.op != '&')
8445                   exp.op.exp2.opDestType = true;
8446                if(exp.destType)
8447                   exp.destType.refCount++;
8448             }
8449             else
8450             {
8451                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8452                exp.op.exp2.destType = dummy;
8453                dummy.refCount++;
8454             }
8455
8456             // TESTING THIS HERE... (DANGEROUS)
8457             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8458                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8459             {
8460                FreeType(exp.op.exp2.destType);
8461                exp.op.exp2.destType = type1;
8462                type1.refCount++;
8463             }
8464             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8465             // Cannot lose the cast on a sizeof
8466             if(exp.op.op == SIZEOF)
8467             {
8468                Expression e = exp.op.exp2;
8469                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8470                {
8471                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8472                   {
8473                      if(e.type == extensionCompoundExp)
8474                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8475                      else
8476                         e = e.list->last;
8477                   }
8478                }
8479                if(e.type == castExp && e.cast.exp)
8480                   e.cast.exp.needCast = true;
8481             }
8482             ProcessExpressionType(exp.op.exp2);
8483             exp.op.exp2.opDestType = false;
8484             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8485
8486             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8487             {
8488                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)
8489                {
8490                   if(exp.op.op != '=' && type1.type.kind == voidType)
8491                      Compiler_Error($"void *: unknown size\n");
8492                }
8493                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||
8494                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8495                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8496                               exp.op.exp2.expType._class.registered.type == structClass ||
8497                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8498                {
8499                   if(exp.op.op == ADD_ASSIGN)
8500                      Compiler_Error($"cannot add two pointers\n");
8501                }
8502                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8503                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8504                {
8505                   if(exp.op.op == ADD_ASSIGN)
8506                      Compiler_Error($"cannot add two pointers\n");
8507                }
8508                else if(inCompiler)
8509                {
8510                   char type1String[1024];
8511                   char type2String[1024];
8512                   type1String[0] = '\0';
8513                   type2String[0] = '\0';
8514
8515                   PrintType(exp.op.exp2.expType, type1String, false, true);
8516                   PrintType(type1, type2String, false, true);
8517                   ChangeCh(expString, '\n', ' ');
8518                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8519                }
8520             }
8521
8522             if(exp.op.exp2.destType == dummy)
8523             {
8524                FreeType(dummy);
8525                exp.op.exp2.destType = null;
8526             }
8527
8528             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8529             {
8530                type2 = { };
8531                type2.refCount = 1;
8532                CopyTypeInto(type2, exp.op.exp2.expType);
8533                type2.isSigned = true;
8534             }
8535             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8536             {
8537                type2 = { kind = intType };
8538                type2.refCount = 1;
8539                type2.isSigned = true;
8540             }
8541             else
8542             {
8543                type2 = exp.op.exp2.expType;
8544                if(type2) type2.refCount++;
8545             }
8546          }
8547
8548          dummy.kind = voidType;
8549
8550          if(exp.op.op == SIZEOF)
8551          {
8552             exp.expType = Type
8553             {
8554                refCount = 1;
8555                kind = intSizeType;
8556             };
8557             exp.isConstant = true;
8558          }
8559          // Get type of dereferenced pointer
8560          else if(exp.op.op == '*' && !exp.op.exp1)
8561          {
8562             exp.expType = Dereference(type2);
8563             if(type2 && type2.kind == classType)
8564                notByReference = true;
8565          }
8566          else if(exp.op.op == '&' && !exp.op.exp1)
8567             exp.expType = Reference(type2);
8568          else if(!assign)
8569          {
8570             if(boolOps)
8571             {
8572                if(exp.op.exp1)
8573                {
8574                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8575                   exp.op.exp1.destType = MkClassType("bool");
8576                   exp.op.exp1.destType.truth = true;
8577                   if(!exp.op.exp1.expType)
8578                      ProcessExpressionType(exp.op.exp1);
8579                   else
8580                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8581                   FreeType(exp.op.exp1.expType);
8582                   exp.op.exp1.expType = MkClassType("bool");
8583                   exp.op.exp1.expType.truth = true;
8584                }
8585                if(exp.op.exp2)
8586                {
8587                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8588                   exp.op.exp2.destType = MkClassType("bool");
8589                   exp.op.exp2.destType.truth = true;
8590                   if(!exp.op.exp2.expType)
8591                      ProcessExpressionType(exp.op.exp2);
8592                   else
8593                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8594                   FreeType(exp.op.exp2.expType);
8595                   exp.op.exp2.expType = MkClassType("bool");
8596                   exp.op.exp2.expType.truth = true;
8597                }
8598             }
8599             else if(exp.op.exp1 && exp.op.exp2 &&
8600                ((useSideType /*&&
8601                      (useSideUnit ||
8602                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8603                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8604                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8605                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8606             {
8607                if(type1 && type2 &&
8608                   // If either both are class or both are not class
8609                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8610                {
8611                   // Added this check for enum subtraction to result in an int type:
8612                   if(exp.op.op == '-' &&
8613                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8614                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8615                   {
8616                      Type intType;
8617                      if(!type1._class.registered.dataType)
8618                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8619                      if(!type2._class.registered.dataType)
8620                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8621
8622                      intType = ProcessTypeString(
8623                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8624
8625                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8626                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8627                      exp.op.exp1.destType = intType;
8628                      exp.op.exp2.destType = intType;
8629                      intType.refCount++;
8630                   }
8631                   else
8632                   {
8633                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8634                      exp.op.exp2.destType = type1;
8635                      type1.refCount++;
8636                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8637                      exp.op.exp1.destType = type2;
8638                      type2.refCount++;
8639                   }
8640
8641                   // Warning here for adding Radians + Degrees with no destination type
8642                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8643                      type1._class.registered && type1._class.registered.type == unitClass &&
8644                      type2._class.registered && type2._class.registered.type == unitClass &&
8645                      type1._class.registered != type2._class.registered)
8646                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8647                         type1._class.string, type2._class.string, type1._class.string);
8648
8649                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8650                   {
8651                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8652                      if(argExp)
8653                      {
8654                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8655
8656                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8657                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8658                            exp.op.exp1)));
8659
8660                         ProcessExpressionType(exp.op.exp1);
8661
8662                         if(type2.kind != pointerType)
8663                         {
8664                            ProcessExpressionType(classExp);
8665
8666                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8667
8668                            if(!exp.op.exp2.expType)
8669                            {
8670                               if(type2)
8671                                  FreeType(type2);
8672                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8673                               type2.refCount++;
8674                            }
8675
8676                            ProcessExpressionType(exp.op.exp2);
8677                         }
8678                      }
8679                   }
8680
8681                   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)))
8682                   {
8683                      if(type1.kind != classType && type1.type.kind == voidType)
8684                         Compiler_Error($"void *: unknown size\n");
8685                      exp.expType = type1;
8686                      if(type1) type1.refCount++;
8687                   }
8688                   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)))
8689                   {
8690                      if(type2.kind != classType && type2.type.kind == voidType)
8691                         Compiler_Error($"void *: unknown size\n");
8692                      exp.expType = type2;
8693                      if(type2) type2.refCount++;
8694                   }
8695                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8696                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8697                   {
8698                      Compiler_Warning($"different levels of indirection\n");
8699                   }
8700                   else
8701                   {
8702                      bool success = false;
8703                      if(type1.kind == pointerType && type2.kind == pointerType)
8704                      {
8705                         if(exp.op.op == '+')
8706                            Compiler_Error($"cannot add two pointers\n");
8707                         else if(exp.op.op == '-')
8708                         {
8709                            // Pointer Subtraction gives integer
8710                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8711                            {
8712                               exp.expType = Type
8713                               {
8714                                  kind = intType;
8715                                  refCount = 1;
8716                               };
8717                               success = true;
8718
8719                               if(type1.type.kind == templateType)
8720                               {
8721                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8722                                  if(argExp)
8723                                  {
8724                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8725
8726                                     ProcessExpressionType(classExp);
8727
8728                                     exp.type = bracketsExp;
8729                                     exp.list = MkListOne(MkExpOp(
8730                                        MkExpBrackets(MkListOne(MkExpOp(
8731                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8732                                              , exp.op.op,
8733                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8734                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8735
8736                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8737                                     FreeType(dummy);
8738                                     return;
8739                                  }
8740                               }
8741                            }
8742                         }
8743                      }
8744
8745                      if(!success && exp.op.exp1.type == constantExp)
8746                      {
8747                         // If first expression is constant, try to match that first
8748                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8749                         {
8750                            if(exp.expType) FreeType(exp.expType);
8751                            exp.expType = exp.op.exp1.destType;
8752                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8753                            success = true;
8754                         }
8755                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8756                         {
8757                            if(exp.expType) FreeType(exp.expType);
8758                            exp.expType = exp.op.exp2.destType;
8759                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8760                            success = true;
8761                         }
8762                      }
8763                      else if(!success)
8764                      {
8765                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8766                         {
8767                            if(exp.expType) FreeType(exp.expType);
8768                            exp.expType = exp.op.exp2.destType;
8769                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8770                            success = true;
8771                         }
8772                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8773                         {
8774                            if(exp.expType) FreeType(exp.expType);
8775                            exp.expType = exp.op.exp1.destType;
8776                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8777                            success = true;
8778                         }
8779                      }
8780                      if(!success)
8781                      {
8782                         char expString1[10240];
8783                         char expString2[10240];
8784                         char type1[1024];
8785                         char type2[1024];
8786                         expString1[0] = '\0';
8787                         expString2[0] = '\0';
8788                         type1[0] = '\0';
8789                         type2[0] = '\0';
8790                         if(inCompiler)
8791                         {
8792                            PrintExpression(exp.op.exp1, expString1);
8793                            ChangeCh(expString1, '\n', ' ');
8794                            PrintExpression(exp.op.exp2, expString2);
8795                            ChangeCh(expString2, '\n', ' ');
8796                            PrintType(exp.op.exp1.expType, type1, false, true);
8797                            PrintType(exp.op.exp2.expType, type2, false, true);
8798                         }
8799
8800                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8801                      }
8802                   }
8803                }
8804                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8805                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8806                {
8807                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8808                   // Convert e.g. / 4 into / 4.0
8809                   exp.op.exp1.destType = type2._class.registered.dataType;
8810                   if(type2._class.registered.dataType)
8811                      type2._class.registered.dataType.refCount++;
8812                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8813                   exp.expType = type2;
8814                   if(type2) type2.refCount++;
8815                }
8816                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8817                {
8818                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8819                   // Convert e.g. / 4 into / 4.0
8820                   exp.op.exp2.destType = type1._class.registered.dataType;
8821                   if(type1._class.registered.dataType)
8822                      type1._class.registered.dataType.refCount++;
8823                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8824                   exp.expType = type1;
8825                   if(type1) type1.refCount++;
8826                }
8827                else if(type1)
8828                {
8829                   bool valid = false;
8830
8831                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8832                   {
8833                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8834
8835                      if(!type1._class.registered.dataType)
8836                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8837                      exp.op.exp2.destType = type1._class.registered.dataType;
8838                      exp.op.exp2.destType.refCount++;
8839
8840                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8841                      if(type2)
8842                         FreeType(type2);
8843                      type2 = exp.op.exp2.destType;
8844                      if(type2) type2.refCount++;
8845
8846                      exp.expType = type2;
8847                      type2.refCount++;
8848                   }
8849
8850                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8851                   {
8852                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8853
8854                      if(!type2._class.registered.dataType)
8855                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8856                      exp.op.exp1.destType = type2._class.registered.dataType;
8857                      exp.op.exp1.destType.refCount++;
8858
8859                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8860                      type1 = exp.op.exp1.destType;
8861                      exp.expType = type1;
8862                      type1.refCount++;
8863                   }
8864
8865                   // TESTING THIS NEW CODE
8866                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8867                   {
8868                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8869                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8870                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8871                      {
8872                         // Convert the enum to an int instead for these operators
8873                         if(op1IsEnum && exp.op.exp2.expType)
8874                         {
8875                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8876                            {
8877                               if(exp.expType) FreeType(exp.expType);
8878                               exp.expType = exp.op.exp2.expType;
8879                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8880                               valid = true;
8881                            }
8882                         }
8883                         else if(op2IsEnum && exp.op.exp1.expType)
8884                         {
8885                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8886                            {
8887                               if(exp.expType) FreeType(exp.expType);
8888                               exp.expType = exp.op.exp1.expType;
8889                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8890                               valid = true;
8891                            }
8892                         }
8893                      }
8894                      else
8895                      {
8896                         if(op1IsEnum && exp.op.exp2.expType)
8897                         {
8898                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8899                            {
8900                               if(exp.expType) FreeType(exp.expType);
8901                               exp.expType = exp.op.exp1.expType;
8902                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8903                               valid = true;
8904                            }
8905                         }
8906                         else if(op2IsEnum && exp.op.exp1.expType)
8907                         {
8908                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8909                            {
8910                               if(exp.expType) FreeType(exp.expType);
8911                               exp.expType = exp.op.exp2.expType;
8912                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8913                               valid = true;
8914                            }
8915                         }
8916                      }
8917                   }
8918
8919                   if(!valid)
8920                   {
8921                      // 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
8922                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
8923                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
8924                      {
8925                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8926                         exp.op.exp1.destType = type2;
8927                         type2.refCount++;
8928
8929                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8930                         {
8931                            if(exp.expType) FreeType(exp.expType);
8932                            exp.expType = exp.op.exp1.destType;
8933                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8934                         }
8935                      }
8936                      else
8937                      {
8938                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8939                         exp.op.exp2.destType = type1;
8940                         type1.refCount++;
8941
8942                      /*
8943                      // Maybe this was meant to be an enum...
8944                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8945                      {
8946                         Type oldType = exp.op.exp2.expType;
8947                         exp.op.exp2.expType = null;
8948                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8949                            FreeType(oldType);
8950                         else
8951                            exp.op.exp2.expType = oldType;
8952                      }
8953                      */
8954
8955                      /*
8956                      // TESTING THIS HERE... LATEST ADDITION
8957                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8958                      {
8959                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8960                         exp.op.exp2.destType = type2._class.registered.dataType;
8961                         if(type2._class.registered.dataType)
8962                            type2._class.registered.dataType.refCount++;
8963                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8964
8965                         //exp.expType = type2._class.registered.dataType; //type2;
8966                         //if(type2) type2.refCount++;
8967                      }
8968
8969                      // TESTING THIS HERE... LATEST ADDITION
8970                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8971                      {
8972                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8973                         exp.op.exp1.destType = type1._class.registered.dataType;
8974                         if(type1._class.registered.dataType)
8975                            type1._class.registered.dataType.refCount++;
8976                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8977                         exp.expType = type1._class.registered.dataType; //type1;
8978                         if(type1) type1.refCount++;
8979                      }
8980                      */
8981
8982                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8983                         {
8984                            if(exp.expType) FreeType(exp.expType);
8985                            exp.expType = exp.op.exp2.destType;
8986                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8987                         }
8988                         else if(type1 && type2)
8989                         {
8990                            char expString1[10240];
8991                            char expString2[10240];
8992                            char type1String[1024];
8993                            char type2String[1024];
8994                            expString1[0] = '\0';
8995                            expString2[0] = '\0';
8996                            type1String[0] = '\0';
8997                            type2String[0] = '\0';
8998                            if(inCompiler)
8999                            {
9000                               PrintExpression(exp.op.exp1, expString1);
9001                               ChangeCh(expString1, '\n', ' ');
9002                               PrintExpression(exp.op.exp2, expString2);
9003                               ChangeCh(expString2, '\n', ' ');
9004                               PrintType(exp.op.exp1.expType, type1String, false, true);
9005                               PrintType(exp.op.exp2.expType, type2String, false, true);
9006                            }
9007
9008                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
9009
9010                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9011                            {
9012                               exp.expType = exp.op.exp1.expType;
9013                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9014                            }
9015                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9016                            {
9017                               exp.expType = exp.op.exp2.expType;
9018                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9019                            }
9020                         }
9021                      }
9022                   }
9023                }
9024                else if(type2)
9025                {
9026                   // Maybe this was meant to be an enum...
9027                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9028                   {
9029                      Type oldType = exp.op.exp1.expType;
9030                      exp.op.exp1.expType = null;
9031                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9032                         FreeType(oldType);
9033                      else
9034                         exp.op.exp1.expType = oldType;
9035                   }
9036
9037                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9038                   exp.op.exp1.destType = type2;
9039                   type2.refCount++;
9040                   /*
9041                   // TESTING THIS HERE... LATEST ADDITION
9042                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9043                   {
9044                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9045                      exp.op.exp1.destType = type1._class.registered.dataType;
9046                      if(type1._class.registered.dataType)
9047                         type1._class.registered.dataType.refCount++;
9048                   }
9049
9050                   // TESTING THIS HERE... LATEST ADDITION
9051                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9052                   {
9053                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9054                      exp.op.exp2.destType = type2._class.registered.dataType;
9055                      if(type2._class.registered.dataType)
9056                         type2._class.registered.dataType.refCount++;
9057                   }
9058                   */
9059
9060                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9061                   {
9062                      if(exp.expType) FreeType(exp.expType);
9063                      exp.expType = exp.op.exp1.destType;
9064                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9065                   }
9066                }
9067             }
9068             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9069             {
9070                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9071                {
9072                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9073                   // Convert e.g. / 4 into / 4.0
9074                   exp.op.exp1.destType = type2._class.registered.dataType;
9075                   if(type2._class.registered.dataType)
9076                      type2._class.registered.dataType.refCount++;
9077                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9078                }
9079                if(exp.op.op == '!')
9080                {
9081                   exp.expType = MkClassType("bool");
9082                   exp.expType.truth = true;
9083                }
9084                else
9085                {
9086                   exp.expType = type2;
9087                   if(type2) type2.refCount++;
9088                }
9089             }
9090             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9091             {
9092                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9093                {
9094                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9095                   // Convert e.g. / 4 into / 4.0
9096                   exp.op.exp2.destType = type1._class.registered.dataType;
9097                   if(type1._class.registered.dataType)
9098                      type1._class.registered.dataType.refCount++;
9099                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9100                }
9101                exp.expType = type1;
9102                if(type1) type1.refCount++;
9103             }
9104          }
9105
9106          yylloc = exp.loc;
9107          if(exp.op.exp1 && !exp.op.exp1.expType)
9108          {
9109             char expString[10000];
9110             expString[0] = '\0';
9111             if(inCompiler)
9112             {
9113                PrintExpression(exp.op.exp1, expString);
9114                ChangeCh(expString, '\n', ' ');
9115             }
9116             if(expString[0])
9117                Compiler_Error($"couldn't determine type of %s\n", expString);
9118          }
9119          if(exp.op.exp2 && !exp.op.exp2.expType)
9120          {
9121             char expString[10240];
9122             expString[0] = '\0';
9123             if(inCompiler)
9124             {
9125                PrintExpression(exp.op.exp2, expString);
9126                ChangeCh(expString, '\n', ' ');
9127             }
9128             if(expString[0])
9129                Compiler_Error($"couldn't determine type of %s\n", expString);
9130          }
9131
9132          if(boolResult)
9133          {
9134             FreeType(exp.expType);
9135             exp.expType = MkClassType("bool");
9136             exp.expType.truth = true;
9137          }
9138
9139          if(exp.op.op != SIZEOF)
9140             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9141                (!exp.op.exp2 || exp.op.exp2.isConstant);
9142
9143          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9144          {
9145             DeclareType(exp.op.exp2.expType, false, false);
9146          }
9147
9148          yylloc = oldyylloc;
9149
9150          FreeType(dummy);
9151          if(type2)
9152             FreeType(type2);
9153          break;
9154       }
9155       case bracketsExp:
9156       case extensionExpressionExp:
9157       {
9158          Expression e;
9159          exp.isConstant = true;
9160          for(e = exp.list->first; e; e = e.next)
9161          {
9162             bool inced = false;
9163             if(!e.next)
9164             {
9165                FreeType(e.destType);
9166                e.opDestType = exp.opDestType;
9167                e.destType = exp.destType;
9168                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
9169             }
9170             ProcessExpressionType(e);
9171             if(inced)
9172                exp.destType.count--;
9173             if(!exp.expType && !e.next)
9174             {
9175                exp.expType = e.expType;
9176                if(e.expType) e.expType.refCount++;
9177             }
9178             if(!e.isConstant)
9179                exp.isConstant = false;
9180          }
9181
9182          // In case a cast became a member...
9183          e = exp.list->first;
9184          if(!e.next && e.type == memberExp)
9185          {
9186             // Preserve prev, next
9187             Expression next = exp.next, prev = exp.prev;
9188
9189
9190             FreeType(exp.expType);
9191             FreeType(exp.destType);
9192             delete exp.list;
9193
9194             *exp = *e;
9195
9196             exp.prev = prev;
9197             exp.next = next;
9198
9199             delete e;
9200
9201             ProcessExpressionType(exp);
9202          }
9203          break;
9204       }
9205       case indexExp:
9206       {
9207          Expression e;
9208          exp.isConstant = true;
9209
9210          ProcessExpressionType(exp.index.exp);
9211          if(!exp.index.exp.isConstant)
9212             exp.isConstant = false;
9213
9214          if(exp.index.exp.expType)
9215          {
9216             Type source = exp.index.exp.expType;
9217             if(source.kind == classType && source._class && source._class.registered)
9218             {
9219                Class _class = source._class.registered;
9220                Class c = _class.templateClass ? _class.templateClass : _class;
9221                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9222                {
9223                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9224
9225                   if(exp.index.index && exp.index.index->last)
9226                   {
9227                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9228
9229                      if(type.kind == classType) type.constant = true;
9230                      else if(type.kind == pointerType)
9231                      {
9232                         Type t = type;
9233                         while(t.kind == pointerType) t = t.type;
9234                         t.constant = true;
9235                      }
9236
9237                      ((Expression)exp.index.index->last).destType = type;
9238                   }
9239                }
9240             }
9241          }
9242
9243          for(e = exp.index.index->first; e; e = e.next)
9244          {
9245             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9246             {
9247                if(e.destType) FreeType(e.destType);
9248                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9249             }
9250             ProcessExpressionType(e);
9251             if(!e.next)
9252             {
9253                // Check if this type is int
9254             }
9255             if(!e.isConstant)
9256                exp.isConstant = false;
9257          }
9258
9259          if(!exp.expType)
9260             exp.expType = Dereference(exp.index.exp.expType);
9261          if(exp.expType)
9262             DeclareType(exp.expType, false, false);
9263          break;
9264       }
9265       case callExp:
9266       {
9267          Expression e;
9268          Type functionType;
9269          Type methodType = null;
9270          char name[1024];
9271          name[0] = '\0';
9272
9273          if(inCompiler)
9274          {
9275             PrintExpression(exp.call.exp,  name);
9276             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9277             {
9278                //exp.call.exp.expType = null;
9279                PrintExpression(exp.call.exp,  name);
9280             }
9281          }
9282          if(exp.call.exp.type == identifierExp)
9283          {
9284             Expression idExp = exp.call.exp;
9285             Identifier id = idExp.identifier;
9286             if(!strcmp(id.string, "__builtin_frame_address"))
9287             {
9288                exp.expType = ProcessTypeString("void *", true);
9289                if(exp.call.arguments && exp.call.arguments->first)
9290                   ProcessExpressionType(exp.call.arguments->first);
9291                break;
9292             }
9293             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9294             {
9295                exp.expType = ProcessTypeString("int", true);
9296                if(exp.call.arguments && exp.call.arguments->first)
9297                   ProcessExpressionType(exp.call.arguments->first);
9298                break;
9299             }
9300             else if(!strcmp(id.string, "Max") ||
9301                !strcmp(id.string, "Min") ||
9302                !strcmp(id.string, "Sgn") ||
9303                !strcmp(id.string, "Abs"))
9304             {
9305                Expression a = null;
9306                Expression b = null;
9307                Expression tempExp1 = null, tempExp2 = null;
9308                if((!strcmp(id.string, "Max") ||
9309                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9310                {
9311                   a = exp.call.arguments->first;
9312                   b = exp.call.arguments->last;
9313                   tempExp1 = a;
9314                   tempExp2 = b;
9315                }
9316                else if(exp.call.arguments->count == 1)
9317                {
9318                   a = exp.call.arguments->first;
9319                   tempExp1 = a;
9320                }
9321
9322                if(a)
9323                {
9324                   exp.call.arguments->Clear();
9325                   idExp.identifier = null;
9326
9327                   FreeExpContents(exp);
9328
9329                   ProcessExpressionType(a);
9330                   if(b)
9331                      ProcessExpressionType(b);
9332
9333                   exp.type = bracketsExp;
9334                   exp.list = MkList();
9335
9336                   if(a.expType && (!b || b.expType))
9337                   {
9338                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9339                      {
9340                         // Use the simpleStruct name/ids for now...
9341                         if(inCompiler)
9342                         {
9343                            OldList * specs = MkList();
9344                            OldList * decls = MkList();
9345                            Declaration decl;
9346                            char temp1[1024], temp2[1024];
9347
9348                            GetTypeSpecs(a.expType, specs);
9349
9350                            if(a && !a.isConstant && a.type != identifierExp)
9351                            {
9352                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9353                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9354                               tempExp1 = QMkExpId(temp1);
9355                               tempExp1.expType = a.expType;
9356                               if(a.expType)
9357                                  a.expType.refCount++;
9358                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9359                            }
9360                            if(b && !b.isConstant && b.type != identifierExp)
9361                            {
9362                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9363                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9364                               tempExp2 = QMkExpId(temp2);
9365                               tempExp2.expType = b.expType;
9366                               if(b.expType)
9367                                  b.expType.refCount++;
9368                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9369                            }
9370
9371                            decl = MkDeclaration(specs, decls);
9372                            if(!curCompound.compound.declarations)
9373                               curCompound.compound.declarations = MkList();
9374                            curCompound.compound.declarations->Insert(null, decl);
9375                         }
9376                      }
9377                   }
9378
9379                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9380                   {
9381                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9382                      ListAdd(exp.list,
9383                         MkExpCondition(MkExpBrackets(MkListOne(
9384                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9385                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9386                      exp.expType = a.expType;
9387                      if(a.expType)
9388                         a.expType.refCount++;
9389                   }
9390                   else if(!strcmp(id.string, "Abs"))
9391                   {
9392                      ListAdd(exp.list,
9393                         MkExpCondition(MkExpBrackets(MkListOne(
9394                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9395                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9396                      exp.expType = a.expType;
9397                      if(a.expType)
9398                         a.expType.refCount++;
9399                   }
9400                   else if(!strcmp(id.string, "Sgn"))
9401                   {
9402                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9403                      ListAdd(exp.list,
9404                         MkExpCondition(MkExpBrackets(MkListOne(
9405                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9406                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9407                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9408                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9409                      exp.expType = ProcessTypeString("int", false);
9410                   }
9411
9412                   FreeExpression(tempExp1);
9413                   if(tempExp2) FreeExpression(tempExp2);
9414
9415                   FreeIdentifier(id);
9416                   break;
9417                }
9418             }
9419          }
9420
9421          {
9422             Type dummy
9423             {
9424                count = 1;
9425                refCount = 1;
9426             };
9427             if(!exp.call.exp.destType)
9428             {
9429                exp.call.exp.destType = dummy;
9430                dummy.refCount++;
9431             }
9432             ProcessExpressionType(exp.call.exp);
9433             if(exp.call.exp.destType == dummy)
9434             {
9435                FreeType(dummy);
9436                exp.call.exp.destType = null;
9437             }
9438             FreeType(dummy);
9439          }
9440
9441          // Check argument types against parameter types
9442          functionType = exp.call.exp.expType;
9443
9444          if(functionType && functionType.kind == TypeKind::methodType)
9445          {
9446             methodType = functionType;
9447             functionType = methodType.method.dataType;
9448
9449             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9450             // TOCHECK: Instead of doing this here could this be done per param?
9451             if(exp.call.exp.expType.usedClass)
9452             {
9453                char typeString[1024];
9454                typeString[0] = '\0';
9455                {
9456                   Symbol back = functionType.thisClass;
9457                   // Do not output class specifier here (thisclass was added to this)
9458                   functionType.thisClass = null;
9459                   PrintType(functionType, typeString, true, true);
9460                   functionType.thisClass = back;
9461                }
9462                if(strstr(typeString, "thisclass"))
9463                {
9464                   OldList * specs = MkList();
9465                   Declarator decl;
9466                   {
9467                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9468
9469                      decl = SpecDeclFromString(typeString, specs, null);
9470
9471                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9472                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9473                         exp.call.exp.expType.usedClass))
9474                         thisClassParams = false;
9475
9476                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9477                      {
9478                         Class backupThisClass = thisClass;
9479                         thisClass = exp.call.exp.expType.usedClass;
9480                         ProcessDeclarator(decl);
9481                         thisClass = backupThisClass;
9482                      }
9483
9484                      thisClassParams = true;
9485
9486                      functionType = ProcessType(specs, decl);
9487                      functionType.refCount = 0;
9488                      FinishTemplatesContext(context);
9489                   }
9490
9491                   FreeList(specs, FreeSpecifier);
9492                   FreeDeclarator(decl);
9493                 }
9494             }
9495          }
9496          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9497          {
9498             Type type = functionType.type;
9499             if(!functionType.refCount)
9500             {
9501                functionType.type = null;
9502                FreeType(functionType);
9503             }
9504             //methodType = functionType;
9505             functionType = type;
9506          }
9507          if(functionType && functionType.kind != TypeKind::functionType)
9508          {
9509             Compiler_Error($"called object %s is not a function\n", name);
9510          }
9511          else if(functionType)
9512          {
9513             bool emptyParams = false, noParams = false;
9514             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9515             Type type = functionType.params.first;
9516             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9517             int extra = 0;
9518             Location oldyylloc = yylloc;
9519
9520             if(!type) emptyParams = true;
9521
9522             // WORKING ON THIS:
9523             if(functionType.extraParam && e && functionType.thisClass)
9524             {
9525                e.destType = MkClassType(functionType.thisClass.string);
9526                e = e.next;
9527             }
9528
9529             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9530             // Fixed #141 by adding '&& !functionType.extraParam'
9531             if(!functionType.staticMethod && !functionType.extraParam)
9532             {
9533                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9534                   memberExp.member.exp.expType._class)
9535                {
9536                   type = MkClassType(memberExp.member.exp.expType._class.string);
9537                   if(e)
9538                   {
9539                      e.destType = type;
9540                      e = e.next;
9541                      type = functionType.params.first;
9542                   }
9543                   else
9544                      type.refCount = 0;
9545                }
9546                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9547                {
9548                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9549                   type.byReference = functionType.byReference;
9550                   type.typedByReference = functionType.typedByReference;
9551                   if(e)
9552                   {
9553                      // Allow manually passing a class for typed object
9554                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9555                         e = e.next;
9556                      e.destType = type;
9557                      e = e.next;
9558                      type = functionType.params.first;
9559                   }
9560                   else
9561                      type.refCount = 0;
9562                   //extra = 1;
9563                }
9564             }
9565
9566             if(type && type.kind == voidType)
9567             {
9568                noParams = true;
9569                if(!type.refCount) FreeType(type);
9570                type = null;
9571             }
9572
9573             for( ; e; e = e.next)
9574             {
9575                if(!type && !emptyParams)
9576                {
9577                   yylloc = e.loc;
9578                   if(methodType && methodType.methodClass)
9579                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9580                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9581                         noParams ? 0 : functionType.params.count);
9582                   else
9583                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9584                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9585                         noParams ? 0 : functionType.params.count);
9586                   break;
9587                }
9588
9589                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9590                {
9591                   Type templatedType = null;
9592                   Class _class = methodType.usedClass;
9593                   ClassTemplateParameter curParam = null;
9594                   int id = 0;
9595                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9596                   {
9597                      Class sClass;
9598                      for(sClass = _class; sClass; sClass = sClass.base)
9599                      {
9600                         if(sClass.templateClass) sClass = sClass.templateClass;
9601                         id = 0;
9602                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9603                         {
9604                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9605                            {
9606                               Class nextClass;
9607                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9608                               {
9609                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9610                                  id += nextClass.templateParams.count;
9611                               }
9612                               break;
9613                            }
9614                            id++;
9615                         }
9616                         if(curParam) break;
9617                      }
9618                   }
9619                   if(curParam && _class.templateArgs[id].dataTypeString)
9620                   {
9621                      bool constant = type.constant;
9622                      ClassTemplateArgument arg = _class.templateArgs[id];
9623                      {
9624                         Context context = SetupTemplatesContext(_class);
9625
9626                         /*if(!arg.dataType)
9627                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9628                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9629                         FinishTemplatesContext(context);
9630                      }
9631
9632                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9633                      else if(templatedType.kind == pointerType)
9634                      {
9635                         Type t = templatedType.type;
9636                         while(t.kind == pointerType) t = t.type;
9637                         if(constant) t.constant = constant;
9638                      }
9639
9640                      e.destType = templatedType;
9641                      if(templatedType)
9642                      {
9643                         templatedType.passAsTemplate = true;
9644                         // templatedType.refCount++;
9645                      }
9646                   }
9647                   else
9648                   {
9649                      e.destType = type;
9650                      if(type) type.refCount++;
9651                   }
9652                }
9653                else
9654                {
9655                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9656                   {
9657                      e.destType = type.prev;
9658                      e.destType.refCount++;
9659                   }
9660                   else
9661                   {
9662                      e.destType = type;
9663                      if(type) type.refCount++;
9664                   }
9665                }
9666                // Don't reach the end for the ellipsis
9667                if(type && type.kind != ellipsisType)
9668                {
9669                   Type next = type.next;
9670                   if(!type.refCount) FreeType(type);
9671                   type = next;
9672                }
9673             }
9674
9675             if(type && type.kind != ellipsisType)
9676             {
9677                if(methodType && methodType.methodClass)
9678                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9679                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9680                      functionType.params.count + extra);
9681                else
9682                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9683                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9684                      functionType.params.count + extra);
9685             }
9686             yylloc = oldyylloc;
9687             if(type && !type.refCount) FreeType(type);
9688          }
9689          else
9690          {
9691             functionType = Type
9692             {
9693                refCount = 0;
9694                kind = TypeKind::functionType;
9695             };
9696
9697             if(exp.call.exp.type == identifierExp)
9698             {
9699                char * string = exp.call.exp.identifier.string;
9700                if(inCompiler)
9701                {
9702                   Symbol symbol;
9703                   Location oldyylloc = yylloc;
9704
9705                   yylloc = exp.call.exp.identifier.loc;
9706                   if(strstr(string, "__builtin_") == string)
9707                   {
9708                      if(exp.destType)
9709                      {
9710                         functionType.returnType = exp.destType;
9711                         exp.destType.refCount++;
9712                      }
9713                   }
9714                   else
9715                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9716                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9717                   globalContext.symbols.Add((BTNode)symbol);
9718                   if(strstr(symbol.string, "::"))
9719                      globalContext.hasNameSpace = true;
9720
9721                   yylloc = oldyylloc;
9722                }
9723             }
9724             else if(exp.call.exp.type == memberExp)
9725             {
9726                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9727                   exp.call.exp.member.member.string);*/
9728             }
9729             else
9730                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9731
9732             if(!functionType.returnType)
9733             {
9734                functionType.returnType = Type
9735                {
9736                   refCount = 1;
9737                   kind = intType;
9738                };
9739             }
9740          }
9741          if(functionType && functionType.kind == TypeKind::functionType)
9742          {
9743             exp.expType = functionType.returnType;
9744
9745             if(functionType.returnType)
9746                functionType.returnType.refCount++;
9747
9748             if(!functionType.refCount)
9749                FreeType(functionType);
9750          }
9751
9752          if(exp.call.arguments)
9753          {
9754             for(e = exp.call.arguments->first; e; e = e.next)
9755             {
9756                Type destType = e.destType;
9757                ProcessExpressionType(e);
9758             }
9759          }
9760          break;
9761       }
9762       case memberExp:
9763       {
9764          Type type;
9765          Location oldyylloc = yylloc;
9766          bool thisPtr;
9767          Expression checkExp = exp.member.exp;
9768          while(checkExp)
9769          {
9770             if(checkExp.type == castExp)
9771                checkExp = checkExp.cast.exp;
9772             else if(checkExp.type == bracketsExp)
9773                checkExp = checkExp.list ? checkExp.list->first : null;
9774             else
9775                break;
9776          }
9777
9778          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9779          exp.thisPtr = thisPtr;
9780
9781          // DOING THIS LATER NOW...
9782          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9783          {
9784             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9785             /* TODO: Name Space Fix ups
9786             if(!exp.member.member.classSym)
9787                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9788             */
9789          }
9790
9791          ProcessExpressionType(exp.member.exp);
9792          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9793             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9794          {
9795             exp.isConstant = false;
9796          }
9797          else
9798             exp.isConstant = exp.member.exp.isConstant;
9799          type = exp.member.exp.expType;
9800
9801          yylloc = exp.loc;
9802
9803          if(type && (type.kind == templateType))
9804          {
9805             Class _class = thisClass ? thisClass : currentClass;
9806             ClassTemplateParameter param = null;
9807             if(_class)
9808             {
9809                for(param = _class.templateParams.first; param; param = param.next)
9810                {
9811                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9812                      break;
9813                }
9814             }
9815             if(param && param.defaultArg.member)
9816             {
9817                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9818                if(argExp)
9819                {
9820                   Expression expMember = exp.member.exp;
9821                   Declarator decl;
9822                   OldList * specs = MkList();
9823                   char thisClassTypeString[1024];
9824
9825                   FreeIdentifier(exp.member.member);
9826
9827                   ProcessExpressionType(argExp);
9828
9829                   {
9830                      char * colon = strstr(param.defaultArg.memberString, "::");
9831                      if(colon)
9832                      {
9833                         char className[1024];
9834                         Class sClass;
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 c;
9851                      int paramCount = 0;
9852                      int lastParam = -1;
9853
9854                      char templateString[1024];
9855                      ClassTemplateParameter param;
9856                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9857                      for(cClass = expClass; cClass; cClass = cClass.base)
9858                      {
9859                         int p = 0;
9860                         for(param = cClass.templateParams.first; param; param = param.next)
9861                         {
9862                            int id = p;
9863                            Class sClass;
9864                            ClassTemplateArgument arg;
9865                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9866                            arg = expClass.templateArgs[id];
9867
9868                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9869                            {
9870                               ClassTemplateParameter cParam;
9871                               //int p = numParams - sClass.templateParams.count;
9872                               int p = 0;
9873                               Class nextClass;
9874                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9875
9876                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9877                               {
9878                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9879                                  {
9880                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9881                                     {
9882                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9883                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9884                                        break;
9885                                     }
9886                                  }
9887                               }
9888                            }
9889
9890                            {
9891                               char argument[256];
9892                               argument[0] = '\0';
9893                               /*if(arg.name)
9894                               {
9895                                  strcat(argument, arg.name.string);
9896                                  strcat(argument, " = ");
9897                               }*/
9898                               switch(param.type)
9899                               {
9900                                  case expression:
9901                                  {
9902                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9903                                     char expString[1024];
9904                                     OldList * specs = MkList();
9905                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9906                                     Expression exp;
9907                                     char * string = PrintHexUInt64(arg.expression.ui64);
9908                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9909                                     delete string;
9910
9911                                     ProcessExpressionType(exp);
9912                                     ComputeExpression(exp);
9913                                     expString[0] = '\0';
9914                                     PrintExpression(exp, expString);
9915                                     strcat(argument, expString);
9916                                     // delete exp;
9917                                     FreeExpression(exp);
9918                                     break;
9919                                  }
9920                                  case identifier:
9921                                  {
9922                                     strcat(argument, arg.member.name);
9923                                     break;
9924                                  }
9925                                  case TemplateParameterType::type:
9926                                  {
9927                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9928                                     {
9929                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9930                                           strcat(argument, thisClassTypeString);
9931                                        else
9932                                           strcat(argument, arg.dataTypeString);
9933                                     }
9934                                     break;
9935                                  }
9936                               }
9937                               if(argument[0])
9938                               {
9939                                  if(paramCount) strcat(templateString, ", ");
9940                                  if(lastParam != p - 1)
9941                                  {
9942                                     strcat(templateString, param.name);
9943                                     strcat(templateString, " = ");
9944                                  }
9945                                  strcat(templateString, argument);
9946                                  paramCount++;
9947                                  lastParam = p;
9948                               }
9949                               p++;
9950                            }
9951                         }
9952                      }
9953                      {
9954                         int len = strlen(templateString);
9955                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9956                         templateString[len++] = '>';
9957                         templateString[len++] = '\0';
9958                      }
9959                      {
9960                         Context context = SetupTemplatesContext(_class);
9961                         FreeType(exp.expType);
9962                         exp.expType = ProcessTypeString(templateString, false);
9963                         FinishTemplatesContext(context);
9964                      }
9965                   }
9966
9967                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9968                   exp.type = bracketsExp;
9969                   exp.list = MkListOne(MkExpOp(null, '*',
9970                   /*opExp;
9971                   exp.op.op = '*';
9972                   exp.op.exp1 = null;
9973                   exp.op.exp2 = */
9974                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9975                      MkExpBrackets(MkListOne(
9976                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9977                            '+',
9978                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9979                            '+',
9980                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9981
9982                            ));
9983                }
9984             }
9985             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9986                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9987             {
9988                type = ProcessTemplateParameterType(type.templateParameter);
9989             }
9990          }
9991          // TODO: *** This seems to be where we should add method support for all basic types ***
9992          if(type && (type.kind == templateType));
9993          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9994                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9995                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9996                           (type.kind == pointerType && type.type.kind == charType)))
9997          {
9998             Identifier id = exp.member.member;
9999             TypeKind typeKind = type.kind;
10000             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10001             if(typeKind == subClassType && exp.member.exp.type == classExp)
10002             {
10003                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
10004                typeKind = classType;
10005             }
10006
10007             if(id)
10008             {
10009                if(typeKind == intType || typeKind == enumType)
10010                   _class = eSystem_FindClass(privateModule, "int");
10011                else if(!_class)
10012                {
10013                   if(type.kind == classType && type._class && type._class.registered)
10014                   {
10015                      _class = type._class.registered;
10016                   }
10017                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10018                   {
10019                      _class = FindClass("char *").registered;
10020                   }
10021                   else if(type.kind == pointerType)
10022                   {
10023                      _class = eSystem_FindClass(privateModule, "uintptr");
10024                      FreeType(exp.expType);
10025                      exp.expType = ProcessTypeString("uintptr", false);
10026                      exp.byReference = true;
10027                   }
10028                   else
10029                   {
10030                      char string[1024] = "";
10031                      Symbol classSym;
10032                      PrintTypeNoConst(type, string, false, true);
10033                      classSym = FindClass(string);
10034                      if(classSym) _class = classSym.registered;
10035                   }
10036                }
10037             }
10038
10039             if(_class && id)
10040             {
10041                /*bool thisPtr =
10042                   (exp.member.exp.type == identifierExp &&
10043                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10044                Property prop = null;
10045                Method method = null;
10046                DataMember member = null;
10047                Property revConvert = null;
10048                ClassProperty classProp = null;
10049
10050                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10051                   exp.member.memberType = propertyMember;
10052
10053                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10054                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10055
10056                if(typeKind != subClassType)
10057                {
10058                   // Prioritize data members over properties for "this"
10059                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10060                   {
10061                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10062                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10063                      {
10064                         prop = eClass_FindProperty(_class, id.string, privateModule);
10065                         if(prop)
10066                            member = null;
10067                      }
10068                      if(!member && !prop)
10069                         prop = eClass_FindProperty(_class, id.string, privateModule);
10070                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10071                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10072                         exp.member.thisPtr = true;
10073                   }
10074                   // Prioritize properties over data members otherwise
10075                   else
10076                   {
10077                      bool useMemberForNonConst = false;
10078                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10079                      if(!id.classSym)
10080                      {
10081                         prop = eClass_FindProperty(_class, id.string, null);
10082
10083                         useMemberForNonConst = prop && exp.destType &&
10084                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10085                               !strncmp(prop.dataTypeString, "const ", 6);
10086
10087                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10088                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10089                      }
10090
10091                      if((!prop || useMemberForNonConst) && !member)
10092                      {
10093                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10094                         if(!method)
10095                         {
10096                            prop = eClass_FindProperty(_class, id.string, privateModule);
10097
10098                            useMemberForNonConst |= prop && exp.destType &&
10099                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10100                                  !strncmp(prop.dataTypeString, "const ", 6);
10101
10102                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10103                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10104                         }
10105                      }
10106
10107                      if(member && prop)
10108                      {
10109                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10110                            prop = null;
10111                         else
10112                            member = null;
10113                      }
10114                   }
10115                }
10116                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10117                   method = eClass_FindMethod(_class, id.string, privateModule);
10118                if(!prop && !member && !method)
10119                {
10120                   if(typeKind == subClassType)
10121                   {
10122                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10123                      if(classProp)
10124                      {
10125                         exp.member.memberType = classPropertyMember;
10126                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10127                      }
10128                      else
10129                      {
10130                         // Assume this is a class_data member
10131                         char structName[1024];
10132                         Identifier id = exp.member.member;
10133                         Expression classExp = exp.member.exp;
10134                         type.refCount++;
10135
10136                         FreeType(classExp.expType);
10137                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10138
10139                         strcpy(structName, "__ecereClassData_");
10140                         FullClassNameCat(structName, type._class.string, false);
10141                         exp.type = pointerExp;
10142                         exp.member.member = id;
10143
10144                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10145                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10146                               MkExpBrackets(MkListOne(MkExpOp(
10147                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10148                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10149                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10150                                  )));
10151
10152                         FreeType(type);
10153
10154                         ProcessExpressionType(exp);
10155                         return;
10156                      }
10157                   }
10158                   else
10159                   {
10160                      // Check for reverse conversion
10161                      // (Convert in an instantiation later, so that we can use
10162                      //  deep properties system)
10163                      Symbol classSym = FindClass(id.string);
10164                      if(classSym)
10165                      {
10166                         Class convertClass = classSym.registered;
10167                         if(convertClass)
10168                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10169                      }
10170                   }
10171                }
10172
10173                if(prop)
10174                {
10175                   exp.member.memberType = propertyMember;
10176                   if(!prop.dataType)
10177                      ProcessPropertyType(prop);
10178                   exp.expType = prop.dataType;
10179                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10180                   {
10181                      Type type { };
10182                      CopyTypeInto(type, exp.expType);
10183                      type.refCount = 1;
10184                      type.constant = true;
10185                      exp.expType = type;
10186                   }
10187                   else if(prop.dataType)
10188                      prop.dataType.refCount++;
10189                }
10190                else if(member)
10191                {
10192                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10193                   {
10194                      FreeExpContents(exp);
10195                      exp.type = identifierExp;
10196                      exp.identifier = MkIdentifier("class");
10197                      ProcessExpressionType(exp);
10198                      return;
10199                   }
10200
10201                   exp.member.memberType = dataMember;
10202                   DeclareStruct(_class.fullName, false);
10203                   if(!member.dataType)
10204                   {
10205                      Context context = SetupTemplatesContext(_class);
10206                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10207                      FinishTemplatesContext(context);
10208                   }
10209                   exp.expType = member.dataType;
10210                   if(member.dataType) member.dataType.refCount++;
10211                }
10212                else if(revConvert)
10213                {
10214                   exp.member.memberType = reverseConversionMember;
10215                   exp.expType = MkClassType(revConvert._class.fullName);
10216                }
10217                else if(method)
10218                {
10219                   //if(inCompiler)
10220                   {
10221                      /*if(id._class)
10222                      {
10223                         exp.type = identifierExp;
10224                         exp.identifier = exp.member.member;
10225                      }
10226                      else*/
10227                         exp.member.memberType = methodMember;
10228                   }
10229                   if(!method.dataType)
10230                      ProcessMethodType(method);
10231                   exp.expType = Type
10232                   {
10233                      refCount = 1;
10234                      kind = methodType;
10235                      method = method;
10236                   };
10237
10238                   // Tricky spot here... To use instance versus class virtual table
10239                   // Put it back to what it was... What did we break?
10240
10241                   // Had to put it back for overriding Main of Thread global instance
10242
10243                   //exp.expType.methodClass = _class;
10244                   exp.expType.methodClass = (id && id._class) ? _class : null;
10245
10246                   // Need the actual class used for templated classes
10247                   exp.expType.usedClass = _class;
10248                }
10249                else if(!classProp)
10250                {
10251                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10252                   {
10253                      FreeExpContents(exp);
10254                      exp.type = identifierExp;
10255                      exp.identifier = MkIdentifier("class");
10256                      FreeType(exp.expType);
10257                      exp.expType = MkClassType("ecere::com::Class");
10258                      return;
10259                   }
10260                   yylloc = exp.member.member.loc;
10261                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10262                   if(inCompiler)
10263                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10264                }
10265
10266                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10267                {
10268                   Class tClass;
10269
10270                   tClass = _class;
10271                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10272
10273                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10274                   {
10275                      int id = 0;
10276                      ClassTemplateParameter curParam = null;
10277                      Class sClass;
10278
10279                      for(sClass = tClass; sClass; sClass = sClass.base)
10280                      {
10281                         id = 0;
10282                         if(sClass.templateClass) sClass = sClass.templateClass;
10283                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10284                         {
10285                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10286                            {
10287                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10288                                  id += sClass.templateParams.count;
10289                               break;
10290                            }
10291                            id++;
10292                         }
10293                         if(curParam) break;
10294                      }
10295
10296                      if(curParam && tClass.templateArgs[id].dataTypeString)
10297                      {
10298                         ClassTemplateArgument arg = tClass.templateArgs[id];
10299                         Context context = SetupTemplatesContext(tClass);
10300                         bool constant = exp.expType.constant;
10301                         /*if(!arg.dataType)
10302                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10303                         FreeType(exp.expType);
10304
10305                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10306                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10307                         else if(exp.expType.kind == pointerType)
10308                         {
10309                            Type t = exp.expType.type;
10310                            while(t.kind == pointerType) t = t.type;
10311                            if(constant) t.constant = constant;
10312                         }
10313                         if(exp.expType)
10314                         {
10315                            if(exp.expType.kind == thisClassType)
10316                            {
10317                               FreeType(exp.expType);
10318                               exp.expType = ReplaceThisClassType(_class);
10319                            }
10320
10321                            if(tClass.templateClass)
10322                               exp.expType.passAsTemplate = true;
10323                            //exp.expType.refCount++;
10324                            if(!exp.destType)
10325                            {
10326                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10327                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10328                               else if(exp.destType.kind == pointerType)
10329                               {
10330                                  Type t = exp.destType.type;
10331                                  while(t.kind == pointerType) t = t.type;
10332                                  if(constant) t.constant = constant;
10333                               }
10334
10335                               //exp.destType.refCount++;
10336
10337                               if(exp.destType.kind == thisClassType)
10338                               {
10339                                  FreeType(exp.destType);
10340                                  exp.destType = ReplaceThisClassType(_class);
10341                               }
10342                            }
10343                         }
10344                         FinishTemplatesContext(context);
10345                      }
10346                   }
10347                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10348                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10349                   {
10350                      int id = 0;
10351                      ClassTemplateParameter curParam = null;
10352                      Class sClass;
10353
10354                      for(sClass = tClass; sClass; sClass = sClass.base)
10355                      {
10356                         id = 0;
10357                         if(sClass.templateClass) sClass = sClass.templateClass;
10358                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10359                         {
10360                            if(curParam.type == TemplateParameterType::type &&
10361                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10362                            {
10363                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10364                                  id += sClass.templateParams.count;
10365                               break;
10366                            }
10367                            id++;
10368                         }
10369                         if(curParam) break;
10370                      }
10371
10372                      if(curParam)
10373                      {
10374                         ClassTemplateArgument arg = tClass.templateArgs[id];
10375                         Context context = SetupTemplatesContext(tClass);
10376                         Type basicType;
10377                         /*if(!arg.dataType)
10378                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10379
10380                         basicType = ProcessTypeString(arg.dataTypeString, false);
10381                         if(basicType)
10382                         {
10383                            if(basicType.kind == thisClassType)
10384                            {
10385                               FreeType(basicType);
10386                               basicType = ReplaceThisClassType(_class);
10387                            }
10388
10389                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10390                            if(tClass.templateClass)
10391                               basicType.passAsTemplate = true;
10392                            */
10393
10394                            FreeType(exp.expType);
10395
10396                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10397                            //exp.expType.refCount++;
10398                            if(!exp.destType)
10399                            {
10400                               exp.destType = exp.expType;
10401                               exp.destType.refCount++;
10402                            }
10403
10404                            {
10405                               Expression newExp { };
10406                               OldList * specs = MkList();
10407                               Declarator decl;
10408                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10409                               *newExp = *exp;
10410                               if(exp.destType) exp.destType.refCount++;
10411                               if(exp.expType)  exp.expType.refCount++;
10412                               exp.type = castExp;
10413                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10414                               exp.cast.exp = newExp;
10415                               //FreeType(exp.expType);
10416                               //exp.expType = null;
10417                               //ProcessExpressionType(sourceExp);
10418                            }
10419                         }
10420                         FinishTemplatesContext(context);
10421                      }
10422                   }
10423                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10424                   {
10425                      Class expClass = exp.expType._class.registered;
10426                      if(expClass)
10427                      {
10428                         Class cClass = null;
10429                         int c;
10430                         int p = 0;
10431                         int paramCount = 0;
10432                         int lastParam = -1;
10433                         char templateString[1024];
10434                         ClassTemplateParameter param;
10435                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10436                         while(cClass != expClass)
10437                         {
10438                            Class sClass;
10439                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10440                            cClass = sClass;
10441
10442                            for(param = cClass.templateParams.first; param; param = param.next)
10443                            {
10444                               Class cClassCur = null;
10445                               int c;
10446                               int cp = 0;
10447                               ClassTemplateParameter paramCur = null;
10448                               ClassTemplateArgument arg;
10449                               while(cClassCur != tClass && !paramCur)
10450                               {
10451                                  Class sClassCur;
10452                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10453                                  cClassCur = sClassCur;
10454
10455                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10456                                  {
10457                                     if(!strcmp(paramCur.name, param.name))
10458                                     {
10459
10460                                        break;
10461                                     }
10462                                     cp++;
10463                                  }
10464                               }
10465                               if(paramCur && paramCur.type == TemplateParameterType::type)
10466                                  arg = tClass.templateArgs[cp];
10467                               else
10468                                  arg = expClass.templateArgs[p];
10469
10470                               {
10471                                  char argument[256];
10472                                  argument[0] = '\0';
10473                                  /*if(arg.name)
10474                                  {
10475                                     strcat(argument, arg.name.string);
10476                                     strcat(argument, " = ");
10477                                  }*/
10478                                  switch(param.type)
10479                                  {
10480                                     case expression:
10481                                     {
10482                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10483                                        char expString[1024];
10484                                        OldList * specs = MkList();
10485                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10486                                        Expression exp;
10487                                        char * string = PrintHexUInt64(arg.expression.ui64);
10488                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10489                                        delete string;
10490
10491                                        ProcessExpressionType(exp);
10492                                        ComputeExpression(exp);
10493                                        expString[0] = '\0';
10494                                        PrintExpression(exp, expString);
10495                                        strcat(argument, expString);
10496                                        // delete exp;
10497                                        FreeExpression(exp);
10498                                        break;
10499                                     }
10500                                     case identifier:
10501                                     {
10502                                        strcat(argument, arg.member.name);
10503                                        break;
10504                                     }
10505                                     case TemplateParameterType::type:
10506                                     {
10507                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10508                                           strcat(argument, arg.dataTypeString);
10509                                        break;
10510                                     }
10511                                  }
10512                                  if(argument[0])
10513                                  {
10514                                     if(paramCount) strcat(templateString, ", ");
10515                                     if(lastParam != p - 1)
10516                                     {
10517                                        strcat(templateString, param.name);
10518                                        strcat(templateString, " = ");
10519                                     }
10520                                     strcat(templateString, argument);
10521                                     paramCount++;
10522                                     lastParam = p;
10523                                  }
10524                               }
10525                               p++;
10526                            }
10527                         }
10528                         {
10529                            int len = strlen(templateString);
10530                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10531                            templateString[len++] = '>';
10532                            templateString[len++] = '\0';
10533                         }
10534
10535                         FreeType(exp.expType);
10536                         {
10537                            Context context = SetupTemplatesContext(tClass);
10538                            exp.expType = ProcessTypeString(templateString, false);
10539                            FinishTemplatesContext(context);
10540                         }
10541                      }
10542                   }
10543                }
10544             }
10545             else
10546                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10547          }
10548          else if(type && (type.kind == structType || type.kind == unionType))
10549          {
10550             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10551             if(memberType)
10552             {
10553                exp.expType = memberType;
10554                if(memberType)
10555                   memberType.refCount++;
10556             }
10557          }
10558          else
10559          {
10560             char expString[10240];
10561             expString[0] = '\0';
10562             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10563             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10564          }
10565
10566          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10567          {
10568             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10569             {
10570                Identifier id = exp.member.member;
10571                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10572                if(_class)
10573                {
10574                   FreeType(exp.expType);
10575                   exp.expType = ReplaceThisClassType(_class);
10576                }
10577             }
10578          }
10579          yylloc = oldyylloc;
10580          break;
10581       }
10582       // Convert x->y into (*x).y
10583       case pointerExp:
10584       {
10585          Type destType = exp.destType;
10586
10587          // DOING THIS LATER NOW...
10588          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10589          {
10590             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10591             /* TODO: Name Space Fix ups
10592             if(!exp.member.member.classSym)
10593                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10594             */
10595          }
10596
10597          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10598          exp.type = memberExp;
10599          if(destType)
10600             destType.count++;
10601          ProcessExpressionType(exp);
10602          if(destType)
10603             destType.count--;
10604          break;
10605       }
10606       case classSizeExp:
10607       {
10608          //ComputeExpression(exp);
10609
10610          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10611          if(classSym && classSym.registered)
10612          {
10613             if(classSym.registered.type == noHeadClass)
10614             {
10615                char name[1024];
10616                name[0] = '\0';
10617                DeclareStruct(classSym.string, false);
10618                FreeSpecifier(exp._class);
10619                exp.type = typeSizeExp;
10620                FullClassNameCat(name, classSym.string, false);
10621                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10622             }
10623             else
10624             {
10625                if(classSym.registered.fixed)
10626                {
10627                   FreeSpecifier(exp._class);
10628                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10629                   exp.type = constantExp;
10630                }
10631                else
10632                {
10633                   char className[1024];
10634                   strcpy(className, "__ecereClass_");
10635                   FullClassNameCat(className, classSym.string, true);
10636                   MangleClassName(className);
10637
10638                   DeclareClass(classSym, className);
10639
10640                   FreeExpContents(exp);
10641                   exp.type = pointerExp;
10642                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10643                   exp.member.member = MkIdentifier("structSize");
10644                }
10645             }
10646          }
10647
10648          exp.expType = Type
10649          {
10650             refCount = 1;
10651             kind = intSizeType;
10652          };
10653          // exp.isConstant = true;
10654          break;
10655       }
10656       case typeSizeExp:
10657       {
10658          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10659
10660          exp.expType = Type
10661          {
10662             refCount = 1;
10663             kind = intSizeType;
10664          };
10665          exp.isConstant = true;
10666
10667          DeclareType(type, false, false);
10668          FreeType(type);
10669          break;
10670       }
10671       case castExp:
10672       {
10673          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10674          type.count = 1;
10675          FreeType(exp.cast.exp.destType);
10676          exp.cast.exp.destType = type;
10677          type.refCount++;
10678          type.casted = true;
10679          ProcessExpressionType(exp.cast.exp);
10680          type.casted = false;
10681          type.count = 0;
10682          exp.expType = type;
10683          //type.refCount++;
10684
10685          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10686          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10687          {
10688             void * prev = exp.prev, * next = exp.next;
10689             Type expType = exp.cast.exp.destType;
10690             Expression castExp = exp.cast.exp;
10691             Type destType = exp.destType;
10692
10693             if(expType) expType.refCount++;
10694
10695             //FreeType(exp.destType);
10696             FreeType(exp.expType);
10697             FreeTypeName(exp.cast.typeName);
10698
10699             *exp = *castExp;
10700             FreeType(exp.expType);
10701             FreeType(exp.destType);
10702
10703             exp.expType = expType;
10704             exp.destType = destType;
10705
10706             delete castExp;
10707
10708             exp.prev = prev;
10709             exp.next = next;
10710
10711          }
10712          else
10713          {
10714             exp.isConstant = exp.cast.exp.isConstant;
10715          }
10716          //FreeType(type);
10717          break;
10718       }
10719       case extensionInitializerExp:
10720       {
10721          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10722          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10723          // ProcessInitializer(exp.initializer.initializer, type);
10724          exp.expType = type;
10725          break;
10726       }
10727       case vaArgExp:
10728       {
10729          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10730          ProcessExpressionType(exp.vaArg.exp);
10731          exp.expType = type;
10732          break;
10733       }
10734       case conditionExp:
10735       {
10736          Expression e;
10737          exp.isConstant = true;
10738
10739          FreeType(exp.cond.cond.destType);
10740          exp.cond.cond.destType = MkClassType("bool");
10741          exp.cond.cond.destType.truth = true;
10742          ProcessExpressionType(exp.cond.cond);
10743          if(!exp.cond.cond.isConstant)
10744             exp.isConstant = false;
10745          for(e = exp.cond.exp->first; e; e = e.next)
10746          {
10747             if(!e.next)
10748             {
10749                FreeType(e.destType);
10750                e.destType = exp.destType;
10751                if(e.destType) e.destType.refCount++;
10752             }
10753             ProcessExpressionType(e);
10754             if(!e.next)
10755             {
10756                exp.expType = e.expType;
10757                if(e.expType) e.expType.refCount++;
10758             }
10759             if(!e.isConstant)
10760                exp.isConstant = false;
10761          }
10762
10763          FreeType(exp.cond.elseExp.destType);
10764          // Added this check if we failed to find an expType
10765          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10766
10767          // Reversed it...
10768          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10769
10770          if(exp.cond.elseExp.destType)
10771             exp.cond.elseExp.destType.refCount++;
10772          ProcessExpressionType(exp.cond.elseExp);
10773
10774          // FIXED THIS: Was done before calling process on elseExp
10775          if(!exp.cond.elseExp.isConstant)
10776             exp.isConstant = false;
10777          break;
10778       }
10779       case extensionCompoundExp:
10780       {
10781          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10782          {
10783             Statement last = exp.compound.compound.statements->last;
10784             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10785             {
10786                ((Expression)last.expressions->last).destType = exp.destType;
10787                if(exp.destType)
10788                   exp.destType.refCount++;
10789             }
10790             ProcessStatement(exp.compound);
10791             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10792             if(exp.expType)
10793                exp.expType.refCount++;
10794          }
10795          break;
10796       }
10797       case classExp:
10798       {
10799          Specifier spec = exp._classExp.specifiers->first;
10800          if(spec && spec.type == nameSpecifier)
10801          {
10802             exp.expType = MkClassType(spec.name);
10803             exp.expType.kind = subClassType;
10804             exp.byReference = true;
10805          }
10806          else
10807          {
10808             exp.expType = MkClassType("ecere::com::Class");
10809             exp.byReference = true;
10810          }
10811          break;
10812       }
10813       case classDataExp:
10814       {
10815          Class _class = thisClass ? thisClass : currentClass;
10816          if(_class)
10817          {
10818             Identifier id = exp.classData.id;
10819             char structName[1024];
10820             Expression classExp;
10821             strcpy(structName, "__ecereClassData_");
10822             FullClassNameCat(structName, _class.fullName, false);
10823             exp.type = pointerExp;
10824             exp.member.member = id;
10825             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10826                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10827             else
10828                classExp = MkExpIdentifier(MkIdentifier("class"));
10829
10830             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10831                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10832                   MkExpBrackets(MkListOne(MkExpOp(
10833                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10834                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10835                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10836                      )));
10837
10838             ProcessExpressionType(exp);
10839             return;
10840          }
10841          break;
10842       }
10843       case arrayExp:
10844       {
10845          Type type = null;
10846          const char * typeString = null;
10847          char typeStringBuf[1024];
10848          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10849             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10850          {
10851             Class templateClass = exp.destType._class.registered;
10852             typeString = templateClass.templateArgs[2].dataTypeString;
10853          }
10854          else if(exp.list)
10855          {
10856             // Guess type from expressions in the array
10857             Expression e;
10858             for(e = exp.list->first; e; e = e.next)
10859             {
10860                ProcessExpressionType(e);
10861                if(e.expType)
10862                {
10863                   if(!type) { type = e.expType; type.refCount++; }
10864                   else
10865                   {
10866                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10867                      if(!MatchTypeExpression(e, type, null, false, true))
10868                      {
10869                         FreeType(type);
10870                         type = e.expType;
10871                         e.expType = null;
10872
10873                         e = exp.list->first;
10874                         ProcessExpressionType(e);
10875                         if(e.expType)
10876                         {
10877                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10878                            if(!MatchTypeExpression(e, type, null, false, true))
10879                            {
10880                               FreeType(e.expType);
10881                               e.expType = null;
10882                               FreeType(type);
10883                               type = null;
10884                               break;
10885                            }
10886                         }
10887                      }
10888                   }
10889                   if(e.expType)
10890                   {
10891                      FreeType(e.expType);
10892                      e.expType = null;
10893                   }
10894                }
10895             }
10896             if(type)
10897             {
10898                typeStringBuf[0] = '\0';
10899                PrintTypeNoConst(type, typeStringBuf, false, true);
10900                typeString = typeStringBuf;
10901                FreeType(type);
10902                type = null;
10903             }
10904          }
10905          if(typeString)
10906          {
10907             /*
10908             (Container)& (struct BuiltInContainer)
10909             {
10910                ._vTbl = class(BuiltInContainer)._vTbl,
10911                ._class = class(BuiltInContainer),
10912                .refCount = 0,
10913                .data = (int[]){ 1, 7, 3, 4, 5 },
10914                .count = 5,
10915                .type = class(int),
10916             }
10917             */
10918             char templateString[1024];
10919             OldList * initializers = MkList();
10920             OldList * structInitializers = MkList();
10921             OldList * specs = MkList();
10922             Expression expExt;
10923             Declarator decl = SpecDeclFromString(typeString, specs, null);
10924             sprintf(templateString, "Container<%s>", typeString);
10925
10926             if(exp.list)
10927             {
10928                Expression e;
10929                type = ProcessTypeString(typeString, false);
10930                while(e = exp.list->first)
10931                {
10932                   exp.list->Remove(e);
10933                   e.destType = type;
10934                   type.refCount++;
10935                   ProcessExpressionType(e);
10936                   ListAdd(initializers, MkInitializerAssignment(e));
10937                }
10938                FreeType(type);
10939                delete exp.list;
10940             }
10941
10942             DeclareStruct("ecere::com::BuiltInContainer", false);
10943
10944             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10945                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10946             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10947                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10948             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10949                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10950             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10951                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10952                MkInitializerList(initializers))));
10953                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10954             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10955                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10956             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10957                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10958             exp.expType = ProcessTypeString(templateString, false);
10959             exp.type = bracketsExp;
10960             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10961                MkExpOp(null, '&',
10962                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10963                   MkInitializerList(structInitializers)))));
10964             ProcessExpressionType(expExt);
10965          }
10966          else
10967          {
10968             exp.expType = ProcessTypeString("Container", false);
10969             Compiler_Error($"Couldn't determine type of array elements\n");
10970          }
10971          break;
10972       }
10973    }
10974
10975    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10976    {
10977       FreeType(exp.expType);
10978       exp.expType = ReplaceThisClassType(thisClass);
10979    }
10980
10981    // Resolve structures here
10982    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10983    {
10984       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10985       // TODO: Fix members reference...
10986       if(symbol)
10987       {
10988          if(exp.expType.kind != enumType)
10989          {
10990             Type member;
10991             String enumName = CopyString(exp.expType.enumName);
10992
10993             // Fixed a memory leak on self-referencing C structs typedefs
10994             // by instantiating a new type rather than simply copying members
10995             // into exp.expType
10996             FreeType(exp.expType);
10997             exp.expType = Type { };
10998             exp.expType.kind = symbol.type.kind;
10999             exp.expType.refCount++;
11000             exp.expType.enumName = enumName;
11001
11002             exp.expType.members = symbol.type.members;
11003             for(member = symbol.type.members.first; member; member = member.next)
11004                member.refCount++;
11005          }
11006          else
11007          {
11008             NamedLink member;
11009             for(member = symbol.type.members.first; member; member = member.next)
11010             {
11011                NamedLink value { name = CopyString(member.name) };
11012                exp.expType.members.Add(value);
11013             }
11014          }
11015       }
11016    }
11017
11018    yylloc = exp.loc;
11019    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
11020    else if(exp.destType && !exp.destType.keepCast)
11021    {
11022       if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11023       {
11024          if(!exp.destType.count || unresolved)
11025          {
11026             if(!exp.expType)
11027             {
11028                yylloc = exp.loc;
11029                if(exp.destType.kind != ellipsisType)
11030                {
11031                   char type2[1024];
11032                   type2[0] = '\0';
11033                   if(inCompiler)
11034                   {
11035                      char expString[10240];
11036                      expString[0] = '\0';
11037
11038                      PrintType(exp.destType, type2, false, true);
11039
11040                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11041                      if(unresolved)
11042                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11043                      else if(exp.type != dummyExp)
11044                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11045                   }
11046                }
11047                else
11048                {
11049                   char expString[10240] ;
11050                   expString[0] = '\0';
11051                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11052
11053                   if(unresolved)
11054                      Compiler_Error($"unresolved identifier %s\n", expString);
11055                   else if(exp.type != dummyExp)
11056                      Compiler_Error($"couldn't determine type of %s\n", expString);
11057                }
11058             }
11059             else
11060             {
11061                char type1[1024];
11062                char type2[1024];
11063                type1[0] = '\0';
11064                type2[0] = '\0';
11065                if(inCompiler)
11066                {
11067                   PrintType(exp.expType, type1, false, true);
11068                   PrintType(exp.destType, type2, false, true);
11069                }
11070
11071                //CheckExpressionType(exp, exp.destType, false);
11072
11073                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11074                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11075                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11076                else
11077                {
11078                   char expString[10240];
11079                   expString[0] = '\0';
11080                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11081
11082 #ifdef _DEBUG
11083                   CheckExpressionType(exp, exp.destType, false, true);
11084 #endif
11085                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11086                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
11087                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11088
11089                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11090                   FreeType(exp.expType);
11091                   exp.destType.refCount++;
11092                   exp.expType = exp.destType;
11093                }
11094             }
11095          }
11096       }
11097       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
11098       {
11099          Expression newExp { };
11100          char typeString[1024];
11101          OldList * specs = MkList();
11102          Declarator decl;
11103
11104          typeString[0] = '\0';
11105
11106          *newExp = *exp;
11107
11108          if(exp.expType)  exp.expType.refCount++;
11109          if(exp.expType)  exp.expType.refCount++;
11110          exp.type = castExp;
11111          newExp.destType = exp.expType;
11112
11113          PrintType(exp.expType, typeString, false, false);
11114          decl = SpecDeclFromString(typeString, specs, null);
11115
11116          exp.cast.typeName = MkTypeName(specs, decl);
11117          exp.cast.exp = newExp;
11118       }
11119    }
11120    else if(unresolved)
11121    {
11122       if(exp.identifier._class && exp.identifier._class.name)
11123          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11124       else if(exp.identifier.string && exp.identifier.string[0])
11125          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11126    }
11127    else if(!exp.expType && exp.type != dummyExp)
11128    {
11129       char expString[10240];
11130       expString[0] = '\0';
11131       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11132       Compiler_Error($"couldn't determine type of %s\n", expString);
11133    }
11134
11135    // Let's try to support any_object & typed_object here:
11136    if(inCompiler)
11137       ApplyAnyObjectLogic(exp);
11138
11139    // Mark nohead classes as by reference, unless we're casting them to an integral type
11140    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11141       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11142          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11143           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11144    {
11145       exp.byReference = true;
11146    }
11147    yylloc = oldyylloc;
11148 }
11149
11150 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11151 {
11152    // THIS CODE WILL FIND NEXT MEMBER...
11153    if(*curMember)
11154    {
11155       *curMember = (*curMember).next;
11156
11157       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11158       {
11159          *curMember = subMemberStack[--(*subMemberStackPos)];
11160          *curMember = (*curMember).next;
11161       }
11162
11163       // SKIP ALL PROPERTIES HERE...
11164       while((*curMember) && (*curMember).isProperty)
11165          *curMember = (*curMember).next;
11166
11167       if(subMemberStackPos)
11168       {
11169          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11170          {
11171             subMemberStack[(*subMemberStackPos)++] = *curMember;
11172
11173             *curMember = (*curMember).members.first;
11174             while(*curMember && (*curMember).isProperty)
11175                *curMember = (*curMember).next;
11176          }
11177       }
11178    }
11179    while(!*curMember)
11180    {
11181       if(!*curMember)
11182       {
11183          if(subMemberStackPos && *subMemberStackPos)
11184          {
11185             *curMember = subMemberStack[--(*subMemberStackPos)];
11186             *curMember = (*curMember).next;
11187          }
11188          else
11189          {
11190             Class lastCurClass = *curClass;
11191
11192             if(*curClass == _class) break;     // REACHED THE END
11193
11194             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11195             *curMember = (*curClass).membersAndProperties.first;
11196          }
11197
11198          while((*curMember) && (*curMember).isProperty)
11199             *curMember = (*curMember).next;
11200          if(subMemberStackPos)
11201          {
11202             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11203             {
11204                subMemberStack[(*subMemberStackPos)++] = *curMember;
11205
11206                *curMember = (*curMember).members.first;
11207                while(*curMember && (*curMember).isProperty)
11208                   *curMember = (*curMember).next;
11209             }
11210          }
11211       }
11212    }
11213 }
11214
11215
11216 static void ProcessInitializer(Initializer init, Type type)
11217 {
11218    switch(init.type)
11219    {
11220       case expInitializer:
11221          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11222          {
11223             // TESTING THIS FOR SHUTTING = 0 WARNING
11224             if(init.exp && !init.exp.destType)
11225             {
11226                FreeType(init.exp.destType);
11227                init.exp.destType = type;
11228                if(type) type.refCount++;
11229             }
11230             if(init.exp)
11231             {
11232                ProcessExpressionType(init.exp);
11233                init.isConstant = init.exp.isConstant;
11234             }
11235             break;
11236          }
11237          else
11238          {
11239             Expression exp = init.exp;
11240             Instantiation inst = exp.instance;
11241             MembersInit members;
11242
11243             init.type = listInitializer;
11244             init.list = MkList();
11245
11246             if(inst.members)
11247             {
11248                for(members = inst.members->first; members; members = members.next)
11249                {
11250                   if(members.type == dataMembersInit)
11251                   {
11252                      MemberInit member;
11253                      for(member = members.dataMembers->first; member; member = member.next)
11254                      {
11255                         ListAdd(init.list, member.initializer);
11256                         member.initializer = null;
11257                      }
11258                   }
11259                   // Discard all MembersInitMethod
11260                }
11261             }
11262             FreeExpression(exp);
11263          }
11264       case listInitializer:
11265       {
11266          Initializer i;
11267          Type initializerType = null;
11268          Class curClass = null;
11269          DataMember curMember = null;
11270          DataMember subMemberStack[256];
11271          int subMemberStackPos = 0;
11272
11273          if(type && type.kind == arrayType)
11274             initializerType = Dereference(type);
11275          else if(type && (type.kind == structType || type.kind == unionType))
11276             initializerType = type.members.first;
11277
11278          for(i = init.list->first; i; i = i.next)
11279          {
11280             if(type && type.kind == classType && type._class && type._class.registered)
11281             {
11282                // 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)
11283                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11284                // TODO: Generate error on initializing a private data member this way from another module...
11285                if(curMember)
11286                {
11287                   if(!curMember.dataType)
11288                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11289                   initializerType = curMember.dataType;
11290                }
11291             }
11292             ProcessInitializer(i, initializerType);
11293             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11294                initializerType = initializerType.next;
11295             if(!i.isConstant)
11296                init.isConstant = false;
11297          }
11298
11299          if(type && type.kind == arrayType)
11300             FreeType(initializerType);
11301
11302          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11303          {
11304             Compiler_Error($"Assigning list initializer to non list\n");
11305          }
11306          break;
11307       }
11308    }
11309 }
11310
11311 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11312 {
11313    switch(spec.type)
11314    {
11315       case baseSpecifier:
11316       {
11317          if(spec.specifier == THISCLASS)
11318          {
11319             if(thisClass)
11320             {
11321                spec.type = nameSpecifier;
11322                spec.name = ReplaceThisClass(thisClass);
11323                spec.symbol = FindClass(spec.name);
11324                ProcessSpecifier(spec, declareStruct);
11325             }
11326          }
11327          break;
11328       }
11329       case nameSpecifier:
11330       {
11331          Symbol symbol = FindType(curContext, spec.name);
11332          if(symbol)
11333             DeclareType(symbol.type, true, true);
11334          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11335             DeclareStruct(spec.name, false);
11336          break;
11337       }
11338       case enumSpecifier:
11339       {
11340          Enumerator e;
11341          if(spec.list)
11342          {
11343             for(e = spec.list->first; e; e = e.next)
11344             {
11345                if(e.exp)
11346                   ProcessExpressionType(e.exp);
11347             }
11348          }
11349          break;
11350       }
11351       case structSpecifier:
11352       case unionSpecifier:
11353       {
11354          if(spec.definitions)
11355          {
11356             ClassDef def;
11357             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11358             //if(symbol)
11359                ProcessClass(spec.definitions, symbol);
11360             /*else
11361             {
11362                for(def = spec.definitions->first; def; def = def.next)
11363                {
11364                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11365                      ProcessDeclaration(def.decl);
11366                }
11367             }*/
11368          }
11369          break;
11370       }
11371       /*
11372       case classSpecifier:
11373       {
11374          Symbol classSym = FindClass(spec.name);
11375          if(classSym && classSym.registered && classSym.registered.type == structClass)
11376             DeclareStruct(spec.name, false);
11377          break;
11378       }
11379       */
11380    }
11381 }
11382
11383
11384 static void ProcessDeclarator(Declarator decl)
11385 {
11386    switch(decl.type)
11387    {
11388       case identifierDeclarator:
11389          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11390          {
11391             FreeSpecifier(decl.identifier._class);
11392             decl.identifier._class = null;
11393          }
11394          break;
11395       case arrayDeclarator:
11396          if(decl.array.exp)
11397             ProcessExpressionType(decl.array.exp);
11398       case structDeclarator:
11399       case bracketsDeclarator:
11400       case functionDeclarator:
11401       case pointerDeclarator:
11402       case extendedDeclarator:
11403       case extendedDeclaratorEnd:
11404          if(decl.declarator)
11405             ProcessDeclarator(decl.declarator);
11406          if(decl.type == functionDeclarator)
11407          {
11408             Identifier id = GetDeclId(decl);
11409             if(id && id._class)
11410             {
11411                TypeName param
11412                {
11413                   qualifiers = MkListOne(id._class);
11414                   declarator = null;
11415                };
11416                if(!decl.function.parameters)
11417                   decl.function.parameters = MkList();
11418                decl.function.parameters->Insert(null, param);
11419                id._class = null;
11420             }
11421             if(decl.function.parameters)
11422             {
11423                TypeName param;
11424
11425                for(param = decl.function.parameters->first; param; param = param.next)
11426                {
11427                   if(param.qualifiers && param.qualifiers->first)
11428                   {
11429                      Specifier spec = param.qualifiers->first;
11430                      if(spec && spec.specifier == TYPED_OBJECT)
11431                      {
11432                         Declarator d = param.declarator;
11433                         TypeName newParam
11434                         {
11435                            qualifiers = MkListOne(MkSpecifier(VOID));
11436                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11437                         };
11438                         if(d.type != pointerDeclarator)
11439                            newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11440
11441                         FreeList(param.qualifiers, FreeSpecifier);
11442
11443                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11444                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11445
11446                         decl.function.parameters->Insert(param, newParam);
11447                         param = newParam;
11448                      }
11449                      else if(spec && spec.specifier == ANY_OBJECT)
11450                      {
11451                         Declarator d = param.declarator;
11452
11453                         FreeList(param.qualifiers, FreeSpecifier);
11454
11455                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11456                         if(d.type != pointerDeclarator)
11457                            param.qualifiers->Insert(null, MkSpecifier(CONST));
11458                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11459                      }
11460                      else if(spec.specifier == THISCLASS)
11461                      {
11462                         if(thisClass)
11463                         {
11464                            spec.type = nameSpecifier;
11465                            spec.name = ReplaceThisClass(thisClass);
11466                            spec.symbol = FindClass(spec.name);
11467                            ProcessSpecifier(spec, false);
11468                         }
11469                      }
11470                   }
11471
11472                   if(param.declarator)
11473                      ProcessDeclarator(param.declarator);
11474                }
11475             }
11476          }
11477          break;
11478    }
11479 }
11480
11481 static void ProcessDeclaration(Declaration decl)
11482 {
11483    yylloc = decl.loc;
11484    switch(decl.type)
11485    {
11486       case initDeclaration:
11487       {
11488          bool declareStruct = false;
11489          /*
11490          lineNum = decl.pos.line;
11491          column = decl.pos.col;
11492          */
11493
11494          if(decl.declarators)
11495          {
11496             InitDeclarator d;
11497
11498             for(d = decl.declarators->first; d; d = d.next)
11499             {
11500                Type type, subType;
11501                ProcessDeclarator(d.declarator);
11502
11503                type = ProcessType(decl.specifiers, d.declarator);
11504
11505                if(d.initializer)
11506                {
11507                   ProcessInitializer(d.initializer, type);
11508
11509                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11510
11511                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11512                      d.initializer.exp.type == instanceExp)
11513                   {
11514                      if(type.kind == classType && type._class ==
11515                         d.initializer.exp.expType._class)
11516                      {
11517                         Instantiation inst = d.initializer.exp.instance;
11518                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11519
11520                         d.initializer.exp.instance = null;
11521                         if(decl.specifiers)
11522                            FreeList(decl.specifiers, FreeSpecifier);
11523                         FreeList(decl.declarators, FreeInitDeclarator);
11524
11525                         d = null;
11526
11527                         decl.type = instDeclaration;
11528                         decl.inst = inst;
11529                      }
11530                   }
11531                }
11532                for(subType = type; subType;)
11533                {
11534                   if(subType.kind == classType)
11535                   {
11536                      declareStruct = true;
11537                      break;
11538                   }
11539                   else if(subType.kind == pointerType)
11540                      break;
11541                   else if(subType.kind == arrayType)
11542                      subType = subType.arrayType;
11543                   else
11544                      break;
11545                }
11546
11547                FreeType(type);
11548                if(!d) break;
11549             }
11550          }
11551
11552          if(decl.specifiers)
11553          {
11554             Specifier s;
11555             for(s = decl.specifiers->first; s; s = s.next)
11556             {
11557                ProcessSpecifier(s, declareStruct);
11558             }
11559          }
11560          break;
11561       }
11562       case instDeclaration:
11563       {
11564          ProcessInstantiationType(decl.inst);
11565          break;
11566       }
11567       case structDeclaration:
11568       {
11569          Specifier spec;
11570          Declarator d;
11571          bool declareStruct = false;
11572
11573          if(decl.declarators)
11574          {
11575             for(d = decl.declarators->first; d; d = d.next)
11576             {
11577                Type type = ProcessType(decl.specifiers, d.declarator);
11578                Type subType;
11579                ProcessDeclarator(d);
11580                for(subType = type; subType;)
11581                {
11582                   if(subType.kind == classType)
11583                   {
11584                      declareStruct = true;
11585                      break;
11586                   }
11587                   else if(subType.kind == pointerType)
11588                      break;
11589                   else if(subType.kind == arrayType)
11590                      subType = subType.arrayType;
11591                   else
11592                      break;
11593                }
11594                FreeType(type);
11595             }
11596          }
11597          if(decl.specifiers)
11598          {
11599             for(spec = decl.specifiers->first; spec; spec = spec.next)
11600                ProcessSpecifier(spec, declareStruct);
11601          }
11602          break;
11603       }
11604    }
11605 }
11606
11607 static FunctionDefinition curFunction;
11608
11609 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11610 {
11611    char propName[1024], propNameM[1024];
11612    char getName[1024], setName[1024];
11613    OldList * args;
11614
11615    DeclareProperty(prop, setName, getName);
11616
11617    // eInstance_FireWatchers(object, prop);
11618    strcpy(propName, "__ecereProp_");
11619    FullClassNameCat(propName, prop._class.fullName, false);
11620    strcat(propName, "_");
11621    // strcat(propName, prop.name);
11622    FullClassNameCat(propName, prop.name, true);
11623    MangleClassName(propName);
11624
11625    strcpy(propNameM, "__ecerePropM_");
11626    FullClassNameCat(propNameM, prop._class.fullName, false);
11627    strcat(propNameM, "_");
11628    // strcat(propNameM, prop.name);
11629    FullClassNameCat(propNameM, prop.name, true);
11630    MangleClassName(propNameM);
11631
11632    if(prop.isWatchable)
11633    {
11634       args = MkList();
11635       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11636       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11637       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11638
11639       args = MkList();
11640       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11641       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11642       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11643    }
11644
11645
11646    {
11647       args = MkList();
11648       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11649       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11650       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11651
11652       args = MkList();
11653       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11654       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11655       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11656    }
11657
11658    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11659       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11660       curFunction.propSet.fireWatchersDone = true;
11661 }
11662
11663 static void ProcessStatement(Statement stmt)
11664 {
11665    yylloc = stmt.loc;
11666    /*
11667    lineNum = stmt.pos.line;
11668    column = stmt.pos.col;
11669    */
11670    switch(stmt.type)
11671    {
11672       case labeledStmt:
11673          ProcessStatement(stmt.labeled.stmt);
11674          break;
11675       case caseStmt:
11676          // This expression should be constant...
11677          if(stmt.caseStmt.exp)
11678          {
11679             FreeType(stmt.caseStmt.exp.destType);
11680             stmt.caseStmt.exp.destType = curSwitchType;
11681             if(curSwitchType) curSwitchType.refCount++;
11682             ProcessExpressionType(stmt.caseStmt.exp);
11683             ComputeExpression(stmt.caseStmt.exp);
11684          }
11685          if(stmt.caseStmt.stmt)
11686             ProcessStatement(stmt.caseStmt.stmt);
11687          break;
11688       case compoundStmt:
11689       {
11690          if(stmt.compound.context)
11691          {
11692             Declaration decl;
11693             Statement s;
11694
11695             Statement prevCompound = curCompound;
11696             Context prevContext = curContext;
11697
11698             if(!stmt.compound.isSwitch)
11699                curCompound = stmt;
11700             curContext = stmt.compound.context;
11701
11702             if(stmt.compound.declarations)
11703             {
11704                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11705                   ProcessDeclaration(decl);
11706             }
11707             if(stmt.compound.statements)
11708             {
11709                for(s = stmt.compound.statements->first; s; s = s.next)
11710                   ProcessStatement(s);
11711             }
11712
11713             curContext = prevContext;
11714             curCompound = prevCompound;
11715          }
11716          break;
11717       }
11718       case expressionStmt:
11719       {
11720          Expression exp;
11721          if(stmt.expressions)
11722          {
11723             for(exp = stmt.expressions->first; exp; exp = exp.next)
11724                ProcessExpressionType(exp);
11725          }
11726          break;
11727       }
11728       case ifStmt:
11729       {
11730          Expression exp;
11731
11732          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11733          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11734          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11735          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11736          {
11737             ProcessExpressionType(exp);
11738          }
11739          if(stmt.ifStmt.stmt)
11740             ProcessStatement(stmt.ifStmt.stmt);
11741          if(stmt.ifStmt.elseStmt)
11742             ProcessStatement(stmt.ifStmt.elseStmt);
11743          break;
11744       }
11745       case switchStmt:
11746       {
11747          Type oldSwitchType = curSwitchType;
11748          if(stmt.switchStmt.exp)
11749          {
11750             Expression exp;
11751             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11752             {
11753                if(!exp.next)
11754                {
11755                   /*
11756                   Type destType
11757                   {
11758                      kind = intType;
11759                      refCount = 1;
11760                   };
11761                   e.exp.destType = destType;
11762                   */
11763
11764                   ProcessExpressionType(exp);
11765                }
11766                if(!exp.next)
11767                   curSwitchType = exp.expType;
11768             }
11769          }
11770          ProcessStatement(stmt.switchStmt.stmt);
11771          curSwitchType = oldSwitchType;
11772          break;
11773       }
11774       case whileStmt:
11775       {
11776          if(stmt.whileStmt.exp)
11777          {
11778             Expression exp;
11779
11780             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11781             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11782             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11783             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11784             {
11785                ProcessExpressionType(exp);
11786             }
11787          }
11788          if(stmt.whileStmt.stmt)
11789             ProcessStatement(stmt.whileStmt.stmt);
11790          break;
11791       }
11792       case doWhileStmt:
11793       {
11794          if(stmt.doWhile.exp)
11795          {
11796             Expression exp;
11797
11798             if(stmt.doWhile.exp->last)
11799             {
11800                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11801                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11802                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11803             }
11804             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11805             {
11806                ProcessExpressionType(exp);
11807             }
11808          }
11809          if(stmt.doWhile.stmt)
11810             ProcessStatement(stmt.doWhile.stmt);
11811          break;
11812       }
11813       case forStmt:
11814       {
11815          Expression exp;
11816          if(stmt.forStmt.init)
11817             ProcessStatement(stmt.forStmt.init);
11818
11819          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11820          {
11821             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11822             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11823             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11824          }
11825
11826          if(stmt.forStmt.check)
11827             ProcessStatement(stmt.forStmt.check);
11828          if(stmt.forStmt.increment)
11829          {
11830             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11831                ProcessExpressionType(exp);
11832          }
11833
11834          if(stmt.forStmt.stmt)
11835             ProcessStatement(stmt.forStmt.stmt);
11836          break;
11837       }
11838       case forEachStmt:
11839       {
11840          Identifier id = stmt.forEachStmt.id;
11841          OldList * exp = stmt.forEachStmt.exp;
11842          OldList * filter = stmt.forEachStmt.filter;
11843          Statement block = stmt.forEachStmt.stmt;
11844          char iteratorType[1024];
11845          Type source;
11846          Expression e;
11847          bool isBuiltin = exp && exp->last &&
11848             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11849               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11850          Expression arrayExp;
11851          const char * typeString = null;
11852          int builtinCount = 0;
11853
11854          for(e = exp ? exp->first : null; e; e = e.next)
11855          {
11856             if(!e.next)
11857             {
11858                FreeType(e.destType);
11859                e.destType = ProcessTypeString("Container", false);
11860             }
11861             if(!isBuiltin || e.next)
11862                ProcessExpressionType(e);
11863          }
11864
11865          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11866          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11867             eClass_IsDerived(source._class.registered, containerClass)))
11868          {
11869             Class _class = source ? source._class.registered : null;
11870             Symbol symbol;
11871             Expression expIt = null;
11872             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
11873             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11874             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11875             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11876             stmt.type = compoundStmt;
11877
11878             stmt.compound.context = Context { };
11879             stmt.compound.context.parent = curContext;
11880             curContext = stmt.compound.context;
11881
11882             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11883             {
11884                Class mapClass = eSystem_FindClass(privateModule, "Map");
11885                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11886                isCustomAVLTree = true;
11887                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
11888                   isAVLTree = true;
11889                else */if(eClass_IsDerived(source._class.registered, mapClass))
11890                   isMap = true;
11891             }
11892             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11893             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11894             {
11895                Class listClass = eSystem_FindClass(privateModule, "List");
11896                isLinkList = true;
11897                isList = eClass_IsDerived(source._class.registered, listClass);
11898             }
11899
11900             if(isArray)
11901             {
11902                Declarator decl;
11903                OldList * specs = MkList();
11904                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11905                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11906                stmt.compound.declarations = MkListOne(
11907                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11908                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11909                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11910                      MkInitializerAssignment(MkExpBrackets(exp))))));
11911             }
11912             else if(isBuiltin)
11913             {
11914                Type type = null;
11915                char typeStringBuf[1024];
11916
11917                // TODO: Merge this code?
11918                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11919                if(((Expression)exp->last).type == castExp)
11920                {
11921                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11922                   if(typeName)
11923                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11924                }
11925
11926                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11927                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11928                   arrayExp.destType._class.registered.templateArgs)
11929                {
11930                   Class templateClass = arrayExp.destType._class.registered;
11931                   typeString = templateClass.templateArgs[2].dataTypeString;
11932                }
11933                else if(arrayExp.list)
11934                {
11935                   // Guess type from expressions in the array
11936                   Expression e;
11937                   for(e = arrayExp.list->first; e; e = e.next)
11938                   {
11939                      ProcessExpressionType(e);
11940                      if(e.expType)
11941                      {
11942                         if(!type) { type = e.expType; type.refCount++; }
11943                         else
11944                         {
11945                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11946                            if(!MatchTypeExpression(e, type, null, false, true))
11947                            {
11948                               FreeType(type);
11949                               type = e.expType;
11950                               e.expType = null;
11951
11952                               e = arrayExp.list->first;
11953                               ProcessExpressionType(e);
11954                               if(e.expType)
11955                               {
11956                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11957                                  if(!MatchTypeExpression(e, type, null, false, true))
11958                                  {
11959                                     FreeType(e.expType);
11960                                     e.expType = null;
11961                                     FreeType(type);
11962                                     type = null;
11963                                     break;
11964                                  }
11965                               }
11966                            }
11967                         }
11968                         if(e.expType)
11969                         {
11970                            FreeType(e.expType);
11971                            e.expType = null;
11972                         }
11973                      }
11974                   }
11975                   if(type)
11976                   {
11977                      typeStringBuf[0] = '\0';
11978                      PrintType(type, typeStringBuf, false, true);
11979                      typeString = typeStringBuf;
11980                      FreeType(type);
11981                   }
11982                }
11983                if(typeString)
11984                {
11985                   OldList * initializers = MkList();
11986                   Declarator decl;
11987                   OldList * specs = MkList();
11988                   if(arrayExp.list)
11989                   {
11990                      Expression e;
11991
11992                      builtinCount = arrayExp.list->count;
11993                      type = ProcessTypeString(typeString, false);
11994                      while(e = arrayExp.list->first)
11995                      {
11996                         arrayExp.list->Remove(e);
11997                         e.destType = type;
11998                         type.refCount++;
11999                         ProcessExpressionType(e);
12000                         ListAdd(initializers, MkInitializerAssignment(e));
12001                      }
12002                      FreeType(type);
12003                      delete arrayExp.list;
12004                   }
12005                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12006                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12007                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12008
12009                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12010                      PlugDeclarator(
12011                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12012                         ), MkInitializerList(initializers)))));
12013                   FreeList(exp, FreeExpression);
12014                }
12015                else
12016                {
12017                   arrayExp.expType = ProcessTypeString("Container", false);
12018                   Compiler_Error($"Couldn't determine type of array elements\n");
12019                }
12020
12021                /*
12022                Declarator decl;
12023                OldList * specs = MkList();
12024
12025                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12026                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12027                stmt.compound.declarations = MkListOne(
12028                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12029                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12030                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12031                      MkInitializerAssignment(MkExpBrackets(exp))))));
12032                */
12033             }
12034             else if(isLinkList && !isList)
12035             {
12036                Declarator decl;
12037                OldList * specs = MkList();
12038                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12039                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12040                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12041                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12042                      MkInitializerAssignment(MkExpBrackets(exp))))));
12043             }
12044             /*else if(isCustomAVLTree)
12045             {
12046                Declarator decl;
12047                OldList * specs = MkList();
12048                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12049                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12050                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12051                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12052                      MkInitializerAssignment(MkExpBrackets(exp))))));
12053             }*/
12054             else if(_class.templateArgs)
12055             {
12056                if(isMap)
12057                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12058                else
12059                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12060
12061                stmt.compound.declarations = MkListOne(
12062                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12063                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12064                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12065             }
12066             symbol = FindSymbol(id.string, curContext, curContext, false, false);
12067
12068             if(block)
12069             {
12070                // Reparent sub-contexts in this statement
12071                switch(block.type)
12072                {
12073                   case compoundStmt:
12074                      if(block.compound.context)
12075                         block.compound.context.parent = stmt.compound.context;
12076                      break;
12077                   case ifStmt:
12078                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12079                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12080                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12081                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12082                      break;
12083                   case switchStmt:
12084                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12085                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12086                      break;
12087                   case whileStmt:
12088                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12089                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12090                      break;
12091                   case doWhileStmt:
12092                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12093                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12094                      break;
12095                   case forStmt:
12096                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12097                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12098                      break;
12099                   case forEachStmt:
12100                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12101                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12102                      break;
12103                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12104                   case labeledStmt:
12105                   case caseStmt
12106                   case expressionStmt:
12107                   case gotoStmt:
12108                   case continueStmt:
12109                   case breakStmt
12110                   case returnStmt:
12111                   case asmStmt:
12112                   case badDeclarationStmt:
12113                   case fireWatchersStmt:
12114                   case stopWatchingStmt:
12115                   case watchStmt:
12116                   */
12117                }
12118             }
12119             if(filter)
12120             {
12121                block = MkIfStmt(filter, block, null);
12122             }
12123             if(isArray)
12124             {
12125                stmt.compound.statements = MkListOne(MkForStmt(
12126                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12127                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12128                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12129                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12130                   block));
12131               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12132               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12133               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12134             }
12135             else if(isBuiltin)
12136             {
12137                char count[128];
12138                //OldList * specs = MkList();
12139                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12140
12141                sprintf(count, "%d", builtinCount);
12142
12143                stmt.compound.statements = MkListOne(MkForStmt(
12144                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12145                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12146                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12147                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12148                   block));
12149
12150                /*
12151                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12152                stmt.compound.statements = MkListOne(MkForStmt(
12153                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12154                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12155                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12156                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12157                   block));
12158               */
12159               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12160               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12161               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12162             }
12163             else if(isLinkList && !isList)
12164             {
12165                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12166                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12167                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12168                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12169                {
12170                   stmt.compound.statements = MkListOne(MkForStmt(
12171                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12172                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12173                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12174                      block));
12175                }
12176                else
12177                {
12178                   OldList * specs = MkList();
12179                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12180                   stmt.compound.statements = MkListOne(MkForStmt(
12181                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12182                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12183                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12184                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12185                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12186                      block));
12187                }
12188                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12189                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12190                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12191             }
12192             /*else if(isCustomAVLTree)
12193             {
12194                stmt.compound.statements = MkListOne(MkForStmt(
12195                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12196                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12197                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12198                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12199                   block));
12200
12201                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12202                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12203                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12204             }*/
12205             else
12206             {
12207                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12208                   MkIdentifier("Next")), null)), block));
12209             }
12210             ProcessExpressionType(expIt);
12211             if(stmt.compound.declarations->first)
12212                ProcessDeclaration(stmt.compound.declarations->first);
12213
12214             if(symbol)
12215                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12216
12217             ProcessStatement(stmt);
12218             curContext = stmt.compound.context.parent;
12219             break;
12220          }
12221          else
12222          {
12223             Compiler_Error($"Expression is not a container\n");
12224          }
12225          break;
12226       }
12227       case gotoStmt:
12228          break;
12229       case continueStmt:
12230          break;
12231       case breakStmt:
12232          break;
12233       case returnStmt:
12234       {
12235          Expression exp;
12236          if(stmt.expressions)
12237          {
12238             for(exp = stmt.expressions->first; exp; exp = exp.next)
12239             {
12240                if(!exp.next)
12241                {
12242                   if(curFunction && !curFunction.type)
12243                      curFunction.type = ProcessType(
12244                         curFunction.specifiers, curFunction.declarator);
12245                   FreeType(exp.destType);
12246                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12247                   if(exp.destType) exp.destType.refCount++;
12248                }
12249                ProcessExpressionType(exp);
12250             }
12251          }
12252          break;
12253       }
12254       case badDeclarationStmt:
12255       {
12256          ProcessDeclaration(stmt.decl);
12257          break;
12258       }
12259       case asmStmt:
12260       {
12261          AsmField field;
12262          if(stmt.asmStmt.inputFields)
12263          {
12264             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12265                if(field.expression)
12266                   ProcessExpressionType(field.expression);
12267          }
12268          if(stmt.asmStmt.outputFields)
12269          {
12270             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12271                if(field.expression)
12272                   ProcessExpressionType(field.expression);
12273          }
12274          if(stmt.asmStmt.clobberedFields)
12275          {
12276             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12277             {
12278                if(field.expression)
12279                   ProcessExpressionType(field.expression);
12280             }
12281          }
12282          break;
12283       }
12284       case watchStmt:
12285       {
12286          PropertyWatch propWatch;
12287          OldList * watches = stmt._watch.watches;
12288          Expression object = stmt._watch.object;
12289          Expression watcher = stmt._watch.watcher;
12290          if(watcher)
12291             ProcessExpressionType(watcher);
12292          if(object)
12293             ProcessExpressionType(object);
12294
12295          if(inCompiler)
12296          {
12297             if(watcher || thisClass)
12298             {
12299                External external = curExternal;
12300                Context context = curContext;
12301
12302                stmt.type = expressionStmt;
12303                stmt.expressions = MkList();
12304
12305                curExternal = external.prev;
12306
12307                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12308                {
12309                   ClassFunction func;
12310                   char watcherName[1024];
12311                   Class watcherClass = watcher ?
12312                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12313                   External createdExternal;
12314
12315                   // Create a declaration above
12316                   External externalDecl = MkExternalDeclaration(null);
12317                   ast->Insert(curExternal.prev, externalDecl);
12318
12319                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12320                   if(propWatch.deleteWatch)
12321                      strcat(watcherName, "_delete");
12322                   else
12323                   {
12324                      Identifier propID;
12325                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12326                      {
12327                         strcat(watcherName, "_");
12328                         strcat(watcherName, propID.string);
12329                      }
12330                   }
12331
12332                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12333                   {
12334                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12335                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12336                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12337                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12338                      ProcessClassFunctionBody(func, propWatch.compound);
12339                      propWatch.compound = null;
12340
12341                      //afterExternal = afterExternal ? afterExternal : curExternal;
12342
12343                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12344                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12345                      // TESTING THIS...
12346                      createdExternal.symbol.idCode = external.symbol.idCode;
12347
12348                      curExternal = createdExternal;
12349                      ProcessFunction(createdExternal.function);
12350
12351
12352                      // Create a declaration above
12353                      {
12354                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12355                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12356                         externalDecl.declaration = decl;
12357                         if(decl.symbol && !decl.symbol.pointerExternal)
12358                            decl.symbol.pointerExternal = externalDecl;
12359                      }
12360
12361                      if(propWatch.deleteWatch)
12362                      {
12363                         OldList * args = MkList();
12364                         ListAdd(args, CopyExpression(object));
12365                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12366                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12367                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12368                      }
12369                      else
12370                      {
12371                         Class _class = object.expType._class.registered;
12372                         Identifier propID;
12373
12374                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12375                         {
12376                            char propName[1024];
12377                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12378                            if(prop)
12379                            {
12380                               char getName[1024], setName[1024];
12381                               OldList * args = MkList();
12382
12383                               DeclareProperty(prop, setName, getName);
12384
12385                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12386                               strcpy(propName, "__ecereProp_");
12387                               FullClassNameCat(propName, prop._class.fullName, false);
12388                               strcat(propName, "_");
12389                               // strcat(propName, prop.name);
12390                               FullClassNameCat(propName, prop.name, true);
12391
12392                               ListAdd(args, CopyExpression(object));
12393                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12394                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12395                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12396
12397                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12398                            }
12399                            else
12400                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12401                         }
12402                      }
12403                   }
12404                   else
12405                      Compiler_Error($"Invalid watched object\n");
12406                }
12407
12408                curExternal = external;
12409                curContext = context;
12410
12411                if(watcher)
12412                   FreeExpression(watcher);
12413                if(object)
12414                   FreeExpression(object);
12415                FreeList(watches, FreePropertyWatch);
12416             }
12417             else
12418                Compiler_Error($"No observer specified and not inside a _class\n");
12419          }
12420          else
12421          {
12422             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12423             {
12424                ProcessStatement(propWatch.compound);
12425             }
12426
12427          }
12428          break;
12429       }
12430       case fireWatchersStmt:
12431       {
12432          OldList * watches = stmt._watch.watches;
12433          Expression object = stmt._watch.object;
12434          Class _class;
12435          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12436          // printf("%X\n", watches);
12437          // printf("%X\n", stmt._watch.watches);
12438          if(object)
12439             ProcessExpressionType(object);
12440
12441          if(inCompiler)
12442          {
12443             _class = object ?
12444                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12445
12446             if(_class)
12447             {
12448                Identifier propID;
12449
12450                stmt.type = expressionStmt;
12451                stmt.expressions = MkList();
12452
12453                // Check if we're inside a property set
12454                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12455                {
12456                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12457                }
12458                else if(!watches)
12459                {
12460                   //Compiler_Error($"No property specified and not inside a property set\n");
12461                }
12462                if(watches)
12463                {
12464                   for(propID = watches->first; propID; propID = propID.next)
12465                   {
12466                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12467                      if(prop)
12468                      {
12469                         CreateFireWatcher(prop, object, stmt);
12470                      }
12471                      else
12472                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12473                   }
12474                }
12475                else
12476                {
12477                   // Fire all properties!
12478                   Property prop;
12479                   Class base;
12480                   for(base = _class; base; base = base.base)
12481                   {
12482                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12483                      {
12484                         if(prop.isProperty && prop.isWatchable)
12485                         {
12486                            CreateFireWatcher(prop, object, stmt);
12487                         }
12488                      }
12489                   }
12490                }
12491
12492                if(object)
12493                   FreeExpression(object);
12494                FreeList(watches, FreeIdentifier);
12495             }
12496             else
12497                Compiler_Error($"Invalid object specified and not inside a class\n");
12498          }
12499          break;
12500       }
12501       case stopWatchingStmt:
12502       {
12503          OldList * watches = stmt._watch.watches;
12504          Expression object = stmt._watch.object;
12505          Expression watcher = stmt._watch.watcher;
12506          Class _class;
12507          if(object)
12508             ProcessExpressionType(object);
12509          if(watcher)
12510             ProcessExpressionType(watcher);
12511          if(inCompiler)
12512          {
12513             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12514
12515             if(watcher || thisClass)
12516             {
12517                if(_class)
12518                {
12519                   Identifier propID;
12520
12521                   stmt.type = expressionStmt;
12522                   stmt.expressions = MkList();
12523
12524                   if(!watches)
12525                   {
12526                      OldList * args;
12527                      // eInstance_StopWatching(object, null, watcher);
12528                      args = MkList();
12529                      ListAdd(args, CopyExpression(object));
12530                      ListAdd(args, MkExpConstant("0"));
12531                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12532                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12533                   }
12534                   else
12535                   {
12536                      for(propID = watches->first; propID; propID = propID.next)
12537                      {
12538                         char propName[1024];
12539                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12540                         if(prop)
12541                         {
12542                            char getName[1024], setName[1024];
12543                            OldList * args = MkList();
12544
12545                            DeclareProperty(prop, setName, getName);
12546
12547                            // eInstance_StopWatching(object, prop, watcher);
12548                            strcpy(propName, "__ecereProp_");
12549                            FullClassNameCat(propName, prop._class.fullName, false);
12550                            strcat(propName, "_");
12551                            // strcat(propName, prop.name);
12552                            FullClassNameCat(propName, prop.name, true);
12553                            MangleClassName(propName);
12554
12555                            ListAdd(args, CopyExpression(object));
12556                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12557                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12558                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12559                         }
12560                         else
12561                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12562                      }
12563                   }
12564
12565                   if(object)
12566                      FreeExpression(object);
12567                   if(watcher)
12568                      FreeExpression(watcher);
12569                   FreeList(watches, FreeIdentifier);
12570                }
12571                else
12572                   Compiler_Error($"Invalid object specified and not inside a class\n");
12573             }
12574             else
12575                Compiler_Error($"No observer specified and not inside a class\n");
12576          }
12577          break;
12578       }
12579    }
12580 }
12581
12582 static void ProcessFunction(FunctionDefinition function)
12583 {
12584    Identifier id = GetDeclId(function.declarator);
12585    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12586    Type type = symbol ? symbol.type : null;
12587    Class oldThisClass = thisClass;
12588    Context oldTopContext = topContext;
12589
12590    yylloc = function.loc;
12591    // Process thisClass
12592
12593    if(type && type.thisClass)
12594    {
12595       Symbol classSym = type.thisClass;
12596       Class _class = type.thisClass.registered;
12597       char className[1024];
12598       char structName[1024];
12599       Declarator funcDecl;
12600       Symbol thisSymbol;
12601
12602       bool typedObject = false;
12603
12604       if(_class && !_class.base)
12605       {
12606          _class = currentClass;
12607          if(_class && !_class.symbol)
12608             _class.symbol = FindClass(_class.fullName);
12609          classSym = _class ? _class.symbol : null;
12610          typedObject = true;
12611       }
12612
12613       thisClass = _class;
12614
12615       if(inCompiler && _class)
12616       {
12617          if(type.kind == functionType)
12618          {
12619             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12620             {
12621                //TypeName param = symbol.type.params.first;
12622                Type param = symbol.type.params.first;
12623                symbol.type.params.Remove(param);
12624                //FreeTypeName(param);
12625                FreeType(param);
12626             }
12627             if(type.classObjectType != classPointer)
12628             {
12629                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12630                symbol.type.staticMethod = true;
12631                symbol.type.thisClass = null;
12632
12633                // HIGH DANGER: VERIFYING THIS...
12634                symbol.type.extraParam = false;
12635             }
12636          }
12637
12638          strcpy(className, "__ecereClass_");
12639          FullClassNameCat(className, _class.fullName, true);
12640
12641          MangleClassName(className);
12642
12643          structName[0] = 0;
12644          FullClassNameCat(structName, _class.fullName, false);
12645
12646          // [class] this
12647
12648
12649          funcDecl = GetFuncDecl(function.declarator);
12650          if(funcDecl)
12651          {
12652             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12653             {
12654                TypeName param = funcDecl.function.parameters->first;
12655                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12656                {
12657                   funcDecl.function.parameters->Remove(param);
12658                   FreeTypeName(param);
12659                }
12660             }
12661
12662             // DANGER: Watch for this... Check if it's a Conversion?
12663             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12664
12665             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12666             if(!function.propertyNoThis)
12667             {
12668                TypeName thisParam;
12669
12670                if(type.classObjectType != classPointer)
12671                {
12672                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12673                   if(!funcDecl.function.parameters)
12674                      funcDecl.function.parameters = MkList();
12675                   funcDecl.function.parameters->Insert(null, thisParam);
12676                }
12677
12678                if(typedObject)
12679                {
12680                   if(type.classObjectType != classPointer)
12681                   {
12682                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12683                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12684                   }
12685
12686                   thisParam = TypeName
12687                   {
12688                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12689                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12690                   };
12691                   funcDecl.function.parameters->Insert(null, thisParam);
12692                }
12693             }
12694          }
12695
12696          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12697          {
12698             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12699             funcDecl = GetFuncDecl(initDecl.declarator);
12700             if(funcDecl)
12701             {
12702                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12703                {
12704                   TypeName param = funcDecl.function.parameters->first;
12705                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12706                   {
12707                      funcDecl.function.parameters->Remove(param);
12708                      FreeTypeName(param);
12709                   }
12710                }
12711
12712                if(type.classObjectType != classPointer)
12713                {
12714                   // DANGER: Watch for this... Check if it's a Conversion?
12715                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12716                   {
12717                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12718
12719                      if(!funcDecl.function.parameters)
12720                         funcDecl.function.parameters = MkList();
12721                      funcDecl.function.parameters->Insert(null, thisParam);
12722                   }
12723                }
12724             }
12725          }
12726       }
12727
12728       // Add this to the context
12729       if(function.body)
12730       {
12731          if(type.classObjectType != classPointer)
12732          {
12733             thisSymbol = Symbol
12734             {
12735                string = CopyString("this");
12736                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12737             };
12738             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12739
12740             if(typedObject && thisSymbol.type)
12741             {
12742                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12743                thisSymbol.type.byReference = type.byReference;
12744                thisSymbol.type.typedByReference = type.byReference;
12745                /*
12746                thisSymbol = Symbol { string = CopyString("class") };
12747                function.body.compound.context.symbols.Add(thisSymbol);
12748                */
12749             }
12750          }
12751       }
12752
12753       // Pointer to class data
12754
12755       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12756       {
12757          DataMember member = null;
12758          {
12759             Class base;
12760             for(base = _class; base && base.type != systemClass; base = base.next)
12761             {
12762                for(member = base.membersAndProperties.first; member; member = member.next)
12763                   if(!member.isProperty)
12764                      break;
12765                if(member)
12766                   break;
12767             }
12768          }
12769          for(member = _class.membersAndProperties.first; member; member = member.next)
12770             if(!member.isProperty)
12771                break;
12772          if(member)
12773          {
12774             char pointerName[1024];
12775
12776             Declaration decl;
12777             Initializer initializer;
12778             Expression exp, bytePtr;
12779
12780             strcpy(pointerName, "__ecerePointer_");
12781             FullClassNameCat(pointerName, _class.fullName, false);
12782             {
12783                char className[1024];
12784                strcpy(className, "__ecereClass_");
12785                FullClassNameCat(className, classSym.string, true);
12786                MangleClassName(className);
12787
12788                // Testing This
12789                DeclareClass(classSym, className);
12790             }
12791
12792             // ((byte *) this)
12793             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12794
12795             if(_class.fixed)
12796             {
12797                char string[256];
12798                sprintf(string, "%d", _class.offset);
12799                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12800             }
12801             else
12802             {
12803                // ([bytePtr] + [className]->offset)
12804                exp = QBrackets(MkExpOp(bytePtr, '+',
12805                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12806             }
12807
12808             // (this ? [exp] : 0)
12809             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12810             exp.expType = Type
12811             {
12812                refCount = 1;
12813                kind = pointerType;
12814                type = Type { refCount = 1, kind = voidType };
12815             };
12816
12817             if(function.body)
12818             {
12819                yylloc = function.body.loc;
12820                // ([structName] *) [exp]
12821                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12822                initializer = MkInitializerAssignment(
12823                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12824
12825                // [structName] * [pointerName] = [initializer];
12826                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12827
12828                {
12829                   Context prevContext = curContext;
12830                   curContext = function.body.compound.context;
12831
12832                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12833                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12834
12835                   curContext = prevContext;
12836                }
12837
12838                // WHY?
12839                decl.symbol = null;
12840
12841                if(!function.body.compound.declarations)
12842                   function.body.compound.declarations = MkList();
12843                function.body.compound.declarations->Insert(null, decl);
12844             }
12845          }
12846       }
12847
12848
12849       // Loop through the function and replace undeclared identifiers
12850       // which are a member of the class (methods, properties or data)
12851       // by "this.[member]"
12852    }
12853    else
12854       thisClass = null;
12855
12856    if(id)
12857    {
12858       FreeSpecifier(id._class);
12859       id._class = null;
12860
12861       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12862       {
12863          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12864          id = GetDeclId(initDecl.declarator);
12865
12866          FreeSpecifier(id._class);
12867          id._class = null;
12868       }
12869    }
12870    if(function.body)
12871       topContext = function.body.compound.context;
12872    {
12873       FunctionDefinition oldFunction = curFunction;
12874       curFunction = function;
12875       if(function.body)
12876          ProcessStatement(function.body);
12877
12878       // If this is a property set and no firewatchers has been done yet, add one here
12879       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12880       {
12881          Statement prevCompound = curCompound;
12882          Context prevContext = curContext;
12883
12884          Statement fireWatchers = MkFireWatchersStmt(null, null);
12885          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12886          ListAdd(function.body.compound.statements, fireWatchers);
12887
12888          curCompound = function.body;
12889          curContext = function.body.compound.context;
12890
12891          ProcessStatement(fireWatchers);
12892
12893          curContext = prevContext;
12894          curCompound = prevCompound;
12895
12896       }
12897
12898       curFunction = oldFunction;
12899    }
12900
12901    if(function.declarator)
12902    {
12903       ProcessDeclarator(function.declarator);
12904    }
12905
12906    topContext = oldTopContext;
12907    thisClass = oldThisClass;
12908 }
12909
12910 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12911 static void ProcessClass(OldList definitions, Symbol symbol)
12912 {
12913    ClassDef def;
12914    External external = curExternal;
12915    Class regClass = symbol ? symbol.registered : null;
12916
12917    // Process all functions
12918    for(def = definitions.first; def; def = def.next)
12919    {
12920       if(def.type == functionClassDef)
12921       {
12922          if(def.function.declarator)
12923             curExternal = def.function.declarator.symbol.pointerExternal;
12924          else
12925             curExternal = external;
12926
12927          ProcessFunction((FunctionDefinition)def.function);
12928       }
12929       else if(def.type == declarationClassDef)
12930       {
12931          if(def.decl.type == instDeclaration)
12932          {
12933             thisClass = regClass;
12934             ProcessInstantiationType(def.decl.inst);
12935             thisClass = null;
12936          }
12937          // Testing this
12938          else
12939          {
12940             Class backThisClass = thisClass;
12941             if(regClass) thisClass = regClass;
12942             ProcessDeclaration(def.decl);
12943             thisClass = backThisClass;
12944          }
12945       }
12946       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12947       {
12948          MemberInit defProperty;
12949
12950          // Add this to the context
12951          Symbol thisSymbol = Symbol
12952          {
12953             string = CopyString("this");
12954             type = regClass ? MkClassType(regClass.fullName) : null;
12955          };
12956          globalContext.symbols.Add((BTNode)thisSymbol);
12957
12958          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12959          {
12960             thisClass = regClass;
12961             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12962             thisClass = null;
12963          }
12964
12965          globalContext.symbols.Remove((BTNode)thisSymbol);
12966          FreeSymbol(thisSymbol);
12967       }
12968       else if(def.type == propertyClassDef && def.propertyDef)
12969       {
12970          PropertyDef prop = def.propertyDef;
12971
12972          // Add this to the context
12973          /*
12974          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12975          globalContext.symbols.Add(thisSymbol);
12976          */
12977
12978          thisClass = regClass;
12979          if(prop.setStmt)
12980          {
12981             if(regClass)
12982             {
12983                Symbol thisSymbol
12984                {
12985                   string = CopyString("this");
12986                   type = MkClassType(regClass.fullName);
12987                };
12988                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12989             }
12990
12991             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12992             ProcessStatement(prop.setStmt);
12993          }
12994          if(prop.getStmt)
12995          {
12996             if(regClass)
12997             {
12998                Symbol thisSymbol
12999                {
13000                   string = CopyString("this");
13001                   type = MkClassType(regClass.fullName);
13002                };
13003                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13004             }
13005
13006             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13007             ProcessStatement(prop.getStmt);
13008          }
13009          if(prop.issetStmt)
13010          {
13011             if(regClass)
13012             {
13013                Symbol thisSymbol
13014                {
13015                   string = CopyString("this");
13016                   type = MkClassType(regClass.fullName);
13017                };
13018                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13019             }
13020
13021             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13022             ProcessStatement(prop.issetStmt);
13023          }
13024
13025          thisClass = null;
13026
13027          /*
13028          globalContext.symbols.Remove(thisSymbol);
13029          FreeSymbol(thisSymbol);
13030          */
13031       }
13032       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13033       {
13034          PropertyWatch propertyWatch = def.propertyWatch;
13035
13036          thisClass = regClass;
13037          if(propertyWatch.compound)
13038          {
13039             Symbol thisSymbol
13040             {
13041                string = CopyString("this");
13042                type = regClass ? MkClassType(regClass.fullName) : null;
13043             };
13044
13045             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13046
13047             curExternal = null;
13048             ProcessStatement(propertyWatch.compound);
13049          }
13050          thisClass = null;
13051       }
13052    }
13053 }
13054
13055 void DeclareFunctionUtil(const String s)
13056 {
13057    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13058    if(function)
13059    {
13060       char name[1024];
13061       name[0] = 0;
13062       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13063          strcpy(name, "__ecereFunction_");
13064       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13065       DeclareFunction(function, name);
13066    }
13067 }
13068
13069 void ComputeDataTypes()
13070 {
13071    External external;
13072    External temp { };
13073    External after = null;
13074
13075    currentClass = null;
13076
13077    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13078
13079    for(external = ast->first; external; external = external.next)
13080    {
13081       if(external.type == declarationExternal)
13082       {
13083          Declaration decl = external.declaration;
13084          if(decl)
13085          {
13086             OldList * decls = decl.declarators;
13087             if(decls)
13088             {
13089                InitDeclarator initDecl = decls->first;
13090                if(initDecl)
13091                {
13092                   Declarator declarator = initDecl.declarator;
13093                   if(declarator && declarator.type == identifierDeclarator)
13094                   {
13095                      Identifier id = declarator.identifier;
13096                      if(id && id.string)
13097                      {
13098                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
13099                         {
13100                            external.symbol.id = -1001, external.symbol.idCode = -1001;
13101                            after = external;
13102                         }
13103                      }
13104                   }
13105                }
13106             }
13107          }
13108        }
13109    }
13110
13111    temp.symbol = Symbol { id = -1000, idCode = -1000 };
13112    ast->Insert(after, temp);
13113    curExternal = temp;
13114
13115    DeclareFunctionUtil("eSystem_New");
13116    DeclareFunctionUtil("eSystem_New0");
13117    DeclareFunctionUtil("eSystem_Renew");
13118    DeclareFunctionUtil("eSystem_Renew0");
13119    DeclareFunctionUtil("eSystem_Delete");
13120    DeclareFunctionUtil("eClass_GetProperty");
13121    DeclareFunctionUtil("eClass_SetProperty");
13122    DeclareFunctionUtil("eInstance_FireSelfWatchers");
13123    DeclareFunctionUtil("eInstance_SetMethod");
13124    DeclareFunctionUtil("eInstance_IncRef");
13125    DeclareFunctionUtil("eInstance_StopWatching");
13126    DeclareFunctionUtil("eInstance_Watch");
13127    DeclareFunctionUtil("eInstance_FireWatchers");
13128
13129    DeclareStruct("ecere::com::Class", false);
13130    DeclareStruct("ecere::com::Instance", false);
13131    DeclareStruct("ecere::com::Property", false);
13132    DeclareStruct("ecere::com::DataMember", false);
13133    DeclareStruct("ecere::com::Method", false);
13134    DeclareStruct("ecere::com::SerialBuffer", false);
13135    DeclareStruct("ecere::com::ClassTemplateArgument", false);
13136
13137    ast->Remove(temp);
13138
13139    for(external = ast->first; external; external = external.next)
13140    {
13141       afterExternal = curExternal = external;
13142       if(external.type == functionExternal)
13143       {
13144          currentClass = external.function._class;
13145          ProcessFunction(external.function);
13146       }
13147       // There shouldn't be any _class member access here anyways...
13148       else if(external.type == declarationExternal)
13149       {
13150          currentClass = null;
13151          if(external.declaration)
13152             ProcessDeclaration(external.declaration);
13153       }
13154       else if(external.type == classExternal)
13155       {
13156          ClassDefinition _class = external._class;
13157          currentClass = external.symbol.registered;
13158          if(_class.definitions)
13159          {
13160             ProcessClass(_class.definitions, _class.symbol);
13161          }
13162          if(inCompiler)
13163          {
13164             // Free class data...
13165             ast->Remove(external);
13166             delete external;
13167          }
13168       }
13169       else if(external.type == nameSpaceExternal)
13170       {
13171          thisNameSpace = external.id.string;
13172       }
13173    }
13174    currentClass = null;
13175    thisNameSpace = null;
13176    curExternal = null;
13177
13178    delete temp.symbol;
13179    delete temp;
13180 }