771a9d1e9529f068d29e7f3add7faf01cc52fd9a
[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                   if(exp.destType.casted)
6514                      exp.needCast = true;
6515                   if(exp.expType) exp.expType.refCount++;
6516                }
6517                else
6518                {
6519                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6520                   if(exp.destType.casted)
6521                      exp.needCast = true;
6522                   if(convert.resultType)
6523                      convert.resultType.refCount++;
6524                }
6525             }
6526          }
6527          if(exp.isConstant && inCompiler)
6528             ComputeExpression(exp);
6529
6530          converts.Free(FreeConvert);
6531       }
6532
6533       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6534       {
6535          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false, warnConst);
6536       }
6537       if(!result && exp.expType && exp.destType)
6538       {
6539          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6540              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6541             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6542             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6543             result = true;
6544       }
6545    }
6546    // if(result) CheckTemplateTypes(exp);
6547    return result;
6548 }
6549
6550 void CheckTemplateTypes(Expression exp)
6551 {
6552    Expression nbExp = GetNonBracketsExp(exp);
6553    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate &&
6554       (nbExp == exp || nbExp.type != castExp))
6555    {
6556       Expression newExp { };
6557       Context context;
6558       *newExp = *exp;
6559       if(exp.destType) exp.destType.refCount++;
6560       if(exp.expType)  exp.expType.refCount++;
6561       newExp.prev = null;
6562       newExp.next = null;
6563
6564       switch(exp.expType.kind)
6565       {
6566          case doubleType:
6567             if(exp.destType.classObjectType)
6568             {
6569                // We need to pass the address, just pass it along (Undo what was done above)
6570                if(exp.destType) exp.destType.refCount--;
6571                if(exp.expType)  exp.expType.refCount--;
6572                delete newExp;
6573             }
6574             else
6575             {
6576                // If we're looking for value:
6577                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6578                OldList * specs;
6579                OldList * unionDefs = MkList();
6580                OldList * statements = MkList();
6581                context = PushContext();
6582                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6583                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6584                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6585                exp.type = extensionCompoundExp;
6586                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6587                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6588                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6589                exp.compound.compound.context = context;
6590                PopContext(context);
6591             }
6592             break;
6593          default:
6594             exp.type = castExp;
6595             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6596             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6597             exp.needCast = true;
6598             break;
6599       }
6600    }
6601    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6602    {
6603       Expression newExp { };
6604       Statement compound;
6605       Context context;
6606       *newExp = *exp;
6607       if(exp.destType) exp.destType.refCount++;
6608       if(exp.expType)  exp.expType.refCount++;
6609       newExp.prev = null;
6610       newExp.next = null;
6611
6612       switch(exp.expType.kind)
6613       {
6614          case doubleType:
6615             if(exp.destType.classObjectType)
6616             {
6617                // We need to pass the address, just pass it along (Undo what was done above)
6618                if(exp.destType) exp.destType.refCount--;
6619                if(exp.expType)  exp.expType.refCount--;
6620                delete newExp;
6621             }
6622             else
6623             {
6624                // If we're looking for value:
6625                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6626                OldList * specs;
6627                OldList * unionDefs = MkList();
6628                OldList * statements = MkList();
6629                context = PushContext();
6630                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6631                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6632                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6633                exp.type = extensionCompoundExp;
6634                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6635                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6636                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6637                exp.compound.compound.context = context;
6638                PopContext(context);
6639             }
6640             break;
6641          case classType:
6642          {
6643             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6644             {
6645                exp.type = bracketsExp;
6646                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6647                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6648                ProcessExpressionType(exp.list->first);
6649                break;
6650             }
6651             else
6652             {
6653                exp.type = bracketsExp;
6654                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6655                exp.needTemplateCast = 2;
6656                newExp.needCast = true;
6657                newExp.needTemplateCast = 2;
6658                ProcessExpressionType(exp.list->first);
6659                break;
6660             }
6661          }
6662          default:
6663          {
6664             if(exp.expType.kind == templateType)
6665             {
6666                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6667                if(type)
6668                {
6669                   FreeType(exp.destType);
6670                   FreeType(exp.expType);
6671                   delete newExp;
6672                   break;
6673                }
6674             }
6675             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6676             {
6677                exp.type = opExp;
6678                exp.op.op = '*';
6679                exp.op.exp1 = null;
6680                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6681                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6682             }
6683             else
6684             {
6685                char typeString[1024];
6686                Declarator decl;
6687                OldList * specs = MkList();
6688                typeString[0] = '\0';
6689                PrintType(exp.expType, typeString, false, false);
6690                decl = SpecDeclFromString(typeString, specs, null);
6691
6692                exp.type = castExp;
6693                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6694                exp.cast.typeName = MkTypeName(specs, decl);
6695                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6696                exp.cast.exp.needCast = true;
6697             }
6698             break;
6699          }
6700       }
6701    }
6702 }
6703 // TODO: The Symbol tree should be reorganized by namespaces
6704 // Name Space:
6705 //    - Tree of all symbols within (stored without namespace)
6706 //    - Tree of sub-namespaces
6707
6708 static Symbol ScanWithNameSpace(BinaryTree tree, const char * nameSpace, const char * name)
6709 {
6710    int nsLen = strlen(nameSpace);
6711    Symbol symbol;
6712    // Start at the name space prefix
6713    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6714    {
6715       char * s = symbol.string;
6716       if(!strncmp(s, nameSpace, nsLen))
6717       {
6718          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6719          int c;
6720          char * namePart;
6721          for(c = strlen(s)-1; c >= 0; c--)
6722             if(s[c] == ':')
6723                break;
6724
6725          namePart = s+c+1;
6726          if(!strcmp(namePart, name))
6727          {
6728             // TODO: Error on ambiguity
6729             return symbol;
6730          }
6731       }
6732       else
6733          break;
6734    }
6735    return null;
6736 }
6737
6738 static Symbol FindWithNameSpace(BinaryTree tree, const char * name)
6739 {
6740    int c;
6741    char nameSpace[1024];
6742    const char * namePart;
6743    bool gotColon = false;
6744
6745    nameSpace[0] = '\0';
6746    for(c = strlen(name)-1; c >= 0; c--)
6747       if(name[c] == ':')
6748       {
6749          gotColon = true;
6750          break;
6751       }
6752
6753    namePart = name+c+1;
6754    while(c >= 0 && name[c] == ':') c--;
6755    if(c >= 0)
6756    {
6757       // Try an exact match first
6758       Symbol symbol = (Symbol)tree.FindString(name);
6759       if(symbol)
6760          return symbol;
6761
6762       // Namespace specified
6763       memcpy(nameSpace, name, c + 1);
6764       nameSpace[c+1] = 0;
6765
6766       return ScanWithNameSpace(tree, nameSpace, namePart);
6767    }
6768    else if(gotColon)
6769    {
6770       // Looking for a global symbol, e.g. ::Sleep()
6771       Symbol symbol = (Symbol)tree.FindString(namePart);
6772       return symbol;
6773    }
6774    else
6775    {
6776       // Name only (no namespace specified)
6777       Symbol symbol = (Symbol)tree.FindString(namePart);
6778       if(symbol)
6779          return symbol;
6780       return ScanWithNameSpace(tree, "", namePart);
6781    }
6782    return null;
6783 }
6784
6785 static void ProcessDeclaration(Declaration decl);
6786
6787 /*static */Symbol FindSymbol(const char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6788 {
6789 #ifdef _DEBUG
6790    //Time startTime = GetTime();
6791 #endif
6792    // Optimize this later? Do this before/less?
6793    Context ctx;
6794    Symbol symbol = null;
6795    // First, check if the identifier is declared inside the function
6796    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6797
6798    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6799    {
6800       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6801       {
6802          symbol = null;
6803          if(thisNameSpace)
6804          {
6805             char curName[1024];
6806             strcpy(curName, thisNameSpace);
6807             strcat(curName, "::");
6808             strcat(curName, name);
6809             // Try to resolve in current namespace first
6810             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6811          }
6812          if(!symbol)
6813             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6814       }
6815       else
6816          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6817
6818       if(symbol || ctx == endContext) break;
6819    }
6820    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6821    {
6822       if(symbol.pointerExternal.type == functionExternal)
6823       {
6824          FunctionDefinition function = symbol.pointerExternal.function;
6825
6826          // Modified this recently...
6827          Context tmpContext = curContext;
6828          curContext = null;
6829          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6830          curContext = tmpContext;
6831
6832          symbol.pointerExternal.symbol = symbol;
6833
6834          // TESTING THIS:
6835          DeclareType(symbol.type, true, true);
6836
6837          ast->Insert(curExternal.prev, symbol.pointerExternal);
6838
6839          symbol.id = curExternal.symbol.idCode;
6840
6841       }
6842       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6843       {
6844          ast->Move(symbol.pointerExternal, curExternal.prev);
6845          symbol.id = curExternal.symbol.idCode;
6846       }
6847    }
6848 #ifdef _DEBUG
6849    //findSymbolTotalTime += GetTime() - startTime;
6850 #endif
6851    return symbol;
6852 }
6853
6854 static void GetTypeSpecs(Type type, OldList * specs)
6855 {
6856    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6857    switch(type.kind)
6858    {
6859       case classType:
6860       {
6861          if(type._class.registered)
6862          {
6863             if(!type._class.registered.dataType)
6864                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6865             GetTypeSpecs(type._class.registered.dataType, specs);
6866          }
6867          break;
6868       }
6869       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6870       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6871       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6872       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6873       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6874       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6875       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6876       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6877       case intType:
6878       default:
6879          ListAdd(specs, MkSpecifier(INT)); break;
6880    }
6881 }
6882
6883 static void PrintArraySize(Type arrayType, char * string)
6884 {
6885    char size[256];
6886    size[0] = '\0';
6887    strcat(size, "[");
6888    if(arrayType.enumClass)
6889       strcat(size, arrayType.enumClass.string);
6890    else if(arrayType.arraySizeExp)
6891       PrintExpression(arrayType.arraySizeExp, size);
6892    strcat(size, "]");
6893    strcat(string, size);
6894 }
6895
6896 // WARNING : This function expects a null terminated string since it recursively concatenate...
6897 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6898 {
6899    if(type)
6900    {
6901       if(printConst && type.constant)
6902          strcat(string, "const ");
6903       switch(type.kind)
6904       {
6905          case classType:
6906          {
6907             Symbol c = type._class;
6908             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6909             //       look into merging with thisclass ?
6910             if(type.classObjectType == typedObject)
6911                strcat(string, "typed_object");
6912             else if(type.classObjectType == anyObject)
6913                strcat(string, "any_object");
6914             else
6915             {
6916                if(c && c.string)
6917                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6918             }
6919             if(type.byReference)
6920                strcat(string, " &");
6921             break;
6922          }
6923          case voidType: strcat(string, "void"); break;
6924          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6925          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6926          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6927          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6928          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6929          case _BoolType: strcat(string, "_Bool"); break;
6930          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6931          case floatType: strcat(string, "float"); break;
6932          case doubleType: strcat(string, "double"); break;
6933          case structType:
6934             if(type.enumName)
6935             {
6936                strcat(string, "struct ");
6937                strcat(string, type.enumName);
6938             }
6939             else if(type.typeName)
6940                strcat(string, type.typeName);
6941             else
6942             {
6943                Type member;
6944                strcat(string, "struct { ");
6945                for(member = type.members.first; member; member = member.next)
6946                {
6947                   PrintType(member, string, true, fullName);
6948                   strcat(string,"; ");
6949                }
6950                strcat(string,"}");
6951             }
6952             break;
6953          case unionType:
6954             if(type.enumName)
6955             {
6956                strcat(string, "union ");
6957                strcat(string, type.enumName);
6958             }
6959             else if(type.typeName)
6960                strcat(string, type.typeName);
6961             else
6962             {
6963                strcat(string, "union ");
6964                strcat(string,"(unnamed)");
6965             }
6966             break;
6967          case enumType:
6968             if(type.enumName)
6969             {
6970                strcat(string, "enum ");
6971                strcat(string, type.enumName);
6972             }
6973             else if(type.typeName)
6974                strcat(string, type.typeName);
6975             else
6976                strcat(string, "int"); // "enum");
6977             break;
6978          case ellipsisType:
6979             strcat(string, "...");
6980             break;
6981          case subClassType:
6982             strcat(string, "subclass(");
6983             strcat(string, type._class ? type._class.string : "int");
6984             strcat(string, ")");
6985             break;
6986          case templateType:
6987             strcat(string, type.templateParameter.identifier.string);
6988             break;
6989          case thisClassType:
6990             strcat(string, "thisclass");
6991             break;
6992          case vaListType:
6993             strcat(string, "__builtin_va_list");
6994             break;
6995       }
6996    }
6997 }
6998
6999 static void PrintName(Type type, char * string, bool fullName)
7000 {
7001    if(type.name && type.name[0])
7002    {
7003       if(fullName)
7004          strcat(string, type.name);
7005       else
7006       {
7007          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
7008          if(name) name += 2; else name = type.name;
7009          strcat(string, name);
7010       }
7011    }
7012 }
7013
7014 static void PrintAttribs(Type type, char * string)
7015 {
7016    if(type)
7017    {
7018       if(type.dllExport)   strcat(string, "dllexport ");
7019       if(type.attrStdcall) strcat(string, "stdcall ");
7020    }
7021 }
7022
7023 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
7024 {
7025    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7026    {
7027       Type attrType = null;
7028       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
7029          PrintAttribs(type, string);
7030       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
7031          strcat(string, " const");
7032       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
7033       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7034          strcat(string, " (");
7035       if(type.kind == pointerType)
7036       {
7037          if(type.type.kind == functionType || type.type.kind == methodType)
7038             PrintAttribs(type.type, string);
7039       }
7040       if(type.kind == pointerType)
7041       {
7042          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
7043             strcat(string, "*");
7044          else
7045             strcat(string, " *");
7046       }
7047       if(printConst && type.constant && type.kind == pointerType)
7048          strcat(string, " const");
7049    }
7050    else
7051       PrintTypeSpecs(type, string, fullName, printConst);
7052 }
7053
7054 static void PostPrintType(Type type, char * string, bool fullName)
7055 {
7056    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7057       strcat(string, ")");
7058    if(type.kind == arrayType)
7059       PrintArraySize(type, string);
7060    else if(type.kind == functionType)
7061    {
7062       Type param;
7063       strcat(string, "(");
7064       for(param = type.params.first; param; param = param.next)
7065       {
7066          PrintType(param, string, true, fullName);
7067          if(param.next) strcat(string, ", ");
7068       }
7069       strcat(string, ")");
7070    }
7071    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7072       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7073 }
7074
7075 // *****
7076 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7077 // *****
7078 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7079 {
7080    PrePrintType(type, string, fullName, null, printConst);
7081
7082    if(type.thisClass || (printName && type.name && type.name[0]))
7083       strcat(string, " ");
7084    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7085    {
7086       Symbol _class = type.thisClass;
7087       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7088       {
7089          if(type.classObjectType == classPointer)
7090             strcat(string, "class");
7091          else
7092             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7093       }
7094       else if(_class && _class.string)
7095       {
7096          String s = _class.string;
7097          if(fullName)
7098             strcat(string, s);
7099          else
7100          {
7101             char * name = RSearchString(s, "::", strlen(s), true, false);
7102             if(name) name += 2; else name = s;
7103             strcat(string, name);
7104          }
7105       }
7106       strcat(string, "::");
7107    }
7108
7109    if(printName && type.name)
7110       PrintName(type, string, fullName);
7111    PostPrintType(type, string, fullName);
7112    if(type.bitFieldCount)
7113    {
7114       char count[100];
7115       sprintf(count, ":%d", type.bitFieldCount);
7116       strcat(string, count);
7117    }
7118 }
7119
7120 void PrintType(Type type, char * string, bool printName, bool fullName)
7121 {
7122    _PrintType(type, string, printName, fullName, true);
7123 }
7124
7125 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7126 {
7127    _PrintType(type, string, printName, fullName, false);
7128 }
7129
7130 static Type FindMember(Type type, char * string)
7131 {
7132    Type memberType;
7133    for(memberType = type.members.first; memberType; memberType = memberType.next)
7134    {
7135       if(!memberType.name)
7136       {
7137          Type subType = FindMember(memberType, string);
7138          if(subType)
7139             return subType;
7140       }
7141       else if(!strcmp(memberType.name, string))
7142          return memberType;
7143    }
7144    return null;
7145 }
7146
7147 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7148 {
7149    Type memberType;
7150    for(memberType = type.members.first; memberType; memberType = memberType.next)
7151    {
7152       if(!memberType.name)
7153       {
7154          Type subType = FindMember(memberType, string);
7155          if(subType)
7156          {
7157             *offset += memberType.offset;
7158             return subType;
7159          }
7160       }
7161       else if(!strcmp(memberType.name, string))
7162       {
7163          *offset += memberType.offset;
7164          return memberType;
7165       }
7166    }
7167    return null;
7168 }
7169
7170 public bool GetParseError() { return parseError; }
7171
7172 Expression ParseExpressionString(char * expression)
7173 {
7174    parseError = false;
7175
7176    fileInput = TempFile { };
7177    fileInput.Write(expression, 1, strlen(expression));
7178    fileInput.Seek(0, start);
7179
7180    echoOn = false;
7181    parsedExpression = null;
7182    resetScanner();
7183    expression_yyparse();
7184    delete fileInput;
7185
7186    return parsedExpression;
7187 }
7188
7189 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7190 {
7191    Identifier id = exp.identifier;
7192    Method method = null;
7193    Property prop = null;
7194    DataMember member = null;
7195    ClassProperty classProp = null;
7196
7197    if(_class && _class.type == enumClass)
7198    {
7199       NamedLink value = null;
7200       Class enumClass = eSystem_FindClass(privateModule, "enum");
7201       if(enumClass)
7202       {
7203          Class baseClass;
7204          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7205          {
7206             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7207             for(value = e.values.first; value; value = value.next)
7208             {
7209                if(!strcmp(value.name, id.string))
7210                   break;
7211             }
7212             if(value)
7213             {
7214                char constant[256];
7215
7216                FreeExpContents(exp);
7217
7218                exp.type = constantExp;
7219                exp.isConstant = true;
7220                if(!strcmp(baseClass.dataTypeString, "int"))
7221                   sprintf(constant, "%d",(int)value.data);
7222                else
7223                   sprintf(constant, "0x%X",(int)value.data);
7224                exp.constant = CopyString(constant);
7225                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7226                exp.expType = MkClassType(baseClass.fullName);
7227                break;
7228             }
7229          }
7230       }
7231       if(value)
7232          return true;
7233    }
7234    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7235    {
7236       ProcessMethodType(method);
7237       exp.expType = Type
7238       {
7239          refCount = 1;
7240          kind = methodType;
7241          method = method;
7242          // Crash here?
7243          // TOCHECK: Put it back to what it was...
7244          // methodClass = _class;
7245          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7246       };
7247       //id._class = null;
7248       return true;
7249    }
7250    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7251    {
7252       if(!prop.dataType)
7253          ProcessPropertyType(prop);
7254       exp.expType = prop.dataType;
7255       if(prop.dataType) prop.dataType.refCount++;
7256       return true;
7257    }
7258    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7259    {
7260       if(!member.dataType)
7261          member.dataType = ProcessTypeString(member.dataTypeString, false);
7262       exp.expType = member.dataType;
7263       if(member.dataType) member.dataType.refCount++;
7264       return true;
7265    }
7266    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7267    {
7268       if(!classProp.dataType)
7269          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7270
7271       if(classProp.constant)
7272       {
7273          FreeExpContents(exp);
7274
7275          exp.isConstant = true;
7276          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7277          {
7278             //char constant[256];
7279             exp.type = stringExp;
7280             exp.constant = QMkString((char *)classProp.Get(_class));
7281          }
7282          else
7283          {
7284             char constant[256];
7285             exp.type = constantExp;
7286             sprintf(constant, "%d", (int)classProp.Get(_class));
7287             exp.constant = CopyString(constant);
7288          }
7289       }
7290       else
7291       {
7292          // TO IMPLEMENT...
7293       }
7294
7295       exp.expType = classProp.dataType;
7296       if(classProp.dataType) classProp.dataType.refCount++;
7297       return true;
7298    }
7299    return false;
7300 }
7301
7302 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7303 {
7304    BinaryTree * tree = &nameSpace.functions;
7305    GlobalData data = (GlobalData)tree->FindString(name);
7306    NameSpace * child;
7307    if(!data)
7308    {
7309       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7310       {
7311          data = ScanGlobalData(child, name);
7312          if(data)
7313             break;
7314       }
7315    }
7316    return data;
7317 }
7318
7319 static GlobalData FindGlobalData(char * name)
7320 {
7321    int start = 0, c;
7322    NameSpace * nameSpace;
7323    nameSpace = globalData;
7324    for(c = 0; name[c]; c++)
7325    {
7326       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7327       {
7328          NameSpace * newSpace;
7329          char * spaceName = new char[c - start + 1];
7330          strncpy(spaceName, name + start, c - start);
7331          spaceName[c-start] = '\0';
7332          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7333          delete spaceName;
7334          if(!newSpace)
7335             return null;
7336          nameSpace = newSpace;
7337          if(name[c] == ':') c++;
7338          start = c+1;
7339       }
7340    }
7341    if(c - start)
7342    {
7343       return ScanGlobalData(nameSpace, name + start);
7344    }
7345    return null;
7346 }
7347
7348 static int definedExpStackPos;
7349 static void * definedExpStack[512];
7350
7351 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7352 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7353 {
7354    Expression prev = checkedExp.prev, next = checkedExp.next;
7355
7356    FreeExpContents(checkedExp);
7357    FreeType(checkedExp.expType);
7358    FreeType(checkedExp.destType);
7359
7360    *checkedExp = *newExp;
7361
7362    delete newExp;
7363
7364    checkedExp.prev = prev;
7365    checkedExp.next = next;
7366 }
7367
7368 void ApplyAnyObjectLogic(Expression e)
7369 {
7370    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7371 #ifdef _DEBUG
7372    char debugExpString[4096];
7373    debugExpString[0] = '\0';
7374    PrintExpression(e, debugExpString);
7375 #endif
7376
7377    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7378    {
7379       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7380       //ellipsisDestType = destType;
7381       if(e && e.expType)
7382       {
7383          Type type = e.expType;
7384          Class _class = null;
7385          //Type destType = e.destType;
7386
7387          if(type.kind == classType && type._class && type._class.registered)
7388          {
7389             _class = type._class.registered;
7390          }
7391          else if(type.kind == subClassType)
7392          {
7393             _class = FindClass("ecere::com::Class").registered;
7394          }
7395          else
7396          {
7397             char string[1024] = "";
7398             Symbol classSym;
7399
7400             PrintTypeNoConst(type, string, false, true);
7401             classSym = FindClass(string);
7402             if(classSym) _class = classSym.registered;
7403          }
7404
7405          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...
7406             (!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))) ||
7407             destType.byReference)))
7408          {
7409             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7410             {
7411                Expression checkedExp = e, newExp;
7412
7413                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7414                {
7415                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7416                   {
7417                      if(checkedExp.type == extensionCompoundExp)
7418                      {
7419                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7420                      }
7421                      else
7422                         checkedExp = checkedExp.list->last;
7423                   }
7424                   else if(checkedExp.type == castExp)
7425                      checkedExp = checkedExp.cast.exp;
7426                }
7427
7428                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7429                {
7430                   newExp = checkedExp.op.exp2;
7431                   checkedExp.op.exp2 = null;
7432                   FreeExpContents(checkedExp);
7433
7434                   if(e.expType && e.expType.passAsTemplate)
7435                   {
7436                      char size[100];
7437                      ComputeTypeSize(e.expType);
7438                      sprintf(size, "%d", e.expType.size);
7439                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7440                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7441                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7442                   }
7443
7444                   ReplaceExpContents(checkedExp, newExp);
7445                   e.byReference = true;
7446                }
7447                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7448                {
7449                   Expression checkedExp; //, newExp;
7450
7451                   {
7452                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7453                      bool hasAddress =
7454                         e.type == identifierExp ||
7455                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7456                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7457                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7458                         e.type == indexExp;
7459
7460                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7461                      {
7462                         Context context = PushContext();
7463                         Declarator decl;
7464                         OldList * specs = MkList();
7465                         char typeString[1024];
7466                         Expression newExp { };
7467
7468                         typeString[0] = '\0';
7469                         *newExp = *e;
7470
7471                         //if(e.destType) e.destType.refCount++;
7472                         // if(exp.expType) exp.expType.refCount++;
7473                         newExp.prev = null;
7474                         newExp.next = null;
7475                         newExp.expType = null;
7476
7477                         PrintTypeNoConst(e.expType, typeString, false, true);
7478                         decl = SpecDeclFromString(typeString, specs, null);
7479                         newExp.destType = ProcessType(specs, decl);
7480
7481                         curContext = context;
7482
7483                         // We need a current compound for this
7484                         if(curCompound)
7485                         {
7486                            char name[100];
7487                            OldList * stmts = MkList();
7488                            e.type = extensionCompoundExp;
7489                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7490                            if(!curCompound.compound.declarations)
7491                               curCompound.compound.declarations = MkList();
7492                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7493                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7494                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7495                            e.compound = MkCompoundStmt(null, stmts);
7496                         }
7497                         else
7498                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7499
7500                         /*
7501                         e.compound = MkCompoundStmt(
7502                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7503                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7504
7505                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7506                         */
7507
7508                         {
7509                            Type type = e.destType;
7510                            e.destType = { };
7511                            CopyTypeInto(e.destType, type);
7512                            e.destType.refCount = 1;
7513                            e.destType.classObjectType = none;
7514                            FreeType(type);
7515                         }
7516
7517                         e.compound.compound.context = context;
7518                         PopContext(context);
7519                         curContext = context.parent;
7520                      }
7521                   }
7522
7523                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7524                   checkedExp = e;
7525                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7526                   {
7527                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7528                      {
7529                         if(checkedExp.type == extensionCompoundExp)
7530                         {
7531                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7532                         }
7533                         else
7534                            checkedExp = checkedExp.list->last;
7535                      }
7536                      else if(checkedExp.type == castExp)
7537                         checkedExp = checkedExp.cast.exp;
7538                   }
7539                   {
7540                      Expression operand { };
7541                      operand = *checkedExp;
7542                      checkedExp.destType = null;
7543                      checkedExp.expType = null;
7544                      checkedExp.Clear();
7545                      checkedExp.type = opExp;
7546                      checkedExp.op.op = '&';
7547                      checkedExp.op.exp1 = null;
7548                      checkedExp.op.exp2 = operand;
7549
7550                      //newExp = MkExpOp(null, '&', checkedExp);
7551                   }
7552                   //ReplaceExpContents(checkedExp, newExp);
7553                }
7554             }
7555          }
7556       }
7557    }
7558    {
7559       // If expression type is a simple class, make it an address
7560       // FixReference(e, true);
7561    }
7562 //#if 0
7563    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7564       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7565          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7566    {
7567       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"))
7568       {
7569          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7570       }
7571       else
7572       {
7573          Expression thisExp { };
7574
7575          *thisExp = *e;
7576          thisExp.prev = null;
7577          thisExp.next = null;
7578          e.Clear();
7579
7580          e.type = bracketsExp;
7581          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7582          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7583             ((Expression)e.list->first).byReference = true;
7584
7585          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7586          {
7587             e.expType = thisExp.expType;
7588             e.expType.refCount++;
7589          }
7590          else*/
7591          {
7592             e.expType = { };
7593             CopyTypeInto(e.expType, thisExp.expType);
7594             e.expType.byReference = false;
7595             e.expType.refCount = 1;
7596
7597             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7598                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7599             {
7600                e.expType.classObjectType = none;
7601             }
7602          }
7603       }
7604    }
7605 // TOFIX: Try this for a nice IDE crash!
7606 //#endif
7607    // The other way around
7608    else
7609 //#endif
7610    if(destType && e.expType &&
7611          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7612          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7613          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7614    {
7615       if(destType.kind == ellipsisType)
7616       {
7617          Compiler_Error($"Unspecified type\n");
7618       }
7619       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7620       {
7621          bool byReference = e.expType.byReference;
7622          Expression thisExp { };
7623          Declarator decl;
7624          OldList * specs = MkList();
7625          char typeString[1024]; // Watch buffer overruns
7626          Type type;
7627          ClassObjectType backupClassObjectType;
7628          bool backupByReference;
7629
7630          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7631             type = e.expType;
7632          else
7633             type = destType;
7634
7635          backupClassObjectType = type.classObjectType;
7636          backupByReference = type.byReference;
7637
7638          type.classObjectType = none;
7639          type.byReference = false;
7640
7641          typeString[0] = '\0';
7642          PrintType(type, typeString, false, true);
7643          decl = SpecDeclFromString(typeString, specs, null);
7644
7645          type.classObjectType = backupClassObjectType;
7646          type.byReference = backupByReference;
7647
7648          *thisExp = *e;
7649          thisExp.prev = null;
7650          thisExp.next = null;
7651          e.Clear();
7652
7653          if( ( type.kind == classType && type._class && type._class.registered &&
7654                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7655                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7656              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7657              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7658          {
7659             e.type = opExp;
7660             e.op.op = '*';
7661             e.op.exp1 = null;
7662             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7663
7664             e.expType = { };
7665             CopyTypeInto(e.expType, type);
7666             e.expType.byReference = false;
7667             e.expType.refCount = 1;
7668          }
7669          else
7670          {
7671             e.type = castExp;
7672             e.cast.typeName = MkTypeName(specs, decl);
7673             e.cast.exp = thisExp;
7674             e.byReference = true;
7675             e.expType = type;
7676             type.refCount++;
7677          }
7678          e.destType = destType;
7679          destType.refCount++;
7680       }
7681    }
7682 }
7683
7684 void ApplyLocation(Expression exp, Location loc)
7685 {
7686    exp.loc = loc;
7687    switch(exp.type)
7688    {
7689       case opExp:
7690          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7691          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7692          break;
7693       case bracketsExp:
7694          if(exp.list)
7695          {
7696             Expression e;
7697             for(e = exp.list->first; e; e = e.next)
7698                ApplyLocation(e, loc);
7699          }
7700          break;
7701       case indexExp:
7702          if(exp.index.index)
7703          {
7704             Expression e;
7705             for(e = exp.index.index->first; e; e = e.next)
7706                ApplyLocation(e, loc);
7707          }
7708          if(exp.index.exp)
7709             ApplyLocation(exp.index.exp, loc);
7710          break;
7711       case callExp:
7712          if(exp.call.arguments)
7713          {
7714             Expression arg;
7715             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7716                ApplyLocation(arg, loc);
7717          }
7718          if(exp.call.exp)
7719             ApplyLocation(exp.call.exp, loc);
7720          break;
7721       case memberExp:
7722       case pointerExp:
7723          if(exp.member.exp)
7724             ApplyLocation(exp.member.exp, loc);
7725          break;
7726       case castExp:
7727          if(exp.cast.exp)
7728             ApplyLocation(exp.cast.exp, loc);
7729          break;
7730       case conditionExp:
7731          if(exp.cond.exp)
7732          {
7733             Expression e;
7734             for(e = exp.cond.exp->first; e; e = e.next)
7735                ApplyLocation(e, loc);
7736          }
7737          if(exp.cond.cond)
7738             ApplyLocation(exp.cond.cond, loc);
7739          if(exp.cond.elseExp)
7740             ApplyLocation(exp.cond.elseExp, loc);
7741          break;
7742       case vaArgExp:
7743          if(exp.vaArg.exp)
7744             ApplyLocation(exp.vaArg.exp, loc);
7745          break;
7746       default:
7747          break;
7748    }
7749 }
7750
7751 void ProcessExpressionType(Expression exp)
7752 {
7753    bool unresolved = false;
7754    Location oldyylloc = yylloc;
7755    bool notByReference = false;
7756 #ifdef _DEBUG
7757    char debugExpString[4096];
7758    debugExpString[0] = '\0';
7759    PrintExpression(exp, debugExpString);
7760 #endif
7761    if(!exp || exp.expType)
7762       return;
7763
7764    //eSystem_Logf("%s\n", expString);
7765
7766    // Testing this here
7767    yylloc = exp.loc;
7768    switch(exp.type)
7769    {
7770       case identifierExp:
7771       {
7772          Identifier id = exp.identifier;
7773          if(!id || !topContext) return;
7774
7775          // DOING THIS LATER NOW...
7776          if(id._class && id._class.name)
7777          {
7778             id.classSym = id._class.symbol; // FindClass(id._class.name);
7779             /* TODO: Name Space Fix ups
7780             if(!id.classSym)
7781                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7782             */
7783          }
7784
7785          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7786          {
7787             exp.expType = ProcessTypeString("Module", true);
7788             break;
7789          }
7790          else */if(strstr(id.string, "__ecereClass") == id.string)
7791          {
7792             exp.expType = ProcessTypeString("ecere::com::Class", true);
7793             break;
7794          }
7795          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7796          {
7797             // Added this here as well
7798             ReplaceClassMembers(exp, thisClass);
7799             if(exp.type != identifierExp)
7800             {
7801                ProcessExpressionType(exp);
7802                break;
7803             }
7804
7805             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7806                break;
7807          }
7808          else
7809          {
7810             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7811             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7812             if(!symbol/* && exp.destType*/)
7813             {
7814                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
7815                   break;
7816                else
7817                {
7818                   if(thisClass)
7819                   {
7820                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7821                      if(exp.type != identifierExp)
7822                      {
7823                         ProcessExpressionType(exp);
7824                         break;
7825                      }
7826                   }
7827                   // Static methods called from inside the _class
7828                   else if(currentClass && !id._class)
7829                   {
7830                      if(ResolveIdWithClass(exp, currentClass, true))
7831                         break;
7832                   }
7833                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7834                }
7835             }
7836
7837             // If we manage to resolve this symbol
7838             if(symbol)
7839             {
7840                Type type = symbol.type;
7841                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7842
7843                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7844                {
7845                   Context context = SetupTemplatesContext(_class);
7846                   type = ReplaceThisClassType(_class);
7847                   FinishTemplatesContext(context);
7848                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7849                }
7850
7851                FreeSpecifier(id._class);
7852                id._class = null;
7853                delete id.string;
7854                id.string = CopyString(symbol.string);
7855
7856                id.classSym = null;
7857                exp.expType = type;
7858                if(type)
7859                   type.refCount++;
7860
7861                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7862                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7863                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7864                   // Add missing cases here... enum Classes...
7865                   exp.isConstant = true;
7866
7867                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7868                if(symbol.isParam || !strcmp(id.string, "this"))
7869                {
7870                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7871                      exp.byReference = true;
7872
7873                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7874                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7875                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7876                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7877                   {
7878                      Identifier id = exp.identifier;
7879                      exp.type = bracketsExp;
7880                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7881                   }*/
7882                }
7883
7884                if(symbol.isIterator)
7885                {
7886                   if(symbol.isIterator == 3)
7887                   {
7888                      exp.type = bracketsExp;
7889                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7890                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7891                      exp.expType = null;
7892                      ProcessExpressionType(exp);
7893                   }
7894                   else if(symbol.isIterator != 4)
7895                   {
7896                      exp.type = memberExp;
7897                      exp.member.exp = MkExpIdentifier(exp.identifier);
7898                      exp.member.exp.expType = exp.expType;
7899                      /*if(symbol.isIterator == 6)
7900                         exp.member.member = MkIdentifier("key");
7901                      else*/
7902                         exp.member.member = MkIdentifier("data");
7903                      exp.expType = null;
7904                      ProcessExpressionType(exp);
7905                   }
7906                }
7907                break;
7908             }
7909             else
7910             {
7911                DefinedExpression definedExp = null;
7912                if(thisNameSpace && !(id._class && !id._class.name))
7913                {
7914                   char name[1024];
7915                   strcpy(name, thisNameSpace);
7916                   strcat(name, "::");
7917                   strcat(name, id.string);
7918                   definedExp = eSystem_FindDefine(privateModule, name);
7919                }
7920                if(!definedExp)
7921                   definedExp = eSystem_FindDefine(privateModule, id.string);
7922                if(definedExp)
7923                {
7924                   int c;
7925                   for(c = 0; c<definedExpStackPos; c++)
7926                      if(definedExpStack[c] == definedExp)
7927                         break;
7928                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7929                   {
7930                      Location backupYylloc = yylloc;
7931                      File backInput = fileInput;
7932                      definedExpStack[definedExpStackPos++] = definedExp;
7933
7934                      fileInput = TempFile { };
7935                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7936                      fileInput.Seek(0, start);
7937
7938                      echoOn = false;
7939                      parsedExpression = null;
7940                      resetScanner();
7941                      expression_yyparse();
7942                      delete fileInput;
7943                      if(backInput)
7944                         fileInput = backInput;
7945
7946                      yylloc = backupYylloc;
7947
7948                      if(parsedExpression)
7949                      {
7950                         FreeIdentifier(id);
7951                         exp.type = bracketsExp;
7952                         exp.list = MkListOne(parsedExpression);
7953                         ApplyLocation(parsedExpression, yylloc);
7954                         ProcessExpressionType(exp);
7955                         definedExpStackPos--;
7956                         return;
7957                      }
7958                      definedExpStackPos--;
7959                   }
7960                   else
7961                   {
7962                      if(inCompiler)
7963                      {
7964                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7965                      }
7966                   }
7967                }
7968                else
7969                {
7970                   GlobalData data = null;
7971                   if(thisNameSpace && !(id._class && !id._class.name))
7972                   {
7973                      char name[1024];
7974                      strcpy(name, thisNameSpace);
7975                      strcat(name, "::");
7976                      strcat(name, id.string);
7977                      data = FindGlobalData(name);
7978                   }
7979                   if(!data)
7980                      data = FindGlobalData(id.string);
7981                   if(data)
7982                   {
7983                      DeclareGlobalData(data);
7984                      exp.expType = data.dataType;
7985                      if(data.dataType) data.dataType.refCount++;
7986
7987                      delete id.string;
7988                      id.string = CopyString(data.fullName);
7989                      FreeSpecifier(id._class);
7990                      id._class = null;
7991
7992                      break;
7993                   }
7994                   else
7995                   {
7996                      GlobalFunction function = null;
7997                      if(thisNameSpace && !(id._class && !id._class.name))
7998                      {
7999                         char name[1024];
8000                         strcpy(name, thisNameSpace);
8001                         strcat(name, "::");
8002                         strcat(name, id.string);
8003                         function = eSystem_FindFunction(privateModule, name);
8004                      }
8005                      if(!function)
8006                         function = eSystem_FindFunction(privateModule, id.string);
8007                      if(function)
8008                      {
8009                         char name[1024];
8010                         delete id.string;
8011                         id.string = CopyString(function.name);
8012                         name[0] = 0;
8013
8014                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
8015                            strcpy(name, "__ecereFunction_");
8016                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
8017                         if(DeclareFunction(function, name))
8018                         {
8019                            delete id.string;
8020                            id.string = CopyString(name);
8021                         }
8022                         exp.expType = function.dataType;
8023                         if(function.dataType) function.dataType.refCount++;
8024
8025                         FreeSpecifier(id._class);
8026                         id._class = null;
8027
8028                         break;
8029                      }
8030                   }
8031                }
8032             }
8033          }
8034          unresolved = true;
8035          break;
8036       }
8037       case instanceExp:
8038       {
8039          Class _class;
8040          // Symbol classSym;
8041
8042          if(!exp.instance._class)
8043          {
8044             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8045             {
8046                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8047             }
8048          }
8049
8050          //classSym = FindClass(exp.instance._class.fullName);
8051          //_class = classSym ? classSym.registered : null;
8052
8053          ProcessInstantiationType(exp.instance);
8054
8055          exp.isConstant = exp.instance.isConstant;
8056
8057          /*
8058          if(_class.type == unitClass && _class.base.type != systemClass)
8059          {
8060             {
8061                Type destType = exp.destType;
8062
8063                exp.destType = MkClassType(_class.base.fullName);
8064                exp.expType = MkClassType(_class.fullName);
8065                CheckExpressionType(exp, exp.destType, true);
8066
8067                exp.destType = destType;
8068             }
8069             exp.expType = MkClassType(_class.fullName);
8070          }
8071          else*/
8072          if(exp.instance._class)
8073          {
8074             exp.expType = MkClassType(exp.instance._class.name);
8075             /*if(exp.expType._class && exp.expType._class.registered &&
8076                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8077                exp.expType.byReference = true;*/
8078          }
8079          break;
8080       }
8081       case constantExp:
8082       {
8083          if(!exp.expType)
8084          {
8085             char * constant = exp.constant;
8086             Type type
8087             {
8088                refCount = 1;
8089                constant = true;
8090             };
8091             exp.expType = type;
8092
8093             if(constant[0] == '\'')
8094             {
8095                if((int)((byte *)constant)[1] > 127)
8096                {
8097                   int nb;
8098                   unichar ch = UTF8GetChar(constant + 1, &nb);
8099                   if(nb < 2) ch = constant[1];
8100                   delete constant;
8101                   exp.constant = PrintUInt(ch);
8102                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8103                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8104                   type._class = FindClass("unichar");
8105
8106                   type.isSigned = false;
8107                }
8108                else
8109                {
8110                   type.kind = charType;
8111                   type.isSigned = true;
8112                }
8113             }
8114             else
8115             {
8116                char * dot = strchr(constant, '.');
8117                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8118                char * exponent;
8119                if(isHex)
8120                {
8121                   exponent = strchr(constant, 'p');
8122                   if(!exponent) exponent = strchr(constant, 'P');
8123                }
8124                else
8125                {
8126                   exponent = strchr(constant, 'e');
8127                   if(!exponent) exponent = strchr(constant, 'E');
8128                }
8129
8130                if(dot || exponent)
8131                {
8132                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8133                      type.kind = floatType;
8134                   else
8135                      type.kind = doubleType;
8136                   type.isSigned = true;
8137                }
8138                else
8139                {
8140                   bool isSigned = constant[0] == '-';
8141                   char * endP = null;
8142                   int64 i64 = strtoll(constant, &endP, 0);
8143                   uint64 ui64 = strtoull(constant, &endP, 0);
8144                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
8145                   if(isSigned)
8146                   {
8147                      if(i64 < MININT)
8148                         is64Bit = true;
8149                   }
8150                   else
8151                   {
8152                      if(ui64 > MAXINT)
8153                      {
8154                         if(ui64 > MAXDWORD)
8155                         {
8156                            is64Bit = true;
8157                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8158                               isSigned = true;
8159                         }
8160                      }
8161                      else if(constant[0] != '0' || !constant[1])
8162                         isSigned = true;
8163                   }
8164                   type.kind = is64Bit ? int64Type : intType;
8165                   type.isSigned = isSigned;
8166                }
8167             }
8168             exp.isConstant = true;
8169             if(exp.destType && exp.destType.kind == doubleType)
8170                type.kind = doubleType;
8171             else if(exp.destType && exp.destType.kind == floatType)
8172                type.kind = floatType;
8173             else if(exp.destType && exp.destType.kind == int64Type)
8174                type.kind = int64Type;
8175          }
8176          break;
8177       }
8178       case stringExp:
8179       {
8180          exp.isConstant = true;      // Why wasn't this constant?
8181          exp.expType = Type
8182          {
8183             refCount = 1;
8184             kind = pointerType;
8185             type = Type
8186             {
8187                refCount = 1;
8188                kind = charType;
8189                constant = true;
8190                isSigned = true;
8191             }
8192          };
8193          break;
8194       }
8195       case newExp:
8196       case new0Exp:
8197          ProcessExpressionType(exp._new.size);
8198          exp.expType = Type
8199          {
8200             refCount = 1;
8201             kind = pointerType;
8202             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8203          };
8204          DeclareType(exp.expType.type, false, false);
8205          break;
8206       case renewExp:
8207       case renew0Exp:
8208          ProcessExpressionType(exp._renew.size);
8209          ProcessExpressionType(exp._renew.exp);
8210          exp.expType = Type
8211          {
8212             refCount = 1;
8213             kind = pointerType;
8214             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8215          };
8216          DeclareType(exp.expType.type, false, false);
8217          break;
8218       case opExp:
8219       {
8220          bool assign = false, boolResult = false, boolOps = false;
8221          Type type1 = null, type2 = null;
8222          bool useDestType = false, useSideType = false;
8223          Location oldyylloc = yylloc;
8224          bool useSideUnit = false;
8225          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8226
8227          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8228          Type dummy
8229          {
8230             count = 1;
8231             refCount = 1;
8232          };
8233
8234          switch(exp.op.op)
8235          {
8236             // Assignment Operators
8237             case '=':
8238             case MUL_ASSIGN:
8239             case DIV_ASSIGN:
8240             case MOD_ASSIGN:
8241             case ADD_ASSIGN:
8242             case SUB_ASSIGN:
8243             case LEFT_ASSIGN:
8244             case RIGHT_ASSIGN:
8245             case AND_ASSIGN:
8246             case XOR_ASSIGN:
8247             case OR_ASSIGN:
8248                assign = true;
8249                break;
8250             // boolean Operators
8251             case '!':
8252                // Expect boolean operators
8253                //boolOps = true;
8254                //boolResult = true;
8255                break;
8256             case AND_OP:
8257             case OR_OP:
8258                // Expect boolean operands
8259                boolOps = true;
8260                boolResult = true;
8261                break;
8262             // Comparisons
8263             case EQ_OP:
8264             case '<':
8265             case '>':
8266             case LE_OP:
8267             case GE_OP:
8268             case NE_OP:
8269                // Gives boolean result
8270                boolResult = true;
8271                useSideType = true;
8272                break;
8273             case '+':
8274             case '-':
8275                useSideUnit = true;
8276                useSideType = true;
8277                useDestType = true;
8278                break;
8279
8280             case LEFT_OP:
8281             case RIGHT_OP:
8282                useSideType = true;
8283                useDestType = true;
8284                break;
8285
8286             case '|':
8287             case '^':
8288                useSideType = true;
8289                useDestType = true;
8290                break;
8291
8292             case '/':
8293             case '%':
8294                useSideType = true;
8295                useDestType = true;
8296                break;
8297             case '&':
8298             case '*':
8299                if(exp.op.exp1)
8300                {
8301                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8302                   useSideType = true;
8303                   useDestType = true;
8304                }
8305                break;
8306
8307             /*// Implement speed etc.
8308             case '*':
8309             case '/':
8310                break;
8311             */
8312          }
8313          if(exp.op.op == '&')
8314          {
8315             // Added this here earlier for Iterator address as key
8316             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8317             {
8318                Identifier id = exp.op.exp2.identifier;
8319                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8320                if(symbol && symbol.isIterator == 2)
8321                {
8322                   exp.type = memberExp;
8323                   exp.member.exp = exp.op.exp2;
8324                   exp.member.member = MkIdentifier("key");
8325                   exp.expType = null;
8326                   exp.op.exp2.expType = symbol.type;
8327                   symbol.type.refCount++;
8328                   ProcessExpressionType(exp);
8329                   FreeType(dummy);
8330                   break;
8331                }
8332                // exp.op.exp2.usage.usageRef = true;
8333             }
8334          }
8335
8336          //dummy.kind = TypeDummy;
8337          if(exp.op.exp1)
8338          {
8339             // Added this check here to use the dest type only for units derived from the base unit
8340             // So that untyped units will use the side unit as opposed to the untyped destination unit
8341             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8342             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8343                useDestType = false;
8344
8345             if(destClass && useDestType &&
8346               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8347
8348               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8349             {
8350                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8351                exp.op.exp1.destType = exp.destType;
8352                exp.op.exp1.opDestType = true;
8353                if(exp.destType)
8354                   exp.destType.refCount++;
8355             }
8356             else if(!assign)
8357             {
8358                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8359                exp.op.exp1.destType = dummy;
8360                dummy.refCount++;
8361             }
8362
8363             // TESTING THIS HERE...
8364             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8365                ProcessExpressionType(exp.op.exp1);
8366             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8367
8368             exp.op.exp1.opDestType = false;
8369
8370             // Fix for unit and ++ / --
8371             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8372                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8373             {
8374                exp.op.exp2 = MkExpConstant("1");
8375                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8376                assign = true;
8377             }
8378
8379             if(exp.op.exp1.destType == dummy)
8380             {
8381                FreeType(dummy);
8382                exp.op.exp1.destType = null;
8383             }
8384             type1 = exp.op.exp1.expType;
8385          }
8386
8387          if(exp.op.exp2)
8388          {
8389             char expString[10240];
8390             expString[0] = '\0';
8391             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8392             {
8393                if(exp.op.exp1)
8394                {
8395                   exp.op.exp2.destType = exp.op.exp1.expType;
8396                   if(exp.op.exp1.expType)
8397                      exp.op.exp1.expType.refCount++;
8398                }
8399                else
8400                {
8401                   exp.op.exp2.destType = exp.destType;
8402                   if(!exp.op.exp1 || exp.op.op != '&')
8403                      exp.op.exp2.opDestType = true;
8404                   if(exp.destType)
8405                      exp.destType.refCount++;
8406                }
8407
8408                if(type1) type1.refCount++;
8409                exp.expType = type1;
8410             }
8411             else if(assign)
8412             {
8413                if(inCompiler)
8414                   PrintExpression(exp.op.exp2, expString);
8415
8416                if(type1 && type1.kind == pointerType)
8417                {
8418                   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 ||
8419                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8420                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8421                   else if(exp.op.op == '=')
8422                   {
8423                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8424                      exp.op.exp2.destType = type1;
8425                      if(type1)
8426                         type1.refCount++;
8427                   }
8428                }
8429                else
8430                {
8431                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8432                   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/* ||
8433                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8434                   else
8435                   {
8436                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8437                      exp.op.exp2.destType = type1;
8438                      if(type1)
8439                         type1.refCount++;
8440                   }
8441                }
8442                if(type1) type1.refCount++;
8443                exp.expType = type1;
8444             }
8445             else if(destClass &&
8446                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8447                   (destClass.type == enumClass && useDestType)))
8448             {
8449                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8450                exp.op.exp2.destType = exp.destType;
8451                if(exp.op.op != '&')
8452                   exp.op.exp2.opDestType = true;
8453                if(exp.destType)
8454                   exp.destType.refCount++;
8455             }
8456             else
8457             {
8458                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8459                exp.op.exp2.destType = dummy;
8460                dummy.refCount++;
8461             }
8462
8463             // TESTING THIS HERE... (DANGEROUS)
8464             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8465                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8466             {
8467                FreeType(exp.op.exp2.destType);
8468                exp.op.exp2.destType = type1;
8469                type1.refCount++;
8470             }
8471             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8472             // Cannot lose the cast on a sizeof
8473             if(exp.op.op == SIZEOF)
8474             {
8475                Expression e = exp.op.exp2;
8476                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8477                {
8478                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8479                   {
8480                      if(e.type == extensionCompoundExp)
8481                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8482                      else
8483                         e = e.list->last;
8484                   }
8485                }
8486                if(e.type == castExp && e.cast.exp)
8487                   e.cast.exp.needCast = true;
8488             }
8489             ProcessExpressionType(exp.op.exp2);
8490             exp.op.exp2.opDestType = false;
8491             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8492
8493             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8494             {
8495                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)
8496                {
8497                   if(exp.op.op != '=' && type1.type.kind == voidType)
8498                      Compiler_Error($"void *: unknown size\n");
8499                }
8500                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||
8501                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8502                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8503                               exp.op.exp2.expType._class.registered.type == structClass ||
8504                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8505                {
8506                   if(exp.op.op == ADD_ASSIGN)
8507                      Compiler_Error($"cannot add two pointers\n");
8508                }
8509                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8510                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8511                {
8512                   if(exp.op.op == ADD_ASSIGN)
8513                      Compiler_Error($"cannot add two pointers\n");
8514                }
8515                else if(inCompiler)
8516                {
8517                   char type1String[1024];
8518                   char type2String[1024];
8519                   type1String[0] = '\0';
8520                   type2String[0] = '\0';
8521
8522                   PrintType(exp.op.exp2.expType, type1String, false, true);
8523                   PrintType(type1, type2String, false, true);
8524                   ChangeCh(expString, '\n', ' ');
8525                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8526                }
8527             }
8528
8529             if(exp.op.exp2.destType == dummy)
8530             {
8531                FreeType(dummy);
8532                exp.op.exp2.destType = null;
8533             }
8534
8535             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8536             {
8537                type2 = { };
8538                type2.refCount = 1;
8539                CopyTypeInto(type2, exp.op.exp2.expType);
8540                type2.isSigned = true;
8541             }
8542             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8543             {
8544                type2 = { kind = intType };
8545                type2.refCount = 1;
8546                type2.isSigned = true;
8547             }
8548             else
8549             {
8550                type2 = exp.op.exp2.expType;
8551                if(type2) type2.refCount++;
8552             }
8553          }
8554
8555          dummy.kind = voidType;
8556
8557          if(exp.op.op == SIZEOF)
8558          {
8559             exp.expType = Type
8560             {
8561                refCount = 1;
8562                kind = intSizeType;
8563             };
8564             exp.isConstant = true;
8565          }
8566          // Get type of dereferenced pointer
8567          else if(exp.op.op == '*' && !exp.op.exp1)
8568          {
8569             exp.expType = Dereference(type2);
8570             if(type2 && type2.kind == classType)
8571                notByReference = true;
8572          }
8573          else if(exp.op.op == '&' && !exp.op.exp1)
8574             exp.expType = Reference(type2);
8575          else if(!assign)
8576          {
8577             if(boolOps)
8578             {
8579                if(exp.op.exp1)
8580                {
8581                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8582                   exp.op.exp1.destType = MkClassType("bool");
8583                   exp.op.exp1.destType.truth = true;
8584                   if(!exp.op.exp1.expType)
8585                      ProcessExpressionType(exp.op.exp1);
8586                   else
8587                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8588                   FreeType(exp.op.exp1.expType);
8589                   exp.op.exp1.expType = MkClassType("bool");
8590                   exp.op.exp1.expType.truth = true;
8591                }
8592                if(exp.op.exp2)
8593                {
8594                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8595                   exp.op.exp2.destType = MkClassType("bool");
8596                   exp.op.exp2.destType.truth = true;
8597                   if(!exp.op.exp2.expType)
8598                      ProcessExpressionType(exp.op.exp2);
8599                   else
8600                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8601                   FreeType(exp.op.exp2.expType);
8602                   exp.op.exp2.expType = MkClassType("bool");
8603                   exp.op.exp2.expType.truth = true;
8604                }
8605             }
8606             else if(exp.op.exp1 && exp.op.exp2 &&
8607                ((useSideType /*&&
8608                      (useSideUnit ||
8609                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8610                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8611                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8612                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8613             {
8614                if(type1 && type2 &&
8615                   // If either both are class or both are not class
8616                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8617                {
8618                   // Added this check for enum subtraction to result in an int type:
8619                   if(exp.op.op == '-' &&
8620                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8621                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8622                   {
8623                      Type intType;
8624                      if(!type1._class.registered.dataType)
8625                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8626                      if(!type2._class.registered.dataType)
8627                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8628
8629                      intType = ProcessTypeString(
8630                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8631
8632                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8633                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8634                      exp.op.exp1.destType = intType;
8635                      exp.op.exp2.destType = intType;
8636                      intType.refCount++;
8637                   }
8638                   else
8639                   {
8640                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8641                      exp.op.exp2.destType = type1;
8642                      type1.refCount++;
8643                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8644                      exp.op.exp1.destType = type2;
8645                      type2.refCount++;
8646                   }
8647
8648                   // Warning here for adding Radians + Degrees with no destination type
8649                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8650                      type1._class.registered && type1._class.registered.type == unitClass &&
8651                      type2._class.registered && type2._class.registered.type == unitClass &&
8652                      type1._class.registered != type2._class.registered)
8653                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8654                         type1._class.string, type2._class.string, type1._class.string);
8655
8656                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8657                   {
8658                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8659                      if(argExp)
8660                      {
8661                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8662
8663                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8664                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8665                            exp.op.exp1)));
8666
8667                         ProcessExpressionType(exp.op.exp1);
8668
8669                         if(type2.kind != pointerType)
8670                         {
8671                            ProcessExpressionType(classExp);
8672
8673                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8674
8675                            if(!exp.op.exp2.expType)
8676                            {
8677                               if(type2)
8678                                  FreeType(type2);
8679                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8680                               type2.refCount++;
8681                            }
8682
8683                            ProcessExpressionType(exp.op.exp2);
8684                         }
8685                      }
8686                   }
8687
8688                   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)))
8689                   {
8690                      if(type1.kind != classType && type1.type.kind == voidType)
8691                         Compiler_Error($"void *: unknown size\n");
8692                      exp.expType = type1;
8693                      if(type1) type1.refCount++;
8694                   }
8695                   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)))
8696                   {
8697                      if(type2.kind != classType && type2.type.kind == voidType)
8698                         Compiler_Error($"void *: unknown size\n");
8699                      exp.expType = type2;
8700                      if(type2) type2.refCount++;
8701                   }
8702                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8703                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8704                   {
8705                      Compiler_Warning($"different levels of indirection\n");
8706                   }
8707                   else
8708                   {
8709                      bool success = false;
8710                      if(type1.kind == pointerType && type2.kind == pointerType)
8711                      {
8712                         if(exp.op.op == '+')
8713                            Compiler_Error($"cannot add two pointers\n");
8714                         else if(exp.op.op == '-')
8715                         {
8716                            // Pointer Subtraction gives integer
8717                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8718                            {
8719                               exp.expType = Type
8720                               {
8721                                  kind = intType;
8722                                  refCount = 1;
8723                               };
8724                               success = true;
8725
8726                               if(type1.type.kind == templateType)
8727                               {
8728                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8729                                  if(argExp)
8730                                  {
8731                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8732
8733                                     ProcessExpressionType(classExp);
8734
8735                                     exp.type = bracketsExp;
8736                                     exp.list = MkListOne(MkExpOp(
8737                                        MkExpBrackets(MkListOne(MkExpOp(
8738                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8739                                              , exp.op.op,
8740                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8741                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8742
8743                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8744                                     FreeType(dummy);
8745                                     return;
8746                                  }
8747                               }
8748                            }
8749                         }
8750                      }
8751
8752                      if(!success && exp.op.exp1.type == constantExp)
8753                      {
8754                         // If first expression is constant, try to match that first
8755                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8756                         {
8757                            if(exp.expType) FreeType(exp.expType);
8758                            exp.expType = exp.op.exp1.destType;
8759                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8760                            success = true;
8761                         }
8762                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8763                         {
8764                            if(exp.expType) FreeType(exp.expType);
8765                            exp.expType = exp.op.exp2.destType;
8766                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8767                            success = true;
8768                         }
8769                      }
8770                      else if(!success)
8771                      {
8772                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8773                         {
8774                            if(exp.expType) FreeType(exp.expType);
8775                            exp.expType = exp.op.exp2.destType;
8776                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8777                            success = true;
8778                         }
8779                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8780                         {
8781                            if(exp.expType) FreeType(exp.expType);
8782                            exp.expType = exp.op.exp1.destType;
8783                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8784                            success = true;
8785                         }
8786                      }
8787                      if(!success)
8788                      {
8789                         char expString1[10240];
8790                         char expString2[10240];
8791                         char type1[1024];
8792                         char type2[1024];
8793                         expString1[0] = '\0';
8794                         expString2[0] = '\0';
8795                         type1[0] = '\0';
8796                         type2[0] = '\0';
8797                         if(inCompiler)
8798                         {
8799                            PrintExpression(exp.op.exp1, expString1);
8800                            ChangeCh(expString1, '\n', ' ');
8801                            PrintExpression(exp.op.exp2, expString2);
8802                            ChangeCh(expString2, '\n', ' ');
8803                            PrintType(exp.op.exp1.expType, type1, false, true);
8804                            PrintType(exp.op.exp2.expType, type2, false, true);
8805                         }
8806
8807                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8808                      }
8809                   }
8810                }
8811                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8812                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8813                {
8814                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8815                   // Convert e.g. / 4 into / 4.0
8816                   exp.op.exp1.destType = type2._class.registered.dataType;
8817                   if(type2._class.registered.dataType)
8818                      type2._class.registered.dataType.refCount++;
8819                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8820                   exp.expType = type2;
8821                   if(type2) type2.refCount++;
8822                }
8823                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8824                {
8825                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8826                   // Convert e.g. / 4 into / 4.0
8827                   exp.op.exp2.destType = type1._class.registered.dataType;
8828                   if(type1._class.registered.dataType)
8829                      type1._class.registered.dataType.refCount++;
8830                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8831                   exp.expType = type1;
8832                   if(type1) type1.refCount++;
8833                }
8834                else if(type1)
8835                {
8836                   bool valid = false;
8837
8838                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8839                   {
8840                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8841
8842                      if(!type1._class.registered.dataType)
8843                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8844                      exp.op.exp2.destType = type1._class.registered.dataType;
8845                      exp.op.exp2.destType.refCount++;
8846
8847                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8848                      if(type2)
8849                         FreeType(type2);
8850                      type2 = exp.op.exp2.destType;
8851                      if(type2) type2.refCount++;
8852
8853                      exp.expType = type2;
8854                      type2.refCount++;
8855                   }
8856
8857                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8858                   {
8859                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8860
8861                      if(!type2._class.registered.dataType)
8862                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8863                      exp.op.exp1.destType = type2._class.registered.dataType;
8864                      exp.op.exp1.destType.refCount++;
8865
8866                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8867                      type1 = exp.op.exp1.destType;
8868                      exp.expType = type1;
8869                      type1.refCount++;
8870                   }
8871
8872                   // TESTING THIS NEW CODE
8873                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8874                   {
8875                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8876                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8877                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8878                      {
8879                         // Convert the enum to an int instead for these operators
8880                         if(op1IsEnum && exp.op.exp2.expType)
8881                         {
8882                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8883                            {
8884                               if(exp.expType) FreeType(exp.expType);
8885                               exp.expType = exp.op.exp2.expType;
8886                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8887                               valid = true;
8888                            }
8889                         }
8890                         else if(op2IsEnum && exp.op.exp1.expType)
8891                         {
8892                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8893                            {
8894                               if(exp.expType) FreeType(exp.expType);
8895                               exp.expType = exp.op.exp1.expType;
8896                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8897                               valid = true;
8898                            }
8899                         }
8900                      }
8901                      else
8902                      {
8903                         if(op1IsEnum && exp.op.exp2.expType)
8904                         {
8905                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8906                            {
8907                               if(exp.expType) FreeType(exp.expType);
8908                               exp.expType = exp.op.exp1.expType;
8909                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8910                               valid = true;
8911                            }
8912                         }
8913                         else if(op2IsEnum && exp.op.exp1.expType)
8914                         {
8915                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8916                            {
8917                               if(exp.expType) FreeType(exp.expType);
8918                               exp.expType = exp.op.exp2.expType;
8919                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8920                               valid = true;
8921                            }
8922                         }
8923                      }
8924                   }
8925
8926                   if(!valid)
8927                   {
8928                      // 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
8929                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
8930                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
8931                      {
8932                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8933                         exp.op.exp1.destType = type2;
8934                         type2.refCount++;
8935
8936                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8937                         {
8938                            if(exp.expType) FreeType(exp.expType);
8939                            exp.expType = exp.op.exp1.destType;
8940                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8941                         }
8942                      }
8943                      else
8944                      {
8945                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8946                         exp.op.exp2.destType = type1;
8947                         type1.refCount++;
8948
8949                      /*
8950                      // Maybe this was meant to be an enum...
8951                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8952                      {
8953                         Type oldType = exp.op.exp2.expType;
8954                         exp.op.exp2.expType = null;
8955                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8956                            FreeType(oldType);
8957                         else
8958                            exp.op.exp2.expType = oldType;
8959                      }
8960                      */
8961
8962                      /*
8963                      // TESTING THIS HERE... LATEST ADDITION
8964                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8965                      {
8966                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8967                         exp.op.exp2.destType = type2._class.registered.dataType;
8968                         if(type2._class.registered.dataType)
8969                            type2._class.registered.dataType.refCount++;
8970                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8971
8972                         //exp.expType = type2._class.registered.dataType; //type2;
8973                         //if(type2) type2.refCount++;
8974                      }
8975
8976                      // TESTING THIS HERE... LATEST ADDITION
8977                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8978                      {
8979                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8980                         exp.op.exp1.destType = type1._class.registered.dataType;
8981                         if(type1._class.registered.dataType)
8982                            type1._class.registered.dataType.refCount++;
8983                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8984                         exp.expType = type1._class.registered.dataType; //type1;
8985                         if(type1) type1.refCount++;
8986                      }
8987                      */
8988
8989                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8990                         {
8991                            if(exp.expType) FreeType(exp.expType);
8992                            exp.expType = exp.op.exp2.destType;
8993                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8994                         }
8995                         else if(type1 && type2)
8996                         {
8997                            char expString1[10240];
8998                            char expString2[10240];
8999                            char type1String[1024];
9000                            char type2String[1024];
9001                            expString1[0] = '\0';
9002                            expString2[0] = '\0';
9003                            type1String[0] = '\0';
9004                            type2String[0] = '\0';
9005                            if(inCompiler)
9006                            {
9007                               PrintExpression(exp.op.exp1, expString1);
9008                               ChangeCh(expString1, '\n', ' ');
9009                               PrintExpression(exp.op.exp2, expString2);
9010                               ChangeCh(expString2, '\n', ' ');
9011                               PrintType(exp.op.exp1.expType, type1String, false, true);
9012                               PrintType(exp.op.exp2.expType, type2String, false, true);
9013                            }
9014
9015                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
9016
9017                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9018                            {
9019                               exp.expType = exp.op.exp1.expType;
9020                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9021                            }
9022                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9023                            {
9024                               exp.expType = exp.op.exp2.expType;
9025                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9026                            }
9027                         }
9028                      }
9029                   }
9030                }
9031                else if(type2)
9032                {
9033                   // Maybe this was meant to be an enum...
9034                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9035                   {
9036                      Type oldType = exp.op.exp1.expType;
9037                      exp.op.exp1.expType = null;
9038                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9039                         FreeType(oldType);
9040                      else
9041                         exp.op.exp1.expType = oldType;
9042                   }
9043
9044                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9045                   exp.op.exp1.destType = type2;
9046                   type2.refCount++;
9047                   /*
9048                   // TESTING THIS HERE... LATEST ADDITION
9049                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9050                   {
9051                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9052                      exp.op.exp1.destType = type1._class.registered.dataType;
9053                      if(type1._class.registered.dataType)
9054                         type1._class.registered.dataType.refCount++;
9055                   }
9056
9057                   // TESTING THIS HERE... LATEST ADDITION
9058                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9059                   {
9060                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9061                      exp.op.exp2.destType = type2._class.registered.dataType;
9062                      if(type2._class.registered.dataType)
9063                         type2._class.registered.dataType.refCount++;
9064                   }
9065                   */
9066
9067                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9068                   {
9069                      if(exp.expType) FreeType(exp.expType);
9070                      exp.expType = exp.op.exp1.destType;
9071                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9072                   }
9073                }
9074             }
9075             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9076             {
9077                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9078                {
9079                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9080                   // Convert e.g. / 4 into / 4.0
9081                   exp.op.exp1.destType = type2._class.registered.dataType;
9082                   if(type2._class.registered.dataType)
9083                      type2._class.registered.dataType.refCount++;
9084                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9085                }
9086                if(exp.op.op == '!')
9087                {
9088                   exp.expType = MkClassType("bool");
9089                   exp.expType.truth = true;
9090                }
9091                else
9092                {
9093                   exp.expType = type2;
9094                   if(type2) type2.refCount++;
9095                }
9096             }
9097             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9098             {
9099                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9100                {
9101                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9102                   // Convert e.g. / 4 into / 4.0
9103                   exp.op.exp2.destType = type1._class.registered.dataType;
9104                   if(type1._class.registered.dataType)
9105                      type1._class.registered.dataType.refCount++;
9106                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9107                }
9108                exp.expType = type1;
9109                if(type1) type1.refCount++;
9110             }
9111          }
9112
9113          yylloc = exp.loc;
9114          if(exp.op.exp1 && !exp.op.exp1.expType)
9115          {
9116             char expString[10000];
9117             expString[0] = '\0';
9118             if(inCompiler)
9119             {
9120                PrintExpression(exp.op.exp1, expString);
9121                ChangeCh(expString, '\n', ' ');
9122             }
9123             if(expString[0])
9124                Compiler_Error($"couldn't determine type of %s\n", expString);
9125          }
9126          if(exp.op.exp2 && !exp.op.exp2.expType)
9127          {
9128             char expString[10240];
9129             expString[0] = '\0';
9130             if(inCompiler)
9131             {
9132                PrintExpression(exp.op.exp2, expString);
9133                ChangeCh(expString, '\n', ' ');
9134             }
9135             if(expString[0])
9136                Compiler_Error($"couldn't determine type of %s\n", expString);
9137          }
9138
9139          if(boolResult)
9140          {
9141             FreeType(exp.expType);
9142             exp.expType = MkClassType("bool");
9143             exp.expType.truth = true;
9144          }
9145
9146          if(exp.op.op != SIZEOF)
9147             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9148                (!exp.op.exp2 || exp.op.exp2.isConstant);
9149
9150          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9151          {
9152             DeclareType(exp.op.exp2.expType, false, false);
9153          }
9154
9155          yylloc = oldyylloc;
9156
9157          FreeType(dummy);
9158          if(type2)
9159             FreeType(type2);
9160          break;
9161       }
9162       case bracketsExp:
9163       case extensionExpressionExp:
9164       {
9165          Expression e;
9166          exp.isConstant = true;
9167          for(e = exp.list->first; e; e = e.next)
9168          {
9169             bool inced = false;
9170             if(!e.next)
9171             {
9172                FreeType(e.destType);
9173                e.opDestType = exp.opDestType;
9174                e.destType = exp.destType;
9175                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
9176             }
9177             ProcessExpressionType(e);
9178             if(inced)
9179                exp.destType.count--;
9180             if(!exp.expType && !e.next)
9181             {
9182                exp.expType = e.expType;
9183                if(e.expType) e.expType.refCount++;
9184             }
9185             if(!e.isConstant)
9186                exp.isConstant = false;
9187          }
9188
9189          // In case a cast became a member...
9190          e = exp.list->first;
9191          if(!e.next && e.type == memberExp)
9192          {
9193             // Preserve prev, next
9194             Expression next = exp.next, prev = exp.prev;
9195
9196
9197             FreeType(exp.expType);
9198             FreeType(exp.destType);
9199             delete exp.list;
9200
9201             *exp = *e;
9202
9203             exp.prev = prev;
9204             exp.next = next;
9205
9206             delete e;
9207
9208             ProcessExpressionType(exp);
9209          }
9210          break;
9211       }
9212       case indexExp:
9213       {
9214          Expression e;
9215          exp.isConstant = true;
9216
9217          ProcessExpressionType(exp.index.exp);
9218          if(!exp.index.exp.isConstant)
9219             exp.isConstant = false;
9220
9221          if(exp.index.exp.expType)
9222          {
9223             Type source = exp.index.exp.expType;
9224             if(source.kind == classType && source._class && source._class.registered)
9225             {
9226                Class _class = source._class.registered;
9227                Class c = _class.templateClass ? _class.templateClass : _class;
9228                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9229                {
9230                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9231
9232                   if(exp.index.index && exp.index.index->last)
9233                   {
9234                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9235
9236                      if(type.kind == classType) type.constant = true;
9237                      else if(type.kind == pointerType)
9238                      {
9239                         Type t = type;
9240                         while(t.kind == pointerType) t = t.type;
9241                         t.constant = true;
9242                      }
9243
9244                      ((Expression)exp.index.index->last).destType = type;
9245                   }
9246                }
9247             }
9248          }
9249
9250          for(e = exp.index.index->first; e; e = e.next)
9251          {
9252             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9253             {
9254                if(e.destType) FreeType(e.destType);
9255                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9256             }
9257             ProcessExpressionType(e);
9258             if(!e.next)
9259             {
9260                // Check if this type is int
9261             }
9262             if(!e.isConstant)
9263                exp.isConstant = false;
9264          }
9265
9266          if(!exp.expType)
9267             exp.expType = Dereference(exp.index.exp.expType);
9268          if(exp.expType)
9269             DeclareType(exp.expType, false, false);
9270          break;
9271       }
9272       case callExp:
9273       {
9274          Expression e;
9275          Type functionType;
9276          Type methodType = null;
9277          char name[1024];
9278          name[0] = '\0';
9279
9280          if(inCompiler)
9281          {
9282             PrintExpression(exp.call.exp,  name);
9283             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9284             {
9285                //exp.call.exp.expType = null;
9286                PrintExpression(exp.call.exp,  name);
9287             }
9288          }
9289          if(exp.call.exp.type == identifierExp)
9290          {
9291             Expression idExp = exp.call.exp;
9292             Identifier id = idExp.identifier;
9293             if(!strcmp(id.string, "__builtin_frame_address"))
9294             {
9295                exp.expType = ProcessTypeString("void *", 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, "__ENDIAN_PAD"))
9301             {
9302                exp.expType = ProcessTypeString("int", true);
9303                if(exp.call.arguments && exp.call.arguments->first)
9304                   ProcessExpressionType(exp.call.arguments->first);
9305                break;
9306             }
9307             else if(!strcmp(id.string, "Max") ||
9308                !strcmp(id.string, "Min") ||
9309                !strcmp(id.string, "Sgn") ||
9310                !strcmp(id.string, "Abs"))
9311             {
9312                Expression a = null;
9313                Expression b = null;
9314                Expression tempExp1 = null, tempExp2 = null;
9315                if((!strcmp(id.string, "Max") ||
9316                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9317                {
9318                   a = exp.call.arguments->first;
9319                   b = exp.call.arguments->last;
9320                   tempExp1 = a;
9321                   tempExp2 = b;
9322                }
9323                else if(exp.call.arguments->count == 1)
9324                {
9325                   a = exp.call.arguments->first;
9326                   tempExp1 = a;
9327                }
9328
9329                if(a)
9330                {
9331                   exp.call.arguments->Clear();
9332                   idExp.identifier = null;
9333
9334                   FreeExpContents(exp);
9335
9336                   ProcessExpressionType(a);
9337                   if(b)
9338                      ProcessExpressionType(b);
9339
9340                   exp.type = bracketsExp;
9341                   exp.list = MkList();
9342
9343                   if(a.expType && (!b || b.expType))
9344                   {
9345                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9346                      {
9347                         // Use the simpleStruct name/ids for now...
9348                         if(inCompiler)
9349                         {
9350                            OldList * specs = MkList();
9351                            OldList * decls = MkList();
9352                            Declaration decl;
9353                            char temp1[1024], temp2[1024];
9354
9355                            GetTypeSpecs(a.expType, specs);
9356
9357                            if(a && !a.isConstant && a.type != identifierExp)
9358                            {
9359                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9360                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9361                               tempExp1 = QMkExpId(temp1);
9362                               tempExp1.expType = a.expType;
9363                               if(a.expType)
9364                                  a.expType.refCount++;
9365                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9366                            }
9367                            if(b && !b.isConstant && b.type != identifierExp)
9368                            {
9369                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9370                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9371                               tempExp2 = QMkExpId(temp2);
9372                               tempExp2.expType = b.expType;
9373                               if(b.expType)
9374                                  b.expType.refCount++;
9375                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9376                            }
9377
9378                            decl = MkDeclaration(specs, decls);
9379                            if(!curCompound.compound.declarations)
9380                               curCompound.compound.declarations = MkList();
9381                            curCompound.compound.declarations->Insert(null, decl);
9382                         }
9383                      }
9384                   }
9385
9386                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9387                   {
9388                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9389                      ListAdd(exp.list,
9390                         MkExpCondition(MkExpBrackets(MkListOne(
9391                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9392                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9393                      exp.expType = a.expType;
9394                      if(a.expType)
9395                         a.expType.refCount++;
9396                   }
9397                   else if(!strcmp(id.string, "Abs"))
9398                   {
9399                      ListAdd(exp.list,
9400                         MkExpCondition(MkExpBrackets(MkListOne(
9401                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9402                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9403                      exp.expType = a.expType;
9404                      if(a.expType)
9405                         a.expType.refCount++;
9406                   }
9407                   else if(!strcmp(id.string, "Sgn"))
9408                   {
9409                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9410                      ListAdd(exp.list,
9411                         MkExpCondition(MkExpBrackets(MkListOne(
9412                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9413                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9414                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9415                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9416                      exp.expType = ProcessTypeString("int", false);
9417                   }
9418
9419                   FreeExpression(tempExp1);
9420                   if(tempExp2) FreeExpression(tempExp2);
9421
9422                   FreeIdentifier(id);
9423                   break;
9424                }
9425             }
9426          }
9427
9428          {
9429             Type dummy
9430             {
9431                count = 1;
9432                refCount = 1;
9433             };
9434             if(!exp.call.exp.destType)
9435             {
9436                exp.call.exp.destType = dummy;
9437                dummy.refCount++;
9438             }
9439             ProcessExpressionType(exp.call.exp);
9440             if(exp.call.exp.destType == dummy)
9441             {
9442                FreeType(dummy);
9443                exp.call.exp.destType = null;
9444             }
9445             FreeType(dummy);
9446          }
9447
9448          // Check argument types against parameter types
9449          functionType = exp.call.exp.expType;
9450
9451          if(functionType && functionType.kind == TypeKind::methodType)
9452          {
9453             methodType = functionType;
9454             functionType = methodType.method.dataType;
9455
9456             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9457             // TOCHECK: Instead of doing this here could this be done per param?
9458             if(exp.call.exp.expType.usedClass)
9459             {
9460                char typeString[1024];
9461                typeString[0] = '\0';
9462                {
9463                   Symbol back = functionType.thisClass;
9464                   // Do not output class specifier here (thisclass was added to this)
9465                   functionType.thisClass = null;
9466                   PrintType(functionType, typeString, true, true);
9467                   functionType.thisClass = back;
9468                }
9469                if(strstr(typeString, "thisclass"))
9470                {
9471                   OldList * specs = MkList();
9472                   Declarator decl;
9473                   {
9474                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9475
9476                      decl = SpecDeclFromString(typeString, specs, null);
9477
9478                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9479                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9480                         exp.call.exp.expType.usedClass))
9481                         thisClassParams = false;
9482
9483                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9484                      {
9485                         Class backupThisClass = thisClass;
9486                         thisClass = exp.call.exp.expType.usedClass;
9487                         ProcessDeclarator(decl);
9488                         thisClass = backupThisClass;
9489                      }
9490
9491                      thisClassParams = true;
9492
9493                      functionType = ProcessType(specs, decl);
9494                      functionType.refCount = 0;
9495                      FinishTemplatesContext(context);
9496                   }
9497
9498                   FreeList(specs, FreeSpecifier);
9499                   FreeDeclarator(decl);
9500                 }
9501             }
9502          }
9503          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9504          {
9505             Type type = functionType.type;
9506             if(!functionType.refCount)
9507             {
9508                functionType.type = null;
9509                FreeType(functionType);
9510             }
9511             //methodType = functionType;
9512             functionType = type;
9513          }
9514          if(functionType && functionType.kind != TypeKind::functionType)
9515          {
9516             Compiler_Error($"called object %s is not a function\n", name);
9517          }
9518          else if(functionType)
9519          {
9520             bool emptyParams = false, noParams = false;
9521             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9522             Type type = functionType.params.first;
9523             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9524             int extra = 0;
9525             Location oldyylloc = yylloc;
9526
9527             if(!type) emptyParams = true;
9528
9529             // WORKING ON THIS:
9530             if(functionType.extraParam && e && functionType.thisClass)
9531             {
9532                e.destType = MkClassType(functionType.thisClass.string);
9533                e = e.next;
9534             }
9535
9536             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9537             // Fixed #141 by adding '&& !functionType.extraParam'
9538             if(!functionType.staticMethod && !functionType.extraParam)
9539             {
9540                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9541                   memberExp.member.exp.expType._class)
9542                {
9543                   type = MkClassType(memberExp.member.exp.expType._class.string);
9544                   if(e)
9545                   {
9546                      e.destType = type;
9547                      e = e.next;
9548                      type = functionType.params.first;
9549                   }
9550                   else
9551                      type.refCount = 0;
9552                }
9553                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9554                {
9555                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9556                   type.byReference = functionType.byReference;
9557                   type.typedByReference = functionType.typedByReference;
9558                   if(e)
9559                   {
9560                      // Allow manually passing a class for typed object
9561                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9562                         e = e.next;
9563                      e.destType = type;
9564                      e = e.next;
9565                      type = functionType.params.first;
9566                   }
9567                   else
9568                      type.refCount = 0;
9569                   //extra = 1;
9570                }
9571             }
9572
9573             if(type && type.kind == voidType)
9574             {
9575                noParams = true;
9576                if(!type.refCount) FreeType(type);
9577                type = null;
9578             }
9579
9580             for( ; e; e = e.next)
9581             {
9582                if(!type && !emptyParams)
9583                {
9584                   yylloc = e.loc;
9585                   if(methodType && methodType.methodClass)
9586                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9587                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9588                         noParams ? 0 : functionType.params.count);
9589                   else
9590                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9591                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9592                         noParams ? 0 : functionType.params.count);
9593                   break;
9594                }
9595
9596                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9597                {
9598                   Type templatedType = null;
9599                   Class _class = methodType.usedClass;
9600                   ClassTemplateParameter curParam = null;
9601                   int id = 0;
9602                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9603                   {
9604                      Class sClass;
9605                      for(sClass = _class; sClass; sClass = sClass.base)
9606                      {
9607                         if(sClass.templateClass) sClass = sClass.templateClass;
9608                         id = 0;
9609                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9610                         {
9611                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9612                            {
9613                               Class nextClass;
9614                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9615                               {
9616                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9617                                  id += nextClass.templateParams.count;
9618                               }
9619                               break;
9620                            }
9621                            id++;
9622                         }
9623                         if(curParam) break;
9624                      }
9625                   }
9626                   if(curParam && _class.templateArgs[id].dataTypeString)
9627                   {
9628                      bool constant = type.constant;
9629                      ClassTemplateArgument arg = _class.templateArgs[id];
9630                      {
9631                         Context context = SetupTemplatesContext(_class);
9632
9633                         /*if(!arg.dataType)
9634                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9635                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9636                         FinishTemplatesContext(context);
9637                      }
9638
9639                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9640                      else if(templatedType.kind == pointerType)
9641                      {
9642                         Type t = templatedType.type;
9643                         while(t.kind == pointerType) t = t.type;
9644                         if(constant) t.constant = constant;
9645                      }
9646
9647                      e.destType = templatedType;
9648                      if(templatedType)
9649                      {
9650                         templatedType.passAsTemplate = true;
9651                         // templatedType.refCount++;
9652                      }
9653                   }
9654                   else
9655                   {
9656                      e.destType = type;
9657                      if(type) type.refCount++;
9658                   }
9659                }
9660                else
9661                {
9662                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9663                   {
9664                      e.destType = type.prev;
9665                      e.destType.refCount++;
9666                   }
9667                   else
9668                   {
9669                      e.destType = type;
9670                      if(type) type.refCount++;
9671                   }
9672                }
9673                // Don't reach the end for the ellipsis
9674                if(type && type.kind != ellipsisType)
9675                {
9676                   Type next = type.next;
9677                   if(!type.refCount) FreeType(type);
9678                   type = next;
9679                }
9680             }
9681
9682             if(type && type.kind != ellipsisType)
9683             {
9684                if(methodType && methodType.methodClass)
9685                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9686                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9687                      functionType.params.count + extra);
9688                else
9689                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9690                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9691                      functionType.params.count + extra);
9692             }
9693             yylloc = oldyylloc;
9694             if(type && !type.refCount) FreeType(type);
9695          }
9696          else
9697          {
9698             functionType = Type
9699             {
9700                refCount = 0;
9701                kind = TypeKind::functionType;
9702             };
9703
9704             if(exp.call.exp.type == identifierExp)
9705             {
9706                char * string = exp.call.exp.identifier.string;
9707                if(inCompiler)
9708                {
9709                   Symbol symbol;
9710                   Location oldyylloc = yylloc;
9711
9712                   yylloc = exp.call.exp.identifier.loc;
9713                   if(strstr(string, "__builtin_") == string)
9714                   {
9715                      if(exp.destType)
9716                      {
9717                         functionType.returnType = exp.destType;
9718                         exp.destType.refCount++;
9719                      }
9720                   }
9721                   else
9722                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9723                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9724                   globalContext.symbols.Add((BTNode)symbol);
9725                   if(strstr(symbol.string, "::"))
9726                      globalContext.hasNameSpace = true;
9727
9728                   yylloc = oldyylloc;
9729                }
9730             }
9731             else if(exp.call.exp.type == memberExp)
9732             {
9733                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9734                   exp.call.exp.member.member.string);*/
9735             }
9736             else
9737                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9738
9739             if(!functionType.returnType)
9740             {
9741                functionType.returnType = Type
9742                {
9743                   refCount = 1;
9744                   kind = intType;
9745                };
9746             }
9747          }
9748          if(functionType && functionType.kind == TypeKind::functionType)
9749          {
9750             exp.expType = functionType.returnType;
9751
9752             if(functionType.returnType)
9753                functionType.returnType.refCount++;
9754
9755             if(!functionType.refCount)
9756                FreeType(functionType);
9757          }
9758
9759          if(exp.call.arguments)
9760          {
9761             for(e = exp.call.arguments->first; e; e = e.next)
9762             {
9763                Type destType = e.destType;
9764                ProcessExpressionType(e);
9765             }
9766          }
9767          break;
9768       }
9769       case memberExp:
9770       {
9771          Type type;
9772          Location oldyylloc = yylloc;
9773          bool thisPtr;
9774          Expression checkExp = exp.member.exp;
9775          while(checkExp)
9776          {
9777             if(checkExp.type == castExp)
9778                checkExp = checkExp.cast.exp;
9779             else if(checkExp.type == bracketsExp)
9780                checkExp = checkExp.list ? checkExp.list->first : null;
9781             else
9782                break;
9783          }
9784
9785          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9786          exp.thisPtr = thisPtr;
9787
9788          // DOING THIS LATER NOW...
9789          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9790          {
9791             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9792             /* TODO: Name Space Fix ups
9793             if(!exp.member.member.classSym)
9794                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9795             */
9796          }
9797
9798          ProcessExpressionType(exp.member.exp);
9799          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9800             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9801          {
9802             exp.isConstant = false;
9803          }
9804          else
9805             exp.isConstant = exp.member.exp.isConstant;
9806          type = exp.member.exp.expType;
9807
9808          yylloc = exp.loc;
9809
9810          if(type && (type.kind == templateType))
9811          {
9812             Class _class = thisClass ? thisClass : currentClass;
9813             ClassTemplateParameter param = null;
9814             if(_class)
9815             {
9816                for(param = _class.templateParams.first; param; param = param.next)
9817                {
9818                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9819                      break;
9820                }
9821             }
9822             if(param && param.defaultArg.member)
9823             {
9824                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9825                if(argExp)
9826                {
9827                   Expression expMember = exp.member.exp;
9828                   Declarator decl;
9829                   OldList * specs = MkList();
9830                   char thisClassTypeString[1024];
9831
9832                   FreeIdentifier(exp.member.member);
9833
9834                   ProcessExpressionType(argExp);
9835
9836                   {
9837                      char * colon = strstr(param.defaultArg.memberString, "::");
9838                      if(colon)
9839                      {
9840                         char className[1024];
9841                         Class sClass;
9842
9843                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9844                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9845                      }
9846                      else
9847                         strcpy(thisClassTypeString, _class.fullName);
9848                   }
9849
9850                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9851
9852                   exp.expType = ProcessType(specs, decl);
9853                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9854                   {
9855                      Class expClass = exp.expType._class.registered;
9856                      Class cClass = null;
9857                      int c;
9858                      int paramCount = 0;
9859                      int lastParam = -1;
9860
9861                      char templateString[1024];
9862                      ClassTemplateParameter param;
9863                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9864                      for(cClass = expClass; cClass; cClass = cClass.base)
9865                      {
9866                         int p = 0;
9867                         for(param = cClass.templateParams.first; param; param = param.next)
9868                         {
9869                            int id = p;
9870                            Class sClass;
9871                            ClassTemplateArgument arg;
9872                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9873                            arg = expClass.templateArgs[id];
9874
9875                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9876                            {
9877                               ClassTemplateParameter cParam;
9878                               //int p = numParams - sClass.templateParams.count;
9879                               int p = 0;
9880                               Class nextClass;
9881                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9882
9883                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9884                               {
9885                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9886                                  {
9887                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9888                                     {
9889                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9890                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9891                                        break;
9892                                     }
9893                                  }
9894                               }
9895                            }
9896
9897                            {
9898                               char argument[256];
9899                               argument[0] = '\0';
9900                               /*if(arg.name)
9901                               {
9902                                  strcat(argument, arg.name.string);
9903                                  strcat(argument, " = ");
9904                               }*/
9905                               switch(param.type)
9906                               {
9907                                  case expression:
9908                                  {
9909                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9910                                     char expString[1024];
9911                                     OldList * specs = MkList();
9912                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9913                                     Expression exp;
9914                                     char * string = PrintHexUInt64(arg.expression.ui64);
9915                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9916                                     delete string;
9917
9918                                     ProcessExpressionType(exp);
9919                                     ComputeExpression(exp);
9920                                     expString[0] = '\0';
9921                                     PrintExpression(exp, expString);
9922                                     strcat(argument, expString);
9923                                     // delete exp;
9924                                     FreeExpression(exp);
9925                                     break;
9926                                  }
9927                                  case identifier:
9928                                  {
9929                                     strcat(argument, arg.member.name);
9930                                     break;
9931                                  }
9932                                  case TemplateParameterType::type:
9933                                  {
9934                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9935                                     {
9936                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9937                                           strcat(argument, thisClassTypeString);
9938                                        else
9939                                           strcat(argument, arg.dataTypeString);
9940                                     }
9941                                     break;
9942                                  }
9943                               }
9944                               if(argument[0])
9945                               {
9946                                  if(paramCount) strcat(templateString, ", ");
9947                                  if(lastParam != p - 1)
9948                                  {
9949                                     strcat(templateString, param.name);
9950                                     strcat(templateString, " = ");
9951                                  }
9952                                  strcat(templateString, argument);
9953                                  paramCount++;
9954                                  lastParam = p;
9955                               }
9956                               p++;
9957                            }
9958                         }
9959                      }
9960                      {
9961                         int len = strlen(templateString);
9962                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9963                         templateString[len++] = '>';
9964                         templateString[len++] = '\0';
9965                      }
9966                      {
9967                         Context context = SetupTemplatesContext(_class);
9968                         FreeType(exp.expType);
9969                         exp.expType = ProcessTypeString(templateString, false);
9970                         FinishTemplatesContext(context);
9971                      }
9972                   }
9973
9974                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9975                   exp.type = bracketsExp;
9976                   exp.list = MkListOne(MkExpOp(null, '*',
9977                   /*opExp;
9978                   exp.op.op = '*';
9979                   exp.op.exp1 = null;
9980                   exp.op.exp2 = */
9981                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9982                      MkExpBrackets(MkListOne(
9983                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9984                            '+',
9985                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9986                            '+',
9987                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9988
9989                            ));
9990                }
9991             }
9992             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9993                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9994             {
9995                type = ProcessTemplateParameterType(type.templateParameter);
9996             }
9997          }
9998          // TODO: *** This seems to be where we should add method support for all basic types ***
9999          if(type && (type.kind == templateType));
10000          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
10001                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
10002                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
10003                           (type.kind == pointerType && type.type.kind == charType)))
10004          {
10005             Identifier id = exp.member.member;
10006             TypeKind typeKind = type.kind;
10007             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10008             if(typeKind == subClassType && exp.member.exp.type == classExp)
10009             {
10010                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
10011                typeKind = classType;
10012             }
10013
10014             if(id)
10015             {
10016                if(typeKind == intType || typeKind == enumType)
10017                   _class = eSystem_FindClass(privateModule, "int");
10018                else if(!_class)
10019                {
10020                   if(type.kind == classType && type._class && type._class.registered)
10021                   {
10022                      _class = type._class.registered;
10023                   }
10024                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10025                   {
10026                      _class = FindClass("char *").registered;
10027                   }
10028                   else if(type.kind == pointerType)
10029                   {
10030                      _class = eSystem_FindClass(privateModule, "uintptr");
10031                      FreeType(exp.expType);
10032                      exp.expType = ProcessTypeString("uintptr", false);
10033                      exp.byReference = true;
10034                   }
10035                   else
10036                   {
10037                      char string[1024] = "";
10038                      Symbol classSym;
10039                      PrintTypeNoConst(type, string, false, true);
10040                      classSym = FindClass(string);
10041                      if(classSym) _class = classSym.registered;
10042                   }
10043                }
10044             }
10045
10046             if(_class && id)
10047             {
10048                /*bool thisPtr =
10049                   (exp.member.exp.type == identifierExp &&
10050                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10051                Property prop = null;
10052                Method method = null;
10053                DataMember member = null;
10054                Property revConvert = null;
10055                ClassProperty classProp = null;
10056
10057                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10058                   exp.member.memberType = propertyMember;
10059
10060                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10061                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10062
10063                if(typeKind != subClassType)
10064                {
10065                   // Prioritize data members over properties for "this"
10066                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10067                   {
10068                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10069                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10070                      {
10071                         prop = eClass_FindProperty(_class, id.string, privateModule);
10072                         if(prop)
10073                            member = null;
10074                      }
10075                      if(!member && !prop)
10076                         prop = eClass_FindProperty(_class, id.string, privateModule);
10077                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10078                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10079                         exp.member.thisPtr = true;
10080                   }
10081                   // Prioritize properties over data members otherwise
10082                   else
10083                   {
10084                      bool useMemberForNonConst = false;
10085                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10086                      if(!id.classSym)
10087                      {
10088                         prop = eClass_FindProperty(_class, id.string, null);
10089
10090                         useMemberForNonConst = prop && exp.destType &&
10091                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10092                               !strncmp(prop.dataTypeString, "const ", 6);
10093
10094                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10095                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10096                      }
10097
10098                      if((!prop || useMemberForNonConst) && !member)
10099                      {
10100                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10101                         if(!method)
10102                         {
10103                            prop = eClass_FindProperty(_class, id.string, privateModule);
10104
10105                            useMemberForNonConst |= prop && exp.destType &&
10106                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10107                                  !strncmp(prop.dataTypeString, "const ", 6);
10108
10109                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10110                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10111                         }
10112                      }
10113
10114                      if(member && prop)
10115                      {
10116                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10117                            prop = null;
10118                         else
10119                            member = null;
10120                      }
10121                   }
10122                }
10123                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10124                   method = eClass_FindMethod(_class, id.string, privateModule);
10125                if(!prop && !member && !method)
10126                {
10127                   if(typeKind == subClassType)
10128                   {
10129                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10130                      if(classProp)
10131                      {
10132                         exp.member.memberType = classPropertyMember;
10133                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10134                      }
10135                      else
10136                      {
10137                         // Assume this is a class_data member
10138                         char structName[1024];
10139                         Identifier id = exp.member.member;
10140                         Expression classExp = exp.member.exp;
10141                         type.refCount++;
10142
10143                         FreeType(classExp.expType);
10144                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10145
10146                         strcpy(structName, "__ecereClassData_");
10147                         FullClassNameCat(structName, type._class.string, false);
10148                         exp.type = pointerExp;
10149                         exp.member.member = id;
10150
10151                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10152                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10153                               MkExpBrackets(MkListOne(MkExpOp(
10154                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10155                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10156                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10157                                  )));
10158
10159                         FreeType(type);
10160
10161                         ProcessExpressionType(exp);
10162                         return;
10163                      }
10164                   }
10165                   else
10166                   {
10167                      // Check for reverse conversion
10168                      // (Convert in an instantiation later, so that we can use
10169                      //  deep properties system)
10170                      Symbol classSym = FindClass(id.string);
10171                      if(classSym)
10172                      {
10173                         Class convertClass = classSym.registered;
10174                         if(convertClass)
10175                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10176                      }
10177                   }
10178                }
10179
10180                if(prop)
10181                {
10182                   exp.member.memberType = propertyMember;
10183                   if(!prop.dataType)
10184                      ProcessPropertyType(prop);
10185                   exp.expType = prop.dataType;
10186                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10187                   {
10188                      Type type { };
10189                      CopyTypeInto(type, exp.expType);
10190                      type.refCount = 1;
10191                      type.constant = true;
10192                      exp.expType = type;
10193                   }
10194                   else if(prop.dataType)
10195                      prop.dataType.refCount++;
10196                }
10197                else if(member)
10198                {
10199                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10200                   {
10201                      FreeExpContents(exp);
10202                      exp.type = identifierExp;
10203                      exp.identifier = MkIdentifier("class");
10204                      ProcessExpressionType(exp);
10205                      return;
10206                   }
10207
10208                   exp.member.memberType = dataMember;
10209                   DeclareStruct(_class.fullName, false);
10210                   if(!member.dataType)
10211                   {
10212                      Context context = SetupTemplatesContext(_class);
10213                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10214                      FinishTemplatesContext(context);
10215                   }
10216                   exp.expType = member.dataType;
10217                   if(member.dataType) member.dataType.refCount++;
10218                }
10219                else if(revConvert)
10220                {
10221                   exp.member.memberType = reverseConversionMember;
10222                   exp.expType = MkClassType(revConvert._class.fullName);
10223                }
10224                else if(method)
10225                {
10226                   //if(inCompiler)
10227                   {
10228                      /*if(id._class)
10229                      {
10230                         exp.type = identifierExp;
10231                         exp.identifier = exp.member.member;
10232                      }
10233                      else*/
10234                         exp.member.memberType = methodMember;
10235                   }
10236                   if(!method.dataType)
10237                      ProcessMethodType(method);
10238                   exp.expType = Type
10239                   {
10240                      refCount = 1;
10241                      kind = methodType;
10242                      method = method;
10243                   };
10244
10245                   // Tricky spot here... To use instance versus class virtual table
10246                   // Put it back to what it was... What did we break?
10247
10248                   // Had to put it back for overriding Main of Thread global instance
10249
10250                   //exp.expType.methodClass = _class;
10251                   exp.expType.methodClass = (id && id._class) ? _class : null;
10252
10253                   // Need the actual class used for templated classes
10254                   exp.expType.usedClass = _class;
10255                }
10256                else if(!classProp)
10257                {
10258                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10259                   {
10260                      FreeExpContents(exp);
10261                      exp.type = identifierExp;
10262                      exp.identifier = MkIdentifier("class");
10263                      FreeType(exp.expType);
10264                      exp.expType = MkClassType("ecere::com::Class");
10265                      return;
10266                   }
10267                   yylloc = exp.member.member.loc;
10268                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10269                   if(inCompiler)
10270                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10271                }
10272
10273                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10274                {
10275                   Class tClass;
10276
10277                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10278                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10279
10280                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10281                   {
10282                      int id = 0;
10283                      ClassTemplateParameter curParam = null;
10284                      Class sClass;
10285
10286                      for(sClass = tClass; sClass; sClass = sClass.base)
10287                      {
10288                         id = 0;
10289                         if(sClass.templateClass) sClass = sClass.templateClass;
10290                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10291                         {
10292                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10293                            {
10294                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10295                                  id += sClass.templateParams.count;
10296                               break;
10297                            }
10298                            id++;
10299                         }
10300                         if(curParam) break;
10301                      }
10302
10303                      if(curParam && tClass.templateArgs[id].dataTypeString)
10304                      {
10305                         ClassTemplateArgument arg = tClass.templateArgs[id];
10306                         Context context = SetupTemplatesContext(tClass);
10307                         bool constant = exp.expType.constant;
10308                         /*if(!arg.dataType)
10309                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10310                         FreeType(exp.expType);
10311
10312                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10313                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10314                         else if(exp.expType.kind == pointerType)
10315                         {
10316                            Type t = exp.expType.type;
10317                            while(t.kind == pointerType) t = t.type;
10318                            if(constant) t.constant = constant;
10319                         }
10320                         if(exp.expType)
10321                         {
10322                            if(exp.expType.kind == thisClassType)
10323                            {
10324                               FreeType(exp.expType);
10325                               exp.expType = ReplaceThisClassType(_class);
10326                            }
10327
10328                            if(tClass.templateClass && (exp.expType.kind != templateType || (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType))))
10329                               exp.expType.passAsTemplate = true;
10330                            //exp.expType.refCount++;
10331                            if(!exp.destType)
10332                            {
10333                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10334                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10335                               else if(exp.destType.kind == pointerType)
10336                               {
10337                                  Type t = exp.destType.type;
10338                                  while(t.kind == pointerType) t = t.type;
10339                                  if(constant) t.constant = constant;
10340                               }
10341
10342                               //exp.destType.refCount++;
10343
10344                               if(exp.destType.kind == thisClassType)
10345                               {
10346                                  FreeType(exp.destType);
10347                                  exp.destType = ReplaceThisClassType(_class);
10348                               }
10349                            }
10350                         }
10351                         FinishTemplatesContext(context);
10352                      }
10353                   }
10354                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10355                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10356                   {
10357                      int id = 0;
10358                      ClassTemplateParameter curParam = null;
10359                      Class sClass;
10360
10361                      for(sClass = tClass; sClass; sClass = sClass.base)
10362                      {
10363                         id = 0;
10364                         if(sClass.templateClass) sClass = sClass.templateClass;
10365                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10366                         {
10367                            if(curParam.type == TemplateParameterType::type &&
10368                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10369                            {
10370                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10371                                  id += sClass.templateParams.count;
10372                               break;
10373                            }
10374                            id++;
10375                         }
10376                         if(curParam) break;
10377                      }
10378
10379                      if(curParam)
10380                      {
10381                         ClassTemplateArgument arg = tClass.templateArgs[id];
10382                         Context context = SetupTemplatesContext(tClass);
10383                         Type basicType;
10384                         /*if(!arg.dataType)
10385                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10386
10387                         basicType = ProcessTypeString(arg.dataTypeString, false);
10388                         if(basicType)
10389                         {
10390                            if(basicType.kind == thisClassType)
10391                            {
10392                               FreeType(basicType);
10393                               basicType = ReplaceThisClassType(_class);
10394                            }
10395
10396                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10397                            if(tClass.templateClass)
10398                               basicType.passAsTemplate = true;
10399                            */
10400
10401                            FreeType(exp.expType);
10402
10403                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10404                            //exp.expType.refCount++;
10405                            if(!exp.destType)
10406                            {
10407                               exp.destType = exp.expType;
10408                               exp.destType.refCount++;
10409                            }
10410
10411                            {
10412                               Expression newExp { };
10413                               OldList * specs = MkList();
10414                               Declarator decl;
10415                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10416                               *newExp = *exp;
10417                               if(exp.destType) exp.destType.refCount++;
10418                               if(exp.expType)  exp.expType.refCount++;
10419                               exp.type = castExp;
10420                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10421                               exp.cast.exp = newExp;
10422                               //FreeType(exp.expType);
10423                               //exp.expType = null;
10424                               //ProcessExpressionType(sourceExp);
10425                            }
10426                         }
10427                         FinishTemplatesContext(context);
10428                      }
10429                   }
10430                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10431                   {
10432                      Class expClass = exp.expType._class.registered;
10433                      if(expClass)
10434                      {
10435                         Class cClass = null;
10436                         int c;
10437                         int p = 0;
10438                         int paramCount = 0;
10439                         int lastParam = -1;
10440                         char templateString[1024];
10441                         ClassTemplateParameter param;
10442                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10443                         while(cClass != expClass)
10444                         {
10445                            Class sClass;
10446                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10447                            cClass = sClass;
10448
10449                            for(param = cClass.templateParams.first; param; param = param.next)
10450                            {
10451                               Class cClassCur = null;
10452                               int c;
10453                               int cp = 0;
10454                               ClassTemplateParameter paramCur = null;
10455                               ClassTemplateArgument arg;
10456                               while(cClassCur != tClass && !paramCur)
10457                               {
10458                                  Class sClassCur;
10459                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10460                                  cClassCur = sClassCur;
10461
10462                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10463                                  {
10464                                     if(!strcmp(paramCur.name, param.name))
10465                                     {
10466
10467                                        break;
10468                                     }
10469                                     cp++;
10470                                  }
10471                               }
10472                               if(paramCur && paramCur.type == TemplateParameterType::type)
10473                                  arg = tClass.templateArgs[cp];
10474                               else
10475                                  arg = expClass.templateArgs[p];
10476
10477                               {
10478                                  char argument[256];
10479                                  argument[0] = '\0';
10480                                  /*if(arg.name)
10481                                  {
10482                                     strcat(argument, arg.name.string);
10483                                     strcat(argument, " = ");
10484                                  }*/
10485                                  switch(param.type)
10486                                  {
10487                                     case expression:
10488                                     {
10489                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10490                                        char expString[1024];
10491                                        OldList * specs = MkList();
10492                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10493                                        Expression exp;
10494                                        char * string = PrintHexUInt64(arg.expression.ui64);
10495                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10496                                        delete string;
10497
10498                                        ProcessExpressionType(exp);
10499                                        ComputeExpression(exp);
10500                                        expString[0] = '\0';
10501                                        PrintExpression(exp, expString);
10502                                        strcat(argument, expString);
10503                                        // delete exp;
10504                                        FreeExpression(exp);
10505                                        break;
10506                                     }
10507                                     case identifier:
10508                                     {
10509                                        strcat(argument, arg.member.name);
10510                                        break;
10511                                     }
10512                                     case TemplateParameterType::type:
10513                                     {
10514                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10515                                           strcat(argument, arg.dataTypeString);
10516                                        break;
10517                                     }
10518                                  }
10519                                  if(argument[0])
10520                                  {
10521                                     if(paramCount) strcat(templateString, ", ");
10522                                     if(lastParam != p - 1)
10523                                     {
10524                                        strcat(templateString, param.name);
10525                                        strcat(templateString, " = ");
10526                                     }
10527                                     strcat(templateString, argument);
10528                                     paramCount++;
10529                                     lastParam = p;
10530                                  }
10531                               }
10532                               p++;
10533                            }
10534                         }
10535                         {
10536                            int len = strlen(templateString);
10537                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10538                            templateString[len++] = '>';
10539                            templateString[len++] = '\0';
10540                         }
10541
10542                         FreeType(exp.expType);
10543                         {
10544                            Context context = SetupTemplatesContext(tClass);
10545                            exp.expType = ProcessTypeString(templateString, false);
10546                            FinishTemplatesContext(context);
10547                         }
10548                      }
10549                   }
10550                }
10551             }
10552             else
10553                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10554          }
10555          else if(type && (type.kind == structType || type.kind == unionType))
10556          {
10557             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10558             if(memberType)
10559             {
10560                exp.expType = memberType;
10561                if(memberType)
10562                   memberType.refCount++;
10563             }
10564          }
10565          else
10566          {
10567             char expString[10240];
10568             expString[0] = '\0';
10569             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10570             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10571          }
10572
10573          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10574          {
10575             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10576             {
10577                Identifier id = exp.member.member;
10578                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10579                if(_class)
10580                {
10581                   FreeType(exp.expType);
10582                   exp.expType = ReplaceThisClassType(_class);
10583                }
10584             }
10585          }
10586          yylloc = oldyylloc;
10587          break;
10588       }
10589       // Convert x->y into (*x).y
10590       case pointerExp:
10591       {
10592          Type destType = exp.destType;
10593
10594          // DOING THIS LATER NOW...
10595          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10596          {
10597             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10598             /* TODO: Name Space Fix ups
10599             if(!exp.member.member.classSym)
10600                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10601             */
10602          }
10603
10604          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10605          exp.type = memberExp;
10606          if(destType)
10607             destType.count++;
10608          ProcessExpressionType(exp);
10609          if(destType)
10610             destType.count--;
10611          break;
10612       }
10613       case classSizeExp:
10614       {
10615          //ComputeExpression(exp);
10616
10617          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10618          if(classSym && classSym.registered)
10619          {
10620             if(classSym.registered.type == noHeadClass)
10621             {
10622                char name[1024];
10623                name[0] = '\0';
10624                DeclareStruct(classSym.string, false);
10625                FreeSpecifier(exp._class);
10626                exp.type = typeSizeExp;
10627                FullClassNameCat(name, classSym.string, false);
10628                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10629             }
10630             else
10631             {
10632                if(classSym.registered.fixed)
10633                {
10634                   FreeSpecifier(exp._class);
10635                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10636                   exp.type = constantExp;
10637                }
10638                else
10639                {
10640                   char className[1024];
10641                   strcpy(className, "__ecereClass_");
10642                   FullClassNameCat(className, classSym.string, true);
10643                   MangleClassName(className);
10644
10645                   DeclareClass(classSym, className);
10646
10647                   FreeExpContents(exp);
10648                   exp.type = pointerExp;
10649                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10650                   exp.member.member = MkIdentifier("structSize");
10651                }
10652             }
10653          }
10654
10655          exp.expType = Type
10656          {
10657             refCount = 1;
10658             kind = intSizeType;
10659          };
10660          // exp.isConstant = true;
10661          break;
10662       }
10663       case typeSizeExp:
10664       {
10665          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10666
10667          exp.expType = Type
10668          {
10669             refCount = 1;
10670             kind = intSizeType;
10671          };
10672          exp.isConstant = true;
10673
10674          DeclareType(type, false, false);
10675          FreeType(type);
10676          break;
10677       }
10678       case castExp:
10679       {
10680          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10681          type.count = 1;
10682          FreeType(exp.cast.exp.destType);
10683          exp.cast.exp.destType = type;
10684          type.refCount++;
10685          type.casted = true;
10686          ProcessExpressionType(exp.cast.exp);
10687          type.casted = false;
10688          type.count = 0;
10689          exp.expType = type;
10690          //type.refCount++;
10691
10692          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10693          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10694          {
10695             void * prev = exp.prev, * next = exp.next;
10696             Type expType = exp.cast.exp.destType;
10697             Expression castExp = exp.cast.exp;
10698             Type destType = exp.destType;
10699
10700             if(expType) expType.refCount++;
10701
10702             //FreeType(exp.destType);
10703             FreeType(exp.expType);
10704             FreeTypeName(exp.cast.typeName);
10705
10706             *exp = *castExp;
10707             FreeType(exp.expType);
10708             FreeType(exp.destType);
10709
10710             exp.expType = expType;
10711             exp.destType = destType;
10712
10713             delete castExp;
10714
10715             exp.prev = prev;
10716             exp.next = next;
10717
10718          }
10719          else
10720          {
10721             exp.isConstant = exp.cast.exp.isConstant;
10722          }
10723          //FreeType(type);
10724          break;
10725       }
10726       case extensionInitializerExp:
10727       {
10728          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10729          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10730          // ProcessInitializer(exp.initializer.initializer, type);
10731          exp.expType = type;
10732          break;
10733       }
10734       case vaArgExp:
10735       {
10736          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10737          ProcessExpressionType(exp.vaArg.exp);
10738          exp.expType = type;
10739          break;
10740       }
10741       case conditionExp:
10742       {
10743          Expression e;
10744          exp.isConstant = true;
10745
10746          FreeType(exp.cond.cond.destType);
10747          exp.cond.cond.destType = MkClassType("bool");
10748          exp.cond.cond.destType.truth = true;
10749          ProcessExpressionType(exp.cond.cond);
10750          if(!exp.cond.cond.isConstant)
10751             exp.isConstant = false;
10752          for(e = exp.cond.exp->first; e; e = e.next)
10753          {
10754             if(!e.next)
10755             {
10756                FreeType(e.destType);
10757                e.destType = exp.destType;
10758                if(e.destType) e.destType.refCount++;
10759             }
10760             ProcessExpressionType(e);
10761             if(!e.next)
10762             {
10763                exp.expType = e.expType;
10764                if(e.expType) e.expType.refCount++;
10765             }
10766             if(!e.isConstant)
10767                exp.isConstant = false;
10768          }
10769
10770          FreeType(exp.cond.elseExp.destType);
10771          // Added this check if we failed to find an expType
10772          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10773
10774          // Reversed it...
10775          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10776
10777          if(exp.cond.elseExp.destType)
10778             exp.cond.elseExp.destType.refCount++;
10779          ProcessExpressionType(exp.cond.elseExp);
10780
10781          // FIXED THIS: Was done before calling process on elseExp
10782          if(!exp.cond.elseExp.isConstant)
10783             exp.isConstant = false;
10784          break;
10785       }
10786       case extensionCompoundExp:
10787       {
10788          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10789          {
10790             Statement last = exp.compound.compound.statements->last;
10791             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10792             {
10793                ((Expression)last.expressions->last).destType = exp.destType;
10794                if(exp.destType)
10795                   exp.destType.refCount++;
10796             }
10797             ProcessStatement(exp.compound);
10798             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10799             if(exp.expType)
10800                exp.expType.refCount++;
10801          }
10802          break;
10803       }
10804       case classExp:
10805       {
10806          Specifier spec = exp._classExp.specifiers->first;
10807          if(spec && spec.type == nameSpecifier)
10808          {
10809             exp.expType = MkClassType(spec.name);
10810             exp.expType.kind = subClassType;
10811             exp.byReference = true;
10812          }
10813          else
10814          {
10815             exp.expType = MkClassType("ecere::com::Class");
10816             exp.byReference = true;
10817          }
10818          break;
10819       }
10820       case classDataExp:
10821       {
10822          Class _class = thisClass ? thisClass : currentClass;
10823          if(_class)
10824          {
10825             Identifier id = exp.classData.id;
10826             char structName[1024];
10827             Expression classExp;
10828             strcpy(structName, "__ecereClassData_");
10829             FullClassNameCat(structName, _class.fullName, false);
10830             exp.type = pointerExp;
10831             exp.member.member = id;
10832             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10833                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10834             else
10835                classExp = MkExpIdentifier(MkIdentifier("class"));
10836
10837             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10838                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10839                   MkExpBrackets(MkListOne(MkExpOp(
10840                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10841                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10842                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10843                      )));
10844
10845             ProcessExpressionType(exp);
10846             return;
10847          }
10848          break;
10849       }
10850       case arrayExp:
10851       {
10852          Type type = null;
10853          const char * typeString = null;
10854          char typeStringBuf[1024];
10855          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10856             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10857          {
10858             Class templateClass = exp.destType._class.registered;
10859             typeString = templateClass.templateArgs[2].dataTypeString;
10860          }
10861          else if(exp.list)
10862          {
10863             // Guess type from expressions in the array
10864             Expression e;
10865             for(e = exp.list->first; e; e = e.next)
10866             {
10867                ProcessExpressionType(e);
10868                if(e.expType)
10869                {
10870                   if(!type) { type = e.expType; type.refCount++; }
10871                   else
10872                   {
10873                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10874                      if(!MatchTypeExpression(e, type, null, false, true))
10875                      {
10876                         FreeType(type);
10877                         type = e.expType;
10878                         e.expType = null;
10879
10880                         e = exp.list->first;
10881                         ProcessExpressionType(e);
10882                         if(e.expType)
10883                         {
10884                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10885                            if(!MatchTypeExpression(e, type, null, false, true))
10886                            {
10887                               FreeType(e.expType);
10888                               e.expType = null;
10889                               FreeType(type);
10890                               type = null;
10891                               break;
10892                            }
10893                         }
10894                      }
10895                   }
10896                   if(e.expType)
10897                   {
10898                      FreeType(e.expType);
10899                      e.expType = null;
10900                   }
10901                }
10902             }
10903             if(type)
10904             {
10905                typeStringBuf[0] = '\0';
10906                PrintTypeNoConst(type, typeStringBuf, false, true);
10907                typeString = typeStringBuf;
10908                FreeType(type);
10909                type = null;
10910             }
10911          }
10912          if(typeString)
10913          {
10914             /*
10915             (Container)& (struct BuiltInContainer)
10916             {
10917                ._vTbl = class(BuiltInContainer)._vTbl,
10918                ._class = class(BuiltInContainer),
10919                .refCount = 0,
10920                .data = (int[]){ 1, 7, 3, 4, 5 },
10921                .count = 5,
10922                .type = class(int),
10923             }
10924             */
10925             char templateString[1024];
10926             OldList * initializers = MkList();
10927             OldList * structInitializers = MkList();
10928             OldList * specs = MkList();
10929             Expression expExt;
10930             Declarator decl = SpecDeclFromString(typeString, specs, null);
10931             sprintf(templateString, "Container<%s>", typeString);
10932
10933             if(exp.list)
10934             {
10935                Expression e;
10936                type = ProcessTypeString(typeString, false);
10937                while(e = exp.list->first)
10938                {
10939                   exp.list->Remove(e);
10940                   e.destType = type;
10941                   type.refCount++;
10942                   ProcessExpressionType(e);
10943                   ListAdd(initializers, MkInitializerAssignment(e));
10944                }
10945                FreeType(type);
10946                delete exp.list;
10947             }
10948
10949             DeclareStruct("ecere::com::BuiltInContainer", false);
10950
10951             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10952                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10953             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10954                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10955             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10956                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10957             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10958                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10959                MkInitializerList(initializers))));
10960                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10961             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10962                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10963             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10964                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10965             exp.expType = ProcessTypeString(templateString, false);
10966             exp.type = bracketsExp;
10967             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10968                MkExpOp(null, '&',
10969                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10970                   MkInitializerList(structInitializers)))));
10971             ProcessExpressionType(expExt);
10972          }
10973          else
10974          {
10975             exp.expType = ProcessTypeString("Container", false);
10976             Compiler_Error($"Couldn't determine type of array elements\n");
10977          }
10978          break;
10979       }
10980    }
10981
10982    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10983    {
10984       FreeType(exp.expType);
10985       exp.expType = ReplaceThisClassType(thisClass);
10986    }
10987
10988    // Resolve structures here
10989    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10990    {
10991       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10992       // TODO: Fix members reference...
10993       if(symbol)
10994       {
10995          if(exp.expType.kind != enumType)
10996          {
10997             Type member;
10998             String enumName = CopyString(exp.expType.enumName);
10999
11000             // Fixed a memory leak on self-referencing C structs typedefs
11001             // by instantiating a new type rather than simply copying members
11002             // into exp.expType
11003             FreeType(exp.expType);
11004             exp.expType = Type { };
11005             exp.expType.kind = symbol.type.kind;
11006             exp.expType.refCount++;
11007             exp.expType.enumName = enumName;
11008
11009             exp.expType.members = symbol.type.members;
11010             for(member = symbol.type.members.first; member; member = member.next)
11011                member.refCount++;
11012          }
11013          else
11014          {
11015             NamedLink member;
11016             for(member = symbol.type.members.first; member; member = member.next)
11017             {
11018                NamedLink value { name = CopyString(member.name) };
11019                exp.expType.members.Add(value);
11020             }
11021          }
11022       }
11023    }
11024
11025    yylloc = exp.loc;
11026    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11027    else if(exp.destType && !exp.destType.keepCast)
11028    {
11029       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11030          exp.needTemplateCast = 1;
11031
11032       if(exp.destType.kind == voidType);
11033       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11034       {
11035          if(!exp.destType.count || unresolved)
11036          {
11037             if(!exp.expType)
11038             {
11039                yylloc = exp.loc;
11040                if(exp.destType.kind != ellipsisType)
11041                {
11042                   char type2[1024];
11043                   type2[0] = '\0';
11044                   if(inCompiler)
11045                   {
11046                      char expString[10240];
11047                      expString[0] = '\0';
11048
11049                      PrintType(exp.destType, type2, false, true);
11050
11051                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11052                      if(unresolved)
11053                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11054                      else if(exp.type != dummyExp)
11055                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11056                   }
11057                }
11058                else
11059                {
11060                   char expString[10240] ;
11061                   expString[0] = '\0';
11062                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11063
11064                   if(unresolved)
11065                      Compiler_Error($"unresolved identifier %s\n", expString);
11066                   else if(exp.type != dummyExp)
11067                      Compiler_Error($"couldn't determine type of %s\n", expString);
11068                }
11069             }
11070             else
11071             {
11072                char type1[1024];
11073                char type2[1024];
11074                type1[0] = '\0';
11075                type2[0] = '\0';
11076                if(inCompiler)
11077                {
11078                   PrintType(exp.expType, type1, false, true);
11079                   PrintType(exp.destType, type2, false, true);
11080                }
11081
11082                //CheckExpressionType(exp, exp.destType, false);
11083
11084                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11085                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11086                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11087                else
11088                {
11089                   char expString[10240];
11090                   expString[0] = '\0';
11091                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11092
11093 #ifdef _DEBUG
11094                   CheckExpressionType(exp, exp.destType, false, true);
11095 #endif
11096                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11097                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
11098                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11099
11100                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11101                   FreeType(exp.expType);
11102                   exp.destType.refCount++;
11103                   exp.expType = exp.destType;
11104                }
11105             }
11106          }
11107       }
11108       /*else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
11109       {
11110          Expression newExp { };
11111          char typeString[1024];
11112          OldList * specs = MkList();
11113          Declarator decl;
11114
11115          typeString[0] = '\0';
11116
11117          *newExp = *exp;
11118
11119          if(exp.expType)  exp.expType.refCount++;
11120          if(exp.expType)  exp.expType.refCount++;
11121          exp.type = castExp;
11122          newExp.destType = exp.expType;
11123
11124          PrintType(exp.expType, typeString, false, false);
11125          decl = SpecDeclFromString(typeString, specs, null);
11126
11127          exp.cast.typeName = MkTypeName(specs, decl);
11128          exp.cast.exp = newExp;
11129       }*/
11130    }
11131    else if(unresolved)
11132    {
11133       if(exp.identifier._class && exp.identifier._class.name)
11134          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11135       else if(exp.identifier.string && exp.identifier.string[0])
11136          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11137    }
11138    else if(!exp.expType && exp.type != dummyExp)
11139    {
11140       char expString[10240];
11141       expString[0] = '\0';
11142       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11143       Compiler_Error($"couldn't determine type of %s\n", expString);
11144    }
11145
11146    // Let's try to support any_object & typed_object here:
11147    if(inCompiler)
11148       ApplyAnyObjectLogic(exp);
11149
11150    // Mark nohead classes as by reference, unless we're casting them to an integral type
11151    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11152       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11153          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11154           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11155    {
11156       exp.byReference = true;
11157    }
11158    yylloc = oldyylloc;
11159 }
11160
11161 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11162 {
11163    // THIS CODE WILL FIND NEXT MEMBER...
11164    if(*curMember)
11165    {
11166       *curMember = (*curMember).next;
11167
11168       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11169       {
11170          *curMember = subMemberStack[--(*subMemberStackPos)];
11171          *curMember = (*curMember).next;
11172       }
11173
11174       // SKIP ALL PROPERTIES HERE...
11175       while((*curMember) && (*curMember).isProperty)
11176          *curMember = (*curMember).next;
11177
11178       if(subMemberStackPos)
11179       {
11180          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11181          {
11182             subMemberStack[(*subMemberStackPos)++] = *curMember;
11183
11184             *curMember = (*curMember).members.first;
11185             while(*curMember && (*curMember).isProperty)
11186                *curMember = (*curMember).next;
11187          }
11188       }
11189    }
11190    while(!*curMember)
11191    {
11192       if(!*curMember)
11193       {
11194          if(subMemberStackPos && *subMemberStackPos)
11195          {
11196             *curMember = subMemberStack[--(*subMemberStackPos)];
11197             *curMember = (*curMember).next;
11198          }
11199          else
11200          {
11201             Class lastCurClass = *curClass;
11202
11203             if(*curClass == _class) break;     // REACHED THE END
11204
11205             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11206             *curMember = (*curClass).membersAndProperties.first;
11207          }
11208
11209          while((*curMember) && (*curMember).isProperty)
11210             *curMember = (*curMember).next;
11211          if(subMemberStackPos)
11212          {
11213             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11214             {
11215                subMemberStack[(*subMemberStackPos)++] = *curMember;
11216
11217                *curMember = (*curMember).members.first;
11218                while(*curMember && (*curMember).isProperty)
11219                   *curMember = (*curMember).next;
11220             }
11221          }
11222       }
11223    }
11224 }
11225
11226
11227 static void ProcessInitializer(Initializer init, Type type)
11228 {
11229    switch(init.type)
11230    {
11231       case expInitializer:
11232          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11233          {
11234             // TESTING THIS FOR SHUTTING = 0 WARNING
11235             if(init.exp && !init.exp.destType)
11236             {
11237                FreeType(init.exp.destType);
11238                init.exp.destType = type;
11239                if(type) type.refCount++;
11240             }
11241             if(init.exp)
11242             {
11243                ProcessExpressionType(init.exp);
11244                init.isConstant = init.exp.isConstant;
11245             }
11246             break;
11247          }
11248          else
11249          {
11250             Expression exp = init.exp;
11251             Instantiation inst = exp.instance;
11252             MembersInit members;
11253
11254             init.type = listInitializer;
11255             init.list = MkList();
11256
11257             if(inst.members)
11258             {
11259                for(members = inst.members->first; members; members = members.next)
11260                {
11261                   if(members.type == dataMembersInit)
11262                   {
11263                      MemberInit member;
11264                      for(member = members.dataMembers->first; member; member = member.next)
11265                      {
11266                         ListAdd(init.list, member.initializer);
11267                         member.initializer = null;
11268                      }
11269                   }
11270                   // Discard all MembersInitMethod
11271                }
11272             }
11273             FreeExpression(exp);
11274          }
11275       case listInitializer:
11276       {
11277          Initializer i;
11278          Type initializerType = null;
11279          Class curClass = null;
11280          DataMember curMember = null;
11281          DataMember subMemberStack[256];
11282          int subMemberStackPos = 0;
11283
11284          if(type && type.kind == arrayType)
11285             initializerType = Dereference(type);
11286          else if(type && (type.kind == structType || type.kind == unionType))
11287             initializerType = type.members.first;
11288
11289          for(i = init.list->first; i; i = i.next)
11290          {
11291             if(type && type.kind == classType && type._class && type._class.registered)
11292             {
11293                // THIS IS FOR A C STYLE INSTANTIATION OF STRUCT CLASSES ONLY... WE ONLY CARE ABOUT DATA MEMBERS, AND ACTUAL MEMORY ORDER (PRIVATE MEMBERS ARE INCLUDED)
11294                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11295                // TODO: Generate error on initializing a private data member this way from another module...
11296                if(curMember)
11297                {
11298                   if(!curMember.dataType)
11299                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11300                   initializerType = curMember.dataType;
11301                }
11302             }
11303             ProcessInitializer(i, initializerType);
11304             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11305                initializerType = initializerType.next;
11306             if(!i.isConstant)
11307                init.isConstant = false;
11308          }
11309
11310          if(type && type.kind == arrayType)
11311             FreeType(initializerType);
11312
11313          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11314          {
11315             Compiler_Error($"Assigning list initializer to non list\n");
11316          }
11317          break;
11318       }
11319    }
11320 }
11321
11322 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11323 {
11324    switch(spec.type)
11325    {
11326       case baseSpecifier:
11327       {
11328          if(spec.specifier == THISCLASS)
11329          {
11330             if(thisClass)
11331             {
11332                spec.type = nameSpecifier;
11333                spec.name = ReplaceThisClass(thisClass);
11334                spec.symbol = FindClass(spec.name);
11335                ProcessSpecifier(spec, declareStruct);
11336             }
11337          }
11338          break;
11339       }
11340       case nameSpecifier:
11341       {
11342          Symbol symbol = FindType(curContext, spec.name);
11343          if(symbol)
11344             DeclareType(symbol.type, true, true);
11345          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11346             DeclareStruct(spec.name, false);
11347          break;
11348       }
11349       case enumSpecifier:
11350       {
11351          Enumerator e;
11352          if(spec.list)
11353          {
11354             for(e = spec.list->first; e; e = e.next)
11355             {
11356                if(e.exp)
11357                   ProcessExpressionType(e.exp);
11358             }
11359          }
11360          break;
11361       }
11362       case structSpecifier:
11363       case unionSpecifier:
11364       {
11365          if(spec.definitions)
11366          {
11367             ClassDef def;
11368             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11369             //if(symbol)
11370                ProcessClass(spec.definitions, symbol);
11371             /*else
11372             {
11373                for(def = spec.definitions->first; def; def = def.next)
11374                {
11375                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11376                      ProcessDeclaration(def.decl);
11377                }
11378             }*/
11379          }
11380          break;
11381       }
11382       /*
11383       case classSpecifier:
11384       {
11385          Symbol classSym = FindClass(spec.name);
11386          if(classSym && classSym.registered && classSym.registered.type == structClass)
11387             DeclareStruct(spec.name, false);
11388          break;
11389       }
11390       */
11391    }
11392 }
11393
11394
11395 static void ProcessDeclarator(Declarator decl)
11396 {
11397    switch(decl.type)
11398    {
11399       case identifierDeclarator:
11400          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11401          {
11402             FreeSpecifier(decl.identifier._class);
11403             decl.identifier._class = null;
11404          }
11405          break;
11406       case arrayDeclarator:
11407          if(decl.array.exp)
11408             ProcessExpressionType(decl.array.exp);
11409       case structDeclarator:
11410       case bracketsDeclarator:
11411       case functionDeclarator:
11412       case pointerDeclarator:
11413       case extendedDeclarator:
11414       case extendedDeclaratorEnd:
11415          if(decl.declarator)
11416             ProcessDeclarator(decl.declarator);
11417          if(decl.type == functionDeclarator)
11418          {
11419             Identifier id = GetDeclId(decl);
11420             if(id && id._class)
11421             {
11422                TypeName param
11423                {
11424                   qualifiers = MkListOne(id._class);
11425                   declarator = null;
11426                };
11427                if(!decl.function.parameters)
11428                   decl.function.parameters = MkList();
11429                decl.function.parameters->Insert(null, param);
11430                id._class = null;
11431             }
11432             if(decl.function.parameters)
11433             {
11434                TypeName param;
11435
11436                for(param = decl.function.parameters->first; param; param = param.next)
11437                {
11438                   if(param.qualifiers && param.qualifiers->first)
11439                   {
11440                      Specifier spec = param.qualifiers->first;
11441                      if(spec && spec.specifier == TYPED_OBJECT)
11442                      {
11443                         Declarator d = param.declarator;
11444                         TypeName newParam
11445                         {
11446                            qualifiers = MkListOne(MkSpecifier(VOID));
11447                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11448                         };
11449                         if(d.type != pointerDeclarator)
11450                            newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11451
11452                         FreeList(param.qualifiers, FreeSpecifier);
11453
11454                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11455                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11456
11457                         decl.function.parameters->Insert(param, newParam);
11458                         param = newParam;
11459                      }
11460                      else if(spec && spec.specifier == ANY_OBJECT)
11461                      {
11462                         Declarator d = param.declarator;
11463
11464                         FreeList(param.qualifiers, FreeSpecifier);
11465
11466                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11467                         if(d.type != pointerDeclarator)
11468                            param.qualifiers->Insert(null, MkSpecifier(CONST));
11469                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11470                      }
11471                      else if(spec.specifier == THISCLASS)
11472                      {
11473                         if(thisClass)
11474                         {
11475                            spec.type = nameSpecifier;
11476                            spec.name = ReplaceThisClass(thisClass);
11477                            spec.symbol = FindClass(spec.name);
11478                            ProcessSpecifier(spec, false);
11479                         }
11480                      }
11481                   }
11482
11483                   if(param.declarator)
11484                      ProcessDeclarator(param.declarator);
11485                }
11486             }
11487          }
11488          break;
11489    }
11490 }
11491
11492 static void ProcessDeclaration(Declaration decl)
11493 {
11494    yylloc = decl.loc;
11495    switch(decl.type)
11496    {
11497       case initDeclaration:
11498       {
11499          bool declareStruct = false;
11500          /*
11501          lineNum = decl.pos.line;
11502          column = decl.pos.col;
11503          */
11504
11505          if(decl.declarators)
11506          {
11507             InitDeclarator d;
11508
11509             for(d = decl.declarators->first; d; d = d.next)
11510             {
11511                Type type, subType;
11512                ProcessDeclarator(d.declarator);
11513
11514                type = ProcessType(decl.specifiers, d.declarator);
11515
11516                if(d.initializer)
11517                {
11518                   ProcessInitializer(d.initializer, type);
11519
11520                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11521
11522                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11523                      d.initializer.exp.type == instanceExp)
11524                   {
11525                      if(type.kind == classType && type._class ==
11526                         d.initializer.exp.expType._class)
11527                      {
11528                         Instantiation inst = d.initializer.exp.instance;
11529                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11530
11531                         d.initializer.exp.instance = null;
11532                         if(decl.specifiers)
11533                            FreeList(decl.specifiers, FreeSpecifier);
11534                         FreeList(decl.declarators, FreeInitDeclarator);
11535
11536                         d = null;
11537
11538                         decl.type = instDeclaration;
11539                         decl.inst = inst;
11540                      }
11541                   }
11542                }
11543                for(subType = type; subType;)
11544                {
11545                   if(subType.kind == classType)
11546                   {
11547                      declareStruct = true;
11548                      break;
11549                   }
11550                   else if(subType.kind == pointerType)
11551                      break;
11552                   else if(subType.kind == arrayType)
11553                      subType = subType.arrayType;
11554                   else
11555                      break;
11556                }
11557
11558                FreeType(type);
11559                if(!d) break;
11560             }
11561          }
11562
11563          if(decl.specifiers)
11564          {
11565             Specifier s;
11566             for(s = decl.specifiers->first; s; s = s.next)
11567             {
11568                ProcessSpecifier(s, declareStruct);
11569             }
11570          }
11571          break;
11572       }
11573       case instDeclaration:
11574       {
11575          ProcessInstantiationType(decl.inst);
11576          break;
11577       }
11578       case structDeclaration:
11579       {
11580          Specifier spec;
11581          Declarator d;
11582          bool declareStruct = false;
11583
11584          if(decl.declarators)
11585          {
11586             for(d = decl.declarators->first; d; d = d.next)
11587             {
11588                Type type = ProcessType(decl.specifiers, d.declarator);
11589                Type subType;
11590                ProcessDeclarator(d);
11591                for(subType = type; subType;)
11592                {
11593                   if(subType.kind == classType)
11594                   {
11595                      declareStruct = true;
11596                      break;
11597                   }
11598                   else if(subType.kind == pointerType)
11599                      break;
11600                   else if(subType.kind == arrayType)
11601                      subType = subType.arrayType;
11602                   else
11603                      break;
11604                }
11605                FreeType(type);
11606             }
11607          }
11608          if(decl.specifiers)
11609          {
11610             for(spec = decl.specifiers->first; spec; spec = spec.next)
11611                ProcessSpecifier(spec, declareStruct);
11612          }
11613          break;
11614       }
11615    }
11616 }
11617
11618 static FunctionDefinition curFunction;
11619
11620 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11621 {
11622    char propName[1024], propNameM[1024];
11623    char getName[1024], setName[1024];
11624    OldList * args;
11625
11626    DeclareProperty(prop, setName, getName);
11627
11628    // eInstance_FireWatchers(object, prop);
11629    strcpy(propName, "__ecereProp_");
11630    FullClassNameCat(propName, prop._class.fullName, false);
11631    strcat(propName, "_");
11632    // strcat(propName, prop.name);
11633    FullClassNameCat(propName, prop.name, true);
11634    MangleClassName(propName);
11635
11636    strcpy(propNameM, "__ecerePropM_");
11637    FullClassNameCat(propNameM, prop._class.fullName, false);
11638    strcat(propNameM, "_");
11639    // strcat(propNameM, prop.name);
11640    FullClassNameCat(propNameM, prop.name, true);
11641    MangleClassName(propNameM);
11642
11643    if(prop.isWatchable)
11644    {
11645       args = MkList();
11646       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11647       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11648       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11649
11650       args = MkList();
11651       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11652       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11653       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11654    }
11655
11656
11657    {
11658       args = MkList();
11659       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11660       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11661       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11662
11663       args = MkList();
11664       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11665       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11666       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11667    }
11668
11669    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11670       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11671       curFunction.propSet.fireWatchersDone = true;
11672 }
11673
11674 static void ProcessStatement(Statement stmt)
11675 {
11676    yylloc = stmt.loc;
11677    /*
11678    lineNum = stmt.pos.line;
11679    column = stmt.pos.col;
11680    */
11681    switch(stmt.type)
11682    {
11683       case labeledStmt:
11684          ProcessStatement(stmt.labeled.stmt);
11685          break;
11686       case caseStmt:
11687          // This expression should be constant...
11688          if(stmt.caseStmt.exp)
11689          {
11690             FreeType(stmt.caseStmt.exp.destType);
11691             stmt.caseStmt.exp.destType = curSwitchType;
11692             if(curSwitchType) curSwitchType.refCount++;
11693             ProcessExpressionType(stmt.caseStmt.exp);
11694             ComputeExpression(stmt.caseStmt.exp);
11695          }
11696          if(stmt.caseStmt.stmt)
11697             ProcessStatement(stmt.caseStmt.stmt);
11698          break;
11699       case compoundStmt:
11700       {
11701          if(stmt.compound.context)
11702          {
11703             Declaration decl;
11704             Statement s;
11705
11706             Statement prevCompound = curCompound;
11707             Context prevContext = curContext;
11708
11709             if(!stmt.compound.isSwitch)
11710                curCompound = stmt;
11711             curContext = stmt.compound.context;
11712
11713             if(stmt.compound.declarations)
11714             {
11715                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11716                   ProcessDeclaration(decl);
11717             }
11718             if(stmt.compound.statements)
11719             {
11720                for(s = stmt.compound.statements->first; s; s = s.next)
11721                   ProcessStatement(s);
11722             }
11723
11724             curContext = prevContext;
11725             curCompound = prevCompound;
11726          }
11727          break;
11728       }
11729       case expressionStmt:
11730       {
11731          Expression exp;
11732          if(stmt.expressions)
11733          {
11734             for(exp = stmt.expressions->first; exp; exp = exp.next)
11735                ProcessExpressionType(exp);
11736          }
11737          break;
11738       }
11739       case ifStmt:
11740       {
11741          Expression exp;
11742
11743          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11744          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11745          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11746          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11747          {
11748             ProcessExpressionType(exp);
11749          }
11750          if(stmt.ifStmt.stmt)
11751             ProcessStatement(stmt.ifStmt.stmt);
11752          if(stmt.ifStmt.elseStmt)
11753             ProcessStatement(stmt.ifStmt.elseStmt);
11754          break;
11755       }
11756       case switchStmt:
11757       {
11758          Type oldSwitchType = curSwitchType;
11759          if(stmt.switchStmt.exp)
11760          {
11761             Expression exp;
11762             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11763             {
11764                if(!exp.next)
11765                {
11766                   /*
11767                   Type destType
11768                   {
11769                      kind = intType;
11770                      refCount = 1;
11771                   };
11772                   e.exp.destType = destType;
11773                   */
11774
11775                   ProcessExpressionType(exp);
11776                }
11777                if(!exp.next)
11778                   curSwitchType = exp.expType;
11779             }
11780          }
11781          ProcessStatement(stmt.switchStmt.stmt);
11782          curSwitchType = oldSwitchType;
11783          break;
11784       }
11785       case whileStmt:
11786       {
11787          if(stmt.whileStmt.exp)
11788          {
11789             Expression exp;
11790
11791             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11792             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11793             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11794             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11795             {
11796                ProcessExpressionType(exp);
11797             }
11798          }
11799          if(stmt.whileStmt.stmt)
11800             ProcessStatement(stmt.whileStmt.stmt);
11801          break;
11802       }
11803       case doWhileStmt:
11804       {
11805          if(stmt.doWhile.exp)
11806          {
11807             Expression exp;
11808
11809             if(stmt.doWhile.exp->last)
11810             {
11811                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11812                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11813                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11814             }
11815             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11816             {
11817                ProcessExpressionType(exp);
11818             }
11819          }
11820          if(stmt.doWhile.stmt)
11821             ProcessStatement(stmt.doWhile.stmt);
11822          break;
11823       }
11824       case forStmt:
11825       {
11826          Expression exp;
11827          if(stmt.forStmt.init)
11828             ProcessStatement(stmt.forStmt.init);
11829
11830          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11831          {
11832             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11833             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11834             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11835          }
11836
11837          if(stmt.forStmt.check)
11838             ProcessStatement(stmt.forStmt.check);
11839          if(stmt.forStmt.increment)
11840          {
11841             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11842                ProcessExpressionType(exp);
11843          }
11844
11845          if(stmt.forStmt.stmt)
11846             ProcessStatement(stmt.forStmt.stmt);
11847          break;
11848       }
11849       case forEachStmt:
11850       {
11851          Identifier id = stmt.forEachStmt.id;
11852          OldList * exp = stmt.forEachStmt.exp;
11853          OldList * filter = stmt.forEachStmt.filter;
11854          Statement block = stmt.forEachStmt.stmt;
11855          char iteratorType[1024];
11856          Type source;
11857          Expression e;
11858          bool isBuiltin = exp && exp->last &&
11859             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11860               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11861          Expression arrayExp;
11862          const char * typeString = null;
11863          int builtinCount = 0;
11864
11865          for(e = exp ? exp->first : null; e; e = e.next)
11866          {
11867             if(!e.next)
11868             {
11869                FreeType(e.destType);
11870                e.destType = ProcessTypeString("Container", false);
11871             }
11872             if(!isBuiltin || e.next)
11873                ProcessExpressionType(e);
11874          }
11875
11876          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11877          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11878             eClass_IsDerived(source._class.registered, containerClass)))
11879          {
11880             Class _class = source ? source._class.registered : null;
11881             Symbol symbol;
11882             Expression expIt = null;
11883             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
11884             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11885             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11886             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11887             stmt.type = compoundStmt;
11888
11889             stmt.compound.context = Context { };
11890             stmt.compound.context.parent = curContext;
11891             curContext = stmt.compound.context;
11892
11893             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11894             {
11895                Class mapClass = eSystem_FindClass(privateModule, "Map");
11896                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11897                isCustomAVLTree = true;
11898                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
11899                   isAVLTree = true;
11900                else */if(eClass_IsDerived(source._class.registered, mapClass))
11901                   isMap = true;
11902             }
11903             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11904             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11905             {
11906                Class listClass = eSystem_FindClass(privateModule, "List");
11907                isLinkList = true;
11908                isList = eClass_IsDerived(source._class.registered, listClass);
11909             }
11910
11911             if(isArray)
11912             {
11913                Declarator decl;
11914                OldList * specs = MkList();
11915                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11916                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11917                stmt.compound.declarations = MkListOne(
11918                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11919                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11920                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11921                      MkInitializerAssignment(MkExpBrackets(exp))))));
11922             }
11923             else if(isBuiltin)
11924             {
11925                Type type = null;
11926                char typeStringBuf[1024];
11927
11928                // TODO: Merge this code?
11929                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11930                if(((Expression)exp->last).type == castExp)
11931                {
11932                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11933                   if(typeName)
11934                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11935                }
11936
11937                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11938                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11939                   arrayExp.destType._class.registered.templateArgs)
11940                {
11941                   Class templateClass = arrayExp.destType._class.registered;
11942                   typeString = templateClass.templateArgs[2].dataTypeString;
11943                }
11944                else if(arrayExp.list)
11945                {
11946                   // Guess type from expressions in the array
11947                   Expression e;
11948                   for(e = arrayExp.list->first; e; e = e.next)
11949                   {
11950                      ProcessExpressionType(e);
11951                      if(e.expType)
11952                      {
11953                         if(!type) { type = e.expType; type.refCount++; }
11954                         else
11955                         {
11956                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11957                            if(!MatchTypeExpression(e, type, null, false, true))
11958                            {
11959                               FreeType(type);
11960                               type = e.expType;
11961                               e.expType = null;
11962
11963                               e = arrayExp.list->first;
11964                               ProcessExpressionType(e);
11965                               if(e.expType)
11966                               {
11967                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11968                                  if(!MatchTypeExpression(e, type, null, false, true))
11969                                  {
11970                                     FreeType(e.expType);
11971                                     e.expType = null;
11972                                     FreeType(type);
11973                                     type = null;
11974                                     break;
11975                                  }
11976                               }
11977                            }
11978                         }
11979                         if(e.expType)
11980                         {
11981                            FreeType(e.expType);
11982                            e.expType = null;
11983                         }
11984                      }
11985                   }
11986                   if(type)
11987                   {
11988                      typeStringBuf[0] = '\0';
11989                      PrintType(type, typeStringBuf, false, true);
11990                      typeString = typeStringBuf;
11991                      FreeType(type);
11992                   }
11993                }
11994                if(typeString)
11995                {
11996                   OldList * initializers = MkList();
11997                   Declarator decl;
11998                   OldList * specs = MkList();
11999                   if(arrayExp.list)
12000                   {
12001                      Expression e;
12002
12003                      builtinCount = arrayExp.list->count;
12004                      type = ProcessTypeString(typeString, false);
12005                      while(e = arrayExp.list->first)
12006                      {
12007                         arrayExp.list->Remove(e);
12008                         e.destType = type;
12009                         type.refCount++;
12010                         ProcessExpressionType(e);
12011                         ListAdd(initializers, MkInitializerAssignment(e));
12012                      }
12013                      FreeType(type);
12014                      delete arrayExp.list;
12015                   }
12016                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12017                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12018                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12019
12020                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12021                      PlugDeclarator(
12022                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12023                         ), MkInitializerList(initializers)))));
12024                   FreeList(exp, FreeExpression);
12025                }
12026                else
12027                {
12028                   arrayExp.expType = ProcessTypeString("Container", false);
12029                   Compiler_Error($"Couldn't determine type of array elements\n");
12030                }
12031
12032                /*
12033                Declarator decl;
12034                OldList * specs = MkList();
12035
12036                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12037                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12038                stmt.compound.declarations = MkListOne(
12039                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12040                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12041                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12042                      MkInitializerAssignment(MkExpBrackets(exp))))));
12043                */
12044             }
12045             else if(isLinkList && !isList)
12046             {
12047                Declarator decl;
12048                OldList * specs = MkList();
12049                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12050                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12051                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12052                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12053                      MkInitializerAssignment(MkExpBrackets(exp))))));
12054             }
12055             /*else if(isCustomAVLTree)
12056             {
12057                Declarator decl;
12058                OldList * specs = MkList();
12059                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12060                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12061                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12062                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12063                      MkInitializerAssignment(MkExpBrackets(exp))))));
12064             }*/
12065             else if(_class.templateArgs)
12066             {
12067                if(isMap)
12068                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12069                else
12070                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12071
12072                stmt.compound.declarations = MkListOne(
12073                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12074                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12075                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12076             }
12077             symbol = FindSymbol(id.string, curContext, curContext, false, false);
12078
12079             if(block)
12080             {
12081                // Reparent sub-contexts in this statement
12082                switch(block.type)
12083                {
12084                   case compoundStmt:
12085                      if(block.compound.context)
12086                         block.compound.context.parent = stmt.compound.context;
12087                      break;
12088                   case ifStmt:
12089                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12090                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12091                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12092                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12093                      break;
12094                   case switchStmt:
12095                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12096                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12097                      break;
12098                   case whileStmt:
12099                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12100                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12101                      break;
12102                   case doWhileStmt:
12103                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12104                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12105                      break;
12106                   case forStmt:
12107                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12108                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12109                      break;
12110                   case forEachStmt:
12111                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12112                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12113                      break;
12114                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12115                   case labeledStmt:
12116                   case caseStmt
12117                   case expressionStmt:
12118                   case gotoStmt:
12119                   case continueStmt:
12120                   case breakStmt
12121                   case returnStmt:
12122                   case asmStmt:
12123                   case badDeclarationStmt:
12124                   case fireWatchersStmt:
12125                   case stopWatchingStmt:
12126                   case watchStmt:
12127                   */
12128                }
12129             }
12130             if(filter)
12131             {
12132                block = MkIfStmt(filter, block, null);
12133             }
12134             if(isArray)
12135             {
12136                stmt.compound.statements = MkListOne(MkForStmt(
12137                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12138                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12139                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12140                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12141                   block));
12142               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12143               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12144               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12145             }
12146             else if(isBuiltin)
12147             {
12148                char count[128];
12149                //OldList * specs = MkList();
12150                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12151
12152                sprintf(count, "%d", builtinCount);
12153
12154                stmt.compound.statements = MkListOne(MkForStmt(
12155                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12156                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12157                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12158                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12159                   block));
12160
12161                /*
12162                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12163                stmt.compound.statements = MkListOne(MkForStmt(
12164                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12165                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12166                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12167                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12168                   block));
12169               */
12170               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12171               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12172               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12173             }
12174             else if(isLinkList && !isList)
12175             {
12176                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12177                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12178                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12179                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12180                {
12181                   stmt.compound.statements = MkListOne(MkForStmt(
12182                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12183                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12184                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12185                      block));
12186                }
12187                else
12188                {
12189                   OldList * specs = MkList();
12190                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12191                   stmt.compound.statements = MkListOne(MkForStmt(
12192                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12193                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12194                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12195                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12196                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12197                      block));
12198                }
12199                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12200                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12201                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12202             }
12203             /*else if(isCustomAVLTree)
12204             {
12205                stmt.compound.statements = MkListOne(MkForStmt(
12206                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12207                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12208                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12209                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12210                   block));
12211
12212                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12213                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12214                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12215             }*/
12216             else
12217             {
12218                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12219                   MkIdentifier("Next")), null)), block));
12220             }
12221             ProcessExpressionType(expIt);
12222             if(stmt.compound.declarations->first)
12223                ProcessDeclaration(stmt.compound.declarations->first);
12224
12225             if(symbol)
12226                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12227
12228             ProcessStatement(stmt);
12229             curContext = stmt.compound.context.parent;
12230             break;
12231          }
12232          else
12233          {
12234             Compiler_Error($"Expression is not a container\n");
12235          }
12236          break;
12237       }
12238       case gotoStmt:
12239          break;
12240       case continueStmt:
12241          break;
12242       case breakStmt:
12243          break;
12244       case returnStmt:
12245       {
12246          Expression exp;
12247          if(stmt.expressions)
12248          {
12249             for(exp = stmt.expressions->first; exp; exp = exp.next)
12250             {
12251                if(!exp.next)
12252                {
12253                   if(curFunction && !curFunction.type)
12254                      curFunction.type = ProcessType(
12255                         curFunction.specifiers, curFunction.declarator);
12256                   FreeType(exp.destType);
12257                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12258                   if(exp.destType) exp.destType.refCount++;
12259                }
12260                ProcessExpressionType(exp);
12261             }
12262          }
12263          break;
12264       }
12265       case badDeclarationStmt:
12266       {
12267          ProcessDeclaration(stmt.decl);
12268          break;
12269       }
12270       case asmStmt:
12271       {
12272          AsmField field;
12273          if(stmt.asmStmt.inputFields)
12274          {
12275             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12276                if(field.expression)
12277                   ProcessExpressionType(field.expression);
12278          }
12279          if(stmt.asmStmt.outputFields)
12280          {
12281             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12282                if(field.expression)
12283                   ProcessExpressionType(field.expression);
12284          }
12285          if(stmt.asmStmt.clobberedFields)
12286          {
12287             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12288             {
12289                if(field.expression)
12290                   ProcessExpressionType(field.expression);
12291             }
12292          }
12293          break;
12294       }
12295       case watchStmt:
12296       {
12297          PropertyWatch propWatch;
12298          OldList * watches = stmt._watch.watches;
12299          Expression object = stmt._watch.object;
12300          Expression watcher = stmt._watch.watcher;
12301          if(watcher)
12302             ProcessExpressionType(watcher);
12303          if(object)
12304             ProcessExpressionType(object);
12305
12306          if(inCompiler)
12307          {
12308             if(watcher || thisClass)
12309             {
12310                External external = curExternal;
12311                Context context = curContext;
12312
12313                stmt.type = expressionStmt;
12314                stmt.expressions = MkList();
12315
12316                curExternal = external.prev;
12317
12318                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12319                {
12320                   ClassFunction func;
12321                   char watcherName[1024];
12322                   Class watcherClass = watcher ?
12323                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12324                   External createdExternal;
12325
12326                   // Create a declaration above
12327                   External externalDecl = MkExternalDeclaration(null);
12328                   ast->Insert(curExternal.prev, externalDecl);
12329
12330                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12331                   if(propWatch.deleteWatch)
12332                      strcat(watcherName, "_delete");
12333                   else
12334                   {
12335                      Identifier propID;
12336                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12337                      {
12338                         strcat(watcherName, "_");
12339                         strcat(watcherName, propID.string);
12340                      }
12341                   }
12342
12343                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12344                   {
12345                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12346                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12347                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12348                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12349                      ProcessClassFunctionBody(func, propWatch.compound);
12350                      propWatch.compound = null;
12351
12352                      //afterExternal = afterExternal ? afterExternal : curExternal;
12353
12354                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12355                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12356                      // TESTING THIS...
12357                      createdExternal.symbol.idCode = external.symbol.idCode;
12358
12359                      curExternal = createdExternal;
12360                      ProcessFunction(createdExternal.function);
12361
12362
12363                      // Create a declaration above
12364                      {
12365                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12366                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12367                         externalDecl.declaration = decl;
12368                         if(decl.symbol && !decl.symbol.pointerExternal)
12369                            decl.symbol.pointerExternal = externalDecl;
12370                      }
12371
12372                      if(propWatch.deleteWatch)
12373                      {
12374                         OldList * args = MkList();
12375                         ListAdd(args, CopyExpression(object));
12376                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12377                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12378                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12379                      }
12380                      else
12381                      {
12382                         Class _class = object.expType._class.registered;
12383                         Identifier propID;
12384
12385                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12386                         {
12387                            char propName[1024];
12388                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12389                            if(prop)
12390                            {
12391                               char getName[1024], setName[1024];
12392                               OldList * args = MkList();
12393
12394                               DeclareProperty(prop, setName, getName);
12395
12396                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12397                               strcpy(propName, "__ecereProp_");
12398                               FullClassNameCat(propName, prop._class.fullName, false);
12399                               strcat(propName, "_");
12400                               // strcat(propName, prop.name);
12401                               FullClassNameCat(propName, prop.name, true);
12402
12403                               ListAdd(args, CopyExpression(object));
12404                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12405                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12406                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12407
12408                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12409                            }
12410                            else
12411                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12412                         }
12413                      }
12414                   }
12415                   else
12416                      Compiler_Error($"Invalid watched object\n");
12417                }
12418
12419                curExternal = external;
12420                curContext = context;
12421
12422                if(watcher)
12423                   FreeExpression(watcher);
12424                if(object)
12425                   FreeExpression(object);
12426                FreeList(watches, FreePropertyWatch);
12427             }
12428             else
12429                Compiler_Error($"No observer specified and not inside a _class\n");
12430          }
12431          else
12432          {
12433             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12434             {
12435                ProcessStatement(propWatch.compound);
12436             }
12437
12438          }
12439          break;
12440       }
12441       case fireWatchersStmt:
12442       {
12443          OldList * watches = stmt._watch.watches;
12444          Expression object = stmt._watch.object;
12445          Class _class;
12446          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12447          // printf("%X\n", watches);
12448          // printf("%X\n", stmt._watch.watches);
12449          if(object)
12450             ProcessExpressionType(object);
12451
12452          if(inCompiler)
12453          {
12454             _class = object ?
12455                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12456
12457             if(_class)
12458             {
12459                Identifier propID;
12460
12461                stmt.type = expressionStmt;
12462                stmt.expressions = MkList();
12463
12464                // Check if we're inside a property set
12465                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12466                {
12467                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12468                }
12469                else if(!watches)
12470                {
12471                   //Compiler_Error($"No property specified and not inside a property set\n");
12472                }
12473                if(watches)
12474                {
12475                   for(propID = watches->first; propID; propID = propID.next)
12476                   {
12477                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12478                      if(prop)
12479                      {
12480                         CreateFireWatcher(prop, object, stmt);
12481                      }
12482                      else
12483                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12484                   }
12485                }
12486                else
12487                {
12488                   // Fire all properties!
12489                   Property prop;
12490                   Class base;
12491                   for(base = _class; base; base = base.base)
12492                   {
12493                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12494                      {
12495                         if(prop.isProperty && prop.isWatchable)
12496                         {
12497                            CreateFireWatcher(prop, object, stmt);
12498                         }
12499                      }
12500                   }
12501                }
12502
12503                if(object)
12504                   FreeExpression(object);
12505                FreeList(watches, FreeIdentifier);
12506             }
12507             else
12508                Compiler_Error($"Invalid object specified and not inside a class\n");
12509          }
12510          break;
12511       }
12512       case stopWatchingStmt:
12513       {
12514          OldList * watches = stmt._watch.watches;
12515          Expression object = stmt._watch.object;
12516          Expression watcher = stmt._watch.watcher;
12517          Class _class;
12518          if(object)
12519             ProcessExpressionType(object);
12520          if(watcher)
12521             ProcessExpressionType(watcher);
12522          if(inCompiler)
12523          {
12524             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12525
12526             if(watcher || thisClass)
12527             {
12528                if(_class)
12529                {
12530                   Identifier propID;
12531
12532                   stmt.type = expressionStmt;
12533                   stmt.expressions = MkList();
12534
12535                   if(!watches)
12536                   {
12537                      OldList * args;
12538                      // eInstance_StopWatching(object, null, watcher);
12539                      args = MkList();
12540                      ListAdd(args, CopyExpression(object));
12541                      ListAdd(args, MkExpConstant("0"));
12542                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12543                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12544                   }
12545                   else
12546                   {
12547                      for(propID = watches->first; propID; propID = propID.next)
12548                      {
12549                         char propName[1024];
12550                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12551                         if(prop)
12552                         {
12553                            char getName[1024], setName[1024];
12554                            OldList * args = MkList();
12555
12556                            DeclareProperty(prop, setName, getName);
12557
12558                            // eInstance_StopWatching(object, prop, watcher);
12559                            strcpy(propName, "__ecereProp_");
12560                            FullClassNameCat(propName, prop._class.fullName, false);
12561                            strcat(propName, "_");
12562                            // strcat(propName, prop.name);
12563                            FullClassNameCat(propName, prop.name, true);
12564                            MangleClassName(propName);
12565
12566                            ListAdd(args, CopyExpression(object));
12567                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12568                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12569                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12570                         }
12571                         else
12572                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12573                      }
12574                   }
12575
12576                   if(object)
12577                      FreeExpression(object);
12578                   if(watcher)
12579                      FreeExpression(watcher);
12580                   FreeList(watches, FreeIdentifier);
12581                }
12582                else
12583                   Compiler_Error($"Invalid object specified and not inside a class\n");
12584             }
12585             else
12586                Compiler_Error($"No observer specified and not inside a class\n");
12587          }
12588          break;
12589       }
12590    }
12591 }
12592
12593 static void ProcessFunction(FunctionDefinition function)
12594 {
12595    Identifier id = GetDeclId(function.declarator);
12596    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12597    Type type = symbol ? symbol.type : null;
12598    Class oldThisClass = thisClass;
12599    Context oldTopContext = topContext;
12600
12601    yylloc = function.loc;
12602    // Process thisClass
12603
12604    if(type && type.thisClass)
12605    {
12606       Symbol classSym = type.thisClass;
12607       Class _class = type.thisClass.registered;
12608       char className[1024];
12609       char structName[1024];
12610       Declarator funcDecl;
12611       Symbol thisSymbol;
12612
12613       bool typedObject = false;
12614
12615       if(_class && !_class.base)
12616       {
12617          _class = currentClass;
12618          if(_class && !_class.symbol)
12619             _class.symbol = FindClass(_class.fullName);
12620          classSym = _class ? _class.symbol : null;
12621          typedObject = true;
12622       }
12623
12624       thisClass = _class;
12625
12626       if(inCompiler && _class)
12627       {
12628          if(type.kind == functionType)
12629          {
12630             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12631             {
12632                //TypeName param = symbol.type.params.first;
12633                Type param = symbol.type.params.first;
12634                symbol.type.params.Remove(param);
12635                //FreeTypeName(param);
12636                FreeType(param);
12637             }
12638             if(type.classObjectType != classPointer)
12639             {
12640                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12641                symbol.type.staticMethod = true;
12642                symbol.type.thisClass = null;
12643
12644                // HIGH DANGER: VERIFYING THIS...
12645                symbol.type.extraParam = false;
12646             }
12647          }
12648
12649          strcpy(className, "__ecereClass_");
12650          FullClassNameCat(className, _class.fullName, true);
12651
12652          MangleClassName(className);
12653
12654          structName[0] = 0;
12655          FullClassNameCat(structName, _class.fullName, false);
12656
12657          // [class] this
12658
12659
12660          funcDecl = GetFuncDecl(function.declarator);
12661          if(funcDecl)
12662          {
12663             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12664             {
12665                TypeName param = funcDecl.function.parameters->first;
12666                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12667                {
12668                   funcDecl.function.parameters->Remove(param);
12669                   FreeTypeName(param);
12670                }
12671             }
12672
12673             // DANGER: Watch for this... Check if it's a Conversion?
12674             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12675
12676             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12677             if(!function.propertyNoThis)
12678             {
12679                TypeName thisParam;
12680
12681                if(type.classObjectType != classPointer)
12682                {
12683                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12684                   if(!funcDecl.function.parameters)
12685                      funcDecl.function.parameters = MkList();
12686                   funcDecl.function.parameters->Insert(null, thisParam);
12687                }
12688
12689                if(typedObject)
12690                {
12691                   if(type.classObjectType != classPointer)
12692                   {
12693                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12694                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12695                   }
12696
12697                   thisParam = TypeName
12698                   {
12699                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12700                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12701                   };
12702                   funcDecl.function.parameters->Insert(null, thisParam);
12703                }
12704             }
12705          }
12706
12707          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12708          {
12709             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12710             funcDecl = GetFuncDecl(initDecl.declarator);
12711             if(funcDecl)
12712             {
12713                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12714                {
12715                   TypeName param = funcDecl.function.parameters->first;
12716                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12717                   {
12718                      funcDecl.function.parameters->Remove(param);
12719                      FreeTypeName(param);
12720                   }
12721                }
12722
12723                if(type.classObjectType != classPointer)
12724                {
12725                   // DANGER: Watch for this... Check if it's a Conversion?
12726                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12727                   {
12728                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12729
12730                      if(!funcDecl.function.parameters)
12731                         funcDecl.function.parameters = MkList();
12732                      funcDecl.function.parameters->Insert(null, thisParam);
12733                   }
12734                }
12735             }
12736          }
12737       }
12738
12739       // Add this to the context
12740       if(function.body)
12741       {
12742          if(type.classObjectType != classPointer)
12743          {
12744             thisSymbol = Symbol
12745             {
12746                string = CopyString("this");
12747                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12748             };
12749             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12750
12751             if(typedObject && thisSymbol.type)
12752             {
12753                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12754                thisSymbol.type.byReference = type.byReference;
12755                thisSymbol.type.typedByReference = type.byReference;
12756                /*
12757                thisSymbol = Symbol { string = CopyString("class") };
12758                function.body.compound.context.symbols.Add(thisSymbol);
12759                */
12760             }
12761          }
12762       }
12763
12764       // Pointer to class data
12765
12766       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12767       {
12768          DataMember member = null;
12769          {
12770             Class base;
12771             for(base = _class; base && base.type != systemClass; base = base.next)
12772             {
12773                for(member = base.membersAndProperties.first; member; member = member.next)
12774                   if(!member.isProperty)
12775                      break;
12776                if(member)
12777                   break;
12778             }
12779          }
12780          for(member = _class.membersAndProperties.first; member; member = member.next)
12781             if(!member.isProperty)
12782                break;
12783          if(member)
12784          {
12785             char pointerName[1024];
12786
12787             Declaration decl;
12788             Initializer initializer;
12789             Expression exp, bytePtr;
12790
12791             strcpy(pointerName, "__ecerePointer_");
12792             FullClassNameCat(pointerName, _class.fullName, false);
12793             {
12794                char className[1024];
12795                strcpy(className, "__ecereClass_");
12796                FullClassNameCat(className, classSym.string, true);
12797                MangleClassName(className);
12798
12799                // Testing This
12800                DeclareClass(classSym, className);
12801             }
12802
12803             // ((byte *) this)
12804             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12805
12806             if(_class.fixed)
12807             {
12808                char string[256];
12809                sprintf(string, "%d", _class.offset);
12810                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12811             }
12812             else
12813             {
12814                // ([bytePtr] + [className]->offset)
12815                exp = QBrackets(MkExpOp(bytePtr, '+',
12816                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12817             }
12818
12819             // (this ? [exp] : 0)
12820             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12821             exp.expType = Type
12822             {
12823                refCount = 1;
12824                kind = pointerType;
12825                type = Type { refCount = 1, kind = voidType };
12826             };
12827
12828             if(function.body)
12829             {
12830                yylloc = function.body.loc;
12831                // ([structName] *) [exp]
12832                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12833                initializer = MkInitializerAssignment(
12834                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12835
12836                // [structName] * [pointerName] = [initializer];
12837                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12838
12839                {
12840                   Context prevContext = curContext;
12841                   curContext = function.body.compound.context;
12842
12843                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12844                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12845
12846                   curContext = prevContext;
12847                }
12848
12849                // WHY?
12850                decl.symbol = null;
12851
12852                if(!function.body.compound.declarations)
12853                   function.body.compound.declarations = MkList();
12854                function.body.compound.declarations->Insert(null, decl);
12855             }
12856          }
12857       }
12858
12859
12860       // Loop through the function and replace undeclared identifiers
12861       // which are a member of the class (methods, properties or data)
12862       // by "this.[member]"
12863    }
12864    else
12865       thisClass = null;
12866
12867    if(id)
12868    {
12869       FreeSpecifier(id._class);
12870       id._class = null;
12871
12872       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12873       {
12874          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12875          id = GetDeclId(initDecl.declarator);
12876
12877          FreeSpecifier(id._class);
12878          id._class = null;
12879       }
12880    }
12881    if(function.body)
12882       topContext = function.body.compound.context;
12883    {
12884       FunctionDefinition oldFunction = curFunction;
12885       curFunction = function;
12886       if(function.body)
12887          ProcessStatement(function.body);
12888
12889       // If this is a property set and no firewatchers has been done yet, add one here
12890       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12891       {
12892          Statement prevCompound = curCompound;
12893          Context prevContext = curContext;
12894
12895          Statement fireWatchers = MkFireWatchersStmt(null, null);
12896          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12897          ListAdd(function.body.compound.statements, fireWatchers);
12898
12899          curCompound = function.body;
12900          curContext = function.body.compound.context;
12901
12902          ProcessStatement(fireWatchers);
12903
12904          curContext = prevContext;
12905          curCompound = prevCompound;
12906
12907       }
12908
12909       curFunction = oldFunction;
12910    }
12911
12912    if(function.declarator)
12913    {
12914       ProcessDeclarator(function.declarator);
12915    }
12916
12917    topContext = oldTopContext;
12918    thisClass = oldThisClass;
12919 }
12920
12921 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12922 static void ProcessClass(OldList definitions, Symbol symbol)
12923 {
12924    ClassDef def;
12925    External external = curExternal;
12926    Class regClass = symbol ? symbol.registered : null;
12927
12928    // Process all functions
12929    for(def = definitions.first; def; def = def.next)
12930    {
12931       if(def.type == functionClassDef)
12932       {
12933          if(def.function.declarator)
12934             curExternal = def.function.declarator.symbol.pointerExternal;
12935          else
12936             curExternal = external;
12937
12938          ProcessFunction((FunctionDefinition)def.function);
12939       }
12940       else if(def.type == declarationClassDef)
12941       {
12942          if(def.decl.type == instDeclaration)
12943          {
12944             thisClass = regClass;
12945             ProcessInstantiationType(def.decl.inst);
12946             thisClass = null;
12947          }
12948          // Testing this
12949          else
12950          {
12951             Class backThisClass = thisClass;
12952             if(regClass) thisClass = regClass;
12953             ProcessDeclaration(def.decl);
12954             thisClass = backThisClass;
12955          }
12956       }
12957       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12958       {
12959          MemberInit defProperty;
12960
12961          // Add this to the context
12962          Symbol thisSymbol = Symbol
12963          {
12964             string = CopyString("this");
12965             type = regClass ? MkClassType(regClass.fullName) : null;
12966          };
12967          globalContext.symbols.Add((BTNode)thisSymbol);
12968
12969          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12970          {
12971             thisClass = regClass;
12972             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12973             thisClass = null;
12974          }
12975
12976          globalContext.symbols.Remove((BTNode)thisSymbol);
12977          FreeSymbol(thisSymbol);
12978       }
12979       else if(def.type == propertyClassDef && def.propertyDef)
12980       {
12981          PropertyDef prop = def.propertyDef;
12982
12983          // Add this to the context
12984          /*
12985          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12986          globalContext.symbols.Add(thisSymbol);
12987          */
12988
12989          thisClass = regClass;
12990          if(prop.setStmt)
12991          {
12992             if(regClass)
12993             {
12994                Symbol thisSymbol
12995                {
12996                   string = CopyString("this");
12997                   type = MkClassType(regClass.fullName);
12998                };
12999                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13000             }
13001
13002             curExternal = prop.symbol ? prop.symbol.externalSet : null;
13003             ProcessStatement(prop.setStmt);
13004          }
13005          if(prop.getStmt)
13006          {
13007             if(regClass)
13008             {
13009                Symbol thisSymbol
13010                {
13011                   string = CopyString("this");
13012                   type = MkClassType(regClass.fullName);
13013                };
13014                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13015             }
13016
13017             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13018             ProcessStatement(prop.getStmt);
13019          }
13020          if(prop.issetStmt)
13021          {
13022             if(regClass)
13023             {
13024                Symbol thisSymbol
13025                {
13026                   string = CopyString("this");
13027                   type = MkClassType(regClass.fullName);
13028                };
13029                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13030             }
13031
13032             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13033             ProcessStatement(prop.issetStmt);
13034          }
13035
13036          thisClass = null;
13037
13038          /*
13039          globalContext.symbols.Remove(thisSymbol);
13040          FreeSymbol(thisSymbol);
13041          */
13042       }
13043       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13044       {
13045          PropertyWatch propertyWatch = def.propertyWatch;
13046
13047          thisClass = regClass;
13048          if(propertyWatch.compound)
13049          {
13050             Symbol thisSymbol
13051             {
13052                string = CopyString("this");
13053                type = regClass ? MkClassType(regClass.fullName) : null;
13054             };
13055
13056             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13057
13058             curExternal = null;
13059             ProcessStatement(propertyWatch.compound);
13060          }
13061          thisClass = null;
13062       }
13063    }
13064 }
13065
13066 void DeclareFunctionUtil(const String s)
13067 {
13068    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13069    if(function)
13070    {
13071       char name[1024];
13072       name[0] = 0;
13073       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13074          strcpy(name, "__ecereFunction_");
13075       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13076       DeclareFunction(function, name);
13077    }
13078 }
13079
13080 void ComputeDataTypes()
13081 {
13082    External external;
13083    External temp { };
13084    External after = null;
13085
13086    currentClass = null;
13087
13088    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13089
13090    for(external = ast->first; external; external = external.next)
13091    {
13092       if(external.type == declarationExternal)
13093       {
13094          Declaration decl = external.declaration;
13095          if(decl)
13096          {
13097             OldList * decls = decl.declarators;
13098             if(decls)
13099             {
13100                InitDeclarator initDecl = decls->first;
13101                if(initDecl)
13102                {
13103                   Declarator declarator = initDecl.declarator;
13104                   if(declarator && declarator.type == identifierDeclarator)
13105                   {
13106                      Identifier id = declarator.identifier;
13107                      if(id && id.string)
13108                      {
13109                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
13110                         {
13111                            external.symbol.id = -1001, external.symbol.idCode = -1001;
13112                            after = external;
13113                         }
13114                      }
13115                   }
13116                }
13117             }
13118          }
13119        }
13120    }
13121
13122    temp.symbol = Symbol { id = -1000, idCode = -1000 };
13123    ast->Insert(after, temp);
13124    curExternal = temp;
13125
13126    DeclareFunctionUtil("eSystem_New");
13127    DeclareFunctionUtil("eSystem_New0");
13128    DeclareFunctionUtil("eSystem_Renew");
13129    DeclareFunctionUtil("eSystem_Renew0");
13130    DeclareFunctionUtil("eSystem_Delete");
13131    DeclareFunctionUtil("eClass_GetProperty");
13132    DeclareFunctionUtil("eClass_SetProperty");
13133    DeclareFunctionUtil("eInstance_FireSelfWatchers");
13134    DeclareFunctionUtil("eInstance_SetMethod");
13135    DeclareFunctionUtil("eInstance_IncRef");
13136    DeclareFunctionUtil("eInstance_StopWatching");
13137    DeclareFunctionUtil("eInstance_Watch");
13138    DeclareFunctionUtil("eInstance_FireWatchers");
13139
13140    DeclareStruct("ecere::com::Class", false);
13141    DeclareStruct("ecere::com::Instance", false);
13142    DeclareStruct("ecere::com::Property", false);
13143    DeclareStruct("ecere::com::DataMember", false);
13144    DeclareStruct("ecere::com::Method", false);
13145    DeclareStruct("ecere::com::SerialBuffer", false);
13146    DeclareStruct("ecere::com::ClassTemplateArgument", false);
13147
13148    ast->Remove(temp);
13149
13150    for(external = ast->first; external; external = external.next)
13151    {
13152       afterExternal = curExternal = external;
13153       if(external.type == functionExternal)
13154       {
13155          currentClass = external.function._class;
13156          ProcessFunction(external.function);
13157       }
13158       // There shouldn't be any _class member access here anyways...
13159       else if(external.type == declarationExternal)
13160       {
13161          currentClass = null;
13162          if(external.declaration)
13163             ProcessDeclaration(external.declaration);
13164       }
13165       else if(external.type == classExternal)
13166       {
13167          ClassDefinition _class = external._class;
13168          currentClass = external.symbol.registered;
13169          if(_class.definitions)
13170          {
13171             ProcessClass(_class.definitions, _class.symbol);
13172          }
13173          if(inCompiler)
13174          {
13175             // Free class data...
13176             ast->Remove(external);
13177             delete external;
13178          }
13179       }
13180       else if(external.type == nameSpaceExternal)
13181       {
13182          thisNameSpace = external.id.string;
13183       }
13184    }
13185    currentClass = null;
13186    thisNameSpace = null;
13187    curExternal = null;
13188
13189    delete temp.symbol;
13190    delete temp;
13191 }