compiler/libec: Improvements to struct and class:struct casts
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50       bool backOutputLineNumbers = outputLineNumbers;
51       outputLineNumbers = false;
52
53       if(exp)
54          OutputExpression(exp, f);
55       f.Seek(0, start);
56       count = strlen(string);
57       count += f.Read(string + count, 1, 1023);
58       string[count] = '\0';
59       delete f;
60
61       outputLineNumbers = backOutputLineNumbers;
62    }
63 }
64
65 Type ProcessTemplateParameterType(TemplateParameter param)
66 {
67    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
68    {
69       // TOFIX: Will need to free this Type
70       if(!param.baseType)
71       {
72          if(param.dataTypeString)
73             param.baseType = ProcessTypeString(param.dataTypeString, false);
74          else
75             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
76       }
77       return param.baseType;
78    }
79    return null;
80 }
81
82 bool NeedCast(Type type1, Type type2)
83 {
84    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
85
86    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
87    {
88       return false;
89    }
90
91    if(type1.kind == type2.kind)
92    {
93       switch(type1.kind)
94       {
95          case _BoolType:
96          case charType:
97          case shortType:
98          case intType:
99          case int64Type:
100          case intPtrType:
101          case intSizeType:
102             if(type1.passAsTemplate && !type2.passAsTemplate)
103                return true;
104             return type1.isSigned != type2.isSigned;
105          case classType:
106             return type1._class != type2._class;
107          case pointerType:
108             return (type1.type && type2.type && type1.type.constant != type2.type.constant) || NeedCast(type1.type, type2.type);
109          default:
110             return true; //false; ????
111       }
112    }
113    return true;
114 }
115
116 static void ReplaceClassMembers(Expression exp, Class _class)
117 {
118    if(exp.type == identifierExp && exp.identifier)
119    {
120       Identifier id = exp.identifier;
121       Context ctx;
122       Symbol symbol = null;
123       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
124       {
125          // First, check if the identifier is declared inside the function
126          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
127          {
128             symbol = (Symbol)ctx.symbols.FindString(id.string);
129             if(symbol) break;
130          }
131       }
132
133       // If it is not, check if it is a member of the _class
134       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
135       {
136          Property prop = eClass_FindProperty(_class, id.string, privateModule);
137          Method method = null;
138          DataMember member = null;
139          ClassProperty classProp = null;
140          if(!prop)
141          {
142             method = eClass_FindMethod(_class, id.string, privateModule);
143          }
144          if(!prop && !method)
145             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
146          if(!prop && !method && !member)
147          {
148             classProp = eClass_FindClassProperty(_class, id.string);
149          }
150          if(prop || method || member || classProp)
151          {
152             // Replace by this.[member]
153             exp.type = memberExp;
154             exp.member.member = id;
155             exp.member.memberType = unresolvedMember;
156             exp.member.exp = QMkExpId("this");
157             //exp.member.exp.loc = exp.loc;
158             exp.addedThis = true;
159          }
160          else if(_class && _class.templateParams.first)
161          {
162             Class sClass;
163             for(sClass = _class; sClass; sClass = sClass.base)
164             {
165                if(sClass.templateParams.first)
166                {
167                   ClassTemplateParameter param;
168                   for(param = sClass.templateParams.first; param; param = param.next)
169                   {
170                      if(param.type == expression && !strcmp(param.name, id.string))
171                      {
172                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
173
174                         if(argExp)
175                         {
176                            Declarator decl;
177                            OldList * specs = MkList();
178
179                            FreeIdentifier(exp.member.member);
180
181                            ProcessExpressionType(argExp);
182
183                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
184
185                            exp.expType = ProcessType(specs, decl);
186
187                            // *[expType] *[argExp]
188                            exp.type = bracketsExp;
189                            exp.list = MkListOne(MkExpOp(null, '*',
190                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
191                         }
192                      }
193                   }
194                }
195             }
196          }
197       }
198    }
199 }
200
201 ////////////////////////////////////////////////////////////////////////
202 // PRINTING ////////////////////////////////////////////////////////////
203 ////////////////////////////////////////////////////////////////////////
204
205 public char * PrintInt(int64 result)
206 {
207    char temp[100];
208    if(result > MAXINT)
209       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
210    else
211       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
212    if(result > MAXINT || result < MININT)
213       strcat(temp, "LL");
214    return CopyString(temp);
215 }
216
217 public char * PrintUInt(uint64 result)
218 {
219    char temp[100];
220    if(result > MAXDWORD)
221       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
222    else if(result > MAXINT)
223       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
224    else
225       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
226    return CopyString(temp);
227 }
228
229 public char * PrintInt64(int64 result)
230 {
231    char temp[100];
232    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
233    return CopyString(temp);
234 }
235
236 public char * PrintUInt64(uint64 result)
237 {
238    char temp[100];
239    if(result > MAXINT64)
240       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
241    else
242       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
243    return CopyString(temp);
244 }
245
246 public char * PrintHexUInt(uint64 result)
247 {
248    char temp[100];
249    if(result > MAXDWORD)
250       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
251    else
252       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
253    if(result > MAXDWORD)
254       strcat(temp, "LL");
255    return CopyString(temp);
256 }
257
258 public char * PrintHexUInt64(uint64 result)
259 {
260    char temp[100];
261    if(result > MAXDWORD)
262       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
263    else
264       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
265    return CopyString(temp);
266 }
267
268 public char * PrintShort(short result)
269 {
270    char temp[100];
271    sprintf(temp, "%d", (unsigned short)result);
272    return CopyString(temp);
273 }
274
275 public char * PrintUShort(unsigned short result)
276 {
277    char temp[100];
278    if(result > 32767)
279       sprintf(temp, "0x%X", (int)result);
280    else
281       sprintf(temp, "%d", (int)result);
282    return CopyString(temp);
283 }
284
285 public char * PrintChar(char result)
286 {
287    char temp[100];
288    if(result > 0 && isprint(result))
289       sprintf(temp, "'%c'", result);
290    else if(result < 0)
291       sprintf(temp, "%d", (int)result);
292    else
293       //sprintf(temp, "%#X", result);
294       sprintf(temp, "0x%X", (unsigned char)result);
295    return CopyString(temp);
296 }
297
298 public char * PrintUChar(unsigned char result)
299 {
300    char temp[100];
301    sprintf(temp, "0x%X", result);
302    return CopyString(temp);
303 }
304
305 public char * PrintFloat(float result)
306 {
307    char temp[350];
308    if(result.isInf)
309    {
310       if(result.signBit)
311          strcpy(temp, "-inf");
312       else
313          strcpy(temp, "inf");
314    }
315    else if(result.isNan)
316    {
317       if(result.signBit)
318          strcpy(temp, "-nan");
319       else
320          strcpy(temp, "nan");
321    }
322    else
323       sprintf(temp, "%.16ff", result);
324    return CopyString(temp);
325 }
326
327 public char * PrintDouble(double result)
328 {
329    char temp[350];
330    if(result.isInf)
331    {
332       if(result.signBit)
333          strcpy(temp, "-inf");
334       else
335          strcpy(temp, "inf");
336    }
337    else if(result.isNan)
338    {
339       if(result.signBit)
340          strcpy(temp, "-nan");
341       else
342          strcpy(temp, "nan");
343    }
344    else
345       sprintf(temp, "%.16f", result);
346    return CopyString(temp);
347 }
348
349 ////////////////////////////////////////////////////////////////////////
350 ////////////////////////////////////////////////////////////////////////
351
352 //public Operand GetOperand(Expression exp);
353
354 #define GETVALUE(name, t) \
355    public bool GetOp##name(Operand op2, t * value2) \
356    {                                                        \
357       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
358       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
359       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
360       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
361       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
362       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
363       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
364       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
365       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
366       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
367       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
368       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
369       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
370       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
371       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
372       else                                                                          \
373          return false;                                                              \
374       return true;                                                                  \
375    } \
376    public bool Get##name(Expression exp, t * value2) \
377    {                                                        \
378       Operand op2 = GetOperand(exp);                        \
379       return GetOp##name(op2, value2); \
380    }
381
382 // To help the deubugger currently not preprocessing...
383 #define HELP(x) x
384
385 GETVALUE(Int, HELP(int));
386 GETVALUE(UInt, HELP(unsigned int));
387 GETVALUE(Int64, HELP(int64));
388 GETVALUE(UInt64, HELP(uint64));
389 GETVALUE(IntPtr, HELP(intptr));
390 GETVALUE(UIntPtr, HELP(uintptr));
391 GETVALUE(IntSize, HELP(intsize));
392 GETVALUE(UIntSize, HELP(uintsize));
393 GETVALUE(Short, HELP(short));
394 GETVALUE(UShort, HELP(unsigned short));
395 GETVALUE(Char, HELP(char));
396 GETVALUE(UChar, HELP(unsigned char));
397 GETVALUE(Float, HELP(float));
398 GETVALUE(Double, HELP(double));
399
400 void ComputeExpression(Expression exp);
401
402 void ComputeClassMembers(Class _class, bool isMember)
403 {
404    DataMember member = isMember ? (DataMember) _class : null;
405    Context context = isMember ? null : SetupTemplatesContext(_class);
406    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
407                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
408    {
409       int unionMemberOffset = 0;
410       int bitFields = 0;
411
412       /*
413       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
414          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
415       */
416
417       if(member)
418       {
419          member.memberOffset = 0;
420          if(targetBits < sizeof(void *) * 8)
421             member.structAlignment = 0;
422       }
423       else if(targetBits < sizeof(void *) * 8)
424          _class.structAlignment = 0;
425
426       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
427       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
428          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
429
430       if(!member && _class.destructionWatchOffset)
431          _class.memberOffset += sizeof(OldList);
432
433       // To avoid reentrancy...
434       //_class.structSize = -1;
435
436       {
437          DataMember dataMember;
438          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
439          {
440             if(!dataMember.isProperty)
441             {
442                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
443                {
444                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
445                   /*if(!dataMember.dataType)
446                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
447                      */
448                }
449             }
450          }
451       }
452
453       {
454          DataMember dataMember;
455          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
456          {
457             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
458             {
459                if(!isMember && _class.type == bitClass && dataMember.dataType)
460                {
461                   BitMember bitMember = (BitMember) dataMember;
462                   uint64 mask = 0;
463                   int d;
464
465                   ComputeTypeSize(dataMember.dataType);
466
467                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
468                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
469
470                   _class.memberOffset = bitMember.pos + bitMember.size;
471                   for(d = 0; d<bitMember.size; d++)
472                   {
473                      if(d)
474                         mask <<= 1;
475                      mask |= 1;
476                   }
477                   bitMember.mask = mask << bitMember.pos;
478                }
479                else if(dataMember.type == normalMember && dataMember.dataType)
480                {
481                   int size;
482                   int alignment = 0;
483
484                   // Prevent infinite recursion
485                   if(dataMember.dataType.kind != classType ||
486                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
487                      _class.type != structClass)))
488                      ComputeTypeSize(dataMember.dataType);
489
490                   if(dataMember.dataType.bitFieldCount)
491                   {
492                      bitFields += dataMember.dataType.bitFieldCount;
493                      size = 0;
494                   }
495                   else
496                   {
497                      if(bitFields)
498                      {
499                         int size = (bitFields + 7) / 8;
500
501                         if(isMember)
502                         {
503                            // TESTING THIS PADDING CODE
504                            if(alignment)
505                            {
506                               member.structAlignment = Max(member.structAlignment, alignment);
507
508                               if(member.memberOffset % alignment)
509                                  member.memberOffset += alignment - (member.memberOffset % alignment);
510                            }
511
512                            dataMember.offset = member.memberOffset;
513                            if(member.type == unionMember)
514                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
515                            else
516                            {
517                               member.memberOffset += size;
518                            }
519                         }
520                         else
521                         {
522                            // TESTING THIS PADDING CODE
523                            if(alignment)
524                            {
525                               _class.structAlignment = Max(_class.structAlignment, alignment);
526
527                               if(_class.memberOffset % alignment)
528                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
529                            }
530
531                            dataMember.offset = _class.memberOffset;
532                            _class.memberOffset += size;
533                         }
534                         bitFields = 0;
535                      }
536                      size = dataMember.dataType.size;
537                      alignment = dataMember.dataType.alignment;
538                   }
539
540                   if(isMember)
541                   {
542                      // TESTING THIS PADDING CODE
543                      if(alignment)
544                      {
545                         member.structAlignment = Max(member.structAlignment, alignment);
546
547                         if(member.memberOffset % alignment)
548                            member.memberOffset += alignment - (member.memberOffset % alignment);
549                      }
550
551                      dataMember.offset = member.memberOffset;
552                      if(member.type == unionMember)
553                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
554                      else
555                      {
556                         member.memberOffset += size;
557                      }
558                   }
559                   else
560                   {
561                      // TESTING THIS PADDING CODE
562                      if(alignment)
563                      {
564                         _class.structAlignment = Max(_class.structAlignment, alignment);
565
566                         if(_class.memberOffset % alignment)
567                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
568                      }
569
570                      dataMember.offset = _class.memberOffset;
571                      _class.memberOffset += size;
572                   }
573                }
574                else
575                {
576                   int alignment;
577
578                   ComputeClassMembers((Class)dataMember, true);
579                   alignment = dataMember.structAlignment;
580
581                   if(isMember)
582                   {
583                      if(alignment)
584                      {
585                         if(member.memberOffset % alignment)
586                            member.memberOffset += alignment - (member.memberOffset % alignment);
587
588                         member.structAlignment = Max(member.structAlignment, alignment);
589                      }
590                      dataMember.offset = member.memberOffset;
591                      if(member.type == unionMember)
592                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
593                      else
594                         member.memberOffset += dataMember.memberOffset;
595                   }
596                   else
597                   {
598                      if(alignment)
599                      {
600                         if(_class.memberOffset % alignment)
601                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
602                         _class.structAlignment = Max(_class.structAlignment, alignment);
603                      }
604                      dataMember.offset = _class.memberOffset;
605                      _class.memberOffset += dataMember.memberOffset;
606                   }
607                }
608             }
609          }
610          if(bitFields)
611          {
612             int alignment = 0;
613             int size = (bitFields + 7) / 8;
614
615             if(isMember)
616             {
617                // TESTING THIS PADDING CODE
618                if(alignment)
619                {
620                   member.structAlignment = Max(member.structAlignment, alignment);
621
622                   if(member.memberOffset % alignment)
623                      member.memberOffset += alignment - (member.memberOffset % alignment);
624                }
625
626                if(member.type == unionMember)
627                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
628                else
629                {
630                   member.memberOffset += size;
631                }
632             }
633             else
634             {
635                // TESTING THIS PADDING CODE
636                if(alignment)
637                {
638                   _class.structAlignment = Max(_class.structAlignment, alignment);
639
640                   if(_class.memberOffset % alignment)
641                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
642                }
643                _class.memberOffset += size;
644             }
645             bitFields = 0;
646          }
647       }
648       if(member && member.type == unionMember)
649       {
650          member.memberOffset = unionMemberOffset;
651       }
652
653       if(!isMember)
654       {
655          /*if(_class.type == structClass)
656             _class.size = _class.memberOffset;
657          else
658          */
659
660          if(_class.type != bitClass)
661          {
662             int extra = 0;
663             if(_class.structAlignment)
664             {
665                if(_class.memberOffset % _class.structAlignment)
666                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
667             }
668             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
669             if(!member)
670             {
671                Property prop;
672                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
673                {
674                   if(prop.isProperty && prop.isWatchable)
675                   {
676                      prop.watcherOffset = _class.structSize;
677                      _class.structSize += sizeof(OldList);
678                   }
679                }
680             }
681
682             // Fix Derivatives
683             {
684                OldLink derivative;
685                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
686                {
687                   Class deriv = derivative.data;
688
689                   if(deriv.computeSize)
690                   {
691                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
692                      deriv.offset = /*_class.offset + */_class.structSize;
693                      deriv.memberOffset = 0;
694                      // ----------------------
695
696                      deriv.structSize = deriv.offset;
697
698                      ComputeClassMembers(deriv, false);
699                   }
700                }
701             }
702          }
703       }
704    }
705    if(context)
706       FinishTemplatesContext(context);
707 }
708
709 public void ComputeModuleClasses(Module module)
710 {
711    Class _class;
712    OldLink subModule;
713
714    for(subModule = module.modules.first; subModule; subModule = subModule.next)
715       ComputeModuleClasses(subModule.data);
716    for(_class = module.classes.first; _class; _class = _class.next)
717       ComputeClassMembers(_class, false);
718 }
719
720
721 public int ComputeTypeSize(Type type)
722 {
723    uint size = type ? type.size : 0;
724    if(!size && type && !type.computing)
725    {
726       type.computing = true;
727       switch(type.kind)
728       {
729          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
730          case charType: type.alignment = size = sizeof(char); break;
731          case intType: type.alignment = size = sizeof(int); break;
732          case int64Type: type.alignment = size = sizeof(int64); break;
733          case intPtrType: type.alignment = size = targetBits / 8; break;
734          case intSizeType: type.alignment = size = targetBits / 8; break;
735          case longType: type.alignment = size = sizeof(long); break;
736          case shortType: type.alignment = size = sizeof(short); break;
737          case floatType: type.alignment = size = sizeof(float); break;
738          case doubleType: type.alignment = size = sizeof(double); break;
739          case classType:
740          {
741             Class _class = type._class ? type._class.registered : null;
742
743             if(_class && _class.type == structClass)
744             {
745                // Ensure all members are properly registered
746                ComputeClassMembers(_class, false);
747                type.alignment = _class.structAlignment;
748                size = _class.structSize;
749                if(type.alignment && size % type.alignment)
750                   size += type.alignment - (size % type.alignment);
751
752             }
753             else if(_class && (_class.type == unitClass ||
754                    _class.type == enumClass ||
755                    _class.type == bitClass))
756             {
757                if(!_class.dataType)
758                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
759                size = type.alignment = ComputeTypeSize(_class.dataType);
760             }
761             else
762                size = type.alignment = targetBits / 8; // sizeof(Instance *);
763             break;
764          }
765          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
766          case arrayType:
767             if(type.arraySizeExp)
768             {
769                ProcessExpressionType(type.arraySizeExp);
770                ComputeExpression(type.arraySizeExp);
771                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType &&
772                   type.arraySizeExp.expType.kind != shortType &&
773                   type.arraySizeExp.expType.kind != charType &&
774                   type.arraySizeExp.expType.kind != longType &&
775                   type.arraySizeExp.expType.kind != int64Type &&
776                   type.arraySizeExp.expType.kind != intSizeType &&
777                   type.arraySizeExp.expType.kind != intPtrType &&
778                   type.arraySizeExp.expType.kind != enumType &&
779                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
780                {
781                   Location oldLoc = yylloc;
782                   // bool isConstant = type.arraySizeExp.isConstant;
783                   char expression[10240];
784                   expression[0] = '\0';
785                   type.arraySizeExp.expType = null;
786                   yylloc = type.arraySizeExp.loc;
787                   if(inCompiler)
788                      PrintExpression(type.arraySizeExp, expression);
789                   Compiler_Error($"Array size not constant int (%s)\n", expression);
790                   yylloc = oldLoc;
791                }
792                GetInt(type.arraySizeExp, &type.arraySize);
793             }
794             else if(type.enumClass)
795             {
796                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
797                {
798                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
799                }
800                else
801                   type.arraySize = 0;
802             }
803             else
804             {
805                // Unimplemented auto size
806                type.arraySize = 0;
807             }
808
809             size = ComputeTypeSize(type.type) * type.arraySize;
810             if(type.type)
811                type.alignment = type.type.alignment;
812
813             break;
814          case structType:
815          {
816             Type member;
817             for(member = type.members.first; member; member = member.next)
818             {
819                uint addSize = ComputeTypeSize(member);
820
821                member.offset = size;
822                if(member.alignment && size % member.alignment)
823                   member.offset += member.alignment - (size % member.alignment);
824                size = member.offset;
825
826                type.alignment = Max(type.alignment, member.alignment);
827                size += addSize;
828             }
829             if(type.alignment && size % type.alignment)
830                size += type.alignment - (size % type.alignment);
831             break;
832          }
833          case unionType:
834          {
835             Type member;
836             for(member = type.members.first; member; member = member.next)
837             {
838                uint addSize = ComputeTypeSize(member);
839
840                member.offset = size;
841                if(member.alignment && size % member.alignment)
842                   member.offset += member.alignment - (size % member.alignment);
843                size = member.offset;
844
845                type.alignment = Max(type.alignment, member.alignment);
846                size = Max(size, addSize);
847             }
848             if(type.alignment && size % type.alignment)
849                size += type.alignment - (size % type.alignment);
850             break;
851          }
852          case templateType:
853          {
854             TemplateParameter param = type.templateParameter;
855             Type baseType = ProcessTemplateParameterType(param);
856             if(baseType)
857             {
858                size = ComputeTypeSize(baseType);
859                type.alignment = baseType.alignment;
860             }
861             else
862                type.alignment = size = sizeof(uint64);
863             break;
864          }
865          case enumType:
866          {
867             type.alignment = size = sizeof(enum { test });
868             break;
869          }
870          case thisClassType:
871          {
872             type.alignment = size = targetBits / 8; //sizeof(void *);
873             break;
874          }
875       }
876       type.size = size;
877       type.computing = false;
878    }
879    return size;
880 }
881
882
883 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
884 {
885    // This function is in need of a major review when implementing private members etc.
886    DataMember topMember = isMember ? (DataMember) _class : null;
887    uint totalSize = 0;
888    uint maxSize = 0;
889    int alignment;
890    uint size;
891    DataMember member;
892    int anonID = 1;
893    Context context = isMember ? null : SetupTemplatesContext(_class);
894    if(addedPadding)
895       *addedPadding = false;
896
897    if(!isMember && _class.base)
898    {
899       maxSize = _class.structSize;
900       //if(_class.base.type != systemClass) // Commented out with new Instance _class
901       {
902          // DANGER: Testing this noHeadClass here...
903          if(_class.type == structClass || _class.type == noHeadClass)
904             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
905          else
906          {
907             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
908             if(maxSize > baseSize)
909                maxSize -= baseSize;
910             else
911                maxSize = 0;
912          }
913       }
914    }
915
916    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
917    {
918       if(!member.isProperty)
919       {
920          switch(member.type)
921          {
922             case normalMember:
923             {
924                if(member.dataTypeString)
925                {
926                   OldList * specs = MkList(), * decls = MkList();
927                   Declarator decl;
928
929                   decl = SpecDeclFromString(member.dataTypeString, specs,
930                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
931                   ListAdd(decls, MkStructDeclarator(decl, null));
932                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
933
934                   if(!member.dataType)
935                      member.dataType = ProcessType(specs, decl);
936
937                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
938
939                   {
940                      Type type = ProcessType(specs, decl);
941                      DeclareType(member.dataType, false, false);
942                      FreeType(type);
943                   }
944                   /*
945                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
946                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
947                      DeclareStruct(member.dataType._class.string, false);
948                   */
949
950                   ComputeTypeSize(member.dataType);
951                   size = member.dataType.size;
952                   alignment = member.dataType.alignment;
953
954                   if(alignment)
955                   {
956                      if(totalSize % alignment)
957                         totalSize += alignment - (totalSize % alignment);
958                   }
959                   totalSize += size;
960                }
961                break;
962             }
963             case unionMember:
964             case structMember:
965             {
966                OldList * specs = MkList(), * list = MkList();
967                char id[100];
968                sprintf(id, "__anon%d", anonID++);
969
970                size = 0;
971                AddMembers(list, (Class)member, true, &size, topClass, null);
972                ListAdd(specs,
973                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
974                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, MkListOne(MkDeclaratorIdentifier(MkIdentifier(id))),null)));
975                alignment = member.structAlignment;
976
977                if(alignment)
978                {
979                   if(totalSize % alignment)
980                      totalSize += alignment - (totalSize % alignment);
981                }
982                totalSize += size;
983                break;
984             }
985          }
986       }
987    }
988    if(retSize)
989    {
990       if(topMember && topMember.type == unionMember)
991          *retSize = Max(*retSize, totalSize);
992       else
993          *retSize += totalSize;
994    }
995    else if(totalSize < maxSize && _class.type != systemClass)
996    {
997       int autoPadding = 0;
998       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
999          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
1000       if(totalSize + autoPadding < maxSize)
1001       {
1002          char sizeString[50];
1003          sprintf(sizeString, "%d", maxSize - totalSize);
1004          ListAdd(declarations,
1005             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
1006             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
1007          if(addedPadding)
1008             *addedPadding = true;
1009       }
1010    }
1011    if(context)
1012       FinishTemplatesContext(context);
1013    return topMember ? topMember.memberID : _class.memberID;
1014 }
1015
1016 static int DeclareMembers(Class _class, bool isMember)
1017 {
1018    DataMember topMember = isMember ? (DataMember) _class : null;
1019    DataMember member;
1020    Context context = isMember ? null : SetupTemplatesContext(_class);
1021
1022    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1023       DeclareMembers(_class.base, false);
1024
1025    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1026    {
1027       if(!member.isProperty)
1028       {
1029          switch(member.type)
1030          {
1031             case normalMember:
1032             {
1033                /*
1034                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1035                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1036                   DeclareStruct(member.dataType._class.string, false);
1037                   */
1038                if(!member.dataType && member.dataTypeString)
1039                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1040                if(member.dataType)
1041                   DeclareType(member.dataType, false, false);
1042                break;
1043             }
1044             case unionMember:
1045             case structMember:
1046             {
1047                DeclareMembers((Class)member, true);
1048                break;
1049             }
1050          }
1051       }
1052    }
1053    if(context)
1054       FinishTemplatesContext(context);
1055
1056    return topMember ? topMember.memberID : _class.memberID;
1057 }
1058
1059 static void IdentifyAnonStructs(OldList/*<ClassDef>*/ *  definitions)
1060 {
1061    ClassDef def;
1062    int anonID = 1;
1063    for(def = definitions->first; def; def = def.next)
1064    {
1065       if(def.type == declarationClassDef)
1066       {
1067          Declaration decl = def.decl;
1068          if(decl && decl.specifiers)
1069          {
1070             Specifier spec;
1071             bool isStruct = false;
1072             for(spec = decl.specifiers->first; spec; spec = spec.next)
1073             {
1074                if(spec.type == structSpecifier || spec.type == unionSpecifier)
1075                {
1076                   if(spec.definitions)
1077                      IdentifyAnonStructs(spec.definitions);
1078                   isStruct = true;
1079                }
1080             }
1081             if(isStruct)
1082             {
1083                Declarator d = null;
1084                if(decl.declarators)
1085                {
1086                   for(d = decl.declarators->first; d; d = d.next)
1087                   {
1088                      Identifier idDecl = GetDeclId(d);
1089                      if(idDecl)
1090                         break;
1091                   }
1092                }
1093                if(!d)
1094                {
1095                   char id[100];
1096                   sprintf(id, "__anon%d", anonID++);
1097                   if(!decl.declarators)
1098                      decl.declarators = MkList();
1099                   ListAdd(decl.declarators, MkDeclaratorIdentifier(MkIdentifier(id)));
1100                }
1101             }
1102          }
1103       }
1104    }
1105 }
1106
1107 void DeclareStruct(const char * name, bool skipNoHead)
1108 {
1109    External external = null;
1110    Symbol classSym = FindClass(name);
1111
1112    if(!inCompiler || !classSym) return;
1113
1114    // We don't need any declaration for bit classes...
1115    if(classSym.registered &&
1116       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1117       return;
1118
1119    /*if(classSym.registered.templateClass)
1120       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1121    */
1122
1123    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1124    {
1125       // Add typedef struct
1126       Declaration decl;
1127       OldList * specifiers, * declarators;
1128       OldList * declarations = null;
1129       char structName[1024];
1130       Specifier spec = null;
1131       external = (classSym.registered && classSym.registered.type == structClass) ?
1132          classSym.pointerExternal : classSym.structExternal;
1133
1134       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1135       // Moved this one up because DeclareClass done later will need it
1136
1137       classSym.declaring++;
1138
1139       if(strchr(classSym.string, '<'))
1140       {
1141          if(classSym.registered.templateClass)
1142          {
1143             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1144             classSym.declaring--;
1145          }
1146          return;
1147       }
1148
1149       //if(!skipNoHead)
1150          DeclareMembers(classSym.registered, false);
1151
1152       structName[0] = 0;
1153       FullClassNameCat(structName, name, false);
1154
1155       if(external && external.declaration && external.declaration.specifiers)
1156       {
1157          for(spec = external.declaration.specifiers->first; spec; spec = spec.next)
1158          {
1159             if(spec.type == structSpecifier || spec.type == unionSpecifier)
1160                break;
1161          }
1162       }
1163
1164       /*if(!external)
1165          external = MkExternalDeclaration(null);*/
1166
1167       if(!skipNoHead && (!spec || !spec.definitions))
1168       {
1169          bool addedPadding = false;
1170          classSym.declaredStructSym = true;
1171
1172          declarations = MkList();
1173
1174          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1175
1176          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1177          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1178
1179          if(!declarations->count || (declarations->count == 1 && addedPadding))
1180          {
1181             FreeList(declarations, FreeClassDef);
1182             declarations = null;
1183          }
1184       }
1185       if(skipNoHead || declarations)
1186       {
1187          if(spec)
1188          {
1189             if(declarations)
1190                spec.definitions = declarations;
1191
1192             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1193             {
1194                // TODO: Fix this
1195                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1196
1197                // DANGER
1198                if(classSym.structExternal)
1199                   ast->Move(classSym.structExternal, curExternal.prev);
1200                ast->Move(classSym.pointerExternal, curExternal.prev);
1201
1202                classSym.id = curExternal.symbol.idCode;
1203                classSym.idCode = curExternal.symbol.idCode;
1204                // external = classSym.pointerExternal;
1205                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1206             }
1207          }
1208          else
1209          {
1210             if(!external)
1211                external = MkExternalDeclaration(null);
1212
1213             specifiers = MkList();
1214             declarators = MkList();
1215             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1216
1217             /*
1218             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1219             ListAdd(declarators, MkInitDeclarator(d, null));
1220             */
1221             external.declaration = decl = MkDeclaration(specifiers, declarators);
1222             if(decl.symbol && !decl.symbol.pointerExternal)
1223                decl.symbol.pointerExternal = external;
1224
1225             // For simple classes, keep the declaration as the external to move around
1226             if(classSym.registered && classSym.registered.type == structClass)
1227             {
1228                char className[1024];
1229                strcpy(className, "__ecereClass_");
1230                FullClassNameCat(className, classSym.string, true);
1231                //MangleClassName(className);
1232
1233                // Testing This
1234                DeclareClass(classSym, className);
1235
1236                external.symbol = classSym;
1237                classSym.pointerExternal = external;
1238                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1239                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1240             }
1241             else
1242             {
1243                char className[1024];
1244                strcpy(className, "__ecereClass_");
1245                FullClassNameCat(className, classSym.string, true);
1246                //MangleClassName(className);
1247
1248                // TOFIX: TESTING THIS...
1249                classSym.structExternal = external;
1250                DeclareClass(classSym, className);
1251                external.symbol = classSym;
1252             }
1253
1254             //if(curExternal)
1255                ast->Insert(curExternal ? curExternal.prev : null, external);
1256          }
1257       }
1258
1259       classSym.declaring--;
1260    }
1261    else
1262    {
1263       if(classSym.structExternal && classSym.structExternal.declaration && classSym.structExternal.declaration.specifiers)
1264       {
1265          Specifier spec;
1266          for(spec = classSym.structExternal.declaration.specifiers->first; spec; spec = spec.next)
1267          {
1268             IdentifyAnonStructs(spec.definitions);
1269          }
1270       }
1271
1272       if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1273       {
1274          // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1275          // Moved this one up because DeclareClass done later will need it
1276
1277          // TESTING THIS:
1278          classSym.declaring++;
1279
1280          //if(!skipNoHead)
1281          {
1282             if(classSym.registered)
1283                DeclareMembers(classSym.registered, false);
1284          }
1285
1286          if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1287          {
1288             // TODO: Fix this
1289             //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1290
1291             // DANGER
1292             if(classSym.structExternal)
1293                ast->Move(classSym.structExternal, curExternal.prev);
1294             ast->Move(classSym.pointerExternal, curExternal.prev);
1295
1296             classSym.id = curExternal.symbol.idCode;
1297             classSym.idCode = curExternal.symbol.idCode;
1298             // external = classSym.pointerExternal;
1299             // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1300          }
1301
1302          classSym.declaring--;
1303       }
1304    }
1305    //return external;
1306 }
1307
1308 void DeclareProperty(Property prop, char * setName, char * getName)
1309 {
1310    Symbol symbol = prop.symbol;
1311
1312    strcpy(setName, "__ecereProp_");
1313    FullClassNameCat(setName, prop._class.fullName, false);
1314    strcat(setName, "_Set_");
1315    // strcat(setName, prop.name);
1316    FullClassNameCat(setName, prop.name, true);
1317    //MangleClassName(setName);
1318
1319    strcpy(getName, "__ecereProp_");
1320    FullClassNameCat(getName, prop._class.fullName, false);
1321    strcat(getName, "_Get_");
1322    FullClassNameCat(getName, prop.name, true);
1323    // strcat(getName, prop.name);
1324
1325    // To support "char *" property
1326    //MangleClassName(getName);
1327
1328    if(prop._class.type == structClass)
1329       DeclareStruct(prop._class.fullName, false);
1330
1331    if(!symbol || curExternal.symbol.idCode < symbol.id)
1332    {
1333       bool imported = false;
1334       bool dllImport = false;
1335
1336       if(!symbol || symbol._import)
1337       {
1338          if(!symbol)
1339          {
1340             Symbol classSym;
1341             if(!prop._class.symbol)
1342                prop._class.symbol = FindClass(prop._class.fullName);
1343             classSym = prop._class.symbol;
1344             if(classSym && !classSym._import)
1345             {
1346                ModuleImport module;
1347
1348                if(prop._class.module)
1349                   module = FindModule(prop._class.module);
1350                else
1351                   module = mainModule;
1352
1353                classSym._import = ClassImport
1354                {
1355                   name = CopyString(prop._class.fullName);
1356                   isRemote = prop._class.isRemote;
1357                };
1358                module.classes.Add(classSym._import);
1359             }
1360             symbol = prop.symbol = Symbol { };
1361             symbol._import = (ClassImport)PropertyImport
1362             {
1363                name = CopyString(prop.name);
1364                isVirtual = false; //prop.isVirtual;
1365                hasSet = prop.Set ? true : false;
1366                hasGet = prop.Get ? true : false;
1367             };
1368             if(classSym)
1369                classSym._import.properties.Add(symbol._import);
1370          }
1371          imported = true;
1372          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1373          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1374             prop._class.module.importType != staticImport)
1375             dllImport = true;
1376       }
1377
1378       if(!symbol.type)
1379       {
1380          Context context = SetupTemplatesContext(prop._class);
1381          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1382          FinishTemplatesContext(context);
1383       }
1384
1385       // Get
1386       if(prop.Get)
1387       {
1388          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1389          {
1390             Declaration decl;
1391             OldList * specifiers, * declarators;
1392             Declarator d;
1393             OldList * params;
1394             Specifier spec;
1395             External external;
1396             Declarator typeDecl;
1397             bool simple = false;
1398
1399             specifiers = MkList();
1400             declarators = MkList();
1401             params = MkList();
1402
1403             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1404                MkDeclaratorIdentifier(MkIdentifier("this"))));
1405
1406             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1407             //if(imported)
1408             if(dllImport)
1409                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1410
1411             {
1412                Context context = SetupTemplatesContext(prop._class);
1413                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1414                FinishTemplatesContext(context);
1415             }
1416
1417             // Make sure the simple _class's type is declared
1418             for(spec = specifiers->first; spec; spec = spec.next)
1419             {
1420                if(spec.type == nameSpecifier /*SpecifierClass*/)
1421                {
1422                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1423                   {
1424                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1425                      symbol._class = classSym.registered;
1426                      if(classSym.registered && classSym.registered.type == structClass)
1427                      {
1428                         DeclareStruct(spec.name, false);
1429                         simple = true;
1430                      }
1431                   }
1432                }
1433             }
1434
1435             if(!simple)
1436                d = PlugDeclarator(typeDecl, d);
1437             else
1438             {
1439                ListAdd(params, MkTypeName(specifiers,
1440                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1441                specifiers = MkList();
1442             }
1443
1444             d = MkDeclaratorFunction(d, params);
1445
1446             //if(imported)
1447             if(dllImport)
1448                specifiers->Insert(null, MkSpecifier(EXTERN));
1449             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1450                specifiers->Insert(null, MkSpecifier(STATIC));
1451             if(simple)
1452                ListAdd(specifiers, MkSpecifier(VOID));
1453
1454             ListAdd(declarators, MkInitDeclarator(d, null));
1455
1456             decl = MkDeclaration(specifiers, declarators);
1457
1458             external = MkExternalDeclaration(decl);
1459             ast->Insert(curExternal.prev, external);
1460             external.symbol = symbol;
1461             symbol.externalGet = external;
1462
1463             ReplaceThisClassSpecifiers(specifiers, prop._class);
1464
1465             if(typeDecl)
1466                FreeDeclarator(typeDecl);
1467          }
1468          else
1469          {
1470             // Move declaration higher...
1471             ast->Move(symbol.externalGet, curExternal.prev);
1472          }
1473       }
1474
1475       // Set
1476       if(prop.Set)
1477       {
1478          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1479          {
1480             Declaration decl;
1481             OldList * specifiers, * declarators;
1482             Declarator d;
1483             OldList * params;
1484             Specifier spec;
1485             External external;
1486             Declarator typeDecl;
1487
1488             declarators = MkList();
1489             params = MkList();
1490
1491             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1492             if(!prop.conversion || prop._class.type == structClass)
1493             {
1494                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1495                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1496             }
1497
1498             specifiers = MkList();
1499
1500             {
1501                Context context = SetupTemplatesContext(prop._class);
1502                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1503                   MkDeclaratorIdentifier(MkIdentifier("value")));
1504                FinishTemplatesContext(context);
1505             }
1506             if(!strcmp(prop._class.base.fullName, "eda::Row") || !strcmp(prop._class.base.fullName, "eda::Id"))
1507                specifiers->Insert(null, MkSpecifier(CONST));
1508
1509             ListAdd(params, MkTypeName(specifiers, d));
1510
1511             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1512             //if(imported)
1513             if(dllImport)
1514                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1515             d = MkDeclaratorFunction(d, params);
1516
1517             // Make sure the simple _class's type is declared
1518             for(spec = specifiers->first; spec; spec = spec.next)
1519             {
1520                if(spec.type == nameSpecifier /*SpecifierClass*/)
1521                {
1522                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1523                   {
1524                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1525                      symbol._class = classSym.registered;
1526                      if(classSym.registered && classSym.registered.type == structClass)
1527                         DeclareStruct(spec.name, false);
1528                   }
1529                }
1530             }
1531
1532             ListAdd(declarators, MkInitDeclarator(d, null));
1533
1534             specifiers = MkList();
1535             //if(imported)
1536             if(dllImport)
1537                specifiers->Insert(null, MkSpecifier(EXTERN));
1538             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1539                specifiers->Insert(null, MkSpecifier(STATIC));
1540
1541             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1542             if(!prop.conversion || prop._class.type == structClass)
1543                ListAdd(specifiers, MkSpecifier(VOID));
1544             else
1545                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1546
1547             decl = MkDeclaration(specifiers, declarators);
1548
1549             external = MkExternalDeclaration(decl);
1550             ast->Insert(curExternal.prev, external);
1551             external.symbol = symbol;
1552             symbol.externalSet = external;
1553
1554             ReplaceThisClassSpecifiers(specifiers, prop._class);
1555          }
1556          else
1557          {
1558             // Move declaration higher...
1559             ast->Move(symbol.externalSet, curExternal.prev);
1560          }
1561       }
1562
1563       // Property (for Watchers)
1564       if(!symbol.externalPtr)
1565       {
1566          Declaration decl;
1567          External external;
1568          OldList * specifiers = MkList();
1569          char propName[1024];
1570
1571          if(imported)
1572             specifiers->Insert(null, MkSpecifier(EXTERN));
1573          else
1574             specifiers->Insert(null, MkSpecifier(STATIC));
1575
1576          ListAdd(specifiers, MkSpecifierName("Property"));
1577
1578          strcpy(propName, "__ecereProp_");
1579          FullClassNameCat(propName, prop._class.fullName, false);
1580          strcat(propName, "_");
1581          FullClassNameCat(propName, prop.name, true);
1582          // strcat(propName, prop.name);
1583          //MangleClassName(propName);
1584
1585          {
1586             OldList * list = MkList();
1587             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1588                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1589
1590             if(!imported)
1591             {
1592                strcpy(propName, "__ecerePropM_");
1593                FullClassNameCat(propName, prop._class.fullName, false);
1594                strcat(propName, "_");
1595                // strcat(propName, prop.name);
1596                FullClassNameCat(propName, prop.name, true);
1597
1598                //MangleClassName(propName);
1599
1600                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1601                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1602             }
1603             decl = MkDeclaration(specifiers, list);
1604          }
1605
1606          external = MkExternalDeclaration(decl);
1607          ast->Insert(curExternal.prev, external);
1608          external.symbol = symbol;
1609          symbol.externalPtr = external;
1610       }
1611       else
1612       {
1613          // Move declaration higher...
1614          ast->Move(symbol.externalPtr, curExternal.prev);
1615       }
1616
1617       symbol.id = curExternal.symbol.idCode;
1618    }
1619 }
1620
1621 // ***************** EXPRESSION PROCESSING ***************************
1622 public Type Dereference(Type source)
1623 {
1624    Type type = null;
1625    if(source)
1626    {
1627       if(source.kind == pointerType || source.kind == arrayType)
1628       {
1629          type = source.type;
1630          source.type.refCount++;
1631       }
1632       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1633       {
1634          type = Type
1635          {
1636             kind = charType;
1637             refCount = 1;
1638          };
1639       }
1640       // Support dereferencing of no head classes for now...
1641       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1642       {
1643          type = source;
1644          source.refCount++;
1645       }
1646       else
1647          Compiler_Error($"cannot dereference type\n");
1648    }
1649    return type;
1650 }
1651
1652 static Type Reference(Type source)
1653 {
1654    Type type = null;
1655    if(source)
1656    {
1657       type = Type
1658       {
1659          kind = pointerType;
1660          type = source;
1661          refCount = 1;
1662       };
1663       source.refCount++;
1664    }
1665    return type;
1666 }
1667
1668 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1669 {
1670    Identifier ident = member.identifiers ? member.identifiers->first : null;
1671    bool found = false;
1672    DataMember dataMember = null;
1673    Method method = null;
1674    bool freeType = false;
1675
1676    yylloc = member.loc;
1677
1678    if(!ident)
1679    {
1680       if(curMember)
1681       {
1682          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1683          if(*curMember)
1684          {
1685             found = true;
1686             dataMember = *curMember;
1687          }
1688       }
1689    }
1690    else
1691    {
1692       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1693       DataMember _subMemberStack[256];
1694       int _subMemberStackPos = 0;
1695
1696       // FILL MEMBER STACK
1697       if(!thisMember)
1698          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1699       if(thisMember)
1700       {
1701          dataMember = thisMember;
1702          if(curMember && thisMember.memberAccess == publicAccess)
1703          {
1704             *curMember = thisMember;
1705             *curClass = thisMember._class;
1706             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1707             *subMemberStackPos = _subMemberStackPos;
1708          }
1709          found = true;
1710       }
1711       else
1712       {
1713          // Setting a method
1714          method = eClass_FindMethod(_class, ident.string, privateModule);
1715          if(method && method.type == virtualMethod)
1716             found = true;
1717          else
1718             method = null;
1719       }
1720    }
1721
1722    if(found)
1723    {
1724       Type type = null;
1725       if(dataMember)
1726       {
1727          if(!dataMember.dataType && dataMember.dataTypeString)
1728          {
1729             //Context context = SetupTemplatesContext(dataMember._class);
1730             Context context = SetupTemplatesContext(_class);
1731             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1732             FinishTemplatesContext(context);
1733          }
1734          type = dataMember.dataType;
1735       }
1736       else if(method)
1737       {
1738          // This is for destination type...
1739          if(!method.dataType)
1740             ProcessMethodType(method);
1741          //DeclareMethod(method);
1742          // method.dataType = ((Symbol)method.symbol)->type;
1743          type = method.dataType;
1744       }
1745
1746       if(ident && ident.next)
1747       {
1748          for(ident = ident.next; ident && type; ident = ident.next)
1749          {
1750             if(type.kind == classType)
1751             {
1752                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1753                if(!dataMember)
1754                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1755                if(dataMember)
1756                   type = dataMember.dataType;
1757             }
1758             else if(type.kind == structType || type.kind == unionType)
1759             {
1760                Type memberType;
1761                for(memberType = type.members.first; memberType; memberType = memberType.next)
1762                {
1763                   if(!strcmp(memberType.name, ident.string))
1764                   {
1765                      type = memberType;
1766                      break;
1767                   }
1768                }
1769             }
1770          }
1771       }
1772
1773       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1774       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1775       {
1776          int id = 0;
1777          ClassTemplateParameter curParam = null;
1778          Class sClass;
1779          for(sClass = _class; sClass; sClass = sClass.base)
1780          {
1781             id = 0;
1782             if(sClass.templateClass) sClass = sClass.templateClass;
1783             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1784             {
1785                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1786                {
1787                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1788                   {
1789                      if(sClass.templateClass) sClass = sClass.templateClass;
1790                      id += sClass.templateParams.count;
1791                   }
1792                   break;
1793                }
1794                id++;
1795             }
1796             if(curParam) break;
1797          }
1798
1799          if(curParam)
1800          {
1801             ClassTemplateArgument arg = _class.templateArgs[id];
1802             if(arg.dataTypeString)
1803             {
1804                bool constant = type.constant;
1805                // FreeType(type);
1806                type = ProcessTypeString(arg.dataTypeString, false);
1807                if(type.kind == classType && constant) type.constant = true;
1808                else if(type.kind == pointerType)
1809                {
1810                   Type t = type.type;
1811                   while(t.kind == pointerType) t = t.type;
1812                   if(constant) t.constant = constant;
1813                }
1814                freeType = true;
1815                if(type && _class.templateClass)
1816                   type.passAsTemplate = true;
1817                if(type)
1818                {
1819                   // type.refCount++;
1820                   /*if(!exp.destType)
1821                   {
1822                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1823                      exp.destType.refCount++;
1824                   }*/
1825                }
1826             }
1827          }
1828       }
1829       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1830       {
1831          Class expClass = type._class.registered;
1832          Class cClass = null;
1833          int paramCount = 0;
1834          int lastParam = -1;
1835
1836          char templateString[1024];
1837          ClassTemplateParameter param;
1838          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1839          for(cClass = expClass; cClass; cClass = cClass.base)
1840          {
1841             int p = 0;
1842             if(cClass.templateClass) cClass = cClass.templateClass;
1843             for(param = cClass.templateParams.first; param; param = param.next)
1844             {
1845                int id = p;
1846                Class sClass;
1847                ClassTemplateArgument arg;
1848                for(sClass = cClass.base; sClass; sClass = sClass.base)
1849                {
1850                   if(sClass.templateClass) sClass = sClass.templateClass;
1851                   id += sClass.templateParams.count;
1852                }
1853                arg = expClass.templateArgs[id];
1854
1855                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1856                {
1857                   ClassTemplateParameter cParam;
1858                   //int p = numParams - sClass.templateParams.count;
1859                   int p = 0;
1860                   Class nextClass;
1861                   if(sClass.templateClass) sClass = sClass.templateClass;
1862
1863                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1864                   {
1865                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1866                      p += nextClass.templateParams.count;
1867                   }
1868
1869                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1870                   {
1871                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1872                      {
1873                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1874                         {
1875                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1876                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1877                            break;
1878                         }
1879                      }
1880                   }
1881                }
1882
1883                {
1884                   char argument[256];
1885                   argument[0] = '\0';
1886                   /*if(arg.name)
1887                   {
1888                      strcat(argument, arg.name.string);
1889                      strcat(argument, " = ");
1890                   }*/
1891                   switch(param.type)
1892                   {
1893                      case expression:
1894                      {
1895                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1896                         char expString[1024];
1897                         OldList * specs = MkList();
1898                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1899                         Expression exp;
1900                         char * string = PrintHexUInt64(arg.expression.ui64);
1901                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1902                         delete string;
1903
1904                         ProcessExpressionType(exp);
1905                         ComputeExpression(exp);
1906                         expString[0] = '\0';
1907                         PrintExpression(exp, expString);
1908                         strcat(argument, expString);
1909                         //delete exp;
1910                         FreeExpression(exp);
1911                         break;
1912                      }
1913                      case identifier:
1914                      {
1915                         strcat(argument, arg.member.name);
1916                         break;
1917                      }
1918                      case TemplateParameterType::type:
1919                      {
1920                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1921                            strcat(argument, arg.dataTypeString);
1922                         break;
1923                      }
1924                   }
1925                   if(argument[0])
1926                   {
1927                      if(paramCount) strcat(templateString, ", ");
1928                      if(lastParam != p - 1)
1929                      {
1930                         strcat(templateString, param.name);
1931                         strcat(templateString, " = ");
1932                      }
1933                      strcat(templateString, argument);
1934                      paramCount++;
1935                      lastParam = p;
1936                   }
1937                   p++;
1938                }
1939             }
1940          }
1941          {
1942             int len = strlen(templateString);
1943             if(templateString[len-1] == '<')
1944                len--;
1945             else
1946             {
1947                if(templateString[len-1] == '>')
1948                   templateString[len++] = ' ';
1949                templateString[len++] = '>';
1950             }
1951             templateString[len++] = '\0';
1952          }
1953          {
1954             Context context = SetupTemplatesContext(_class);
1955             if(freeType) FreeType(type);
1956             type = ProcessTypeString(templateString, false);
1957             freeType = true;
1958             FinishTemplatesContext(context);
1959          }
1960       }
1961
1962       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1963       {
1964          ProcessExpressionType(member.initializer.exp);
1965          if(!member.initializer.exp.expType)
1966          {
1967             if(inCompiler)
1968             {
1969                char expString[10240];
1970                expString[0] = '\0';
1971                PrintExpression(member.initializer.exp, expString);
1972                ChangeCh(expString, '\n', ' ');
1973                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1974             }
1975          }
1976          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1977          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false, true))
1978          {
1979             Compiler_Error($"incompatible instance method %s\n", ident.string);
1980          }
1981       }
1982       else if(member.initializer)
1983       {
1984          /*
1985          FreeType(member.exp.destType);
1986          member.exp.destType = type;
1987          if(member.exp.destType)
1988             member.exp.destType.refCount++;
1989          ProcessExpressionType(member.exp);
1990          */
1991
1992          ProcessInitializer(member.initializer, type);
1993       }
1994       if(freeType) FreeType(type);
1995    }
1996    else
1997    {
1998       if(_class && _class.type == unitClass)
1999       {
2000          if(member.initializer)
2001          {
2002             /*
2003             FreeType(member.exp.destType);
2004             member.exp.destType = MkClassType(_class.fullName);
2005             ProcessExpressionType(member.initializer, type);
2006             */
2007             Type type = MkClassType(_class.fullName);
2008             ProcessInitializer(member.initializer, type);
2009             FreeType(type);
2010          }
2011       }
2012       else
2013       {
2014          if(member.initializer)
2015          {
2016             //ProcessExpressionType(member.exp);
2017             ProcessInitializer(member.initializer, null);
2018          }
2019          if(ident)
2020          {
2021             if(method)
2022             {
2023                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
2024             }
2025             else if(_class)
2026             {
2027                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
2028                if(inCompiler)
2029                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
2030             }
2031          }
2032          else if(_class)
2033             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
2034       }
2035    }
2036 }
2037
2038 void ProcessInstantiationType(Instantiation inst)
2039 {
2040    yylloc = inst.loc;
2041    if(inst._class)
2042    {
2043       MembersInit members;
2044       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
2045       Class _class;
2046
2047       /*if(!inst._class.symbol)
2048          inst._class.symbol = FindClass(inst._class.name);*/
2049       classSym = inst._class.symbol;
2050       _class = classSym ? classSym.registered : null;
2051
2052       // DANGER: Patch for mutex not declaring its struct when not needed
2053       if(!_class || _class.type != noHeadClass)
2054          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
2055
2056       afterExternal = afterExternal ? afterExternal : curExternal;
2057
2058       if(inst.exp)
2059          ProcessExpressionType(inst.exp);
2060
2061       inst.isConstant = true;
2062       if(inst.members)
2063       {
2064          DataMember curMember = null;
2065          Class curClass = null;
2066          DataMember subMemberStack[256];
2067          int subMemberStackPos = 0;
2068
2069          for(members = inst.members->first; members; members = members.next)
2070          {
2071             switch(members.type)
2072             {
2073                case methodMembersInit:
2074                {
2075                   char name[1024];
2076                   static uint instMethodID = 0;
2077                   External external = curExternal;
2078                   Context context = curContext;
2079                   Declarator declarator = members.function.declarator;
2080                   Identifier nameID = GetDeclId(declarator);
2081                   char * unmangled = nameID ? nameID.string : null;
2082                   Expression exp;
2083                   External createdExternal = null;
2084
2085                   if(inCompiler)
2086                   {
2087                      char number[16];
2088                      //members.function.dontMangle = true;
2089                      strcpy(name, "__ecereInstMeth_");
2090                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2091                      strcat(name, "_");
2092                      strcat(name, nameID.string);
2093                      strcat(name, "_");
2094                      sprintf(number, "_%08d", instMethodID++);
2095                      strcat(name, number);
2096                      nameID.string = CopyString(name);
2097                   }
2098
2099                   // Do modifications here...
2100                   if(declarator)
2101                   {
2102                      Symbol symbol = declarator.symbol;
2103                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2104
2105                      if(method && method.type == virtualMethod)
2106                      {
2107                         symbol.method = method;
2108                         ProcessMethodType(method);
2109
2110                         if(!symbol.type.thisClass)
2111                         {
2112                            if(method.dataType.thisClass && currentClass &&
2113                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2114                            {
2115                               if(!currentClass.symbol)
2116                                  currentClass.symbol = FindClass(currentClass.fullName);
2117                               symbol.type.thisClass = currentClass.symbol;
2118                            }
2119                            else
2120                            {
2121                               if(!_class.symbol)
2122                                  _class.symbol = FindClass(_class.fullName);
2123                               symbol.type.thisClass = _class.symbol;
2124                            }
2125                         }
2126                         // TESTING THIS HERE:
2127                         DeclareType(symbol.type, true, true);
2128
2129                      }
2130                      else if(classSym)
2131                      {
2132                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2133                            unmangled, classSym.string);
2134                      }
2135                   }
2136
2137                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2138                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2139
2140                   if(nameID)
2141                   {
2142                      FreeSpecifier(nameID._class);
2143                      nameID._class = null;
2144                   }
2145
2146                   if(inCompiler)
2147                   {
2148                      //Type type = declarator.symbol.type;
2149                      External oldExternal = curExternal;
2150
2151                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2152                      // *** It was commented out for problems such as
2153                      /*
2154                            class VirtualDesktop : Window
2155                            {
2156                               clientSize = Size { };
2157                               Timer timer
2158                               {
2159                                  bool DelayExpired()
2160                                  {
2161                                     clientSize.w;
2162                                     return true;
2163                                  }
2164                               };
2165                            }
2166                      */
2167                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2168
2169                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2170
2171                      /*
2172                      if(strcmp(declarator.symbol.string, name))
2173                      {
2174                         printf("TOCHECK: Look out for this\n");
2175                         delete declarator.symbol.string;
2176                         declarator.symbol.string = CopyString(name);
2177                      }
2178
2179                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2180                      {
2181                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2182                         excludedSymbols->Remove(declarator.symbol);
2183                         globalContext.symbols.Add((BTNode)declarator.symbol);
2184                         if(strstr(declarator.symbol.string), "::")
2185                            globalContext.hasNameSpace = true;
2186
2187                      }
2188                      */
2189
2190                      //curExternal = curExternal.prev;
2191                      //afterExternal = afterExternal->next;
2192
2193                      //ProcessFunction(afterExternal->function);
2194
2195                      //curExternal = afterExternal;
2196                      {
2197                         External externalDecl;
2198                         externalDecl = MkExternalDeclaration(null);
2199                         ast->Insert(oldExternal.prev, externalDecl);
2200
2201                         // Which function does this process?
2202                         if(createdExternal.function)
2203                         {
2204                            ProcessFunction(createdExternal.function);
2205
2206                            //curExternal = oldExternal;
2207
2208                            {
2209                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2210
2211                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2212                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2213
2214                               //externalDecl = MkExternalDeclaration(decl);
2215
2216                               //***** ast->Insert(external.prev, externalDecl);
2217                               //ast->Insert(curExternal.prev, externalDecl);
2218                               externalDecl.declaration = decl;
2219                               if(decl.symbol && !decl.symbol.pointerExternal)
2220                                  decl.symbol.pointerExternal = externalDecl;
2221
2222                               // Trying this out...
2223                               declarator.symbol.pointerExternal = externalDecl;
2224                            }
2225                         }
2226                      }
2227                   }
2228                   else if(declarator)
2229                   {
2230                      curExternal = declarator.symbol.pointerExternal;
2231                      ProcessFunction((FunctionDefinition)members.function);
2232                   }
2233                   curExternal = external;
2234                   curContext = context;
2235
2236                   if(inCompiler)
2237                   {
2238                      FreeClassFunction(members.function);
2239
2240                      // In this pass, turn this into a MemberInitData
2241                      exp = QMkExpId(name);
2242                      members.type = dataMembersInit;
2243                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2244
2245                      delete unmangled;
2246                   }
2247                   break;
2248                }
2249                case dataMembersInit:
2250                {
2251                   if(members.dataMembers && classSym)
2252                   {
2253                      MemberInit member;
2254                      Location oldyyloc = yylloc;
2255                      for(member = members.dataMembers->first; member; member = member.next)
2256                      {
2257                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2258                         if(member.initializer && !member.initializer.isConstant)
2259                            inst.isConstant = false;
2260                      }
2261                      yylloc = oldyyloc;
2262                   }
2263                   break;
2264                }
2265             }
2266          }
2267       }
2268    }
2269 }
2270
2271 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2272 {
2273    // OPTIMIZATIONS: TESTING THIS...
2274    if(inCompiler)
2275    {
2276       if(type.kind == functionType)
2277       {
2278          Type param;
2279          if(declareParams)
2280          {
2281             for(param = type.params.first; param; param = param.next)
2282                DeclareType(param, declarePointers, true);
2283          }
2284          DeclareType(type.returnType, declarePointers, true);
2285       }
2286       else if(type.kind == pointerType && declarePointers)
2287          DeclareType(type.type, declarePointers, false);
2288       else if(type.kind == classType)
2289       {
2290          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2291             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2292       }
2293       else if(type.kind == structType || type.kind == unionType)
2294       {
2295          Type member;
2296          for(member = type.members.first; member; member = member.next)
2297             DeclareType(member, false, false);
2298       }
2299       else if(type.kind == arrayType)
2300          DeclareType(type.arrayType, declarePointers, false);
2301    }
2302 }
2303
2304 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2305 {
2306    ClassTemplateArgument * arg = null;
2307    int id = 0;
2308    ClassTemplateParameter curParam = null;
2309    Class sClass;
2310    for(sClass = _class; sClass; sClass = sClass.base)
2311    {
2312       id = 0;
2313       if(sClass.templateClass) sClass = sClass.templateClass;
2314       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2315       {
2316          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2317          {
2318             for(sClass = sClass.base; sClass; sClass = sClass.base)
2319             {
2320                if(sClass.templateClass) sClass = sClass.templateClass;
2321                id += sClass.templateParams.count;
2322             }
2323             break;
2324          }
2325          id++;
2326       }
2327       if(curParam) break;
2328    }
2329    if(curParam)
2330    {
2331       arg = &_class.templateArgs[id];
2332       if(arg && param.type == type)
2333          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2334    }
2335    return arg;
2336 }
2337
2338 public Context SetupTemplatesContext(Class _class)
2339 {
2340    Context context = PushContext();
2341    context.templateTypesOnly = true;
2342    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2343    {
2344       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2345       for(; param; param = param.next)
2346       {
2347          if(param.type == type && param.identifier)
2348          {
2349             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2350             curContext.templateTypes.Add((BTNode)type);
2351          }
2352       }
2353    }
2354    else if(_class)
2355    {
2356       Class sClass;
2357       for(sClass = _class; sClass; sClass = sClass.base)
2358       {
2359          ClassTemplateParameter p;
2360          for(p = sClass.templateParams.first; p; p = p.next)
2361          {
2362             //OldList * specs = MkList();
2363             //Declarator decl = null;
2364             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2365             if(p.type == type)
2366             {
2367                TemplateParameter param = p.param;
2368                TemplatedType type;
2369                if(!param)
2370                {
2371                   // ADD DATA TYPE HERE...
2372                   p.param = param = TemplateParameter
2373                   {
2374                      identifier = MkIdentifier(p.name), type = p.type,
2375                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2376                   };
2377                }
2378                type = TemplatedType { key = (uintptr)p.name, param = param };
2379                curContext.templateTypes.Add((BTNode)type);
2380             }
2381          }
2382       }
2383    }
2384    return context;
2385 }
2386
2387 public void FinishTemplatesContext(Context context)
2388 {
2389    PopContext(context);
2390    FreeContext(context);
2391    delete context;
2392 }
2393
2394 public void ProcessMethodType(Method method)
2395 {
2396    if(!method.dataType)
2397    {
2398       Context context = SetupTemplatesContext(method._class);
2399
2400       method.dataType = ProcessTypeString(method.dataTypeString, false);
2401
2402       FinishTemplatesContext(context);
2403
2404       if(method.type != virtualMethod && method.dataType)
2405       {
2406          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2407          {
2408             if(!method._class.symbol)
2409                method._class.symbol = FindClass(method._class.fullName);
2410             method.dataType.thisClass = method._class.symbol;
2411          }
2412       }
2413
2414       // Why was this commented out? Working fine without now...
2415
2416       /*
2417       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2418          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2419          */
2420    }
2421
2422    /*
2423    if(type)
2424    {
2425       char * par = strstr(type, "(");
2426       char * classOp = null;
2427       int classOpLen = 0;
2428       if(par)
2429       {
2430          int c;
2431          for(c = par-type-1; c >= 0; c++)
2432          {
2433             if(type[c] == ':' && type[c+1] == ':')
2434             {
2435                classOp = type + c - 1;
2436                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2437                {
2438                   classOp--;
2439                   classOpLen++;
2440                }
2441                break;
2442             }
2443             else if(!isspace(type[c]))
2444                break;
2445          }
2446       }
2447       if(classOp)
2448       {
2449          char temp[1024];
2450          int typeLen = strlen(type);
2451          memcpy(temp, classOp, classOpLen);
2452          temp[classOpLen] = '\0';
2453          if(temp[0])
2454             _class = eSystem_FindClass(module, temp);
2455          else
2456             _class = null;
2457          method.dataTypeString = new char[typeLen - classOpLen + 1];
2458          memcpy(method.dataTypeString, type, classOp - type);
2459          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2460       }
2461       else
2462          method.dataTypeString = type;
2463    }
2464    */
2465 }
2466
2467
2468 public void ProcessPropertyType(Property prop)
2469 {
2470    if(!prop.dataType)
2471    {
2472       Context context = SetupTemplatesContext(prop._class);
2473       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2474       FinishTemplatesContext(context);
2475    }
2476 }
2477
2478 public void DeclareMethod(Method method, const char * name)
2479 {
2480    Symbol symbol = method.symbol;
2481    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2482    {
2483       //bool imported = false;
2484       bool dllImport = false;
2485
2486       if(!method.dataType)
2487          method.dataType = ProcessTypeString(method.dataTypeString, false);
2488
2489       if(!symbol || symbol._import || method.type == virtualMethod)
2490       {
2491          if(!symbol || method.type == virtualMethod)
2492          {
2493             Symbol classSym;
2494             if(!method._class.symbol)
2495                method._class.symbol = FindClass(method._class.fullName);
2496             classSym = method._class.symbol;
2497             if(!classSym._import)
2498             {
2499                ModuleImport module;
2500
2501                if(method._class.module && method._class.module.name)
2502                   module = FindModule(method._class.module);
2503                else
2504                   module = mainModule;
2505                classSym._import = ClassImport
2506                {
2507                   name = CopyString(method._class.fullName);
2508                   isRemote = method._class.isRemote;
2509                };
2510                module.classes.Add(classSym._import);
2511             }
2512             if(!symbol)
2513             {
2514                symbol = method.symbol = Symbol { };
2515             }
2516             if(!symbol._import)
2517             {
2518                symbol._import = (ClassImport)MethodImport
2519                {
2520                   name = CopyString(method.name);
2521                   isVirtual = method.type == virtualMethod;
2522                };
2523                classSym._import.methods.Add(symbol._import);
2524             }
2525             if(!symbol)
2526             {
2527                // Set the symbol type
2528                /*
2529                if(!type.thisClass)
2530                {
2531                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2532                }
2533                else if(type.thisClass == (void *)-1)
2534                {
2535                   type.thisClass = null;
2536                }
2537                */
2538                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2539                symbol.type = method.dataType;
2540                if(symbol.type) symbol.type.refCount++;
2541             }
2542             /*
2543             if(!method.thisClass || strcmp(method.thisClass, "void"))
2544                symbol.type.params.Insert(null,
2545                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2546             */
2547          }
2548          if(!method.dataType.dllExport)
2549          {
2550             //imported = true;
2551             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2552                dllImport = true;
2553          }
2554       }
2555
2556       /* MOVING THIS UP
2557       if(!method.dataType)
2558          method.dataType = ((Symbol)method.symbol).type;
2559          //ProcessMethodType(method);
2560       */
2561
2562       if(method.type != virtualMethod && method.dataType)
2563          DeclareType(method.dataType, true, true);
2564
2565       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2566       {
2567          // We need a declaration here :)
2568          Declaration decl;
2569          OldList * specifiers, * declarators;
2570          Declarator d;
2571          Declarator funcDecl;
2572          External external;
2573
2574          specifiers = MkList();
2575          declarators = MkList();
2576
2577          //if(imported)
2578          if(dllImport)
2579             ListAdd(specifiers, MkSpecifier(EXTERN));
2580          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2581             ListAdd(specifiers, MkSpecifier(STATIC));
2582
2583          if(method.type == virtualMethod)
2584          {
2585             ListAdd(specifiers, MkSpecifier(INT));
2586             d = MkDeclaratorIdentifier(MkIdentifier(name));
2587          }
2588          else
2589          {
2590             d = MkDeclaratorIdentifier(MkIdentifier(name));
2591             //if(imported)
2592             if(dllImport)
2593                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2594             {
2595                Context context = SetupTemplatesContext(method._class);
2596                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2597                FinishTemplatesContext(context);
2598             }
2599             funcDecl = GetFuncDecl(d);
2600
2601             if(dllImport)
2602             {
2603                Specifier spec, next;
2604                for(spec = specifiers->first; spec; spec = next)
2605                {
2606                   next = spec.next;
2607                   if(spec.type == extendedSpecifier)
2608                   {
2609                      specifiers->Remove(spec);
2610                      FreeSpecifier(spec);
2611                   }
2612                }
2613             }
2614
2615             // Add this parameter if not a static method
2616             if(method.dataType && !method.dataType.staticMethod)
2617             {
2618                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2619                {
2620                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2621                   TypeName thisParam = MkTypeName(MkListOne(
2622                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2623                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2624                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2625                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2626
2627                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2628                   {
2629                      TypeName param = funcDecl.function.parameters->first;
2630                      funcDecl.function.parameters->Remove(param);
2631                      FreeTypeName(param);
2632                   }
2633
2634                   if(!funcDecl.function.parameters)
2635                      funcDecl.function.parameters = MkList();
2636                   funcDecl.function.parameters->Insert(null, thisParam);
2637                }
2638             }
2639             // Make sure we don't have empty parameter declarations for static methods...
2640             /*
2641             else if(!funcDecl.function.parameters)
2642             {
2643                funcDecl.function.parameters = MkList();
2644                funcDecl.function.parameters->Insert(null,
2645                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2646             }*/
2647          }
2648          // TESTING THIS:
2649          ProcessDeclarator(d);
2650
2651          ListAdd(declarators, MkInitDeclarator(d, null));
2652
2653          decl = MkDeclaration(specifiers, declarators);
2654
2655          ReplaceThisClassSpecifiers(specifiers, method._class);
2656
2657          // Keep a different symbol for the function definition than the declaration...
2658          if(symbol.pointerExternal)
2659          {
2660             Symbol functionSymbol { };
2661
2662             // Copy symbol
2663             {
2664                *functionSymbol = *symbol;
2665                functionSymbol.string = CopyString(symbol.string);
2666                if(functionSymbol.type)
2667                   functionSymbol.type.refCount++;
2668             }
2669
2670             excludedSymbols->Add(functionSymbol);
2671             symbol.pointerExternal.symbol = functionSymbol;
2672          }
2673          external = MkExternalDeclaration(decl);
2674          if(curExternal)
2675             ast->Insert(curExternal ? curExternal.prev : null, external);
2676          external.symbol = symbol;
2677          symbol.pointerExternal = external;
2678       }
2679       else if(ast)
2680       {
2681          // Move declaration higher...
2682          ast->Move(symbol.pointerExternal, curExternal.prev);
2683       }
2684
2685       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2686    }
2687 }
2688
2689 char * ReplaceThisClass(Class _class)
2690 {
2691    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2692    {
2693       bool first = true;
2694       int p = 0;
2695       ClassTemplateParameter param;
2696       int lastParam = -1;
2697
2698       char className[1024];
2699       strcpy(className, _class.fullName);
2700       for(param = _class.templateParams.first; param; param = param.next)
2701       {
2702          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2703          {
2704             if(first) strcat(className, "<");
2705             if(!first) strcat(className, ", ");
2706             if(lastParam + 1 != p)
2707             {
2708                strcat(className, param.name);
2709                strcat(className, " = ");
2710             }
2711             strcat(className, param.name);
2712             first = false;
2713             lastParam = p;
2714          }
2715          p++;
2716       }
2717       if(!first)
2718       {
2719          int len = strlen(className);
2720          if(className[len-1] == '>') className[len++] = ' ';
2721          className[len++] = '>';
2722          className[len++] = '\0';
2723       }
2724       return CopyString(className);
2725    }
2726    else
2727       return CopyString(_class.fullName);
2728 }
2729
2730 Type ReplaceThisClassType(Class _class)
2731 {
2732    Type type;
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       type = MkClassType(className);
2767       //type = ProcessTypeString(className, false);
2768    }
2769    else
2770    {
2771       type = MkClassType(_class.fullName);
2772       //type = ProcessTypeString(_class.fullName, false);
2773    }
2774    //type.wasThisClass = true;
2775    return type;
2776 }
2777
2778 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2779 {
2780    if(specs != null && _class)
2781    {
2782       Specifier spec;
2783       for(spec = specs.first; spec; spec = spec.next)
2784       {
2785          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2786          {
2787             spec.type = nameSpecifier;
2788             spec.name = ReplaceThisClass(_class);
2789             spec.symbol = FindClass(spec.name); //_class.symbol;
2790          }
2791       }
2792    }
2793 }
2794
2795 // Returns imported or not
2796 bool DeclareFunction(GlobalFunction function, char * name)
2797 {
2798    Symbol symbol = function.symbol;
2799    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2800    {
2801       bool imported = false;
2802       bool dllImport = false;
2803
2804       if(!function.dataType)
2805       {
2806          function.dataType = ProcessTypeString(function.dataTypeString, false);
2807          if(!function.dataType.thisClass)
2808             function.dataType.staticMethod = true;
2809       }
2810
2811       if(inCompiler)
2812       {
2813          if(!symbol)
2814          {
2815             ModuleImport module = FindModule(function.module);
2816             // WARNING: This is not added anywhere...
2817             symbol = function.symbol = Symbol {  };
2818
2819             if(module.name)
2820             {
2821                if(!function.dataType.dllExport)
2822                {
2823                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2824                   module.functions.Add(symbol._import);
2825                }
2826             }
2827             // Set the symbol type
2828             {
2829                symbol.type = ProcessTypeString(function.dataTypeString, false);
2830                if(!symbol.type.thisClass)
2831                   symbol.type.staticMethod = true;
2832             }
2833          }
2834          imported = symbol._import ? true : false;
2835          if(imported && function.module != privateModule && function.module.importType != staticImport)
2836             dllImport = true;
2837       }
2838
2839       DeclareType(function.dataType, true, true);
2840
2841       if(inCompiler)
2842       {
2843          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2844          {
2845             // We need a declaration here :)
2846             Declaration decl;
2847             OldList * specifiers, * declarators;
2848             Declarator d;
2849             Declarator funcDecl;
2850             External external;
2851
2852             specifiers = MkList();
2853             declarators = MkList();
2854
2855             //if(imported)
2856                ListAdd(specifiers, MkSpecifier(EXTERN));
2857             /*
2858             else
2859                ListAdd(specifiers, MkSpecifier(STATIC));
2860             */
2861
2862             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2863             //if(imported)
2864             if(dllImport)
2865                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2866
2867             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2868             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2869             if(function.module.importType == staticImport)
2870             {
2871                Specifier spec;
2872                for(spec = specifiers->first; spec; spec = spec.next)
2873                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2874                   {
2875                      specifiers->Remove(spec);
2876                      FreeSpecifier(spec);
2877                      break;
2878                   }
2879             }
2880
2881             funcDecl = GetFuncDecl(d);
2882
2883             // Make sure we don't have empty parameter declarations for static methods...
2884             if(funcDecl && !funcDecl.function.parameters)
2885             {
2886                funcDecl.function.parameters = MkList();
2887                funcDecl.function.parameters->Insert(null,
2888                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2889             }
2890
2891             ListAdd(declarators, MkInitDeclarator(d, null));
2892
2893             {
2894                Context oldCtx = curContext;
2895                curContext = globalContext;
2896                decl = MkDeclaration(specifiers, declarators);
2897                curContext = oldCtx;
2898             }
2899
2900             // Keep a different symbol for the function definition than the declaration...
2901             if(symbol.pointerExternal)
2902             {
2903                Symbol functionSymbol { };
2904                // Copy symbol
2905                {
2906                   *functionSymbol = *symbol;
2907                   functionSymbol.string = CopyString(symbol.string);
2908                   if(functionSymbol.type)
2909                      functionSymbol.type.refCount++;
2910                }
2911
2912                excludedSymbols->Add(functionSymbol);
2913
2914                symbol.pointerExternal.symbol = functionSymbol;
2915             }
2916             external = MkExternalDeclaration(decl);
2917             if(curExternal)
2918                ast->Insert(curExternal.prev, external);
2919             external.symbol = symbol;
2920             symbol.pointerExternal = external;
2921          }
2922          else
2923          {
2924             // Move declaration higher...
2925             ast->Move(symbol.pointerExternal, curExternal.prev);
2926          }
2927
2928          if(curExternal)
2929             symbol.id = curExternal.symbol.idCode;
2930       }
2931    }
2932    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2933 }
2934
2935 void DeclareGlobalData(GlobalData data)
2936 {
2937    Symbol symbol = data.symbol;
2938    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2939    {
2940       if(inCompiler)
2941       {
2942          if(!symbol)
2943             symbol = data.symbol = Symbol { };
2944       }
2945       if(!data.dataType)
2946          data.dataType = ProcessTypeString(data.dataTypeString, false);
2947       DeclareType(data.dataType, true, true);
2948       if(inCompiler)
2949       {
2950          if(!symbol.pointerExternal)
2951          {
2952             // We need a declaration here :)
2953             Declaration decl;
2954             OldList * specifiers, * declarators;
2955             Declarator d;
2956             External external;
2957
2958             specifiers = MkList();
2959             declarators = MkList();
2960
2961             ListAdd(specifiers, MkSpecifier(EXTERN));
2962             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2963             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2964
2965             ListAdd(declarators, MkInitDeclarator(d, null));
2966
2967             decl = MkDeclaration(specifiers, declarators);
2968             external = MkExternalDeclaration(decl);
2969             if(curExternal)
2970                ast->Insert(curExternal.prev, external);
2971             external.symbol = symbol;
2972             symbol.pointerExternal = external;
2973          }
2974          else
2975          {
2976             // Move declaration higher...
2977             ast->Move(symbol.pointerExternal, curExternal.prev);
2978          }
2979
2980          if(curExternal)
2981             symbol.id = curExternal.symbol.idCode;
2982       }
2983    }
2984 }
2985
2986 class Conversion : struct
2987 {
2988    Conversion prev, next;
2989    Property convert;
2990    bool isGet;
2991    Type resultType;
2992 };
2993
2994 static bool CheckConstCompatibility(Type source, Type dest, bool warn)
2995 {
2996    bool status = true;
2997    if(((source.kind == classType && source._class && source._class.registered) || source.kind == arrayType || source.kind == pointerType) &&
2998       ((dest.kind == classType && dest._class && dest._class.registered) || /*dest.kind == arrayType || */dest.kind == pointerType))
2999    {
3000       Class sourceClass = source.kind == classType ? source._class.registered : null;
3001       Class destClass = dest.kind == classType ? dest._class.registered : null;
3002       if((!sourceClass || (sourceClass && sourceClass.type == normalClass && !sourceClass.structSize)) &&
3003          (!destClass || (destClass && destClass.type == normalClass && !destClass.structSize)))
3004       {
3005          Type sourceType = source, destType = dest;
3006          while((sourceType.kind == pointerType || sourceType.kind == arrayType) && sourceType.type) sourceType = sourceType.type;
3007          while((destType.kind == pointerType || destType.kind == arrayType) && destType.type) destType = destType.type;
3008          if(!destType.constant && sourceType.constant)
3009          {
3010             status = false;
3011             if(warn)
3012                Compiler_Warning($"discarding const qualifier\n");
3013          }
3014       }
3015    }
3016    return status;
3017 }
3018
3019 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams,
3020                        bool isConversionExploration, bool warnConst)
3021 {
3022    if(source && dest)
3023    {
3024       if(warnConst)
3025          CheckConstCompatibility(source, dest, true);
3026       // Property convert;
3027
3028       if(source.kind == templateType && dest.kind != templateType)
3029       {
3030          Type type = ProcessTemplateParameterType(source.templateParameter);
3031          if(type) source = type;
3032       }
3033
3034       if(dest.kind == templateType && source.kind != templateType)
3035       {
3036          Type type = ProcessTemplateParameterType(dest.templateParameter);
3037          if(type) dest = type;
3038       }
3039
3040       if(dest.classObjectType == typedObject && dest.kind != functionType)
3041       {
3042          if(source.classObjectType != anyObject)
3043             return true;
3044          else
3045          {
3046             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
3047             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
3048             {
3049                return true;
3050             }
3051          }
3052       }
3053       else
3054       {
3055          if(source.kind != functionType && source.classObjectType == anyObject)
3056             return true;
3057          if(dest.kind != functionType && dest.classObjectType == anyObject && source.classObjectType != typedObject)
3058             return true;
3059       }
3060
3061       if((dest.kind == structType && source.kind == structType) ||
3062          (dest.kind == unionType && source.kind == unionType))
3063       {
3064          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
3065              (source.members.first && source.members.first == dest.members.first))
3066             return true;
3067       }
3068
3069       if(dest.kind == ellipsisType && source.kind != voidType)
3070          return true;
3071
3072       if(dest.kind == pointerType && dest.type.kind == voidType &&
3073          ((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))
3074          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
3075
3076          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
3077
3078          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
3079          return true;
3080       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
3081          ((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))
3082          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
3083          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
3084
3085          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
3086          return true;
3087
3088       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
3089       {
3090          if(source._class.registered && source._class.registered.type == unitClass)
3091          {
3092             if(conversions != null)
3093             {
3094                if(source._class.registered == dest._class.registered)
3095                   return true;
3096             }
3097             else
3098             {
3099                Class sourceBase, destBase;
3100                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
3101                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
3102                if(sourceBase == destBase)
3103                   return true;
3104             }
3105          }
3106          // Don't match enum inheriting from other enum if resolving enumeration values
3107          // TESTING: !dest.classObjectType
3108          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
3109             (enumBaseType ||
3110                (!source._class.registered || source._class.registered.type != enumClass) ||
3111                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
3112             return true;
3113          else
3114          {
3115             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
3116             if(enumBaseType &&
3117                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
3118                ((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)
3119             {
3120                if(eClass_IsDerived(dest._class.registered, source._class.registered))
3121                {
3122                   return true;
3123                }
3124             }
3125          }
3126       }
3127
3128       // JUST ADDED THIS...
3129       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3130          return true;
3131
3132       if(doConversion)
3133       {
3134          // Just added this for Straight conversion of ColorAlpha => Color
3135          if(source.kind == classType)
3136          {
3137             Class _class;
3138             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3139             {
3140                Property convert;
3141                for(convert = _class.conversions.first; convert; convert = convert.next)
3142                {
3143                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3144                   {
3145                      Conversion after = (conversions != null) ? conversions.last : null;
3146
3147                      if(!convert.dataType)
3148                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3149                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3150                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3151                         MatchTypes(convert.dataType, dest, conversions, null, null,
3152                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3153                               convert.dataType.kind == classType, false, true, warnConst))
3154                      {
3155                         if(!conversions && !convert.Get)
3156                            return true;
3157                         else if(conversions != null)
3158                         {
3159                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3160                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3161                               (dest.kind != classType || dest._class.registered != _class.base))
3162                               return true;
3163                            else
3164                            {
3165                               Conversion conv { convert = convert, isGet = true };
3166                               // conversions.Add(conv);
3167                               conversions.Insert(after, conv);
3168
3169                               return true;
3170                            }
3171                         }
3172                      }
3173                   }
3174                }
3175             }
3176          }
3177
3178          // MOVING THIS??
3179
3180          if(dest.kind == classType)
3181          {
3182             Class _class;
3183             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3184             {
3185                Property convert;
3186                for(convert = _class.conversions.first; convert; convert = convert.next)
3187                {
3188                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3189                   {
3190                      Type constType = null;
3191                      bool success = false;
3192                      // Conversion after = (conversions != null) ? conversions.last : null;
3193
3194                      if(!convert.dataType)
3195                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3196
3197                      if(warnConst && convert.dataType.kind == pointerType && convert.dataType.type && dest.constant)
3198                      {
3199                         Type ptrType { };
3200                         constType = { kind = pointerType, refCount = 1, type = ptrType };
3201                         CopyTypeInto(ptrType, convert.dataType.type);
3202                         ptrType.constant = true;
3203                      }
3204
3205                      // Just added this equality check to prevent recursion.... Make it safer?
3206                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3207                      if((constType || convert.dataType != dest) && MatchTypes(source, constType ? constType : convert.dataType, conversions, null, null, true, false /*true*/, false, true, warnConst))
3208                      {
3209                         if(!conversions && !convert.Set)
3210                            success = true;
3211                         else if(conversions != null)
3212                         {
3213                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3214                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3215                               (source.kind != classType || source._class.registered != _class.base))
3216                               success = true;
3217                            else
3218                            {
3219                               // *** Testing this! ***
3220                               Conversion conv { convert = convert };
3221                               conversions.Add(conv);
3222                               //conversions.Insert(after, conv);
3223                               success = true;
3224                            }
3225                         }
3226                      }
3227                      if(constType)
3228                         FreeType(constType);
3229                      if(success)
3230                         return true;
3231                   }
3232                }
3233             }
3234             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3235             {
3236                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3237                   (source.kind != classType || source._class.registered.type != structClass))
3238                   return true;
3239             }*/
3240
3241             // TESTING THIS... IS THIS OK??
3242             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3243             {
3244                if(!dest._class.registered.dataType)
3245                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3246                // Only support this for classes...
3247                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3248                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3249                {
3250                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false, warnConst))
3251                   {
3252                      return true;
3253                   }
3254                }
3255             }
3256          }
3257
3258          // Moved this lower
3259          if(source.kind == classType)
3260          {
3261             Class _class;
3262             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3263             {
3264                Property convert;
3265                for(convert = _class.conversions.first; convert; convert = convert.next)
3266                {
3267                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3268                   {
3269                      Conversion after = (conversions != null) ? conversions.last : null;
3270
3271                      if(!convert.dataType)
3272                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3273                      if(convert.dataType != source &&
3274                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3275                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true, warnConst))
3276                      {
3277                         if(!conversions && !convert.Get)
3278                            return true;
3279                         else if(conversions != null)
3280                         {
3281                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3282                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3283                               (dest.kind != classType || dest._class.registered != _class.base))
3284                               return true;
3285                            else
3286                            {
3287                               Conversion conv { convert = convert, isGet = true };
3288
3289                               // conversions.Add(conv);
3290                               conversions.Insert(after, conv);
3291                               return true;
3292                            }
3293                         }
3294                      }
3295                   }
3296                }
3297             }
3298
3299             // TESTING THIS... IS THIS OK??
3300             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3301             {
3302                if(!source._class.registered.dataType)
3303                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3304                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3305                {
3306                   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))
3307                      return true;
3308                   // For bool to be accepted by byte, short, etc.
3309                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false, warnConst))
3310                      return true;
3311                }
3312             }
3313          }
3314       }
3315
3316       if(source.kind == classType || source.kind == subClassType)
3317          ;
3318       else if(dest.kind == source.kind &&
3319          (dest.kind != structType && dest.kind != unionType &&
3320           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3321           return true;
3322       // RECENTLY ADDED THESE
3323       else if(dest.kind == doubleType && source.kind == floatType)
3324          return true;
3325       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3326          return true;
3327       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3328          return true;
3329       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3330          return true;
3331       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3332          return true;
3333       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3334          return true;
3335       else if(source.kind == enumType &&
3336          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3337           return true;
3338       else if(dest.kind == enumType && !isConversionExploration &&
3339          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3340           return true;
3341       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3342               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3343       {
3344          Type paramSource, paramDest;
3345
3346          if(dest.kind == methodType)
3347             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3348          if(source.kind == methodType)
3349             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3350
3351          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3352          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3353          if(dest.kind == methodType)
3354             dest = dest.method.dataType;
3355          if(source.kind == methodType)
3356             source = source.method.dataType;
3357
3358          paramSource = source.params.first;
3359          if(paramSource && paramSource.kind == voidType) paramSource = null;
3360          paramDest = dest.params.first;
3361          if(paramDest && paramDest.kind == voidType) paramDest = null;
3362
3363
3364          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3365             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3366          {
3367             // Source thisClass must be derived from destination thisClass
3368             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3369                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3370             {
3371                if(paramDest && paramDest.kind == classType)
3372                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3373                else
3374                   Compiler_Error($"method class should not take an object\n");
3375                return false;
3376             }
3377             paramDest = paramDest.next;
3378          }
3379          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3380          {
3381             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3382             {
3383                if(dest.thisClass)
3384                {
3385                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3386                   {
3387                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3388                      return false;
3389                   }
3390                }
3391                else
3392                {
3393                   // THIS WAS BACKWARDS:
3394                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3395                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3396                   {
3397                      if(owningClassDest)
3398                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3399                      else
3400                         Compiler_Error($"overriding class expected to be derived from method class\n");
3401                      return false;
3402                   }
3403                }
3404                paramSource = paramSource.next;
3405             }
3406             else
3407             {
3408                if(dest.thisClass)
3409                {
3410                   // Source thisClass must be derived from destination thisClass
3411                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3412                   {
3413                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3414                      return false;
3415                   }
3416                }
3417                else
3418                {
3419                   // THIS WAS BACKWARDS TOO??
3420                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3421                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3422                   {
3423                      //if(owningClass)
3424                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3425                      //else
3426                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3427                      return false;
3428                   }
3429                }
3430             }
3431          }
3432
3433
3434          // Source return type must be derived from destination return type
3435          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false, warnConst))
3436          {
3437             Compiler_Warning($"incompatible return type for function\n");
3438             return false;
3439          }
3440          // The const check is backwards from the MatchTypes above (for derivative classes checks)
3441          else
3442             CheckConstCompatibility(dest.returnType, source.returnType, true);
3443
3444          // Check parameters
3445
3446          for(; paramDest; paramDest = paramDest.next)
3447          {
3448             if(!paramSource)
3449             {
3450                //Compiler_Warning($"not enough parameters\n");
3451                Compiler_Error($"not enough parameters\n");
3452                return false;
3453             }
3454             {
3455                Type paramDestType = paramDest;
3456                Type paramSourceType = paramSource;
3457                Type type = paramDestType;
3458
3459                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3460                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3461                   paramSource.kind != templateType)
3462                {
3463                   int id = 0;
3464                   ClassTemplateParameter curParam = null;
3465                   Class sClass;
3466                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3467                   {
3468                      id = 0;
3469                      if(sClass.templateClass) sClass = sClass.templateClass;
3470                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3471                      {
3472                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3473                         {
3474                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3475                            {
3476                               if(sClass.templateClass) sClass = sClass.templateClass;
3477                               id += sClass.templateParams.count;
3478                            }
3479                            break;
3480                         }
3481                         id++;
3482                      }
3483                      if(curParam) break;
3484                   }
3485
3486                   if(curParam)
3487                   {
3488                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3489                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3490                   }
3491                }
3492
3493                // paramDest must be derived from paramSource
3494                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false, warnConst) &&
3495                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false, warnConst)))
3496                {
3497                   char type[1024];
3498                   type[0] = 0;
3499                   PrintType(paramDest, type, false, true);
3500                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3501
3502                   if(paramDestType != paramDest)
3503                      FreeType(paramDestType);
3504                   return false;
3505                }
3506                if(paramDestType != paramDest)
3507                   FreeType(paramDestType);
3508             }
3509
3510             paramSource = paramSource.next;
3511          }
3512          if(paramSource)
3513          {
3514             Compiler_Error($"too many parameters\n");
3515             return false;
3516          }
3517          return true;
3518       }
3519       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3520       {
3521          return true;
3522       }
3523       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3524          (source.kind == arrayType || source.kind == pointerType))
3525       {
3526          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false, warnConst))
3527             return true;
3528       }
3529    }
3530    return false;
3531 }
3532
3533 static void FreeConvert(Conversion convert)
3534 {
3535    if(convert.resultType)
3536       FreeType(convert.resultType);
3537 }
3538
3539 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3540                               char * string, OldList conversions)
3541 {
3542    BTNamedLink link;
3543
3544    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3545    {
3546       Class _class = link.data;
3547       if(_class.type == enumClass)
3548       {
3549          OldList converts { };
3550          Type type { };
3551          type.kind = classType;
3552
3553          if(!_class.symbol)
3554             _class.symbol = FindClass(_class.fullName);
3555          type._class = _class.symbol;
3556
3557          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false, false))
3558          {
3559             NamedLink value;
3560             Class enumClass = eSystem_FindClass(privateModule, "enum");
3561             if(enumClass)
3562             {
3563                Class baseClass;
3564                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3565                {
3566                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3567                   for(value = e.values.first; value; value = value.next)
3568                   {
3569                      if(!strcmp(value.name, string))
3570                         break;
3571                   }
3572                   if(value)
3573                   {
3574                      FreeExpContents(sourceExp);
3575                      FreeType(sourceExp.expType);
3576
3577                      sourceExp.isConstant = true;
3578                      sourceExp.expType = MkClassType(baseClass.fullName);
3579                      //if(inCompiler)
3580                      {
3581                         char constant[256];
3582                         sourceExp.type = constantExp;
3583                         if(!strcmp(baseClass.dataTypeString, "int"))
3584                            sprintf(constant, "%d",(int)value.data);
3585                         else
3586                            sprintf(constant, "0x%X",(int)value.data);
3587                         sourceExp.constant = CopyString(constant);
3588                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3589                      }
3590
3591                      while(converts.first)
3592                      {
3593                         Conversion convert = converts.first;
3594                         converts.Remove(convert);
3595                         conversions.Add(convert);
3596                      }
3597                      delete type;
3598                      return true;
3599                   }
3600                }
3601             }
3602          }
3603          if(converts.first)
3604             converts.Free(FreeConvert);
3605          delete type;
3606       }
3607    }
3608    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3609       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3610          return true;
3611    return false;
3612 }
3613
3614 public bool ModuleVisibility(Module searchIn, Module searchFor)
3615 {
3616    SubModule subModule;
3617
3618    if(searchFor == searchIn)
3619       return true;
3620
3621    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3622    {
3623       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3624       {
3625          if(ModuleVisibility(subModule.module, searchFor))
3626             return true;
3627       }
3628    }
3629    return false;
3630 }
3631
3632 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3633 {
3634    Module module;
3635
3636    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3637       return true;
3638    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3639       return true;
3640    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3641       return true;
3642
3643    for(module = mainModule.application.allModules.first; module; module = module.next)
3644    {
3645       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3646          return true;
3647    }
3648    return false;
3649 }
3650
3651 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla, bool warnConst)
3652 {
3653    Type source;
3654    Type realDest = dest;
3655    Type backupSourceExpType = null;
3656    Expression computedExp = sourceExp;
3657    dest.refCount++;
3658
3659    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3660       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3661    {
3662       computedExp = CopyExpression(sourceExp);        // Keep the original expression, but compute for checking enum ranges
3663       ComputeExpression(computedExp /*sourceExp*/);
3664    }
3665
3666    source = sourceExp.expType;
3667
3668    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3669    {
3670       if(computedExp != sourceExp)
3671       {
3672          FreeExpression(computedExp);
3673          computedExp = sourceExp;
3674       }
3675       FreeType(dest);
3676       return true;
3677    }
3678
3679    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3680    {
3681        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3682        {
3683           Class sourceBase, destBase;
3684           for(sourceBase = source._class.registered;
3685               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3686               sourceBase = sourceBase.base);
3687           for(destBase = dest._class.registered;
3688               destBase && destBase.base && destBase.base.type != systemClass;
3689               destBase = destBase.base);
3690           //if(source._class.registered == dest._class.registered)
3691           if(sourceBase == destBase)
3692           {
3693             if(computedExp != sourceExp)
3694             {
3695                FreeExpression(computedExp);
3696                computedExp = sourceExp;
3697             }
3698             FreeType(dest);
3699             return true;
3700          }
3701       }
3702    }
3703
3704    if(source)
3705    {
3706       OldList * specs;
3707       bool flag = false;
3708       int64 value = MAXINT;
3709
3710       source.refCount++;
3711
3712       if(computedExp.type == constantExp)
3713       {
3714          if(source.isSigned)
3715             value = strtoll(computedExp.constant, null, 0);
3716          else
3717             value = strtoull(computedExp.constant, null, 0);
3718       }
3719       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3720       {
3721          if(source.isSigned)
3722             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3723          else
3724             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3725       }
3726       if(computedExp != sourceExp)
3727       {
3728          FreeExpression(computedExp);
3729          computedExp = sourceExp;
3730       }
3731
3732       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3733          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3734       {
3735          FreeType(source);
3736          source = Type { kind = intType, isSigned = false, refCount = 1 };
3737       }
3738
3739       if(dest.kind == classType)
3740       {
3741          Class _class = dest._class ? dest._class.registered : null;
3742
3743          if(_class && _class.type == unitClass)
3744          {
3745             if(source.kind != classType)
3746             {
3747                Type tempType { };
3748                Type tempDest, tempSource;
3749
3750                for(; _class.base.type != systemClass; _class = _class.base);
3751                tempSource = dest;
3752                tempDest = tempType;
3753
3754                tempType.kind = classType;
3755                if(!_class.symbol)
3756                   _class.symbol = FindClass(_class.fullName);
3757
3758                tempType._class = _class.symbol;
3759                tempType.truth = dest.truth;
3760                if(tempType._class)
3761                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3762
3763                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3764                backupSourceExpType = sourceExp.expType;
3765                sourceExp.expType = dest; dest.refCount++;
3766                //sourceExp.expType = MkClassType(_class.fullName);
3767                flag = true;
3768
3769                delete tempType;
3770             }
3771          }
3772
3773
3774          // Why wasn't there something like this?
3775          if(_class && _class.type == bitClass && source.kind != classType)
3776          {
3777             if(!dest._class.registered.dataType)
3778                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3779             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false, warnConst))
3780             {
3781                FreeType(source);
3782                FreeType(sourceExp.expType);
3783                source = sourceExp.expType = MkClassType(dest._class.string);
3784                source.refCount++;
3785
3786                //source.kind = classType;
3787                //source._class = dest._class;
3788             }
3789          }
3790
3791          // Adding two enumerations
3792          /*
3793          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3794          {
3795             if(!source._class.registered.dataType)
3796                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3797             if(!dest._class.registered.dataType)
3798                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3799
3800             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3801             {
3802                FreeType(source);
3803                source = sourceExp.expType = MkClassType(dest._class.string);
3804                source.refCount++;
3805
3806                //source.kind = classType;
3807                //source._class = dest._class;
3808             }
3809          }*/
3810
3811          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3812          {
3813             OldList * specs = MkList();
3814             Declarator decl;
3815             char string[1024];
3816
3817             ReadString(string, sourceExp.string);
3818             decl = SpecDeclFromString(string, specs, null);
3819
3820             FreeExpContents(sourceExp);
3821             FreeType(sourceExp.expType);
3822
3823             sourceExp.type = classExp;
3824             sourceExp._classExp.specifiers = specs;
3825             sourceExp._classExp.decl = decl;
3826             sourceExp.expType = dest;
3827             dest.refCount++;
3828
3829             FreeType(source);
3830             FreeType(dest);
3831             if(backupSourceExpType) FreeType(backupSourceExpType);
3832             return true;
3833          }
3834       }
3835       else if(source.kind == classType)
3836       {
3837          Class _class = source._class ? source._class.registered : null;
3838
3839          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3840          {
3841             /*
3842             if(dest.kind != classType)
3843             {
3844                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3845                if(!source._class.registered.dataType)
3846                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3847
3848                FreeType(dest);
3849                dest = MkClassType(source._class.string);
3850                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3851                //   dest = MkClassType(source._class.string);
3852             }
3853             */
3854
3855             if(dest.kind != classType)
3856             {
3857                Type tempType { };
3858                Type tempDest, tempSource;
3859
3860                if(!source._class.registered.dataType)
3861                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3862
3863                for(; _class.base.type != systemClass; _class = _class.base);
3864                tempDest = source;
3865                tempSource = tempType;
3866                tempType.kind = classType;
3867                tempType._class = FindClass(_class.fullName);
3868                tempType.truth = source.truth;
3869                tempType.classObjectType = source.classObjectType;
3870
3871                if(tempType._class)
3872                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3873
3874                // PUT THIS BACK TESTING UNITS?
3875                if(conversions.last)
3876                {
3877                   ((Conversion)(conversions.last)).resultType = dest;
3878                   dest.refCount++;
3879                }
3880
3881                FreeType(sourceExp.expType);
3882                sourceExp.expType = MkClassType(_class.fullName);
3883                sourceExp.expType.truth = source.truth;
3884                sourceExp.expType.classObjectType = source.classObjectType;
3885
3886                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3887
3888                if(!sourceExp.destType)
3889                {
3890                   FreeType(sourceExp.destType);
3891                   sourceExp.destType = sourceExp.expType;
3892                   if(sourceExp.expType)
3893                      sourceExp.expType.refCount++;
3894                }
3895                //flag = true;
3896                //source = _class.dataType;
3897
3898
3899                // TOCHECK: TESTING THIS NEW CODE
3900                if(!_class.dataType)
3901                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3902                FreeType(dest);
3903                dest = MkClassType(source._class.string);
3904                dest.truth = source.truth;
3905                dest.classObjectType = source.classObjectType;
3906
3907                FreeType(source);
3908                source = _class.dataType;
3909                source.refCount++;
3910
3911                delete tempType;
3912             }
3913          }
3914       }
3915
3916       if(!flag)
3917       {
3918          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false, warnConst))
3919          {
3920             FreeType(source);
3921             FreeType(dest);
3922             return true;
3923          }
3924       }
3925
3926       // Implicit Casts
3927       /*
3928       if(source.kind == classType)
3929       {
3930          Class _class = source._class.registered;
3931          if(_class.type == unitClass)
3932          {
3933             if(!_class.dataType)
3934                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3935             source = _class.dataType;
3936          }
3937       }*/
3938
3939       if(dest.kind == classType)
3940       {
3941          Class _class = dest._class ? dest._class.registered : null;
3942          bool fittingValue = false;
3943          if(_class && _class.type == enumClass)
3944          {
3945             Class enumClass = eSystem_FindClass(privateModule, "enum");
3946             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3947             if(c && value >= 0 && value <= c.largest)
3948                fittingValue = true;
3949          }
3950
3951          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3952             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3953          {
3954             if(_class.type == normalClass || _class.type == noHeadClass)
3955             {
3956                Expression newExp { };
3957                *newExp = *sourceExp;
3958                if(sourceExp.destType) sourceExp.destType.refCount++;
3959                if(sourceExp.expType)  sourceExp.expType.refCount++;
3960                sourceExp.type = castExp;
3961                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3962                sourceExp.cast.exp = newExp;
3963                FreeType(sourceExp.expType);
3964                sourceExp.expType = null;
3965                ProcessExpressionType(sourceExp);
3966
3967                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3968                if(!inCompiler)
3969                {
3970                   FreeType(sourceExp.expType);
3971                   sourceExp.expType = dest;
3972                }
3973
3974                FreeType(source);
3975                if(inCompiler) FreeType(dest);
3976
3977                if(backupSourceExpType) FreeType(backupSourceExpType);
3978                return true;
3979             }
3980
3981             if(!_class.dataType)
3982                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3983             FreeType(dest);
3984             dest = _class.dataType;
3985             dest.refCount++;
3986          }
3987
3988          // Accept lower precision types for units, since we want to keep the unit type
3989          if(dest.kind == doubleType &&
3990             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3991              source.kind == charType || source.kind == _BoolType))
3992          {
3993             specs = MkListOne(MkSpecifier(DOUBLE));
3994          }
3995          else if(dest.kind == floatType &&
3996             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3997             source.kind == _BoolType || source.kind == doubleType))
3998          {
3999             specs = MkListOne(MkSpecifier(FLOAT));
4000          }
4001          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
4002             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4003          {
4004             specs = MkList();
4005             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4006             ListAdd(specs, MkSpecifier(INT64));
4007          }
4008          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
4009             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
4010          {
4011             specs = MkList();
4012             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4013             ListAdd(specs, MkSpecifier(INT));
4014          }
4015          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
4016             source.kind == floatType || source.kind == doubleType))
4017          {
4018             specs = MkList();
4019             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4020             ListAdd(specs, MkSpecifier(SHORT));
4021          }
4022          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
4023             source.kind == floatType || source.kind == doubleType))
4024          {
4025             specs = MkList();
4026             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4027             ListAdd(specs, MkSpecifier(CHAR));
4028          }
4029          else
4030          {
4031             FreeType(source);
4032             FreeType(dest);
4033             if(backupSourceExpType)
4034             {
4035                // Failed to convert: revert previous exp type
4036                if(sourceExp.expType) FreeType(sourceExp.expType);
4037                sourceExp.expType = backupSourceExpType;
4038             }
4039             return false;
4040          }
4041       }
4042       else if(dest.kind == doubleType &&
4043          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
4044           source.kind == _BoolType || source.kind == charType))
4045       {
4046          specs = MkListOne(MkSpecifier(DOUBLE));
4047       }
4048       else if(dest.kind == floatType &&
4049          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4050       {
4051          specs = MkListOne(MkSpecifier(FLOAT));
4052       }
4053       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4054          (value == 1 || value == 0))
4055       {
4056          specs = MkList();
4057          ListAdd(specs, MkSpecifier(BOOL));
4058       }
4059       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
4060          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
4061       {
4062          specs = MkList();
4063          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4064          ListAdd(specs, MkSpecifier(CHAR));
4065       }
4066       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
4067          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
4068       {
4069          specs = MkList();
4070          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4071          ListAdd(specs, MkSpecifier(SHORT));
4072       }
4073       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
4074       {
4075          specs = MkList();
4076          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4077          ListAdd(specs, MkSpecifier(INT));
4078       }
4079       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
4080       {
4081          specs = MkList();
4082          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
4083          ListAdd(specs, MkSpecifier(INT64));
4084       }
4085       else if(dest.kind == enumType &&
4086          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
4087       {
4088          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
4089       }
4090       else
4091       {
4092          FreeType(source);
4093          FreeType(dest);
4094          if(backupSourceExpType)
4095          {
4096             // Failed to convert: revert previous exp type
4097             if(sourceExp.expType) FreeType(sourceExp.expType);
4098             sourceExp.expType = backupSourceExpType;
4099          }
4100          return false;
4101       }
4102
4103       if(!flag && !sourceExp.opDestType)
4104       {
4105          Expression newExp { };
4106          *newExp = *sourceExp;
4107          newExp.prev = null;
4108          newExp.next = null;
4109          if(sourceExp.destType) sourceExp.destType.refCount++;
4110          if(sourceExp.expType)  sourceExp.expType.refCount++;
4111
4112          sourceExp.type = castExp;
4113          if(realDest.kind == classType)
4114          {
4115             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
4116             FreeList(specs, FreeSpecifier);
4117          }
4118          else
4119             sourceExp.cast.typeName = MkTypeName(specs, null);
4120          if(newExp.type == opExp)
4121          {
4122             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4123          }
4124          else
4125             sourceExp.cast.exp = newExp;
4126
4127          FreeType(sourceExp.expType);
4128          sourceExp.expType = null;
4129          ProcessExpressionType(sourceExp);
4130       }
4131       else
4132          FreeList(specs, FreeSpecifier);
4133
4134       FreeType(dest);
4135       FreeType(source);
4136       if(backupSourceExpType) FreeType(backupSourceExpType);
4137
4138       return true;
4139    }
4140    else
4141    {
4142       if(computedExp != sourceExp)
4143       {
4144          FreeExpression(computedExp);
4145          computedExp = sourceExp;
4146       }
4147
4148       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4149       if(sourceExp.type == identifierExp)
4150       {
4151          Identifier id = sourceExp.identifier;
4152          if(dest.kind == classType)
4153          {
4154             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4155             {
4156                Class _class = dest._class.registered;
4157                Class enumClass = eSystem_FindClass(privateModule, "enum");
4158                if(enumClass)
4159                {
4160                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4161                   {
4162                      NamedLink value;
4163                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4164                      for(value = e.values.first; value; value = value.next)
4165                      {
4166                         if(!strcmp(value.name, id.string))
4167                            break;
4168                      }
4169                      if(value)
4170                      {
4171                         FreeExpContents(sourceExp);
4172                         FreeType(sourceExp.expType);
4173
4174                         sourceExp.isConstant = true;
4175                         sourceExp.expType = MkClassType(_class.fullName);
4176                         //if(inCompiler)
4177                         {
4178                            char constant[256];
4179                            sourceExp.type = constantExp;
4180                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
4181                               sprintf(constant, "%d", (int) value.data);
4182                            else
4183                               sprintf(constant, "0x%X", (int) value.data);
4184                            sourceExp.constant = CopyString(constant);
4185                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4186                         }
4187                         FreeType(dest);
4188                         return true;
4189                      }
4190                   }
4191                }
4192             }
4193          }
4194
4195          // Loop through all enum classes
4196          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4197          {
4198             FreeType(dest);
4199             return true;
4200          }
4201       }
4202       FreeType(dest);
4203    }
4204    return false;
4205 }
4206
4207 #define TERTIARY(o, name, m, t, p) \
4208    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4209    {                                                              \
4210       exp.type = constantExp;                                    \
4211       exp.string = p(op1.m ? op2.m : op3.m);                     \
4212       if(!exp.expType) \
4213          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4214       return true;                                                \
4215    }
4216
4217 #define BINARY(o, name, m, t, p) \
4218    static bool name(Expression exp, Operand op1, Operand op2)   \
4219    {                                                              \
4220       t value2 = op2.m;                                           \
4221       exp.type = constantExp;                                    \
4222       exp.string = p((t)(op1.m o value2));                     \
4223       if(!exp.expType) \
4224          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4225       return true;                                                \
4226    }
4227
4228 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4229    static bool name(Expression exp, Operand op1, Operand op2)   \
4230    {                                                              \
4231       t value2 = op2.m;                                           \
4232       exp.type = constantExp;                                    \
4233       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4234       if(!exp.expType) \
4235          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4236       return true;                                                \
4237    }
4238
4239 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4240    static bool name(Expression exp, Operand op1, Operand op2)   \
4241    {                                                              \
4242       t value2 = op2.m;                                           \
4243       exp.type = constantExp;                                    \
4244       exp.string = p(op1.m o value2);             \
4245       if(!exp.expType) \
4246          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4247       return true;                                                \
4248    }
4249
4250 #define UNARY(o, name, m, t, p) \
4251    static bool name(Expression exp, Operand op1)                \
4252    {                                                              \
4253       exp.type = constantExp;                                    \
4254       exp.string = p((t)(o op1.m));                                   \
4255       if(!exp.expType) \
4256          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4257       return true;                                                \
4258    }
4259
4260 #define OPERATOR_ALL(macro, o, name) \
4261    macro(o, Int##name, i, int, PrintInt) \
4262    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4263    macro(o, Int64##name, i64, int64, PrintInt64) \
4264    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4265    macro(o, Short##name, s, short, PrintShort) \
4266    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4267    macro(o, Char##name, c, char, PrintChar) \
4268    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4269    macro(o, Float##name, f, float, PrintFloat) \
4270    macro(o, Double##name, d, double, PrintDouble)
4271
4272 #define OPERATOR_INTTYPES(macro, o, name) \
4273    macro(o, Int##name, i, int, PrintInt) \
4274    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4275    macro(o, Int64##name, i64, int64, PrintInt64) \
4276    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4277    macro(o, Short##name, s, short, PrintShort) \
4278    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4279    macro(o, Char##name, c, char, PrintChar) \
4280    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4281
4282 #define OPERATOR_REALTYPES(macro, o, name) \
4283    macro(o, Float##name, f, float, PrintFloat) \
4284    macro(o, Double##name, d, double, PrintDouble)
4285
4286 // binary arithmetic
4287 OPERATOR_ALL(BINARY, +, Add)
4288 OPERATOR_ALL(BINARY, -, Sub)
4289 OPERATOR_ALL(BINARY, *, Mul)
4290 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4291 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4292 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4293
4294 // unary arithmetic
4295 OPERATOR_ALL(UNARY, -, Neg)
4296
4297 // unary arithmetic increment and decrement
4298 OPERATOR_ALL(UNARY, ++, Inc)
4299 OPERATOR_ALL(UNARY, --, Dec)
4300
4301 // binary arithmetic assignment
4302 OPERATOR_ALL(BINARY, =, Asign)
4303 OPERATOR_ALL(BINARY, +=, AddAsign)
4304 OPERATOR_ALL(BINARY, -=, SubAsign)
4305 OPERATOR_ALL(BINARY, *=, MulAsign)
4306 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4307 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4308 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4309
4310 // binary bitwise
4311 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4312 OPERATOR_INTTYPES(BINARY, |, BitOr)
4313 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4314 OPERATOR_INTTYPES(BINARY, <<, LShift)
4315 OPERATOR_INTTYPES(BINARY, >>, RShift)
4316
4317 // unary bitwise
4318 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4319
4320 // binary bitwise assignment
4321 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4322 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4323 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4324 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4325 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4326
4327 // unary logical negation
4328 OPERATOR_INTTYPES(UNARY, !, Not)
4329
4330 // binary logical equality
4331 OPERATOR_ALL(BINARY, ==, Equ)
4332 OPERATOR_ALL(BINARY, !=, Nqu)
4333
4334 // binary logical
4335 OPERATOR_ALL(BINARY, &&, And)
4336 OPERATOR_ALL(BINARY, ||, Or)
4337
4338 // binary logical relational
4339 OPERATOR_ALL(BINARY, >, Grt)
4340 OPERATOR_ALL(BINARY, <, Sma)
4341 OPERATOR_ALL(BINARY, >=, GrtEqu)
4342 OPERATOR_ALL(BINARY, <=, SmaEqu)
4343
4344 // tertiary condition operator
4345 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4346
4347 //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
4348 #define OPERATOR_TABLE_ALL(name, type) \
4349     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4350                           type##Neg, \
4351                           type##Inc, type##Dec, \
4352                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4353                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4354                           type##BitNot, \
4355                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4356                           type##Not, \
4357                           type##Equ, type##Nqu, \
4358                           type##And, type##Or, \
4359                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4360                         }; \
4361
4362 #define OPERATOR_TABLE_INTTYPES(name, type) \
4363     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4364                           type##Neg, \
4365                           type##Inc, type##Dec, \
4366                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4367                           null, null, null, null, null, \
4368                           null, \
4369                           null, null, null, null, null, \
4370                           null, \
4371                           type##Equ, type##Nqu, \
4372                           type##And, type##Or, \
4373                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4374                         }; \
4375
4376 OPERATOR_TABLE_ALL(int, Int)
4377 OPERATOR_TABLE_ALL(uint, UInt)
4378 OPERATOR_TABLE_ALL(int64, Int64)
4379 OPERATOR_TABLE_ALL(uint64, UInt64)
4380 OPERATOR_TABLE_ALL(short, Short)
4381 OPERATOR_TABLE_ALL(ushort, UShort)
4382 OPERATOR_TABLE_INTTYPES(float, Float)
4383 OPERATOR_TABLE_INTTYPES(double, Double)
4384 OPERATOR_TABLE_ALL(char, Char)
4385 OPERATOR_TABLE_ALL(uchar, UChar)
4386
4387 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4388 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4389 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4390 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4391 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4392 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4393 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4394 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4395
4396 public void ReadString(char * output,  char * string)
4397 {
4398    int len = strlen(string);
4399    int c,d = 0;
4400    bool quoted = false, escaped = false;
4401    for(c = 0; c<len; c++)
4402    {
4403       char ch = string[c];
4404       if(escaped)
4405       {
4406          switch(ch)
4407          {
4408             case 'n': output[d] = '\n'; break;
4409             case 't': output[d] = '\t'; break;
4410             case 'a': output[d] = '\a'; break;
4411             case 'b': output[d] = '\b'; break;
4412             case 'f': output[d] = '\f'; break;
4413             case 'r': output[d] = '\r'; break;
4414             case 'v': output[d] = '\v'; break;
4415             case '\\': output[d] = '\\'; break;
4416             case '\"': output[d] = '\"'; break;
4417             case '\'': output[d] = '\''; break;
4418             default: output[d] = ch;
4419          }
4420          d++;
4421          escaped = false;
4422       }
4423       else
4424       {
4425          if(ch == '\"')
4426             quoted ^= true;
4427          else if(quoted)
4428          {
4429             if(ch == '\\')
4430                escaped = true;
4431             else
4432                output[d++] = ch;
4433          }
4434       }
4435    }
4436    output[d] = '\0';
4437 }
4438
4439 // String Unescape Copy
4440
4441 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4442 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4443 public int UnescapeString(char * d, char * s, int len)
4444 {
4445    int j = 0, k = 0;
4446    char ch;
4447    while(j < len && (ch = s[j]))
4448    {
4449       switch(ch)
4450       {
4451          case '\\':
4452             switch((ch = s[++j]))
4453             {
4454                case 'n': d[k] = '\n'; break;
4455                case 't': d[k] = '\t'; break;
4456                case 'a': d[k] = '\a'; break;
4457                case 'b': d[k] = '\b'; break;
4458                case 'f': d[k] = '\f'; break;
4459                case 'r': d[k] = '\r'; break;
4460                case 'v': d[k] = '\v'; break;
4461                case '\\': d[k] = '\\'; break;
4462                case '\"': d[k] = '\"'; break;
4463                case '\'': d[k] = '\''; break;
4464                default: d[k] = '\\'; d[k] = ch;
4465             }
4466             break;
4467          default:
4468             d[k] = ch;
4469       }
4470       j++, k++;
4471    }
4472    d[k] = '\0';
4473    return k;
4474 }
4475
4476 public char * OffsetEscapedString(char * s, int len, int offset)
4477 {
4478    char ch;
4479    int j = 0, k = 0;
4480    while(j < len && k < offset && (ch = s[j]))
4481    {
4482       if(ch == '\\') ++j;
4483       j++, k++;
4484    }
4485    return (k == offset) ? s + j : null;
4486 }
4487
4488 public Operand GetOperand(Expression exp)
4489 {
4490    Operand op { };
4491    Type type = exp.expType;
4492    if(type)
4493    {
4494       while(type.kind == classType &&
4495          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4496       {
4497          if(!type._class.registered.dataType)
4498             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4499          type = type._class.registered.dataType;
4500
4501       }
4502       if(exp.type == stringExp && op.kind == pointerType)
4503       {
4504          op.ui64 = (uint64)exp.string;
4505          op.kind = pointerType;
4506          op.ops = uint64Ops;
4507       }
4508       else if(exp.isConstant && exp.type == constantExp)
4509       {
4510          op.kind = type.kind;
4511          op.type = exp.expType;
4512
4513          switch(op.kind)
4514          {
4515             case _BoolType:
4516             case charType:
4517             {
4518                if(exp.constant[0] == '\'')
4519                {
4520                   op.c = exp.constant[1];
4521                   op.ops = charOps;
4522                }
4523                else if(type.isSigned)
4524                {
4525                   op.c = (char)strtol(exp.constant, null, 0);
4526                   op.ops = charOps;
4527                }
4528                else
4529                {
4530                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4531                   op.ops = ucharOps;
4532                }
4533                break;
4534             }
4535             case shortType:
4536                if(type.isSigned)
4537                {
4538                   op.s = (short)strtol(exp.constant, null, 0);
4539                   op.ops = shortOps;
4540                }
4541                else
4542                {
4543                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4544                   op.ops = ushortOps;
4545                }
4546                break;
4547             case intType:
4548             case longType:
4549                if(type.isSigned)
4550                {
4551                   op.i = (int)strtol(exp.constant, null, 0);
4552                   op.ops = intOps;
4553                }
4554                else
4555                {
4556                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4557                   op.ops = uintOps;
4558                }
4559                op.kind = intType;
4560                break;
4561             case int64Type:
4562                if(type.isSigned)
4563                {
4564                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4565                   op.ops = int64Ops;
4566                }
4567                else
4568                {
4569                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4570                   op.ops = uint64Ops;
4571                }
4572                op.kind = int64Type;
4573                break;
4574             case intPtrType:
4575                if(type.isSigned)
4576                {
4577                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4578                   op.ops = int64Ops;
4579                }
4580                else
4581                {
4582                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4583                   op.ops = uint64Ops;
4584                }
4585                op.kind = int64Type;
4586                break;
4587             case intSizeType:
4588                if(type.isSigned)
4589                {
4590                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4591                   op.ops = int64Ops;
4592                }
4593                else
4594                {
4595                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4596                   op.ops = uint64Ops;
4597                }
4598                op.kind = int64Type;
4599                break;
4600             case floatType:
4601                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4602                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4603                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4604                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4605                else
4606                   op.f = (float)strtod(exp.constant, null);
4607                op.ops = floatOps;
4608                break;
4609             case doubleType:
4610                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4611                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4612                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4613                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4614                else
4615                   op.d = (double)strtod(exp.constant, null);
4616                op.ops = doubleOps;
4617                break;
4618             //case classType:    For when we have operator overloading...
4619             // Pointer additions
4620             //case functionType:
4621             case arrayType:
4622             case pointerType:
4623             case classType:
4624                op.ui64 = _strtoui64(exp.constant, null, 0);
4625                op.kind = pointerType;
4626                op.ops = uint64Ops;
4627                // op.ptrSize =
4628                break;
4629          }
4630       }
4631    }
4632    return op;
4633 }
4634
4635 static __attribute__((unused)) void UnusedFunction()
4636 {
4637    int a;
4638    a.OnGetString(0,0,0);
4639 }
4640 default:
4641 extern int __ecereVMethodID_class_OnGetString;
4642 public:
4643
4644 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4645 {
4646    DataMember dataMember;
4647    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4648    {
4649       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4650          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4651       else
4652       {
4653          Expression exp { };
4654          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4655          Type type;
4656          void * ptr = inst.data + dataMember.offset + offset;
4657          char * result = null;
4658          exp.loc = member.loc = inst.loc;
4659          ((Identifier)member.identifiers->first).loc = inst.loc;
4660
4661          if(!dataMember.dataType)
4662             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4663          type = dataMember.dataType;
4664          if(type.kind == classType)
4665          {
4666             Class _class = type._class.registered;
4667             if(_class.type == enumClass)
4668             {
4669                Class enumClass = eSystem_FindClass(privateModule, "enum");
4670                if(enumClass)
4671                {
4672                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4673                   NamedLink item;
4674                   for(item = e.values.first; item; item = item.next)
4675                   {
4676                      if((int)item.data == *(int *)ptr)
4677                      {
4678                         result = item.name;
4679                         break;
4680                      }
4681                   }
4682                   if(result)
4683                   {
4684                      exp.identifier = MkIdentifier(result);
4685                      exp.type = identifierExp;
4686                      exp.destType = MkClassType(_class.fullName);
4687                      ProcessExpressionType(exp);
4688                   }
4689                }
4690             }
4691             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4692             {
4693                if(!_class.dataType)
4694                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4695                type = _class.dataType;
4696             }
4697          }
4698          if(!result)
4699          {
4700             switch(type.kind)
4701             {
4702                case floatType:
4703                {
4704                   FreeExpContents(exp);
4705
4706                   exp.constant = PrintFloat(*(float*)ptr);
4707                   exp.type = constantExp;
4708                   break;
4709                }
4710                case doubleType:
4711                {
4712                   FreeExpContents(exp);
4713
4714                   exp.constant = PrintDouble(*(double*)ptr);
4715                   exp.type = constantExp;
4716                   break;
4717                }
4718                case intType:
4719                {
4720                   FreeExpContents(exp);
4721
4722                   exp.constant = PrintInt(*(int*)ptr);
4723                   exp.type = constantExp;
4724                   break;
4725                }
4726                case int64Type:
4727                {
4728                   FreeExpContents(exp);
4729
4730                   exp.constant = PrintInt64(*(int64*)ptr);
4731                   exp.type = constantExp;
4732                   break;
4733                }
4734                case intPtrType:
4735                {
4736                   FreeExpContents(exp);
4737                   // TODO: This should probably use proper type
4738                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4739                   exp.type = constantExp;
4740                   break;
4741                }
4742                case intSizeType:
4743                {
4744                   FreeExpContents(exp);
4745                   // TODO: This should probably use proper type
4746                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4747                   exp.type = constantExp;
4748                   break;
4749                }
4750                default:
4751                   Compiler_Error($"Unhandled type populating instance\n");
4752             }
4753          }
4754          ListAdd(memberList, member);
4755       }
4756
4757       if(parentDataMember.type == unionMember)
4758          break;
4759    }
4760 }
4761
4762 void PopulateInstance(Instantiation inst)
4763 {
4764    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4765    Class _class = classSym.registered;
4766    DataMember dataMember;
4767    OldList * memberList = MkList();
4768    // Added this check and ->Add to prevent memory leaks on bad code
4769    if(!inst.members)
4770       inst.members = MkListOne(MkMembersInitList(memberList));
4771    else
4772       inst.members->Add(MkMembersInitList(memberList));
4773    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4774    {
4775       if(!dataMember.isProperty)
4776       {
4777          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4778             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4779          else
4780          {
4781             Expression exp { };
4782             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4783             Type type;
4784             void * ptr = inst.data + dataMember.offset;
4785             char * result = null;
4786
4787             exp.loc = member.loc = inst.loc;
4788             ((Identifier)member.identifiers->first).loc = inst.loc;
4789
4790             if(!dataMember.dataType)
4791                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4792             type = dataMember.dataType;
4793             if(type.kind == classType)
4794             {
4795                Class _class = type._class.registered;
4796                if(_class.type == enumClass)
4797                {
4798                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4799                   if(enumClass)
4800                   {
4801                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4802                      NamedLink item;
4803                      for(item = e.values.first; item; item = item.next)
4804                      {
4805                         if((int)item.data == *(int *)ptr)
4806                         {
4807                            result = item.name;
4808                            break;
4809                         }
4810                      }
4811                   }
4812                   if(result)
4813                   {
4814                      exp.identifier = MkIdentifier(result);
4815                      exp.type = identifierExp;
4816                      exp.destType = MkClassType(_class.fullName);
4817                      ProcessExpressionType(exp);
4818                   }
4819                }
4820                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4821                {
4822                   if(!_class.dataType)
4823                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4824                   type = _class.dataType;
4825                }
4826             }
4827             if(!result)
4828             {
4829                switch(type.kind)
4830                {
4831                   case floatType:
4832                   {
4833                      exp.constant = PrintFloat(*(float*)ptr);
4834                      exp.type = constantExp;
4835                      break;
4836                   }
4837                   case doubleType:
4838                   {
4839                      exp.constant = PrintDouble(*(double*)ptr);
4840                      exp.type = constantExp;
4841                      break;
4842                   }
4843                   case intType:
4844                   {
4845                      exp.constant = PrintInt(*(int*)ptr);
4846                      exp.type = constantExp;
4847                      break;
4848                   }
4849                   case int64Type:
4850                   {
4851                      exp.constant = PrintInt64(*(int64*)ptr);
4852                      exp.type = constantExp;
4853                      break;
4854                   }
4855                   case intPtrType:
4856                   {
4857                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4858                      exp.type = constantExp;
4859                      break;
4860                   }
4861                   default:
4862                      Compiler_Error($"Unhandled type populating instance\n");
4863                }
4864             }
4865             ListAdd(memberList, member);
4866          }
4867       }
4868    }
4869 }
4870
4871 void ComputeInstantiation(Expression exp)
4872 {
4873    Instantiation inst = exp.instance;
4874    MembersInit members;
4875    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4876    Class _class = classSym ? classSym.registered : null;
4877    DataMember curMember = null;
4878    Class curClass = null;
4879    DataMember subMemberStack[256];
4880    int subMemberStackPos = 0;
4881    uint64 bits = 0;
4882
4883    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4884    {
4885       // Don't recompute the instantiation...
4886       // Non Simple classes will have become constants by now
4887       if(inst.data)
4888          return;
4889
4890       if(_class.type == normalClass || _class.type == noHeadClass)
4891       {
4892          inst.data = (byte *)eInstance_New(_class);
4893          if(_class.type == normalClass)
4894             ((Instance)inst.data)._refCount++;
4895       }
4896       else
4897          inst.data = new0 byte[_class.structSize];
4898    }
4899
4900    if(inst.members)
4901    {
4902       for(members = inst.members->first; members; members = members.next)
4903       {
4904          switch(members.type)
4905          {
4906             case dataMembersInit:
4907             {
4908                if(members.dataMembers)
4909                {
4910                   MemberInit member;
4911                   for(member = members.dataMembers->first; member; member = member.next)
4912                   {
4913                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4914                      bool found = false;
4915
4916                      Property prop = null;
4917                      DataMember dataMember = null;
4918                      uint dataMemberOffset;
4919
4920                      if(!ident)
4921                      {
4922                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4923                         if(curMember)
4924                         {
4925                            if(curMember.isProperty)
4926                               prop = (Property)curMember;
4927                            else
4928                            {
4929                               dataMember = curMember;
4930
4931                               // CHANGED THIS HERE
4932                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4933
4934                               // 2013/17/29 -- It seems that this was missing here!
4935                               if(_class.type == normalClass)
4936                                  dataMemberOffset += _class.base.structSize;
4937                               // dataMemberOffset = dataMember.offset;
4938                            }
4939                            found = true;
4940                         }
4941                      }
4942                      else
4943                      {
4944                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4945                         if(prop)
4946                         {
4947                            found = true;
4948                            if(prop.memberAccess == publicAccess)
4949                            {
4950                               curMember = (DataMember)prop;
4951                               curClass = prop._class;
4952                            }
4953                         }
4954                         else
4955                         {
4956                            DataMember _subMemberStack[256];
4957                            int _subMemberStackPos = 0;
4958
4959                            // FILL MEMBER STACK
4960                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4961
4962                            if(dataMember)
4963                            {
4964                               found = true;
4965                               if(dataMember.memberAccess == publicAccess)
4966                               {
4967                                  curMember = dataMember;
4968                                  curClass = dataMember._class;
4969                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4970                                  subMemberStackPos = _subMemberStackPos;
4971                               }
4972                            }
4973                         }
4974                      }
4975
4976                      if(found && member.initializer && member.initializer.type == expInitializer)
4977                      {
4978                         Expression value = member.initializer.exp;
4979                         Type type = null;
4980                         bool deepMember = false;
4981                         if(prop)
4982                         {
4983                            type = prop.dataType;
4984                         }
4985                         else if(dataMember)
4986                         {
4987                            if(!dataMember.dataType)
4988                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4989
4990                            type = dataMember.dataType;
4991                         }
4992
4993                         if(ident && ident.next)
4994                         {
4995                            deepMember = true;
4996
4997                            // for(; ident && type; ident = ident.next)
4998                            for(ident = ident.next; ident && type; ident = ident.next)
4999                            {
5000                               if(type.kind == classType)
5001                               {
5002                                  prop = eClass_FindProperty(type._class.registered,
5003                                     ident.string, privateModule);
5004                                  if(prop)
5005                                     type = prop.dataType;
5006                                  else
5007                                  {
5008                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
5009                                        ident.string, &dataMemberOffset, privateModule, null, null);
5010                                     if(dataMember)
5011                                        type = dataMember.dataType;
5012                                  }
5013                               }
5014                               else if(type.kind == structType || type.kind == unionType)
5015                               {
5016                                  Type memberType;
5017                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
5018                                  {
5019                                     if(!strcmp(memberType.name, ident.string))
5020                                     {
5021                                        type = memberType;
5022                                        break;
5023                                     }
5024                                  }
5025                               }
5026                            }
5027                         }
5028                         if(value)
5029                         {
5030                            FreeType(value.destType);
5031                            value.destType = type;
5032                            if(type) type.refCount++;
5033                            ComputeExpression(value);
5034                         }
5035                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
5036                         {
5037                            if(type.kind == classType)
5038                            {
5039                               Class _class = type._class.registered;
5040                               if(_class.type == bitClass || _class.type == unitClass ||
5041                                  _class.type == enumClass)
5042                               {
5043                                  if(!_class.dataType)
5044                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5045                                  type = _class.dataType;
5046                               }
5047                            }
5048
5049                            if(dataMember)
5050                            {
5051                               void * ptr = inst.data + dataMemberOffset;
5052
5053                               if(value.type == constantExp)
5054                               {
5055                                  switch(type.kind)
5056                                  {
5057                                     case intType:
5058                                     {
5059                                        GetInt(value, (int*)ptr);
5060                                        break;
5061                                     }
5062                                     case int64Type:
5063                                     {
5064                                        GetInt64(value, (int64*)ptr);
5065                                        break;
5066                                     }
5067                                     case intPtrType:
5068                                     {
5069                                        GetIntPtr(value, (intptr*)ptr);
5070                                        break;
5071                                     }
5072                                     case intSizeType:
5073                                     {
5074                                        GetIntSize(value, (intsize*)ptr);
5075                                        break;
5076                                     }
5077                                     case floatType:
5078                                     {
5079                                        GetFloat(value, (float*)ptr);
5080                                        break;
5081                                     }
5082                                     case doubleType:
5083                                     {
5084                                        GetDouble(value, (double *)ptr);
5085                                        break;
5086                                     }
5087                                  }
5088                               }
5089                               else if(value.type == instanceExp)
5090                               {
5091                                  if(type.kind == classType)
5092                                  {
5093                                     Class _class = type._class.registered;
5094                                     if(_class.type == structClass)
5095                                     {
5096                                        ComputeTypeSize(type);
5097                                        if(value.instance.data)
5098                                           memcpy(ptr, value.instance.data, type.size);
5099                                     }
5100                                  }
5101                               }
5102                            }
5103                            else if(prop)
5104                            {
5105                               if(value.type == instanceExp && value.instance.data)
5106                               {
5107                                  if(type.kind == classType)
5108                                  {
5109                                     Class _class = type._class.registered;
5110                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
5111                                     {
5112                                        void (*Set)(void *, void *) = (void *)prop.Set;
5113                                        Set(inst.data, value.instance.data);
5114                                        PopulateInstance(inst);
5115                                     }
5116                                  }
5117                               }
5118                               else if(value.type == constantExp)
5119                               {
5120                                  switch(type.kind)
5121                                  {
5122                                     case doubleType:
5123                                     {
5124                                        void (*Set)(void *, double) = (void *)prop.Set;
5125                                        Set(inst.data, strtod(value.constant, null) );
5126                                        break;
5127                                     }
5128                                     case floatType:
5129                                     {
5130                                        void (*Set)(void *, float) = (void *)prop.Set;
5131                                        Set(inst.data, (float)(strtod(value.constant, null)));
5132                                        break;
5133                                     }
5134                                     case intType:
5135                                     {
5136                                        void (*Set)(void *, int) = (void *)prop.Set;
5137                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5138                                        break;
5139                                     }
5140                                     case int64Type:
5141                                     {
5142                                        void (*Set)(void *, int64) = (void *)prop.Set;
5143                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5144                                        break;
5145                                     }
5146                                     case intPtrType:
5147                                     {
5148                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5149                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5150                                        break;
5151                                     }
5152                                     case intSizeType:
5153                                     {
5154                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5155                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5156                                        break;
5157                                     }
5158                                  }
5159                               }
5160                               else if(value.type == stringExp)
5161                               {
5162                                  char temp[1024];
5163                                  ReadString(temp, value.string);
5164                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5165                               }
5166                            }
5167                         }
5168                         else if(!deepMember && type && _class.type == unitClass)
5169                         {
5170                            if(prop)
5171                            {
5172                               // Only support converting units to units for now...
5173                               if(value.type == constantExp)
5174                               {
5175                                  if(type.kind == classType)
5176                                  {
5177                                     Class _class = type._class.registered;
5178                                     if(_class.type == unitClass)
5179                                     {
5180                                        if(!_class.dataType)
5181                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5182                                        type = _class.dataType;
5183                                     }
5184                                  }
5185                                  // TODO: Assuming same base type for units...
5186                                  switch(type.kind)
5187                                  {
5188                                     case floatType:
5189                                     {
5190                                        float fValue;
5191                                        float (*Set)(float) = (void *)prop.Set;
5192                                        GetFloat(member.initializer.exp, &fValue);
5193                                        exp.constant = PrintFloat(Set(fValue));
5194                                        exp.type = constantExp;
5195                                        break;
5196                                     }
5197                                     case doubleType:
5198                                     {
5199                                        double dValue;
5200                                        double (*Set)(double) = (void *)prop.Set;
5201                                        GetDouble(member.initializer.exp, &dValue);
5202                                        exp.constant = PrintDouble(Set(dValue));
5203                                        exp.type = constantExp;
5204                                        break;
5205                                     }
5206                                  }
5207                               }
5208                            }
5209                         }
5210                         else if(!deepMember && type && _class.type == bitClass)
5211                         {
5212                            if(prop)
5213                            {
5214                               if(value.type == instanceExp && value.instance.data)
5215                               {
5216                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5217                                  bits = Set(value.instance.data);
5218                               }
5219                               else if(value.type == constantExp)
5220                               {
5221                               }
5222                            }
5223                            else if(dataMember)
5224                            {
5225                               BitMember bitMember = (BitMember) dataMember;
5226                               Type type;
5227                               uint64 part;
5228                               bits = (bits & ~bitMember.mask);
5229                               if(!bitMember.dataType)
5230                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5231                               type = bitMember.dataType;
5232                               if(type.kind == classType && type._class && type._class.registered)
5233                               {
5234                                  if(!type._class.registered.dataType)
5235                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5236                                  type = type._class.registered.dataType;
5237                               }
5238                               switch(type.kind)
5239                               {
5240                                  case _BoolType:
5241                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5242                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (short *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5243                                  case intType:
5244                                  case longType:       { uint v; type.isSigned ? GetInt(value, (int *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5245                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (int64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5246                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (intptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5247                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (intsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5248                               }
5249                               bits |= part << bitMember.pos;
5250                            }
5251                         }
5252                      }
5253                      else
5254                      {
5255                         if(_class && _class.type == unitClass)
5256                         {
5257                            ComputeExpression(member.initializer.exp);
5258                            exp.constant = member.initializer.exp.constant;
5259                            exp.type = constantExp;
5260
5261                            member.initializer.exp.constant = null;
5262                         }
5263                      }
5264                   }
5265                }
5266                break;
5267             }
5268          }
5269       }
5270    }
5271    if(_class && _class.type == bitClass)
5272    {
5273       exp.constant = PrintHexUInt(bits);
5274       exp.type = constantExp;
5275    }
5276    if(exp.type != instanceExp)
5277    {
5278       FreeInstance(inst);
5279    }
5280 }
5281
5282 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5283 {
5284    bool result = false;
5285    switch(kind)
5286    {
5287       case shortType:
5288          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5289             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5290          break;
5291       case intType:
5292       case longType:
5293          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5294             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5295          break;
5296       case int64Type:
5297          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5298             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5299             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5300          break;
5301       case floatType:
5302          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5303             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5304             result = GetOpFloat(op, &op.f);
5305          break;
5306       case doubleType:
5307          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5308             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5309             result = GetOpDouble(op, &op.d);
5310          break;
5311       case pointerType:
5312          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5313             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5314             result = GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5315          break;
5316       case enumType:
5317          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5318             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5319             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5320          break;
5321       case intPtrType:
5322          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5323             result = isSigned ? GetOpInt64 /*GetOpIntPtr*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5324          break;
5325       case intSizeType:
5326          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5327             result = isSigned ? GetOpInt64 /*GetOpIntSize*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntSize*/(op, &op.ui64);
5328          break;
5329    }
5330    return result;
5331 }
5332
5333 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5334 {
5335    if(exp.op.op == SIZEOF)
5336    {
5337       FreeExpContents(exp);
5338       exp.type = constantExp;
5339       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5340    }
5341    else
5342    {
5343       if(!exp.op.exp1)
5344       {
5345          switch(exp.op.op)
5346          {
5347             // unary arithmetic
5348             case '+':
5349             {
5350                // Provide default unary +
5351                Expression exp2 = exp.op.exp2;
5352                exp.op.exp2 = null;
5353                FreeExpContents(exp);
5354                FreeType(exp.expType);
5355                FreeType(exp.destType);
5356                *exp = *exp2;
5357                delete exp2;
5358                break;
5359             }
5360             case '-':
5361                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5362                break;
5363             // unary arithmetic increment and decrement
5364                   //OPERATOR_ALL(UNARY, ++, Inc)
5365                   //OPERATOR_ALL(UNARY, --, Dec)
5366             // unary bitwise
5367             case '~':
5368                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5369                break;
5370             // unary logical negation
5371             case '!':
5372                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5373                break;
5374          }
5375       }
5376       else
5377       {
5378          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5379          {
5380             if(Promote(op2, op1.kind, op1.type.isSigned))
5381                op2.kind = op1.kind, op2.ops = op1.ops;
5382             else if(Promote(op1, op2.kind, op2.type.isSigned))
5383                op1.kind = op2.kind, op1.ops = op2.ops;
5384          }
5385          switch(exp.op.op)
5386          {
5387             // binary arithmetic
5388             case '+':
5389                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5390                break;
5391             case '-':
5392                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5393                break;
5394             case '*':
5395                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5396                break;
5397             case '/':
5398                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5399                break;
5400             case '%':
5401                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5402                break;
5403             // binary arithmetic assignment
5404                   //OPERATOR_ALL(BINARY, =, Asign)
5405                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5406                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5407                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5408                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5409                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5410             // binary bitwise
5411             case '&':
5412                if(exp.op.exp2)
5413                {
5414                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5415                }
5416                break;
5417             case '|':
5418                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5419                break;
5420             case '^':
5421                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5422                break;
5423             case LEFT_OP:
5424                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5425                break;
5426             case RIGHT_OP:
5427                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5428                break;
5429             // binary bitwise assignment
5430                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5431                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5432                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5433                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5434                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5435             // binary logical equality
5436             case EQ_OP:
5437                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5438                break;
5439             case NE_OP:
5440                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5441                break;
5442             // binary logical
5443             case AND_OP:
5444                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5445                break;
5446             case OR_OP:
5447                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5448                break;
5449             // binary logical relational
5450             case '>':
5451                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5452                break;
5453             case '<':
5454                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5455                break;
5456             case GE_OP:
5457                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5458                break;
5459             case LE_OP:
5460                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5461                break;
5462          }
5463       }
5464    }
5465 }
5466
5467 void ComputeExpression(Expression exp)
5468 {
5469    char expString[10240];
5470    expString[0] = '\0';
5471 #ifdef _DEBUG
5472    PrintExpression(exp, expString);
5473 #endif
5474
5475    switch(exp.type)
5476    {
5477       case instanceExp:
5478       {
5479          ComputeInstantiation(exp);
5480          break;
5481       }
5482       /*
5483       case constantExp:
5484          break;
5485       */
5486       case opExp:
5487       {
5488          Expression exp1, exp2 = null;
5489          Operand op1 { };
5490          Operand op2 { };
5491
5492          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5493          if(exp.op.exp2)
5494          {
5495             Expression e = exp.op.exp2;
5496
5497             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5498             {
5499                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5500                {
5501                   if(e.type == extensionCompoundExp)
5502                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5503                   else
5504                      e = e.list->last;
5505                }
5506             }
5507             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5508             {
5509                if(e.type == stringExp && e.string)
5510                {
5511                   char * string = e.string;
5512                   int len = strlen(string);
5513                   char * tmp = new char[len-2+1];
5514                   len = UnescapeString(tmp, string + 1, len - 2);
5515                   delete tmp;
5516                   FreeExpContents(exp);
5517                   exp.type = constantExp;
5518                   exp.constant = PrintUInt(len + 1);
5519                }
5520                else
5521                {
5522                   Type type = e.expType;
5523                   type.refCount++;
5524                   FreeExpContents(exp);
5525                   exp.type = constantExp;
5526                   exp.constant = PrintUInt(ComputeTypeSize(type));
5527                   FreeType(type);
5528                }
5529                break;
5530             }
5531             else
5532                ComputeExpression(exp.op.exp2);
5533          }
5534          if(exp.op.exp1)
5535          {
5536             ComputeExpression(exp.op.exp1);
5537             exp1 = exp.op.exp1;
5538             exp2 = exp.op.exp2;
5539             op1 = GetOperand(exp1);
5540             if(op1.type) op1.type.refCount++;
5541             if(exp2)
5542             {
5543                op2 = GetOperand(exp2);
5544                if(op2.type) op2.type.refCount++;
5545             }
5546          }
5547          else
5548          {
5549             exp1 = exp.op.exp2;
5550             op1 = GetOperand(exp1);
5551             if(op1.type) op1.type.refCount++;
5552          }
5553
5554          CallOperator(exp, exp1, exp2, op1, op2);
5555          /*
5556          switch(exp.op.op)
5557          {
5558             // Unary operators
5559             case '&':
5560                // Also binary
5561                if(exp.op.exp1 && exp.op.exp2)
5562                {
5563                   // Binary And
5564                   if(op1.ops.BitAnd)
5565                   {
5566                      FreeExpContents(exp);
5567                      op1.ops.BitAnd(exp, op1, op2);
5568                   }
5569                }
5570                break;
5571             case '*':
5572                if(exp.op.exp1)
5573                {
5574                   if(op1.ops.Mul)
5575                   {
5576                      FreeExpContents(exp);
5577                      op1.ops.Mul(exp, op1, op2);
5578                   }
5579                }
5580                break;
5581             case '+':
5582                if(exp.op.exp1)
5583                {
5584                   if(op1.ops.Add)
5585                   {
5586                      FreeExpContents(exp);
5587                      op1.ops.Add(exp, op1, op2);
5588                   }
5589                }
5590                else
5591                {
5592                   // Provide default unary +
5593                   Expression exp2 = exp.op.exp2;
5594                   exp.op.exp2 = null;
5595                   FreeExpContents(exp);
5596                   FreeType(exp.expType);
5597                   FreeType(exp.destType);
5598
5599                   *exp = *exp2;
5600                   delete exp2;
5601                }
5602                break;
5603             case '-':
5604                if(exp.op.exp1)
5605                {
5606                   if(op1.ops.Sub)
5607                   {
5608                      FreeExpContents(exp);
5609                      op1.ops.Sub(exp, op1, op2);
5610                   }
5611                }
5612                else
5613                {
5614                   if(op1.ops.Neg)
5615                   {
5616                      FreeExpContents(exp);
5617                      op1.ops.Neg(exp, op1);
5618                   }
5619                }
5620                break;
5621             case '~':
5622                if(op1.ops.BitNot)
5623                {
5624                   FreeExpContents(exp);
5625                   op1.ops.BitNot(exp, op1);
5626                }
5627                break;
5628             case '!':
5629                if(op1.ops.Not)
5630                {
5631                   FreeExpContents(exp);
5632                   op1.ops.Not(exp, op1);
5633                }
5634                break;
5635             // Binary only operators
5636             case '/':
5637                if(op1.ops.Div)
5638                {
5639                   FreeExpContents(exp);
5640                   op1.ops.Div(exp, op1, op2);
5641                }
5642                break;
5643             case '%':
5644                if(op1.ops.Mod)
5645                {
5646                   FreeExpContents(exp);
5647                   op1.ops.Mod(exp, op1, op2);
5648                }
5649                break;
5650             case LEFT_OP:
5651                break;
5652             case RIGHT_OP:
5653                break;
5654             case '<':
5655                if(exp.op.exp1)
5656                {
5657                   if(op1.ops.Sma)
5658                   {
5659                      FreeExpContents(exp);
5660                      op1.ops.Sma(exp, op1, op2);
5661                   }
5662                }
5663                break;
5664             case '>':
5665                if(exp.op.exp1)
5666                {
5667                   if(op1.ops.Grt)
5668                   {
5669                      FreeExpContents(exp);
5670                      op1.ops.Grt(exp, op1, op2);
5671                   }
5672                }
5673                break;
5674             case LE_OP:
5675                if(exp.op.exp1)
5676                {
5677                   if(op1.ops.SmaEqu)
5678                   {
5679                      FreeExpContents(exp);
5680                      op1.ops.SmaEqu(exp, op1, op2);
5681                   }
5682                }
5683                break;
5684             case GE_OP:
5685                if(exp.op.exp1)
5686                {
5687                   if(op1.ops.GrtEqu)
5688                   {
5689                      FreeExpContents(exp);
5690                      op1.ops.GrtEqu(exp, op1, op2);
5691                   }
5692                }
5693                break;
5694             case EQ_OP:
5695                if(exp.op.exp1)
5696                {
5697                   if(op1.ops.Equ)
5698                   {
5699                      FreeExpContents(exp);
5700                      op1.ops.Equ(exp, op1, op2);
5701                   }
5702                }
5703                break;
5704             case NE_OP:
5705                if(exp.op.exp1)
5706                {
5707                   if(op1.ops.Nqu)
5708                   {
5709                      FreeExpContents(exp);
5710                      op1.ops.Nqu(exp, op1, op2);
5711                   }
5712                }
5713                break;
5714             case '|':
5715                if(op1.ops.BitOr)
5716                {
5717                   FreeExpContents(exp);
5718                   op1.ops.BitOr(exp, op1, op2);
5719                }
5720                break;
5721             case '^':
5722                if(op1.ops.BitXor)
5723                {
5724                   FreeExpContents(exp);
5725                   op1.ops.BitXor(exp, op1, op2);
5726                }
5727                break;
5728             case AND_OP:
5729                break;
5730             case OR_OP:
5731                break;
5732             case SIZEOF:
5733                FreeExpContents(exp);
5734                exp.type = constantExp;
5735                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5736                break;
5737          }
5738          */
5739          if(op1.type) FreeType(op1.type);
5740          if(op2.type) FreeType(op2.type);
5741          break;
5742       }
5743       case bracketsExp:
5744       case extensionExpressionExp:
5745       {
5746          Expression e, n;
5747          for(e = exp.list->first; e; e = n)
5748          {
5749             n = e.next;
5750             if(!n)
5751             {
5752                OldList * list = exp.list;
5753                Expression prev = exp.prev;
5754                Expression next = exp.next;
5755                ComputeExpression(e);
5756                //FreeExpContents(exp);
5757                FreeType(exp.expType);
5758                FreeType(exp.destType);
5759                *exp = *e;
5760                exp.prev = prev;
5761                exp.next = next;
5762                delete e;
5763                delete list;
5764             }
5765             else
5766             {
5767                FreeExpression(e);
5768             }
5769          }
5770          break;
5771       }
5772       /*
5773
5774       case ExpIndex:
5775       {
5776          Expression e;
5777          exp.isConstant = true;
5778
5779          ComputeExpression(exp.index.exp);
5780          if(!exp.index.exp.isConstant)
5781             exp.isConstant = false;
5782
5783          for(e = exp.index.index->first; e; e = e.next)
5784          {
5785             ComputeExpression(e);
5786             if(!e.next)
5787             {
5788                // Check if this type is int
5789             }
5790             if(!e.isConstant)
5791                exp.isConstant = false;
5792          }
5793          exp.expType = Dereference(exp.index.exp.expType);
5794          break;
5795       }
5796       */
5797       case memberExp:
5798       {
5799          Expression memberExp = exp.member.exp;
5800          Identifier memberID = exp.member.member;
5801
5802          Type type;
5803          ComputeExpression(exp.member.exp);
5804          type = exp.member.exp.expType;
5805          if(type)
5806          {
5807             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);
5808             Property prop = null;
5809             DataMember member = null;
5810             Class convertTo = null;
5811             if(type.kind == subClassType && exp.member.exp.type == classExp)
5812                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5813
5814             if(!_class)
5815             {
5816                char string[256];
5817                Symbol classSym;
5818                string[0] = '\0';
5819                PrintTypeNoConst(type, string, false, true);
5820                classSym = FindClass(string);
5821                _class = classSym ? classSym.registered : null;
5822             }
5823
5824             if(exp.member.member)
5825             {
5826                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5827                if(!prop)
5828                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5829             }
5830             if(!prop && !member && _class && exp.member.member)
5831             {
5832                Symbol classSym = FindClass(exp.member.member.string);
5833                convertTo = _class;
5834                _class = classSym ? classSym.registered : null;
5835                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5836             }
5837
5838             if(prop)
5839             {
5840                if(prop.compiled)
5841                {
5842                   Type type = prop.dataType;
5843                   // TODO: Assuming same base type for units...
5844                   if(_class.type == unitClass)
5845                   {
5846                      if(type.kind == classType)
5847                      {
5848                         Class _class = type._class.registered;
5849                         if(_class.type == unitClass)
5850                         {
5851                            if(!_class.dataType)
5852                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5853                            type = _class.dataType;
5854                         }
5855                      }
5856                      switch(type.kind)
5857                      {
5858                         case floatType:
5859                         {
5860                            float value;
5861                            float (*Get)(float) = (void *)prop.Get;
5862                            GetFloat(exp.member.exp, &value);
5863                            exp.constant = PrintFloat(Get ? Get(value) : value);
5864                            exp.type = constantExp;
5865                            break;
5866                         }
5867                         case doubleType:
5868                         {
5869                            double value;
5870                            double (*Get)(double);
5871                            GetDouble(exp.member.exp, &value);
5872
5873                            if(convertTo)
5874                               Get = (void *)prop.Set;
5875                            else
5876                               Get = (void *)prop.Get;
5877                            exp.constant = PrintDouble(Get ? Get(value) : value);
5878                            exp.type = constantExp;
5879                            break;
5880                         }
5881                      }
5882                   }
5883                   else
5884                   {
5885                      if(convertTo)
5886                      {
5887                         Expression value = exp.member.exp;
5888                         Type type;
5889                         if(!prop.dataType)
5890                            ProcessPropertyType(prop);
5891
5892                         type = prop.dataType;
5893                         if(!type)
5894                         {
5895                             // printf("Investigate this\n");
5896                         }
5897                         else if(_class.type == structClass)
5898                         {
5899                            switch(type.kind)
5900                            {
5901                               case classType:
5902                               {
5903                                  Class propertyClass = type._class.registered;
5904                                  if(propertyClass.type == structClass && value.type == instanceExp)
5905                                  {
5906                                     void (*Set)(void *, void *) = (void *)prop.Set;
5907                                     exp.instance = Instantiation { };
5908                                     exp.instance.data = new0 byte[_class.structSize];
5909                                     exp.instance._class = MkSpecifierName(_class.fullName);
5910                                     exp.instance.loc = exp.loc;
5911                                     exp.type = instanceExp;
5912                                     Set(exp.instance.data, value.instance.data);
5913                                     PopulateInstance(exp.instance);
5914                                  }
5915                                  break;
5916                               }
5917                               case intType:
5918                               {
5919                                  int intValue;
5920                                  void (*Set)(void *, int) = (void *)prop.Set;
5921
5922                                  exp.instance = Instantiation { };
5923                                  exp.instance.data = new0 byte[_class.structSize];
5924                                  exp.instance._class = MkSpecifierName(_class.fullName);
5925                                  exp.instance.loc = exp.loc;
5926                                  exp.type = instanceExp;
5927
5928                                  GetInt(value, &intValue);
5929
5930                                  Set(exp.instance.data, intValue);
5931                                  PopulateInstance(exp.instance);
5932                                  break;
5933                               }
5934                               case int64Type:
5935                               {
5936                                  int64 intValue;
5937                                  void (*Set)(void *, int64) = (void *)prop.Set;
5938
5939                                  exp.instance = Instantiation { };
5940                                  exp.instance.data = new0 byte[_class.structSize];
5941                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5942                                  exp.instance.loc = exp.loc;
5943                                  exp.type = instanceExp;
5944
5945                                  GetInt64(value, &intValue);
5946
5947                                  Set(exp.instance.data, intValue);
5948                                  PopulateInstance(exp.instance);
5949                                  break;
5950                               }
5951                               case intPtrType:
5952                               {
5953                                  // TOFIX:
5954                                  intptr intValue;
5955                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5956
5957                                  exp.instance = Instantiation { };
5958                                  exp.instance.data = new0 byte[_class.structSize];
5959                                  exp.instance._class = MkSpecifierName(_class.fullName);
5960                                  exp.instance.loc = exp.loc;
5961                                  exp.type = instanceExp;
5962
5963                                  GetIntPtr(value, &intValue);
5964
5965                                  Set(exp.instance.data, intValue);
5966                                  PopulateInstance(exp.instance);
5967                                  break;
5968                               }
5969                               case intSizeType:
5970                               {
5971                                  // TOFIX:
5972                                  intsize intValue;
5973                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5974
5975                                  exp.instance = Instantiation { };
5976                                  exp.instance.data = new0 byte[_class.structSize];
5977                                  exp.instance._class = MkSpecifierName(_class.fullName);
5978                                  exp.instance.loc = exp.loc;
5979                                  exp.type = instanceExp;
5980
5981                                  GetIntSize(value, &intValue);
5982
5983                                  Set(exp.instance.data, intValue);
5984                                  PopulateInstance(exp.instance);
5985                                  break;
5986                               }
5987                               case floatType:
5988                               {
5989                                  float floatValue;
5990                                  void (*Set)(void *, float) = (void *)prop.Set;
5991
5992                                  exp.instance = Instantiation { };
5993                                  exp.instance.data = new0 byte[_class.structSize];
5994                                  exp.instance._class = MkSpecifierName(_class.fullName);
5995                                  exp.instance.loc = exp.loc;
5996                                  exp.type = instanceExp;
5997
5998                                  GetFloat(value, &floatValue);
5999
6000                                  Set(exp.instance.data, floatValue);
6001                                  PopulateInstance(exp.instance);
6002                                  break;
6003                               }
6004                               case doubleType:
6005                               {
6006                                  double doubleValue;
6007                                  void (*Set)(void *, double) = (void *)prop.Set;
6008
6009                                  exp.instance = Instantiation { };
6010                                  exp.instance.data = new0 byte[_class.structSize];
6011                                  exp.instance._class = MkSpecifierName(_class.fullName);
6012                                  exp.instance.loc = exp.loc;
6013                                  exp.type = instanceExp;
6014
6015                                  GetDouble(value, &doubleValue);
6016
6017                                  Set(exp.instance.data, doubleValue);
6018                                  PopulateInstance(exp.instance);
6019                                  break;
6020                               }
6021                            }
6022                         }
6023                         else if(_class.type == bitClass)
6024                         {
6025                            switch(type.kind)
6026                            {
6027                               case classType:
6028                               {
6029                                  Class propertyClass = type._class.registered;
6030                                  if(propertyClass.type == structClass && value.instance.data)
6031                                  {
6032                                     unsigned int (*Set)(void *) = (void *)prop.Set;
6033                                     unsigned int bits = Set(value.instance.data);
6034                                     exp.constant = PrintHexUInt(bits);
6035                                     exp.type = constantExp;
6036                                     break;
6037                                  }
6038                                  else if(_class.type == bitClass)
6039                                  {
6040                                     unsigned int value;
6041                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
6042                                     unsigned int bits;
6043
6044                                     GetUInt(exp.member.exp, &value);
6045                                     bits = Set(value);
6046                                     exp.constant = PrintHexUInt(bits);
6047                                     exp.type = constantExp;
6048                                  }
6049                               }
6050                            }
6051                         }
6052                      }
6053                      else
6054                      {
6055                         if(_class.type == bitClass)
6056                         {
6057                            unsigned int value;
6058                            GetUInt(exp.member.exp, &value);
6059
6060                            switch(type.kind)
6061                            {
6062                               case classType:
6063                               {
6064                                  Class _class = type._class.registered;
6065                                  if(_class.type == structClass)
6066                                  {
6067                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
6068
6069                                     exp.instance = Instantiation { };
6070                                     exp.instance.data = new0 byte[_class.structSize];
6071                                     exp.instance._class = MkSpecifierName(_class.fullName);
6072                                     exp.instance.loc = exp.loc;
6073                                     //exp.instance.fullSet = true;
6074                                     exp.type = instanceExp;
6075                                     Get(value, exp.instance.data);
6076                                     PopulateInstance(exp.instance);
6077                                  }
6078                                  else if(_class.type == bitClass)
6079                                  {
6080                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
6081                                     uint64 bits = Get(value);
6082                                     exp.constant = PrintHexUInt64(bits);
6083                                     exp.type = constantExp;
6084                                  }
6085                                  break;
6086                               }
6087                            }
6088                         }
6089                         else if(_class.type == structClass)
6090                         {
6091                            byte * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
6092                            switch(type.kind)
6093                            {
6094                               case classType:
6095                               {
6096                                  Class _class = type._class.registered;
6097                                  if(_class.type == structClass && value)
6098                                  {
6099                                     void (*Get)(void *, void *) = (void *)prop.Get;
6100
6101                                     exp.instance = Instantiation { };
6102                                     exp.instance.data = new0 byte[_class.structSize];
6103                                     exp.instance._class = MkSpecifierName(_class.fullName);
6104                                     exp.instance.loc = exp.loc;
6105                                     //exp.instance.fullSet = true;
6106                                     exp.type = instanceExp;
6107                                     Get(value, exp.instance.data);
6108                                     PopulateInstance(exp.instance);
6109                                  }
6110                                  break;
6111                               }
6112                            }
6113                         }
6114                         /*else
6115                         {
6116                            char * value = exp.member.exp.instance.data;
6117                            switch(type.kind)
6118                            {
6119                               case classType:
6120                               {
6121                                  Class _class = type._class.registered;
6122                                  if(_class.type == normalClass)
6123                                  {
6124                                     void *(*Get)(void *) = (void *)prop.Get;
6125
6126                                     exp.instance = Instantiation { };
6127                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6128                                     exp.type = instanceExp;
6129                                     exp.instance.data = Get(value, exp.instance.data);
6130                                  }
6131                                  break;
6132                               }
6133                            }
6134                         }
6135                         */
6136                      }
6137                   }
6138                }
6139                else
6140                {
6141                   exp.isConstant = false;
6142                }
6143             }
6144             else if(member)
6145             {
6146             }
6147          }
6148
6149          if(exp.type != ExpressionType::memberExp)
6150          {
6151             FreeExpression(memberExp);
6152             FreeIdentifier(memberID);
6153          }
6154          break;
6155       }
6156       case typeSizeExp:
6157       {
6158          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6159          FreeExpContents(exp);
6160          exp.constant = PrintUInt(ComputeTypeSize(type));
6161          exp.type = constantExp;
6162          FreeType(type);
6163          break;
6164       }
6165       case classSizeExp:
6166       {
6167          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6168          if(classSym && classSym.registered)
6169          {
6170             if(classSym.registered.fixed)
6171             {
6172                FreeSpecifier(exp._class);
6173                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6174                exp.type = constantExp;
6175             }
6176             else
6177             {
6178                char className[1024];
6179                strcpy(className, "__ecereClass_");
6180                FullClassNameCat(className, classSym.string, true);
6181                //MangleClassName(className);
6182
6183                DeclareClass(classSym, className);
6184
6185                FreeExpContents(exp);
6186                exp.type = pointerExp;
6187                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6188                exp.member.member = MkIdentifier("structSize");
6189             }
6190          }
6191          break;
6192       }
6193       case castExp:
6194       //case constantExp:
6195       {
6196          Type type;
6197          Expression e = exp;
6198          if(exp.type == castExp)
6199          {
6200             if(exp.cast.exp)
6201                ComputeExpression(exp.cast.exp);
6202             e = exp.cast.exp;
6203          }
6204          if(e && exp.expType)
6205          {
6206             /*if(exp.destType)
6207                type = exp.destType;
6208             else*/
6209                type = exp.expType;
6210             if(type.kind == classType)
6211             {
6212                Class _class = type._class.registered;
6213                if(_class && (_class.type == unitClass || _class.type == bitClass))
6214                {
6215                   if(!_class.dataType)
6216                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6217                   type = _class.dataType;
6218                }
6219             }
6220
6221             switch(type.kind)
6222             {
6223                case _BoolType:
6224                case charType:
6225                   if(type.isSigned)
6226                   {
6227                      char value = 0;
6228                      if(GetChar(e, &value))
6229                      {
6230                         FreeExpContents(exp);
6231                         exp.constant = PrintChar(value);
6232                         exp.type = constantExp;
6233                      }
6234                   }
6235                   else
6236                   {
6237                      unsigned char value = 0;
6238                      if(GetUChar(e, &value))
6239                      {
6240                         FreeExpContents(exp);
6241                         exp.constant = PrintUChar(value);
6242                         exp.type = constantExp;
6243                      }
6244                   }
6245                   break;
6246                case shortType:
6247                   if(type.isSigned)
6248                   {
6249                      short value = 0;
6250                      if(GetShort(e, &value))
6251                      {
6252                         FreeExpContents(exp);
6253                         exp.constant = PrintShort(value);
6254                         exp.type = constantExp;
6255                      }
6256                   }
6257                   else
6258                   {
6259                      unsigned short value = 0;
6260                      if(GetUShort(e, &value))
6261                      {
6262                         FreeExpContents(exp);
6263                         exp.constant = PrintUShort(value);
6264                         exp.type = constantExp;
6265                      }
6266                   }
6267                   break;
6268                case intType:
6269                   if(type.isSigned)
6270                   {
6271                      int value = 0;
6272                      if(GetInt(e, &value))
6273                      {
6274                         FreeExpContents(exp);
6275                         exp.constant = PrintInt(value);
6276                         exp.type = constantExp;
6277                      }
6278                   }
6279                   else
6280                   {
6281                      unsigned int value = 0;
6282                      if(GetUInt(e, &value))
6283                      {
6284                         FreeExpContents(exp);
6285                         exp.constant = PrintUInt(value);
6286                         exp.type = constantExp;
6287                      }
6288                   }
6289                   break;
6290                case int64Type:
6291                   if(type.isSigned)
6292                   {
6293                      int64 value = 0;
6294                      if(GetInt64(e, &value))
6295                      {
6296                         FreeExpContents(exp);
6297                         exp.constant = PrintInt64(value);
6298                         exp.type = constantExp;
6299                      }
6300                   }
6301                   else
6302                   {
6303                      uint64 value = 0;
6304                      if(GetUInt64(e, &value))
6305                      {
6306                         FreeExpContents(exp);
6307                         exp.constant = PrintUInt64(value);
6308                         exp.type = constantExp;
6309                      }
6310                   }
6311                   break;
6312                case intPtrType:
6313                   if(type.isSigned)
6314                   {
6315                      intptr value = 0;
6316                      if(GetIntPtr(e, &value))
6317                      {
6318                         FreeExpContents(exp);
6319                         exp.constant = PrintInt64((int64)value);
6320                         exp.type = constantExp;
6321                      }
6322                   }
6323                   else
6324                   {
6325                      uintptr value = 0;
6326                      if(GetUIntPtr(e, &value))
6327                      {
6328                         FreeExpContents(exp);
6329                         exp.constant = PrintUInt64((uint64)value);
6330                         exp.type = constantExp;
6331                      }
6332                   }
6333                   break;
6334                case intSizeType:
6335                   if(type.isSigned)
6336                   {
6337                      intsize value = 0;
6338                      if(GetIntSize(e, &value))
6339                      {
6340                         FreeExpContents(exp);
6341                         exp.constant = PrintInt64((int64)value);
6342                         exp.type = constantExp;
6343                      }
6344                   }
6345                   else
6346                   {
6347                      uintsize value = 0;
6348                      if(GetUIntSize(e, &value))
6349                      {
6350                         FreeExpContents(exp);
6351                         exp.constant = PrintUInt64((uint64)value);
6352                         exp.type = constantExp;
6353                      }
6354                   }
6355                   break;
6356                case floatType:
6357                {
6358                   float value = 0;
6359                   if(GetFloat(e, &value))
6360                   {
6361                      FreeExpContents(exp);
6362                      exp.constant = PrintFloat(value);
6363                      exp.type = constantExp;
6364                   }
6365                   break;
6366                }
6367                case doubleType:
6368                {
6369                   double value = 0;
6370                   if(GetDouble(e, &value))
6371                   {
6372                      FreeExpContents(exp);
6373                      exp.constant = PrintDouble(value);
6374                      exp.type = constantExp;
6375                   }
6376                   break;
6377                }
6378             }
6379          }
6380          break;
6381       }
6382       case conditionExp:
6383       {
6384          Operand op1 { };
6385          Operand op2 { };
6386          Operand op3 { };
6387
6388          if(exp.cond.exp)
6389             // Caring only about last expression for now...
6390             ComputeExpression(exp.cond.exp->last);
6391          if(exp.cond.elseExp)
6392             ComputeExpression(exp.cond.elseExp);
6393          if(exp.cond.cond)
6394             ComputeExpression(exp.cond.cond);
6395
6396          op1 = GetOperand(exp.cond.cond);
6397          if(op1.type) op1.type.refCount++;
6398          op2 = GetOperand(exp.cond.exp->last);
6399          if(op2.type) op2.type.refCount++;
6400          op3 = GetOperand(exp.cond.elseExp);
6401          if(op3.type) op3.type.refCount++;
6402
6403          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6404          if(op1.type) FreeType(op1.type);
6405          if(op2.type) FreeType(op2.type);
6406          if(op3.type) FreeType(op3.type);
6407          break;
6408       }
6409    }
6410 }
6411
6412 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla, bool warnConst)
6413 {
6414    bool result = true;
6415    if(destType)
6416    {
6417       OldList converts { };
6418       Conversion convert;
6419
6420       if(destType.kind == voidType)
6421          return false;
6422
6423       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla, warnConst))
6424          result = false;
6425       if(converts.count)
6426       {
6427          // for(convert = converts.last; convert; convert = convert.prev)
6428          for(convert = converts.first; convert; convert = convert.next)
6429          {
6430             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6431             if(!empty)
6432             {
6433                Expression newExp { };
6434                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6435
6436                // TODO: Check this...
6437                *newExp = *exp;
6438                newExp.prev = null;
6439                newExp.next = null;
6440                newExp.destType = null;
6441
6442                if(convert.isGet)
6443                {
6444                   // [exp].ColorRGB
6445                   exp.type = memberExp;
6446                   exp.addedThis = true;
6447                   exp.member.exp = newExp;
6448                   FreeType(exp.member.exp.expType);
6449
6450                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6451                   exp.member.exp.expType.classObjectType = objectType;
6452                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6453                   exp.member.memberType = propertyMember;
6454                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6455                   // TESTING THIS... for (int)degrees
6456                   exp.needCast = true;
6457                   if(exp.expType) exp.expType.refCount++;
6458                   ApplyAnyObjectLogic(exp.member.exp);
6459                }
6460                else
6461                {
6462
6463                   /*if(exp.isConstant)
6464                   {
6465                      // Color { ColorRGB = [exp] };
6466                      exp.type = instanceExp;
6467                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6468                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6469                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6470                   }
6471                   else*/
6472                   {
6473                      // If not constant, don't turn it yet into an instantiation
6474                      // (Go through the deep members system first)
6475                      exp.type = memberExp;
6476                      exp.addedThis = true;
6477                      exp.member.exp = newExp;
6478
6479                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6480                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6481                         newExp.expType._class.registered.type == noHeadClass)
6482                      {
6483                         newExp.byReference = true;
6484                      }
6485
6486                      FreeType(exp.member.exp.expType);
6487                      /*exp.member.exp.expType = convert.convert.dataType;
6488                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6489                      exp.member.exp.expType = null;
6490                      if(convert.convert.dataType)
6491                      {
6492                         exp.member.exp.expType = { };
6493                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6494                         exp.member.exp.expType.refCount = 1;
6495                         exp.member.exp.expType.classObjectType = objectType;
6496                         ApplyAnyObjectLogic(exp.member.exp);
6497                      }
6498
6499                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6500                      exp.member.memberType = reverseConversionMember;
6501                      exp.expType = convert.resultType ? convert.resultType :
6502                         MkClassType(convert.convert._class.fullName);
6503                      exp.needCast = true;
6504                      if(convert.resultType) convert.resultType.refCount++;
6505                   }
6506                }
6507             }
6508             else
6509             {
6510                FreeType(exp.expType);
6511                if(convert.isGet)
6512                {
6513                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6514                   if(exp.destType.casted)
6515                      exp.needCast = true;
6516                   if(exp.expType) exp.expType.refCount++;
6517                }
6518                else
6519                {
6520                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6521                   if(exp.destType.casted)
6522                      exp.needCast = true;
6523                   if(convert.resultType)
6524                      convert.resultType.refCount++;
6525                }
6526             }
6527          }
6528          if(exp.isConstant && inCompiler)
6529             ComputeExpression(exp);
6530
6531          converts.Free(FreeConvert);
6532       }
6533
6534       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6535       {
6536          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false, warnConst);
6537       }
6538       if(!result && exp.expType && exp.destType)
6539       {
6540          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6541              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6542             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6543             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6544             result = true;
6545       }
6546    }
6547    // if(result) CheckTemplateTypes(exp);
6548    return result;
6549 }
6550
6551 void CheckTemplateTypes(Expression exp)
6552 {
6553    Expression nbExp = GetNonBracketsExp(exp);
6554    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate &&
6555       (nbExp == exp || nbExp.type != castExp))
6556    {
6557       Expression newExp { };
6558       Context context;
6559       *newExp = *exp;
6560       if(exp.destType) exp.destType.refCount++;
6561       if(exp.expType)  exp.expType.refCount++;
6562       newExp.prev = null;
6563       newExp.next = null;
6564
6565       switch(exp.expType.kind)
6566       {
6567          case doubleType:
6568             if(exp.destType.classObjectType)
6569             {
6570                // We need to pass the address, just pass it along (Undo what was done above)
6571                if(exp.destType) exp.destType.refCount--;
6572                if(exp.expType)  exp.expType.refCount--;
6573                delete newExp;
6574             }
6575             else
6576             {
6577                // If we're looking for value:
6578                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6579                OldList * specs;
6580                OldList * unionDefs = MkList();
6581                OldList * statements = MkList();
6582                context = PushContext();
6583                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6584                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6585                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6586                exp.type = extensionCompoundExp;
6587                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6588                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6589                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6590                exp.compound.compound.context = context;
6591                PopContext(context);
6592             }
6593             break;
6594          default:
6595             exp.type = castExp;
6596             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6597             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6598             exp.needCast = true;
6599             break;
6600       }
6601    }
6602    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6603    {
6604       Expression newExp { };
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       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
7028          PrintAttribs(type, string);
7029       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
7030          strcat(string, " const");
7031       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
7032       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7033          strcat(string, " (");
7034       if(type.kind == pointerType)
7035       {
7036          if(type.type.kind == functionType || type.type.kind == methodType)
7037             PrintAttribs(type.type, string);
7038       }
7039       if(type.kind == pointerType)
7040       {
7041          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
7042             strcat(string, "*");
7043          else
7044             strcat(string, " *");
7045       }
7046       if(printConst && type.constant && type.kind == pointerType)
7047          strcat(string, " const");
7048    }
7049    else
7050       PrintTypeSpecs(type, string, fullName, printConst);
7051 }
7052
7053 static void PostPrintType(Type type, char * string, bool fullName)
7054 {
7055    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7056       strcat(string, ")");
7057    if(type.kind == arrayType)
7058       PrintArraySize(type, string);
7059    else if(type.kind == functionType)
7060    {
7061       Type param;
7062       strcat(string, "(");
7063       for(param = type.params.first; param; param = param.next)
7064       {
7065          PrintType(param, string, true, fullName);
7066          if(param.next) strcat(string, ", ");
7067       }
7068       strcat(string, ")");
7069    }
7070    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7071       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7072 }
7073
7074 // *****
7075 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7076 // *****
7077 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7078 {
7079    PrePrintType(type, string, fullName, null, printConst);
7080
7081    if(type.thisClass || (printName && type.name && type.name[0]))
7082       strcat(string, " ");
7083    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7084    {
7085       Symbol _class = type.thisClass;
7086       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7087       {
7088          if(type.classObjectType == classPointer)
7089             strcat(string, "class");
7090          else
7091             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7092       }
7093       else if(_class && _class.string)
7094       {
7095          String s = _class.string;
7096          if(fullName)
7097             strcat(string, s);
7098          else
7099          {
7100             char * name = RSearchString(s, "::", strlen(s), true, false);
7101             if(name) name += 2; else name = s;
7102             strcat(string, name);
7103          }
7104       }
7105       strcat(string, "::");
7106    }
7107
7108    if(printName && type.name)
7109       PrintName(type, string, fullName);
7110    PostPrintType(type, string, fullName);
7111    if(type.bitFieldCount)
7112    {
7113       char count[100];
7114       sprintf(count, ":%d", type.bitFieldCount);
7115       strcat(string, count);
7116    }
7117 }
7118
7119 void PrintType(Type type, char * string, bool printName, bool fullName)
7120 {
7121    _PrintType(type, string, printName, fullName, true);
7122 }
7123
7124 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7125 {
7126    _PrintType(type, string, printName, fullName, false);
7127 }
7128
7129 static Type FindMember(Type type, char * string)
7130 {
7131    Type memberType;
7132    for(memberType = type.members.first; memberType; memberType = memberType.next)
7133    {
7134       if(!memberType.name)
7135       {
7136          Type subType = FindMember(memberType, string);
7137          if(subType)
7138             return subType;
7139       }
7140       else if(!strcmp(memberType.name, string))
7141          return memberType;
7142    }
7143    return null;
7144 }
7145
7146 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7147 {
7148    Type memberType;
7149    for(memberType = type.members.first; memberType; memberType = memberType.next)
7150    {
7151       if(!memberType.name)
7152       {
7153          Type subType = FindMember(memberType, string);
7154          if(subType)
7155          {
7156             *offset += memberType.offset;
7157             return subType;
7158          }
7159       }
7160       else if(!strcmp(memberType.name, string))
7161       {
7162          *offset += memberType.offset;
7163          return memberType;
7164       }
7165    }
7166    return null;
7167 }
7168
7169 public bool GetParseError() { return parseError; }
7170
7171 Expression ParseExpressionString(char * expression)
7172 {
7173    parseError = false;
7174
7175    fileInput = TempFile { };
7176    fileInput.Write(expression, 1, strlen(expression));
7177    fileInput.Seek(0, start);
7178
7179    echoOn = false;
7180    parsedExpression = null;
7181    resetScanner();
7182    expression_yyparse();
7183    delete fileInput;
7184
7185    return parsedExpression;
7186 }
7187
7188 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7189 {
7190    Identifier id = exp.identifier;
7191    Method method = null;
7192    Property prop = null;
7193    DataMember member = null;
7194    ClassProperty classProp = null;
7195
7196    if(_class && _class.type == enumClass)
7197    {
7198       NamedLink value = null;
7199       Class enumClass = eSystem_FindClass(privateModule, "enum");
7200       if(enumClass)
7201       {
7202          Class baseClass;
7203          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7204          {
7205             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7206             for(value = e.values.first; value; value = value.next)
7207             {
7208                if(!strcmp(value.name, id.string))
7209                   break;
7210             }
7211             if(value)
7212             {
7213                char constant[256];
7214
7215                FreeExpContents(exp);
7216
7217                exp.type = constantExp;
7218                exp.isConstant = true;
7219                if(!strcmp(baseClass.dataTypeString, "int"))
7220                   sprintf(constant, "%d",(int)value.data);
7221                else
7222                   sprintf(constant, "0x%X",(int)value.data);
7223                exp.constant = CopyString(constant);
7224                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7225                exp.expType = MkClassType(baseClass.fullName);
7226                break;
7227             }
7228          }
7229       }
7230       if(value)
7231          return true;
7232    }
7233    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7234    {
7235       ProcessMethodType(method);
7236       exp.expType = Type
7237       {
7238          refCount = 1;
7239          kind = methodType;
7240          method = method;
7241          // Crash here?
7242          // TOCHECK: Put it back to what it was...
7243          // methodClass = _class;
7244          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7245       };
7246       //id._class = null;
7247       return true;
7248    }
7249    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7250    {
7251       if(!prop.dataType)
7252          ProcessPropertyType(prop);
7253       exp.expType = prop.dataType;
7254       if(prop.dataType) prop.dataType.refCount++;
7255       return true;
7256    }
7257    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7258    {
7259       if(!member.dataType)
7260          member.dataType = ProcessTypeString(member.dataTypeString, false);
7261       exp.expType = member.dataType;
7262       if(member.dataType) member.dataType.refCount++;
7263       return true;
7264    }
7265    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7266    {
7267       if(!classProp.dataType)
7268          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7269
7270       if(classProp.constant)
7271       {
7272          FreeExpContents(exp);
7273
7274          exp.isConstant = true;
7275          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7276          {
7277             //char constant[256];
7278             exp.type = stringExp;
7279             exp.constant = QMkString((char *)classProp.Get(_class));
7280          }
7281          else
7282          {
7283             char constant[256];
7284             exp.type = constantExp;
7285             sprintf(constant, "%d", (int)classProp.Get(_class));
7286             exp.constant = CopyString(constant);
7287          }
7288       }
7289       else
7290       {
7291          // TO IMPLEMENT...
7292       }
7293
7294       exp.expType = classProp.dataType;
7295       if(classProp.dataType) classProp.dataType.refCount++;
7296       return true;
7297    }
7298    return false;
7299 }
7300
7301 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7302 {
7303    BinaryTree * tree = &nameSpace.functions;
7304    GlobalData data = (GlobalData)tree->FindString(name);
7305    NameSpace * child;
7306    if(!data)
7307    {
7308       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7309       {
7310          data = ScanGlobalData(child, name);
7311          if(data)
7312             break;
7313       }
7314    }
7315    return data;
7316 }
7317
7318 static GlobalData FindGlobalData(char * name)
7319 {
7320    int start = 0, c;
7321    NameSpace * nameSpace;
7322    nameSpace = globalData;
7323    for(c = 0; name[c]; c++)
7324    {
7325       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7326       {
7327          NameSpace * newSpace;
7328          char * spaceName = new char[c - start + 1];
7329          strncpy(spaceName, name + start, c - start);
7330          spaceName[c-start] = '\0';
7331          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7332          delete spaceName;
7333          if(!newSpace)
7334             return null;
7335          nameSpace = newSpace;
7336          if(name[c] == ':') c++;
7337          start = c+1;
7338       }
7339    }
7340    if(c - start)
7341    {
7342       return ScanGlobalData(nameSpace, name + start);
7343    }
7344    return null;
7345 }
7346
7347 static int definedExpStackPos;
7348 static void * definedExpStack[512];
7349
7350 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7351 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7352 {
7353    Expression prev = checkedExp.prev, next = checkedExp.next;
7354
7355    FreeExpContents(checkedExp);
7356    FreeType(checkedExp.expType);
7357    FreeType(checkedExp.destType);
7358
7359    *checkedExp = *newExp;
7360
7361    delete newExp;
7362
7363    checkedExp.prev = prev;
7364    checkedExp.next = next;
7365 }
7366
7367 void ApplyAnyObjectLogic(Expression e)
7368 {
7369    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7370 #ifdef _DEBUG
7371    char debugExpString[4096];
7372    debugExpString[0] = '\0';
7373    PrintExpression(e, debugExpString);
7374 #endif
7375
7376    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7377    {
7378       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7379       //ellipsisDestType = destType;
7380       if(e && e.expType)
7381       {
7382          Type type = e.expType;
7383          Class _class = null;
7384          //Type destType = e.destType;
7385
7386          if(type.kind == classType && type._class && type._class.registered)
7387          {
7388             _class = type._class.registered;
7389          }
7390          else if(type.kind == subClassType)
7391          {
7392             _class = FindClass("ecere::com::Class").registered;
7393          }
7394          else
7395          {
7396             char string[1024] = "";
7397             Symbol classSym;
7398
7399             PrintTypeNoConst(type, string, false, true);
7400             classSym = FindClass(string);
7401             if(classSym) _class = classSym.registered;
7402          }
7403
7404          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...
7405             (!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))) ||
7406             destType.byReference)))
7407          {
7408             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7409             {
7410                Expression checkedExp = e, newExp;
7411
7412                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7413                {
7414                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7415                   {
7416                      if(checkedExp.type == extensionCompoundExp)
7417                      {
7418                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7419                      }
7420                      else
7421                         checkedExp = checkedExp.list->last;
7422                   }
7423                   else if(checkedExp.type == castExp)
7424                      checkedExp = checkedExp.cast.exp;
7425                }
7426
7427                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7428                {
7429                   newExp = checkedExp.op.exp2;
7430                   checkedExp.op.exp2 = null;
7431                   FreeExpContents(checkedExp);
7432
7433                   if(e.expType && e.expType.passAsTemplate)
7434                   {
7435                      char size[100];
7436                      ComputeTypeSize(e.expType);
7437                      sprintf(size, "%d", e.expType.size);
7438                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7439                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7440                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7441                   }
7442
7443                   ReplaceExpContents(checkedExp, newExp);
7444                   e.byReference = true;
7445                }
7446                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7447                {
7448                   Expression checkedExp; //, newExp;
7449
7450                   {
7451                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7452                      bool hasAddress =
7453                         e.type == identifierExp ||
7454                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7455                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7456                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7457                         e.type == indexExp;
7458
7459                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7460                      {
7461                         Context context = PushContext();
7462                         Declarator decl;
7463                         OldList * specs = MkList();
7464                         char typeString[1024];
7465                         Expression newExp { };
7466
7467                         typeString[0] = '\0';
7468                         *newExp = *e;
7469
7470                         //if(e.destType) e.destType.refCount++;
7471                         // if(exp.expType) exp.expType.refCount++;
7472                         newExp.prev = null;
7473                         newExp.next = null;
7474                         newExp.expType = null;
7475
7476                         PrintTypeNoConst(e.expType, typeString, false, true);
7477                         decl = SpecDeclFromString(typeString, specs, null);
7478                         newExp.destType = ProcessType(specs, decl);
7479
7480                         curContext = context;
7481
7482                         // We need a current compound for this
7483                         if(curCompound)
7484                         {
7485                            char name[100];
7486                            OldList * stmts = MkList();
7487                            e.type = extensionCompoundExp;
7488                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7489                            if(!curCompound.compound.declarations)
7490                               curCompound.compound.declarations = MkList();
7491                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7492                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7493                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7494                            e.compound = MkCompoundStmt(null, stmts);
7495                         }
7496                         else
7497                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7498
7499                         /*
7500                         e.compound = MkCompoundStmt(
7501                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7502                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7503
7504                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7505                         */
7506
7507                         {
7508                            Type type = e.destType;
7509                            e.destType = { };
7510                            CopyTypeInto(e.destType, type);
7511                            e.destType.refCount = 1;
7512                            e.destType.classObjectType = none;
7513                            FreeType(type);
7514                         }
7515
7516                         e.compound.compound.context = context;
7517                         PopContext(context);
7518                         curContext = context.parent;
7519                      }
7520                   }
7521
7522                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7523                   checkedExp = e;
7524                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7525                   {
7526                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7527                      {
7528                         if(checkedExp.type == extensionCompoundExp)
7529                         {
7530                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7531                         }
7532                         else
7533                            checkedExp = checkedExp.list->last;
7534                      }
7535                      else if(checkedExp.type == castExp)
7536                         checkedExp = checkedExp.cast.exp;
7537                   }
7538                   {
7539                      Expression operand { };
7540                      operand = *checkedExp;
7541                      checkedExp.destType = null;
7542                      checkedExp.expType = null;
7543                      checkedExp.Clear();
7544                      checkedExp.type = opExp;
7545                      checkedExp.op.op = '&';
7546                      checkedExp.op.exp1 = null;
7547                      checkedExp.op.exp2 = operand;
7548
7549                      //newExp = MkExpOp(null, '&', checkedExp);
7550                   }
7551                   //ReplaceExpContents(checkedExp, newExp);
7552                }
7553             }
7554          }
7555       }
7556    }
7557    {
7558       // If expression type is a simple class, make it an address
7559       // FixReference(e, true);
7560    }
7561 //#if 0
7562    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7563       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7564          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7565    {
7566       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"))
7567       {
7568          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7569       }
7570       else
7571       {
7572          Expression thisExp { };
7573
7574          *thisExp = *e;
7575          thisExp.prev = null;
7576          thisExp.next = null;
7577          e.Clear();
7578
7579          e.type = bracketsExp;
7580          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7581          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7582             ((Expression)e.list->first).byReference = true;
7583
7584          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7585          {
7586             e.expType = thisExp.expType;
7587             e.expType.refCount++;
7588          }
7589          else*/
7590          {
7591             e.expType = { };
7592             CopyTypeInto(e.expType, thisExp.expType);
7593             e.expType.byReference = false;
7594             e.expType.refCount = 1;
7595
7596             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7597                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7598             {
7599                e.expType.classObjectType = none;
7600             }
7601          }
7602       }
7603    }
7604 // TOFIX: Try this for a nice IDE crash!
7605 //#endif
7606    // The other way around
7607    else
7608 //#endif
7609    if(destType && e.expType &&
7610          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7611          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7612          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7613    {
7614       if(destType.kind == ellipsisType)
7615       {
7616          Compiler_Error($"Unspecified type\n");
7617       }
7618       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7619       {
7620          bool byReference = e.expType.byReference;
7621          Expression thisExp { };
7622          Declarator decl;
7623          OldList * specs = MkList();
7624          char typeString[1024]; // Watch buffer overruns
7625          Type type;
7626          ClassObjectType backupClassObjectType;
7627          bool backupByReference;
7628
7629          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7630             type = e.expType;
7631          else
7632             type = destType;
7633
7634          backupClassObjectType = type.classObjectType;
7635          backupByReference = type.byReference;
7636
7637          type.classObjectType = none;
7638          type.byReference = false;
7639
7640          typeString[0] = '\0';
7641          PrintType(type, typeString, false, true);
7642          decl = SpecDeclFromString(typeString, specs, null);
7643
7644          type.classObjectType = backupClassObjectType;
7645          type.byReference = backupByReference;
7646
7647          *thisExp = *e;
7648          thisExp.prev = null;
7649          thisExp.next = null;
7650          e.Clear();
7651
7652          if( ( type.kind == classType && type._class && type._class.registered &&
7653                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7654                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7655              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7656              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7657          {
7658             e.type = opExp;
7659             e.op.op = '*';
7660             e.op.exp1 = null;
7661             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7662
7663             e.expType = { };
7664             CopyTypeInto(e.expType, type);
7665             e.expType.byReference = false;
7666             e.expType.refCount = 1;
7667          }
7668          else
7669          {
7670             e.type = castExp;
7671             e.cast.typeName = MkTypeName(specs, decl);
7672             e.cast.exp = thisExp;
7673             e.byReference = true;
7674             e.expType = type;
7675             type.refCount++;
7676          }
7677          e.destType = destType;
7678          destType.refCount++;
7679       }
7680    }
7681 }
7682
7683 void ApplyLocation(Expression exp, Location loc)
7684 {
7685    exp.loc = loc;
7686    switch(exp.type)
7687    {
7688       case opExp:
7689          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7690          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7691          break;
7692       case bracketsExp:
7693          if(exp.list)
7694          {
7695             Expression e;
7696             for(e = exp.list->first; e; e = e.next)
7697                ApplyLocation(e, loc);
7698          }
7699          break;
7700       case indexExp:
7701          if(exp.index.index)
7702          {
7703             Expression e;
7704             for(e = exp.index.index->first; e; e = e.next)
7705                ApplyLocation(e, loc);
7706          }
7707          if(exp.index.exp)
7708             ApplyLocation(exp.index.exp, loc);
7709          break;
7710       case callExp:
7711          if(exp.call.arguments)
7712          {
7713             Expression arg;
7714             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7715                ApplyLocation(arg, loc);
7716          }
7717          if(exp.call.exp)
7718             ApplyLocation(exp.call.exp, loc);
7719          break;
7720       case memberExp:
7721       case pointerExp:
7722          if(exp.member.exp)
7723             ApplyLocation(exp.member.exp, loc);
7724          break;
7725       case castExp:
7726          if(exp.cast.exp)
7727             ApplyLocation(exp.cast.exp, loc);
7728          break;
7729       case conditionExp:
7730          if(exp.cond.exp)
7731          {
7732             Expression e;
7733             for(e = exp.cond.exp->first; e; e = e.next)
7734                ApplyLocation(e, loc);
7735          }
7736          if(exp.cond.cond)
7737             ApplyLocation(exp.cond.cond, loc);
7738          if(exp.cond.elseExp)
7739             ApplyLocation(exp.cond.elseExp, loc);
7740          break;
7741       case vaArgExp:
7742          if(exp.vaArg.exp)
7743             ApplyLocation(exp.vaArg.exp, loc);
7744          break;
7745       default:
7746          break;
7747    }
7748 }
7749
7750 void ProcessExpressionType(Expression exp)
7751 {
7752    bool unresolved = false;
7753    Location oldyylloc = yylloc;
7754    bool notByReference = false;
7755 #ifdef _DEBUG
7756    char debugExpString[4096];
7757    debugExpString[0] = '\0';
7758    PrintExpression(exp, debugExpString);
7759 #endif
7760    if(!exp || exp.expType)
7761       return;
7762
7763    //eSystem_Logf("%s\n", expString);
7764
7765    // Testing this here
7766    yylloc = exp.loc;
7767    switch(exp.type)
7768    {
7769       case identifierExp:
7770       {
7771          Identifier id = exp.identifier;
7772          if(!id || !topContext) return;
7773
7774          // DOING THIS LATER NOW...
7775          if(id._class && id._class.name)
7776          {
7777             id.classSym = id._class.symbol; // FindClass(id._class.name);
7778             /* TODO: Name Space Fix ups
7779             if(!id.classSym)
7780                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7781             */
7782          }
7783
7784          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7785          {
7786             exp.expType = ProcessTypeString("Module", true);
7787             break;
7788          }
7789          else */if(strstr(id.string, "__ecereClass") == id.string)
7790          {
7791             exp.expType = ProcessTypeString("ecere::com::Class", true);
7792             break;
7793          }
7794          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7795          {
7796             // Added this here as well
7797             ReplaceClassMembers(exp, thisClass);
7798             if(exp.type != identifierExp)
7799             {
7800                ProcessExpressionType(exp);
7801                break;
7802             }
7803
7804             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7805                break;
7806          }
7807          else
7808          {
7809             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7810             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7811             if(!symbol/* && exp.destType*/)
7812             {
7813                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
7814                   break;
7815                else
7816                {
7817                   if(thisClass)
7818                   {
7819                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7820                      if(exp.type != identifierExp)
7821                      {
7822                         ProcessExpressionType(exp);
7823                         break;
7824                      }
7825                   }
7826                   // Static methods called from inside the _class
7827                   else if(currentClass && !id._class)
7828                   {
7829                      if(ResolveIdWithClass(exp, currentClass, true))
7830                         break;
7831                   }
7832                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7833                }
7834             }
7835
7836             // If we manage to resolve this symbol
7837             if(symbol)
7838             {
7839                Type type = symbol.type;
7840                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7841
7842                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7843                {
7844                   Context context = SetupTemplatesContext(_class);
7845                   type = ReplaceThisClassType(_class);
7846                   FinishTemplatesContext(context);
7847                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7848                }
7849
7850                FreeSpecifier(id._class);
7851                id._class = null;
7852                delete id.string;
7853                id.string = CopyString(symbol.string);
7854
7855                id.classSym = null;
7856                exp.expType = type;
7857                if(type)
7858                   type.refCount++;
7859
7860                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7861                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7862                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7863                   // Add missing cases here... enum Classes...
7864                   exp.isConstant = true;
7865
7866                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7867                if(symbol.isParam || !strcmp(id.string, "this"))
7868                {
7869                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7870                      exp.byReference = true;
7871
7872                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7873                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7874                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7875                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7876                   {
7877                      Identifier id = exp.identifier;
7878                      exp.type = bracketsExp;
7879                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7880                   }*/
7881                }
7882
7883                if(symbol.isIterator)
7884                {
7885                   if(symbol.isIterator == 3)
7886                   {
7887                      exp.type = bracketsExp;
7888                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7889                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7890                      exp.expType = null;
7891                      ProcessExpressionType(exp);
7892                   }
7893                   else if(symbol.isIterator != 4)
7894                   {
7895                      exp.type = memberExp;
7896                      exp.member.exp = MkExpIdentifier(exp.identifier);
7897                      exp.member.exp.expType = exp.expType;
7898                      /*if(symbol.isIterator == 6)
7899                         exp.member.member = MkIdentifier("key");
7900                      else*/
7901                         exp.member.member = MkIdentifier("data");
7902                      exp.expType = null;
7903                      ProcessExpressionType(exp);
7904                   }
7905                }
7906                break;
7907             }
7908             else
7909             {
7910                DefinedExpression definedExp = null;
7911                if(thisNameSpace && !(id._class && !id._class.name))
7912                {
7913                   char name[1024];
7914                   strcpy(name, thisNameSpace);
7915                   strcat(name, "::");
7916                   strcat(name, id.string);
7917                   definedExp = eSystem_FindDefine(privateModule, name);
7918                }
7919                if(!definedExp)
7920                   definedExp = eSystem_FindDefine(privateModule, id.string);
7921                if(definedExp)
7922                {
7923                   int c;
7924                   for(c = 0; c<definedExpStackPos; c++)
7925                      if(definedExpStack[c] == definedExp)
7926                         break;
7927                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7928                   {
7929                      Location backupYylloc = yylloc;
7930                      File backInput = fileInput;
7931                      definedExpStack[definedExpStackPos++] = definedExp;
7932
7933                      fileInput = TempFile { };
7934                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7935                      fileInput.Seek(0, start);
7936
7937                      echoOn = false;
7938                      parsedExpression = null;
7939                      resetScanner();
7940                      expression_yyparse();
7941                      delete fileInput;
7942                      if(backInput)
7943                         fileInput = backInput;
7944
7945                      yylloc = backupYylloc;
7946
7947                      if(parsedExpression)
7948                      {
7949                         FreeIdentifier(id);
7950                         exp.type = bracketsExp;
7951                         exp.list = MkListOne(parsedExpression);
7952                         ApplyLocation(parsedExpression, yylloc);
7953                         ProcessExpressionType(exp);
7954                         definedExpStackPos--;
7955                         return;
7956                      }
7957                      definedExpStackPos--;
7958                   }
7959                   else
7960                   {
7961                      if(inCompiler)
7962                      {
7963                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7964                      }
7965                   }
7966                }
7967                else
7968                {
7969                   GlobalData data = null;
7970                   if(thisNameSpace && !(id._class && !id._class.name))
7971                   {
7972                      char name[1024];
7973                      strcpy(name, thisNameSpace);
7974                      strcat(name, "::");
7975                      strcat(name, id.string);
7976                      data = FindGlobalData(name);
7977                   }
7978                   if(!data)
7979                      data = FindGlobalData(id.string);
7980                   if(data)
7981                   {
7982                      DeclareGlobalData(data);
7983                      exp.expType = data.dataType;
7984                      if(data.dataType) data.dataType.refCount++;
7985
7986                      delete id.string;
7987                      id.string = CopyString(data.fullName);
7988                      FreeSpecifier(id._class);
7989                      id._class = null;
7990
7991                      break;
7992                   }
7993                   else
7994                   {
7995                      GlobalFunction function = null;
7996                      if(thisNameSpace && !(id._class && !id._class.name))
7997                      {
7998                         char name[1024];
7999                         strcpy(name, thisNameSpace);
8000                         strcat(name, "::");
8001                         strcat(name, id.string);
8002                         function = eSystem_FindFunction(privateModule, name);
8003                      }
8004                      if(!function)
8005                         function = eSystem_FindFunction(privateModule, id.string);
8006                      if(function)
8007                      {
8008                         char name[1024];
8009                         delete id.string;
8010                         id.string = CopyString(function.name);
8011                         name[0] = 0;
8012
8013                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
8014                            strcpy(name, "__ecereFunction_");
8015                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
8016                         if(DeclareFunction(function, name))
8017                         {
8018                            delete id.string;
8019                            id.string = CopyString(name);
8020                         }
8021                         exp.expType = function.dataType;
8022                         if(function.dataType) function.dataType.refCount++;
8023
8024                         FreeSpecifier(id._class);
8025                         id._class = null;
8026
8027                         break;
8028                      }
8029                   }
8030                }
8031             }
8032          }
8033          unresolved = true;
8034          break;
8035       }
8036       case instanceExp:
8037       {
8038          // Class _class;
8039          // Symbol classSym;
8040
8041          if(!exp.instance._class)
8042          {
8043             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8044             {
8045                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8046             }
8047          }
8048
8049          //classSym = FindClass(exp.instance._class.fullName);
8050          //_class = classSym ? classSym.registered : null;
8051
8052          ProcessInstantiationType(exp.instance);
8053
8054          exp.isConstant = exp.instance.isConstant;
8055
8056          /*
8057          if(_class.type == unitClass && _class.base.type != systemClass)
8058          {
8059             {
8060                Type destType = exp.destType;
8061
8062                exp.destType = MkClassType(_class.base.fullName);
8063                exp.expType = MkClassType(_class.fullName);
8064                CheckExpressionType(exp, exp.destType, true);
8065
8066                exp.destType = destType;
8067             }
8068             exp.expType = MkClassType(_class.fullName);
8069          }
8070          else*/
8071          if(exp.instance._class)
8072          {
8073             exp.expType = MkClassType(exp.instance._class.name);
8074             /*if(exp.expType._class && exp.expType._class.registered &&
8075                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8076                exp.expType.byReference = true;*/
8077          }
8078          break;
8079       }
8080       case constantExp:
8081       {
8082          if(!exp.expType)
8083          {
8084             char * constant = exp.constant;
8085             Type type
8086             {
8087                refCount = 1;
8088                constant = true;
8089             };
8090             exp.expType = type;
8091
8092             if(constant[0] == '\'')
8093             {
8094                if((int)((byte *)constant)[1] > 127)
8095                {
8096                   int nb;
8097                   unichar ch = UTF8GetChar(constant + 1, &nb);
8098                   if(nb < 2) ch = constant[1];
8099                   delete constant;
8100                   exp.constant = PrintUInt(ch);
8101                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8102                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8103                   type._class = FindClass("unichar");
8104
8105                   type.isSigned = false;
8106                }
8107                else
8108                {
8109                   type.kind = charType;
8110                   type.isSigned = true;
8111                }
8112             }
8113             else
8114             {
8115                char * dot = strchr(constant, '.');
8116                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8117                char * exponent;
8118                if(isHex)
8119                {
8120                   exponent = strchr(constant, 'p');
8121                   if(!exponent) exponent = strchr(constant, 'P');
8122                }
8123                else
8124                {
8125                   exponent = strchr(constant, 'e');
8126                   if(!exponent) exponent = strchr(constant, 'E');
8127                }
8128
8129                if(dot || exponent)
8130                {
8131                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8132                      type.kind = floatType;
8133                   else
8134                      type.kind = doubleType;
8135                   type.isSigned = true;
8136                }
8137                else
8138                {
8139                   bool isSigned = constant[0] == '-';
8140                   char * endP = null;
8141                   int64 i64 = strtoll(constant, &endP, 0);
8142                   uint64 ui64 = strtoull(constant, &endP, 0);
8143                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
8144                   if(isSigned)
8145                   {
8146                      if(i64 < MININT)
8147                         is64Bit = true;
8148                   }
8149                   else
8150                   {
8151                      if(ui64 > MAXINT)
8152                      {
8153                         if(ui64 > MAXDWORD)
8154                         {
8155                            is64Bit = true;
8156                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8157                               isSigned = true;
8158                         }
8159                      }
8160                      else if(constant[0] != '0' || !constant[1])
8161                         isSigned = true;
8162                   }
8163                   type.kind = is64Bit ? int64Type : intType;
8164                   type.isSigned = isSigned;
8165                }
8166             }
8167             exp.isConstant = true;
8168             if(exp.destType && exp.destType.kind == doubleType)
8169                type.kind = doubleType;
8170             else if(exp.destType && exp.destType.kind == floatType)
8171                type.kind = floatType;
8172             else if(exp.destType && exp.destType.kind == int64Type)
8173                type.kind = int64Type;
8174          }
8175          break;
8176       }
8177       case stringExp:
8178       {
8179          exp.isConstant = true;      // Why wasn't this constant?
8180          exp.expType = Type
8181          {
8182             refCount = 1;
8183             kind = pointerType;
8184             type = Type
8185             {
8186                refCount = 1;
8187                kind = charType;
8188                constant = true;
8189                isSigned = true;
8190             }
8191          };
8192          break;
8193       }
8194       case newExp:
8195       case new0Exp:
8196          ProcessExpressionType(exp._new.size);
8197          exp.expType = Type
8198          {
8199             refCount = 1;
8200             kind = pointerType;
8201             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8202          };
8203          DeclareType(exp.expType.type, false, false);
8204          break;
8205       case renewExp:
8206       case renew0Exp:
8207          ProcessExpressionType(exp._renew.size);
8208          ProcessExpressionType(exp._renew.exp);
8209          exp.expType = Type
8210          {
8211             refCount = 1;
8212             kind = pointerType;
8213             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8214          };
8215          DeclareType(exp.expType.type, false, false);
8216          break;
8217       case opExp:
8218       {
8219          bool assign = false, boolResult = false, boolOps = false;
8220          Type type1 = null, type2 = null;
8221          bool useDestType = false, useSideType = false;
8222          Location oldyylloc = yylloc;
8223          bool useSideUnit = false;
8224          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8225
8226          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8227          Type dummy
8228          {
8229             count = 1;
8230             refCount = 1;
8231          };
8232
8233          switch(exp.op.op)
8234          {
8235             // Assignment Operators
8236             case '=':
8237             case MUL_ASSIGN:
8238             case DIV_ASSIGN:
8239             case MOD_ASSIGN:
8240             case ADD_ASSIGN:
8241             case SUB_ASSIGN:
8242             case LEFT_ASSIGN:
8243             case RIGHT_ASSIGN:
8244             case AND_ASSIGN:
8245             case XOR_ASSIGN:
8246             case OR_ASSIGN:
8247                assign = true;
8248                break;
8249             // boolean Operators
8250             case '!':
8251                // Expect boolean operators
8252                //boolOps = true;
8253                //boolResult = true;
8254                break;
8255             case AND_OP:
8256             case OR_OP:
8257                // Expect boolean operands
8258                boolOps = true;
8259                boolResult = true;
8260                break;
8261             // Comparisons
8262             case EQ_OP:
8263             case '<':
8264             case '>':
8265             case LE_OP:
8266             case GE_OP:
8267             case NE_OP:
8268                // Gives boolean result
8269                boolResult = true;
8270                useSideType = true;
8271                break;
8272             case '+':
8273             case '-':
8274                useSideUnit = true;
8275                useSideType = true;
8276                useDestType = true;
8277                break;
8278
8279             case LEFT_OP:
8280             case RIGHT_OP:
8281                useSideType = true;
8282                useDestType = true;
8283                break;
8284
8285             case '|':
8286             case '^':
8287                useSideType = true;
8288                useDestType = true;
8289                break;
8290
8291             case '/':
8292             case '%':
8293                useSideType = true;
8294                useDestType = true;
8295                break;
8296             case '&':
8297             case '*':
8298                if(exp.op.exp1)
8299                {
8300                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8301                   useSideType = true;
8302                   useDestType = true;
8303                }
8304                break;
8305
8306             /*// Implement speed etc.
8307             case '*':
8308             case '/':
8309                break;
8310             */
8311          }
8312          if(exp.op.op == '&')
8313          {
8314             // Added this here earlier for Iterator address as key
8315             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8316             {
8317                Identifier id = exp.op.exp2.identifier;
8318                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8319                if(symbol && symbol.isIterator == 2)
8320                {
8321                   exp.type = memberExp;
8322                   exp.member.exp = exp.op.exp2;
8323                   exp.member.member = MkIdentifier("key");
8324                   exp.expType = null;
8325                   exp.op.exp2.expType = symbol.type;
8326                   symbol.type.refCount++;
8327                   ProcessExpressionType(exp);
8328                   FreeType(dummy);
8329                   break;
8330                }
8331                // exp.op.exp2.usage.usageRef = true;
8332             }
8333          }
8334
8335          //dummy.kind = TypeDummy;
8336          if(exp.op.exp1)
8337          {
8338             // Added this check here to use the dest type only for units derived from the base unit
8339             // So that untyped units will use the side unit as opposed to the untyped destination unit
8340             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8341             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8342                useDestType = false;
8343
8344             if(destClass && useDestType &&
8345               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8346
8347               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8348             {
8349                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8350                exp.op.exp1.destType = exp.destType;
8351                exp.op.exp1.opDestType = true;
8352                if(exp.destType)
8353                   exp.destType.refCount++;
8354             }
8355             else if(!assign)
8356             {
8357                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8358                exp.op.exp1.destType = dummy;
8359                dummy.refCount++;
8360             }
8361
8362             // TESTING THIS HERE...
8363             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8364                ProcessExpressionType(exp.op.exp1);
8365             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8366
8367             exp.op.exp1.opDestType = false;
8368
8369             // Fix for unit and ++ / --
8370             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8371                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8372             {
8373                exp.op.exp2 = MkExpConstant("1");
8374                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8375                assign = true;
8376             }
8377
8378             if(exp.op.exp1.destType == dummy)
8379             {
8380                FreeType(dummy);
8381                exp.op.exp1.destType = null;
8382             }
8383             type1 = exp.op.exp1.expType;
8384          }
8385
8386          if(exp.op.exp2)
8387          {
8388             char expString[10240];
8389             expString[0] = '\0';
8390             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8391             {
8392                if(exp.op.exp1)
8393                {
8394                   exp.op.exp2.destType = exp.op.exp1.expType;
8395                   if(exp.op.exp1.expType)
8396                      exp.op.exp1.expType.refCount++;
8397                }
8398                else
8399                {
8400                   exp.op.exp2.destType = exp.destType;
8401                   if(!exp.op.exp1 || exp.op.op != '&')
8402                      exp.op.exp2.opDestType = true;
8403                   if(exp.destType)
8404                      exp.destType.refCount++;
8405                }
8406
8407                if(type1) type1.refCount++;
8408                exp.expType = type1;
8409             }
8410             else if(assign)
8411             {
8412                if(inCompiler)
8413                   PrintExpression(exp.op.exp2, expString);
8414
8415                if(type1 && type1.kind == pointerType)
8416                {
8417                   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 ||
8418                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8419                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8420                   else if(exp.op.op == '=')
8421                   {
8422                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8423                      exp.op.exp2.destType = type1;
8424                      if(type1)
8425                         type1.refCount++;
8426                   }
8427                }
8428                else
8429                {
8430                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8431                   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/* ||
8432                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8433                   else
8434                   {
8435                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8436                      exp.op.exp2.destType = type1;
8437                      if(type1)
8438                         type1.refCount++;
8439                   }
8440                }
8441                if(type1) type1.refCount++;
8442                exp.expType = type1;
8443             }
8444             else if(destClass &&
8445                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8446                   (destClass.type == enumClass && useDestType)))
8447             {
8448                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8449                exp.op.exp2.destType = exp.destType;
8450                if(exp.op.op != '&')
8451                   exp.op.exp2.opDestType = true;
8452                if(exp.destType)
8453                   exp.destType.refCount++;
8454             }
8455             else
8456             {
8457                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8458                exp.op.exp2.destType = dummy;
8459                dummy.refCount++;
8460             }
8461
8462             // TESTING THIS HERE... (DANGEROUS)
8463             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8464                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8465             {
8466                FreeType(exp.op.exp2.destType);
8467                exp.op.exp2.destType = type1;
8468                type1.refCount++;
8469             }
8470             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8471             // Cannot lose the cast on a sizeof
8472             if(exp.op.op == SIZEOF)
8473             {
8474                Expression e = exp.op.exp2;
8475                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8476                {
8477                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8478                   {
8479                      if(e.type == extensionCompoundExp)
8480                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8481                      else
8482                         e = e.list->last;
8483                   }
8484                }
8485                if(e.type == castExp && e.cast.exp)
8486                   e.cast.exp.needCast = true;
8487             }
8488             ProcessExpressionType(exp.op.exp2);
8489             exp.op.exp2.opDestType = false;
8490             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8491
8492             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8493             {
8494                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)
8495                {
8496                   if(exp.op.op != '=' && type1.type.kind == voidType)
8497                      Compiler_Error($"void *: unknown size\n");
8498                }
8499                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||
8500                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8501                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8502                               exp.op.exp2.expType._class.registered.type == structClass ||
8503                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8504                {
8505                   if(exp.op.op == ADD_ASSIGN)
8506                      Compiler_Error($"cannot add two pointers\n");
8507                }
8508                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8509                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8510                {
8511                   if(exp.op.op == ADD_ASSIGN)
8512                      Compiler_Error($"cannot add two pointers\n");
8513                }
8514                else if(inCompiler)
8515                {
8516                   char type1String[1024];
8517                   char type2String[1024];
8518                   type1String[0] = '\0';
8519                   type2String[0] = '\0';
8520
8521                   PrintType(exp.op.exp2.expType, type1String, false, true);
8522                   PrintType(type1, type2String, false, true);
8523                   ChangeCh(expString, '\n', ' ');
8524                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8525                }
8526             }
8527
8528             if(exp.op.exp2.destType == dummy)
8529             {
8530                FreeType(dummy);
8531                exp.op.exp2.destType = null;
8532             }
8533
8534             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8535             {
8536                type2 = { };
8537                type2.refCount = 1;
8538                CopyTypeInto(type2, exp.op.exp2.expType);
8539                type2.isSigned = true;
8540             }
8541             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8542             {
8543                type2 = { kind = intType };
8544                type2.refCount = 1;
8545                type2.isSigned = true;
8546             }
8547             else
8548             {
8549                type2 = exp.op.exp2.expType;
8550                if(type2) type2.refCount++;
8551             }
8552          }
8553
8554          dummy.kind = voidType;
8555
8556          if(exp.op.op == SIZEOF)
8557          {
8558             exp.expType = Type
8559             {
8560                refCount = 1;
8561                kind = intSizeType;
8562             };
8563             exp.isConstant = true;
8564          }
8565          // Get type of dereferenced pointer
8566          else if(exp.op.op == '*' && !exp.op.exp1)
8567          {
8568             exp.expType = Dereference(type2);
8569             if(type2 && type2.kind == classType)
8570                notByReference = true;
8571          }
8572          else if(exp.op.op == '&' && !exp.op.exp1)
8573             exp.expType = Reference(type2);
8574          else if(!assign)
8575          {
8576             if(boolOps)
8577             {
8578                if(exp.op.exp1)
8579                {
8580                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8581                   exp.op.exp1.destType = MkClassType("bool");
8582                   exp.op.exp1.destType.truth = true;
8583                   if(!exp.op.exp1.expType)
8584                      ProcessExpressionType(exp.op.exp1);
8585                   else
8586                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8587                   FreeType(exp.op.exp1.expType);
8588                   exp.op.exp1.expType = MkClassType("bool");
8589                   exp.op.exp1.expType.truth = true;
8590                }
8591                if(exp.op.exp2)
8592                {
8593                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8594                   exp.op.exp2.destType = MkClassType("bool");
8595                   exp.op.exp2.destType.truth = true;
8596                   if(!exp.op.exp2.expType)
8597                      ProcessExpressionType(exp.op.exp2);
8598                   else
8599                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8600                   FreeType(exp.op.exp2.expType);
8601                   exp.op.exp2.expType = MkClassType("bool");
8602                   exp.op.exp2.expType.truth = true;
8603                }
8604             }
8605             else if(exp.op.exp1 && exp.op.exp2 &&
8606                ((useSideType /*&&
8607                      (useSideUnit ||
8608                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8609                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8610                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8611                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8612             {
8613                if(type1 && type2 &&
8614                   // If either both are class or both are not class
8615                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8616                {
8617                   // Added this check for enum subtraction to result in an int type:
8618                   if(exp.op.op == '-' &&
8619                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8620                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8621                   {
8622                      Type intType;
8623                      if(!type1._class.registered.dataType)
8624                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8625                      if(!type2._class.registered.dataType)
8626                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8627
8628                      intType = ProcessTypeString(
8629                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8630
8631                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8632                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8633                      exp.op.exp1.destType = intType;
8634                      exp.op.exp2.destType = intType;
8635                      intType.refCount++;
8636                   }
8637                   else
8638                   {
8639                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8640                      exp.op.exp2.destType = type1;
8641                      type1.refCount++;
8642                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8643                      exp.op.exp1.destType = type2;
8644                      type2.refCount++;
8645                   }
8646
8647                   // Warning here for adding Radians + Degrees with no destination type
8648                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8649                      type1._class.registered && type1._class.registered.type == unitClass &&
8650                      type2._class.registered && type2._class.registered.type == unitClass &&
8651                      type1._class.registered != type2._class.registered)
8652                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8653                         type1._class.string, type2._class.string, type1._class.string);
8654
8655                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8656                   {
8657                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8658                      if(argExp)
8659                      {
8660                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8661
8662                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8663                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8664                            exp.op.exp1)));
8665
8666                         ProcessExpressionType(exp.op.exp1);
8667
8668                         if(type2.kind != pointerType)
8669                         {
8670                            ProcessExpressionType(classExp);
8671
8672                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8673
8674                            if(!exp.op.exp2.expType)
8675                            {
8676                               if(type2)
8677                                  FreeType(type2);
8678                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8679                               type2.refCount++;
8680                            }
8681
8682                            ProcessExpressionType(exp.op.exp2);
8683                         }
8684                      }
8685                   }
8686
8687                   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)))
8688                   {
8689                      if(type1.kind != classType && type1.type.kind == voidType)
8690                         Compiler_Error($"void *: unknown size\n");
8691                      exp.expType = type1;
8692                      if(type1) type1.refCount++;
8693                   }
8694                   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)))
8695                   {
8696                      if(type2.kind != classType && type2.type.kind == voidType)
8697                         Compiler_Error($"void *: unknown size\n");
8698                      exp.expType = type2;
8699                      if(type2) type2.refCount++;
8700                   }
8701                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8702                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8703                   {
8704                      Compiler_Warning($"different levels of indirection\n");
8705                   }
8706                   else
8707                   {
8708                      bool success = false;
8709                      if(type1.kind == pointerType && type2.kind == pointerType)
8710                      {
8711                         if(exp.op.op == '+')
8712                            Compiler_Error($"cannot add two pointers\n");
8713                         else if(exp.op.op == '-')
8714                         {
8715                            // Pointer Subtraction gives integer
8716                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8717                            {
8718                               exp.expType = Type
8719                               {
8720                                  kind = intType;
8721                                  refCount = 1;
8722                               };
8723                               success = true;
8724
8725                               if(type1.type.kind == templateType)
8726                               {
8727                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8728                                  if(argExp)
8729                                  {
8730                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8731
8732                                     ProcessExpressionType(classExp);
8733
8734                                     exp.type = bracketsExp;
8735                                     exp.list = MkListOne(MkExpOp(
8736                                        MkExpBrackets(MkListOne(MkExpOp(
8737                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8738                                              , exp.op.op,
8739                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8740                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8741
8742                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8743                                     FreeType(dummy);
8744                                     return;
8745                                  }
8746                               }
8747                            }
8748                         }
8749                      }
8750
8751                      if(!success && exp.op.exp1.type == constantExp)
8752                      {
8753                         // If first expression is constant, try to match that first
8754                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8755                         {
8756                            if(exp.expType) FreeType(exp.expType);
8757                            exp.expType = exp.op.exp1.destType;
8758                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8759                            success = true;
8760                         }
8761                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8762                         {
8763                            if(exp.expType) FreeType(exp.expType);
8764                            exp.expType = exp.op.exp2.destType;
8765                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8766                            success = true;
8767                         }
8768                      }
8769                      else if(!success)
8770                      {
8771                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8772                         {
8773                            if(exp.expType) FreeType(exp.expType);
8774                            exp.expType = exp.op.exp2.destType;
8775                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8776                            success = true;
8777                         }
8778                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8779                         {
8780                            if(exp.expType) FreeType(exp.expType);
8781                            exp.expType = exp.op.exp1.destType;
8782                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8783                            success = true;
8784                         }
8785                      }
8786                      if(!success)
8787                      {
8788                         char expString1[10240];
8789                         char expString2[10240];
8790                         char type1[1024];
8791                         char type2[1024];
8792                         expString1[0] = '\0';
8793                         expString2[0] = '\0';
8794                         type1[0] = '\0';
8795                         type2[0] = '\0';
8796                         if(inCompiler)
8797                         {
8798                            PrintExpression(exp.op.exp1, expString1);
8799                            ChangeCh(expString1, '\n', ' ');
8800                            PrintExpression(exp.op.exp2, expString2);
8801                            ChangeCh(expString2, '\n', ' ');
8802                            PrintType(exp.op.exp1.expType, type1, false, true);
8803                            PrintType(exp.op.exp2.expType, type2, false, true);
8804                         }
8805
8806                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8807                      }
8808                   }
8809                }
8810                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8811                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8812                {
8813                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8814                   // Convert e.g. / 4 into / 4.0
8815                   exp.op.exp1.destType = type2._class.registered.dataType;
8816                   if(type2._class.registered.dataType)
8817                      type2._class.registered.dataType.refCount++;
8818                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8819                   exp.expType = type2;
8820                   if(type2) type2.refCount++;
8821                }
8822                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8823                {
8824                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8825                   // Convert e.g. / 4 into / 4.0
8826                   exp.op.exp2.destType = type1._class.registered.dataType;
8827                   if(type1._class.registered.dataType)
8828                      type1._class.registered.dataType.refCount++;
8829                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8830                   exp.expType = type1;
8831                   if(type1) type1.refCount++;
8832                }
8833                else if(type1)
8834                {
8835                   bool valid = false;
8836
8837                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8838                   {
8839                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8840
8841                      if(!type1._class.registered.dataType)
8842                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8843                      exp.op.exp2.destType = type1._class.registered.dataType;
8844                      exp.op.exp2.destType.refCount++;
8845
8846                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8847                      if(type2)
8848                         FreeType(type2);
8849                      type2 = exp.op.exp2.destType;
8850                      if(type2) type2.refCount++;
8851
8852                      exp.expType = type2;
8853                      type2.refCount++;
8854                   }
8855
8856                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8857                   {
8858                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8859
8860                      if(!type2._class.registered.dataType)
8861                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8862                      exp.op.exp1.destType = type2._class.registered.dataType;
8863                      exp.op.exp1.destType.refCount++;
8864
8865                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8866                      type1 = exp.op.exp1.destType;
8867                      exp.expType = type1;
8868                      type1.refCount++;
8869                   }
8870
8871                   // TESTING THIS NEW CODE
8872                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8873                   {
8874                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8875                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8876                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8877                      {
8878                         // Convert the enum to an int instead for these operators
8879                         if(op1IsEnum && exp.op.exp2.expType)
8880                         {
8881                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8882                            {
8883                               if(exp.expType) FreeType(exp.expType);
8884                               exp.expType = exp.op.exp2.expType;
8885                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8886                               valid = true;
8887                            }
8888                         }
8889                         else if(op2IsEnum && exp.op.exp1.expType)
8890                         {
8891                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8892                            {
8893                               if(exp.expType) FreeType(exp.expType);
8894                               exp.expType = exp.op.exp1.expType;
8895                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8896                               valid = true;
8897                            }
8898                         }
8899                      }
8900                      else
8901                      {
8902                         if(op1IsEnum && exp.op.exp2.expType)
8903                         {
8904                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8905                            {
8906                               if(exp.expType) FreeType(exp.expType);
8907                               exp.expType = exp.op.exp1.expType;
8908                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8909                               valid = true;
8910                            }
8911                         }
8912                         else if(op2IsEnum && exp.op.exp1.expType)
8913                         {
8914                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8915                            {
8916                               if(exp.expType) FreeType(exp.expType);
8917                               exp.expType = exp.op.exp2.expType;
8918                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8919                               valid = true;
8920                            }
8921                         }
8922                      }
8923                   }
8924
8925                   if(!valid)
8926                   {
8927                      // 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
8928                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
8929                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
8930                      {
8931                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8932                         exp.op.exp1.destType = type2;
8933                         type2.refCount++;
8934
8935                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8936                         {
8937                            if(exp.expType) FreeType(exp.expType);
8938                            exp.expType = exp.op.exp1.destType;
8939                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8940                         }
8941                      }
8942                      else
8943                      {
8944                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8945                         exp.op.exp2.destType = type1;
8946                         type1.refCount++;
8947
8948                      /*
8949                      // Maybe this was meant to be an enum...
8950                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8951                      {
8952                         Type oldType = exp.op.exp2.expType;
8953                         exp.op.exp2.expType = null;
8954                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8955                            FreeType(oldType);
8956                         else
8957                            exp.op.exp2.expType = oldType;
8958                      }
8959                      */
8960
8961                      /*
8962                      // TESTING THIS HERE... LATEST ADDITION
8963                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8964                      {
8965                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8966                         exp.op.exp2.destType = type2._class.registered.dataType;
8967                         if(type2._class.registered.dataType)
8968                            type2._class.registered.dataType.refCount++;
8969                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8970
8971                         //exp.expType = type2._class.registered.dataType; //type2;
8972                         //if(type2) type2.refCount++;
8973                      }
8974
8975                      // TESTING THIS HERE... LATEST ADDITION
8976                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8977                      {
8978                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8979                         exp.op.exp1.destType = type1._class.registered.dataType;
8980                         if(type1._class.registered.dataType)
8981                            type1._class.registered.dataType.refCount++;
8982                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8983                         exp.expType = type1._class.registered.dataType; //type1;
8984                         if(type1) type1.refCount++;
8985                      }
8986                      */
8987
8988                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8989                         {
8990                            if(exp.expType) FreeType(exp.expType);
8991                            exp.expType = exp.op.exp2.destType;
8992                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8993                         }
8994                         else if(type1 && type2)
8995                         {
8996                            char expString1[10240];
8997                            char expString2[10240];
8998                            char type1String[1024];
8999                            char type2String[1024];
9000                            expString1[0] = '\0';
9001                            expString2[0] = '\0';
9002                            type1String[0] = '\0';
9003                            type2String[0] = '\0';
9004                            if(inCompiler)
9005                            {
9006                               PrintExpression(exp.op.exp1, expString1);
9007                               ChangeCh(expString1, '\n', ' ');
9008                               PrintExpression(exp.op.exp2, expString2);
9009                               ChangeCh(expString2, '\n', ' ');
9010                               PrintType(exp.op.exp1.expType, type1String, false, true);
9011                               PrintType(exp.op.exp2.expType, type2String, false, true);
9012                            }
9013
9014                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
9015
9016                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9017                            {
9018                               exp.expType = exp.op.exp1.expType;
9019                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9020                            }
9021                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9022                            {
9023                               exp.expType = exp.op.exp2.expType;
9024                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9025                            }
9026                         }
9027                      }
9028                   }
9029                }
9030                else if(type2)
9031                {
9032                   // Maybe this was meant to be an enum...
9033                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9034                   {
9035                      Type oldType = exp.op.exp1.expType;
9036                      exp.op.exp1.expType = null;
9037                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9038                         FreeType(oldType);
9039                      else
9040                         exp.op.exp1.expType = oldType;
9041                   }
9042
9043                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9044                   exp.op.exp1.destType = type2;
9045                   type2.refCount++;
9046                   /*
9047                   // TESTING THIS HERE... LATEST ADDITION
9048                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9049                   {
9050                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9051                      exp.op.exp1.destType = type1._class.registered.dataType;
9052                      if(type1._class.registered.dataType)
9053                         type1._class.registered.dataType.refCount++;
9054                   }
9055
9056                   // TESTING THIS HERE... LATEST ADDITION
9057                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9058                   {
9059                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9060                      exp.op.exp2.destType = type2._class.registered.dataType;
9061                      if(type2._class.registered.dataType)
9062                         type2._class.registered.dataType.refCount++;
9063                   }
9064                   */
9065
9066                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9067                   {
9068                      if(exp.expType) FreeType(exp.expType);
9069                      exp.expType = exp.op.exp1.destType;
9070                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9071                   }
9072                }
9073             }
9074             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9075             {
9076                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9077                {
9078                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9079                   // Convert e.g. / 4 into / 4.0
9080                   exp.op.exp1.destType = type2._class.registered.dataType;
9081                   if(type2._class.registered.dataType)
9082                      type2._class.registered.dataType.refCount++;
9083                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9084                }
9085                if(exp.op.op == '!')
9086                {
9087                   exp.expType = MkClassType("bool");
9088                   exp.expType.truth = true;
9089                }
9090                else
9091                {
9092                   exp.expType = type2;
9093                   if(type2) type2.refCount++;
9094                }
9095             }
9096             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9097             {
9098                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9099                {
9100                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9101                   // Convert e.g. / 4 into / 4.0
9102                   exp.op.exp2.destType = type1._class.registered.dataType;
9103                   if(type1._class.registered.dataType)
9104                      type1._class.registered.dataType.refCount++;
9105                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9106                }
9107                exp.expType = type1;
9108                if(type1) type1.refCount++;
9109             }
9110          }
9111
9112          yylloc = exp.loc;
9113          if(exp.op.exp1 && !exp.op.exp1.expType)
9114          {
9115             char expString[10000];
9116             expString[0] = '\0';
9117             if(inCompiler)
9118             {
9119                PrintExpression(exp.op.exp1, expString);
9120                ChangeCh(expString, '\n', ' ');
9121             }
9122             if(expString[0])
9123                Compiler_Error($"couldn't determine type of %s\n", expString);
9124          }
9125          if(exp.op.exp2 && !exp.op.exp2.expType)
9126          {
9127             char expString[10240];
9128             expString[0] = '\0';
9129             if(inCompiler)
9130             {
9131                PrintExpression(exp.op.exp2, expString);
9132                ChangeCh(expString, '\n', ' ');
9133             }
9134             if(expString[0])
9135                Compiler_Error($"couldn't determine type of %s\n", expString);
9136          }
9137
9138          if(boolResult)
9139          {
9140             FreeType(exp.expType);
9141             exp.expType = MkClassType("bool");
9142             exp.expType.truth = true;
9143          }
9144
9145          if(exp.op.op != SIZEOF)
9146             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9147                (!exp.op.exp2 || exp.op.exp2.isConstant);
9148
9149          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9150          {
9151             DeclareType(exp.op.exp2.expType, false, false);
9152          }
9153
9154          if(exp.op.op == DELETE && exp.op.exp2 && exp.op.exp2.expType && exp.op.exp2.expType.specConst)
9155             Compiler_Warning($"deleting const qualified object\n");
9156
9157          yylloc = oldyylloc;
9158
9159          FreeType(dummy);
9160          if(type2)
9161             FreeType(type2);
9162          break;
9163       }
9164       case bracketsExp:
9165       case extensionExpressionExp:
9166       {
9167          Expression e;
9168          exp.isConstant = true;
9169          for(e = exp.list->first; e; e = e.next)
9170          {
9171             bool inced = false;
9172             if(!e.next)
9173             {
9174                FreeType(e.destType);
9175                e.opDestType = exp.opDestType;
9176                e.destType = exp.destType;
9177                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
9178             }
9179             ProcessExpressionType(e);
9180             if(inced)
9181                exp.destType.count--;
9182             if(!exp.expType && !e.next)
9183             {
9184                exp.expType = e.expType;
9185                if(e.expType) e.expType.refCount++;
9186             }
9187             if(!e.isConstant)
9188                exp.isConstant = false;
9189          }
9190
9191          // In case a cast became a member...
9192          e = exp.list->first;
9193          if(!e.next && e.type == memberExp)
9194          {
9195             // Preserve prev, next
9196             Expression next = exp.next, prev = exp.prev;
9197
9198
9199             FreeType(exp.expType);
9200             FreeType(exp.destType);
9201             delete exp.list;
9202
9203             *exp = *e;
9204
9205             exp.prev = prev;
9206             exp.next = next;
9207
9208             delete e;
9209
9210             ProcessExpressionType(exp);
9211          }
9212          break;
9213       }
9214       case indexExp:
9215       {
9216          Expression e;
9217          exp.isConstant = true;
9218
9219          ProcessExpressionType(exp.index.exp);
9220          if(!exp.index.exp.isConstant)
9221             exp.isConstant = false;
9222
9223          if(exp.index.exp.expType)
9224          {
9225             Type source = exp.index.exp.expType;
9226             if(source.kind == classType && source._class && source._class.registered)
9227             {
9228                Class _class = source._class.registered;
9229                Class c = _class.templateClass ? _class.templateClass : _class;
9230                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9231                {
9232                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9233
9234                   if(exp.index.index && exp.index.index->last)
9235                   {
9236                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9237
9238                      if(type.kind == classType) type.constant = true;
9239                      else if(type.kind == pointerType)
9240                      {
9241                         Type t = type;
9242                         while(t.kind == pointerType) t = t.type;
9243                         t.constant = true;
9244                      }
9245
9246                      ((Expression)exp.index.index->last).destType = type;
9247                   }
9248                }
9249             }
9250          }
9251
9252          for(e = exp.index.index->first; e; e = e.next)
9253          {
9254             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9255             {
9256                if(e.destType) FreeType(e.destType);
9257                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9258             }
9259             ProcessExpressionType(e);
9260             if(!e.next)
9261             {
9262                // Check if this type is int
9263             }
9264             if(!e.isConstant)
9265                exp.isConstant = false;
9266          }
9267
9268          if(!exp.expType)
9269             exp.expType = Dereference(exp.index.exp.expType);
9270          if(exp.expType)
9271             DeclareType(exp.expType, false, false);
9272          break;
9273       }
9274       case callExp:
9275       {
9276          Expression e;
9277          Type functionType;
9278          Type methodType = null;
9279          char name[1024];
9280          name[0] = '\0';
9281
9282          if(inCompiler)
9283          {
9284             PrintExpression(exp.call.exp,  name);
9285             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9286             {
9287                //exp.call.exp.expType = null;
9288                PrintExpression(exp.call.exp,  name);
9289             }
9290          }
9291          if(exp.call.exp.type == identifierExp)
9292          {
9293             Expression idExp = exp.call.exp;
9294             Identifier id = idExp.identifier;
9295             if(!strcmp(id.string, "__builtin_frame_address"))
9296             {
9297                exp.expType = ProcessTypeString("void *", true);
9298                if(exp.call.arguments && exp.call.arguments->first)
9299                   ProcessExpressionType(exp.call.arguments->first);
9300                break;
9301             }
9302             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9303             {
9304                exp.expType = ProcessTypeString("int", true);
9305                if(exp.call.arguments && exp.call.arguments->first)
9306                   ProcessExpressionType(exp.call.arguments->first);
9307                break;
9308             }
9309             else if(!strcmp(id.string, "Max") ||
9310                !strcmp(id.string, "Min") ||
9311                !strcmp(id.string, "Sgn") ||
9312                !strcmp(id.string, "Abs"))
9313             {
9314                Expression a = null;
9315                Expression b = null;
9316                Expression tempExp1 = null, tempExp2 = null;
9317                if((!strcmp(id.string, "Max") ||
9318                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9319                {
9320                   a = exp.call.arguments->first;
9321                   b = exp.call.arguments->last;
9322                   tempExp1 = a;
9323                   tempExp2 = b;
9324                }
9325                else if(exp.call.arguments->count == 1)
9326                {
9327                   a = exp.call.arguments->first;
9328                   tempExp1 = a;
9329                }
9330
9331                if(a)
9332                {
9333                   exp.call.arguments->Clear();
9334                   idExp.identifier = null;
9335
9336                   FreeExpContents(exp);
9337
9338                   ProcessExpressionType(a);
9339                   if(b)
9340                      ProcessExpressionType(b);
9341
9342                   exp.type = bracketsExp;
9343                   exp.list = MkList();
9344
9345                   if(a.expType && (!b || b.expType))
9346                   {
9347                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9348                      {
9349                         // Use the simpleStruct name/ids for now...
9350                         if(inCompiler)
9351                         {
9352                            OldList * specs = MkList();
9353                            OldList * decls = MkList();
9354                            Declaration decl;
9355                            char temp1[1024], temp2[1024];
9356
9357                            GetTypeSpecs(a.expType, specs);
9358
9359                            if(a && !a.isConstant && a.type != identifierExp)
9360                            {
9361                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9362                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9363                               tempExp1 = QMkExpId(temp1);
9364                               tempExp1.expType = a.expType;
9365                               if(a.expType)
9366                                  a.expType.refCount++;
9367                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9368                            }
9369                            if(b && !b.isConstant && b.type != identifierExp)
9370                            {
9371                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9372                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9373                               tempExp2 = QMkExpId(temp2);
9374                               tempExp2.expType = b.expType;
9375                               if(b.expType)
9376                                  b.expType.refCount++;
9377                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9378                            }
9379
9380                            decl = MkDeclaration(specs, decls);
9381                            if(!curCompound.compound.declarations)
9382                               curCompound.compound.declarations = MkList();
9383                            curCompound.compound.declarations->Insert(null, decl);
9384                         }
9385                      }
9386                   }
9387
9388                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9389                   {
9390                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9391                      ListAdd(exp.list,
9392                         MkExpCondition(MkExpBrackets(MkListOne(
9393                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9394                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9395                      exp.expType = a.expType;
9396                      if(a.expType)
9397                         a.expType.refCount++;
9398                   }
9399                   else if(!strcmp(id.string, "Abs"))
9400                   {
9401                      ListAdd(exp.list,
9402                         MkExpCondition(MkExpBrackets(MkListOne(
9403                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9404                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9405                      exp.expType = a.expType;
9406                      if(a.expType)
9407                         a.expType.refCount++;
9408                   }
9409                   else if(!strcmp(id.string, "Sgn"))
9410                   {
9411                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9412                      ListAdd(exp.list,
9413                         MkExpCondition(MkExpBrackets(MkListOne(
9414                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9415                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9416                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9417                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9418                      exp.expType = ProcessTypeString("int", false);
9419                   }
9420
9421                   FreeExpression(tempExp1);
9422                   if(tempExp2) FreeExpression(tempExp2);
9423
9424                   FreeIdentifier(id);
9425                   break;
9426                }
9427             }
9428          }
9429
9430          {
9431             Type dummy
9432             {
9433                count = 1;
9434                refCount = 1;
9435             };
9436             if(!exp.call.exp.destType)
9437             {
9438                exp.call.exp.destType = dummy;
9439                dummy.refCount++;
9440             }
9441             ProcessExpressionType(exp.call.exp);
9442             if(exp.call.exp.destType == dummy)
9443             {
9444                FreeType(dummy);
9445                exp.call.exp.destType = null;
9446             }
9447             FreeType(dummy);
9448          }
9449
9450          // Check argument types against parameter types
9451          functionType = exp.call.exp.expType;
9452
9453          if(functionType && functionType.kind == TypeKind::methodType)
9454          {
9455             methodType = functionType;
9456             functionType = methodType.method.dataType;
9457
9458             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9459             // TOCHECK: Instead of doing this here could this be done per param?
9460             if(exp.call.exp.expType.usedClass)
9461             {
9462                char typeString[1024];
9463                typeString[0] = '\0';
9464                {
9465                   Symbol back = functionType.thisClass;
9466                   // Do not output class specifier here (thisclass was added to this)
9467                   functionType.thisClass = null;
9468                   PrintType(functionType, typeString, true, true);
9469                   functionType.thisClass = back;
9470                }
9471                if(strstr(typeString, "thisclass"))
9472                {
9473                   OldList * specs = MkList();
9474                   Declarator decl;
9475                   {
9476                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9477
9478                      decl = SpecDeclFromString(typeString, specs, null);
9479
9480                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9481                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9482                         exp.call.exp.expType.usedClass))
9483                         thisClassParams = false;
9484
9485                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9486                      {
9487                         Class backupThisClass = thisClass;
9488                         thisClass = exp.call.exp.expType.usedClass;
9489                         ProcessDeclarator(decl);
9490                         thisClass = backupThisClass;
9491                      }
9492
9493                      thisClassParams = true;
9494
9495                      functionType = ProcessType(specs, decl);
9496                      functionType.refCount = 0;
9497                      FinishTemplatesContext(context);
9498
9499                      // Mark parameters that were 'thisclass'
9500                      /*{
9501                         Type p, op;
9502                         for(p = functionType.params.first, op = methodType.method.dataType.params.first; p && op; p = p.next, op = op.next)
9503                            p.wasThisClass = op.kind == thisClassType;
9504                      }*/
9505                   }
9506
9507                   FreeList(specs, FreeSpecifier);
9508                   FreeDeclarator(decl);
9509                 }
9510             }
9511          }
9512          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9513          {
9514             Type type = functionType.type;
9515             if(!functionType.refCount)
9516             {
9517                functionType.type = null;
9518                FreeType(functionType);
9519             }
9520             //methodType = functionType;
9521             functionType = type;
9522          }
9523          if(functionType && functionType.kind != TypeKind::functionType)
9524          {
9525             Compiler_Error($"called object %s is not a function\n", name);
9526          }
9527          else if(functionType)
9528          {
9529             bool emptyParams = false, noParams = false;
9530             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9531             Type type = functionType.params.first;
9532             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9533             int extra = 0;
9534             Location oldyylloc = yylloc;
9535
9536             if(!type) emptyParams = true;
9537
9538             // WORKING ON THIS:
9539             if(functionType.extraParam && e && functionType.thisClass)
9540             {
9541                e.destType = MkClassType(functionType.thisClass.string);
9542                e = e.next;
9543             }
9544
9545             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9546             // Fixed #141 by adding '&& !functionType.extraParam'
9547             if(!functionType.staticMethod && !functionType.extraParam)
9548             {
9549                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9550                   memberExp.member.exp.expType._class)
9551                {
9552                   type = MkClassType(memberExp.member.exp.expType._class.string);
9553                   if(e)
9554                   {
9555                      e.destType = type;
9556                      e = e.next;
9557                      type = functionType.params.first;
9558                   }
9559                   else
9560                      type.refCount = 0;
9561                }
9562                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9563                {
9564                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9565                   type.byReference = functionType.byReference;
9566                   type.typedByReference = functionType.typedByReference;
9567                   if(e)
9568                   {
9569                      // Allow manually passing a class for typed object
9570                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9571                         e = e.next;
9572                      e.destType = type;
9573                      e = e.next;
9574                      type = functionType.params.first;
9575                   }
9576                   else
9577                      type.refCount = 0;
9578                   //extra = 1;
9579                }
9580             }
9581
9582             if(type && type.kind == voidType)
9583             {
9584                noParams = true;
9585                if(!type.refCount) FreeType(type);
9586                type = null;
9587             }
9588
9589             for( ; e; e = e.next)
9590             {
9591                if(!type && !emptyParams)
9592                {
9593                   yylloc = e.loc;
9594                   if(methodType && methodType.methodClass)
9595                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9596                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9597                         noParams ? 0 : functionType.params.count);
9598                   else
9599                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9600                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9601                         noParams ? 0 : functionType.params.count);
9602                   break;
9603                }
9604
9605                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9606                {
9607                   Type templatedType = null;
9608                   Class _class = methodType.usedClass;
9609                   ClassTemplateParameter curParam = null;
9610                   int id = 0;
9611                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9612                   {
9613                      Class sClass;
9614                      for(sClass = _class; sClass; sClass = sClass.base)
9615                      {
9616                         if(sClass.templateClass) sClass = sClass.templateClass;
9617                         id = 0;
9618                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9619                         {
9620                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9621                            {
9622                               Class nextClass;
9623                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9624                               {
9625                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9626                                  id += nextClass.templateParams.count;
9627                               }
9628                               break;
9629                            }
9630                            id++;
9631                         }
9632                         if(curParam) break;
9633                      }
9634                   }
9635                   if(curParam && _class.templateArgs[id].dataTypeString)
9636                   {
9637                      bool constant = type.constant;
9638                      ClassTemplateArgument arg = _class.templateArgs[id];
9639                      {
9640                         Context context = SetupTemplatesContext(_class);
9641
9642                         /*if(!arg.dataType)
9643                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9644                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9645                         FinishTemplatesContext(context);
9646                      }
9647
9648                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9649                      else if(templatedType.kind == pointerType)
9650                      {
9651                         Type t = templatedType.type;
9652                         while(t.kind == pointerType) t = t.type;
9653                         if(constant) t.constant = constant;
9654                      }
9655
9656                      e.destType = templatedType;
9657                      if(templatedType)
9658                      {
9659                         templatedType.passAsTemplate = true;
9660                         // templatedType.refCount++;
9661                      }
9662                   }
9663                   else
9664                   {
9665                      e.destType = type;
9666                      if(type) type.refCount++;
9667                   }
9668                }
9669                else
9670                {
9671                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9672                   {
9673                      e.destType = type.prev;
9674                      e.destType.refCount++;
9675                   }
9676                   else
9677                   {
9678                      e.destType = type;
9679                      if(type) type.refCount++;
9680                   }
9681                }
9682                // Don't reach the end for the ellipsis
9683                if(type && type.kind != ellipsisType)
9684                {
9685                   Type next = type.next;
9686                   if(!type.refCount) FreeType(type);
9687                   type = next;
9688                }
9689             }
9690
9691             if(type && type.kind != ellipsisType)
9692             {
9693                if(methodType && methodType.methodClass)
9694                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9695                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9696                      functionType.params.count + extra);
9697                else
9698                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9699                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9700                      functionType.params.count + extra);
9701             }
9702             yylloc = oldyylloc;
9703             if(type && !type.refCount) FreeType(type);
9704          }
9705          else
9706          {
9707             functionType = Type
9708             {
9709                refCount = 0;
9710                kind = TypeKind::functionType;
9711             };
9712
9713             if(exp.call.exp.type == identifierExp)
9714             {
9715                char * string = exp.call.exp.identifier.string;
9716                if(inCompiler)
9717                {
9718                   Symbol symbol;
9719                   Location oldyylloc = yylloc;
9720
9721                   yylloc = exp.call.exp.identifier.loc;
9722                   if(strstr(string, "__builtin_") == string)
9723                   {
9724                      if(exp.destType)
9725                      {
9726                         functionType.returnType = exp.destType;
9727                         exp.destType.refCount++;
9728                      }
9729                   }
9730                   else
9731                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9732                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9733                   globalContext.symbols.Add((BTNode)symbol);
9734                   if(strstr(symbol.string, "::"))
9735                      globalContext.hasNameSpace = true;
9736
9737                   yylloc = oldyylloc;
9738                }
9739             }
9740             else if(exp.call.exp.type == memberExp)
9741             {
9742                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9743                   exp.call.exp.member.member.string);*/
9744             }
9745             else
9746                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9747
9748             if(!functionType.returnType)
9749             {
9750                functionType.returnType = Type
9751                {
9752                   refCount = 1;
9753                   kind = intType;
9754                };
9755             }
9756          }
9757          if(functionType && functionType.kind == TypeKind::functionType)
9758          {
9759             exp.expType = functionType.returnType;
9760
9761             if(functionType.returnType)
9762                functionType.returnType.refCount++;
9763
9764             if(!functionType.refCount)
9765                FreeType(functionType);
9766          }
9767
9768          if(exp.call.arguments)
9769          {
9770             for(e = exp.call.arguments->first; e; e = e.next)
9771                ProcessExpressionType(e);
9772          }
9773          break;
9774       }
9775       case memberExp:
9776       {
9777          Type type;
9778          Location oldyylloc = yylloc;
9779          bool thisPtr;
9780          Expression checkExp = exp.member.exp;
9781          while(checkExp)
9782          {
9783             if(checkExp.type == castExp)
9784                checkExp = checkExp.cast.exp;
9785             else if(checkExp.type == bracketsExp)
9786                checkExp = checkExp.list ? checkExp.list->first : null;
9787             else
9788                break;
9789          }
9790
9791          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9792          exp.thisPtr = thisPtr;
9793
9794          // DOING THIS LATER NOW...
9795          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9796          {
9797             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9798             /* TODO: Name Space Fix ups
9799             if(!exp.member.member.classSym)
9800                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9801             */
9802          }
9803
9804          ProcessExpressionType(exp.member.exp);
9805          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9806             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9807          {
9808             exp.isConstant = false;
9809          }
9810          else
9811             exp.isConstant = exp.member.exp.isConstant;
9812          type = exp.member.exp.expType;
9813
9814          yylloc = exp.loc;
9815
9816          if(type && (type.kind == templateType))
9817          {
9818             Class _class = thisClass ? thisClass : currentClass;
9819             ClassTemplateParameter param = null;
9820             if(_class)
9821             {
9822                for(param = _class.templateParams.first; param; param = param.next)
9823                {
9824                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9825                      break;
9826                }
9827             }
9828             if(param && param.defaultArg.member)
9829             {
9830                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9831                if(argExp)
9832                {
9833                   Expression expMember = exp.member.exp;
9834                   Declarator decl;
9835                   OldList * specs = MkList();
9836                   char thisClassTypeString[1024];
9837
9838                   FreeIdentifier(exp.member.member);
9839
9840                   ProcessExpressionType(argExp);
9841
9842                   {
9843                      char * colon = strstr(param.defaultArg.memberString, "::");
9844                      if(colon)
9845                      {
9846                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9847                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9848                      }
9849                      else
9850                         strcpy(thisClassTypeString, _class.fullName);
9851                   }
9852
9853                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9854
9855                   exp.expType = ProcessType(specs, decl);
9856                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9857                   {
9858                      Class expClass = exp.expType._class.registered;
9859                      Class cClass = null;
9860                      int paramCount = 0;
9861                      int lastParam = -1;
9862
9863                      char templateString[1024];
9864                      ClassTemplateParameter param;
9865                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9866                      for(cClass = expClass; cClass; cClass = cClass.base)
9867                      {
9868                         int p = 0;
9869                         for(param = cClass.templateParams.first; param; param = param.next)
9870                         {
9871                            int id = p;
9872                            Class sClass;
9873                            ClassTemplateArgument arg;
9874                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9875                            arg = expClass.templateArgs[id];
9876
9877                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9878                            {
9879                               ClassTemplateParameter cParam;
9880                               //int p = numParams - sClass.templateParams.count;
9881                               int p = 0;
9882                               Class nextClass;
9883                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9884
9885                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9886                               {
9887                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9888                                  {
9889                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9890                                     {
9891                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9892                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9893                                        break;
9894                                     }
9895                                  }
9896                               }
9897                            }
9898
9899                            {
9900                               char argument[256];
9901                               argument[0] = '\0';
9902                               /*if(arg.name)
9903                               {
9904                                  strcat(argument, arg.name.string);
9905                                  strcat(argument, " = ");
9906                               }*/
9907                               switch(param.type)
9908                               {
9909                                  case expression:
9910                                  {
9911                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9912                                     char expString[1024];
9913                                     OldList * specs = MkList();
9914                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9915                                     Expression exp;
9916                                     char * string = PrintHexUInt64(arg.expression.ui64);
9917                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9918                                     delete string;
9919
9920                                     ProcessExpressionType(exp);
9921                                     ComputeExpression(exp);
9922                                     expString[0] = '\0';
9923                                     PrintExpression(exp, expString);
9924                                     strcat(argument, expString);
9925                                     // delete exp;
9926                                     FreeExpression(exp);
9927                                     break;
9928                                  }
9929                                  case identifier:
9930                                  {
9931                                     strcat(argument, arg.member.name);
9932                                     break;
9933                                  }
9934                                  case TemplateParameterType::type:
9935                                  {
9936                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9937                                     {
9938                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9939                                           strcat(argument, thisClassTypeString);
9940                                        else
9941                                           strcat(argument, arg.dataTypeString);
9942                                     }
9943                                     break;
9944                                  }
9945                               }
9946                               if(argument[0])
9947                               {
9948                                  if(paramCount) strcat(templateString, ", ");
9949                                  if(lastParam != p - 1)
9950                                  {
9951                                     strcat(templateString, param.name);
9952                                     strcat(templateString, " = ");
9953                                  }
9954                                  strcat(templateString, argument);
9955                                  paramCount++;
9956                                  lastParam = p;
9957                               }
9958                               p++;
9959                            }
9960                         }
9961                      }
9962                      {
9963                         int len = strlen(templateString);
9964                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9965                         templateString[len++] = '>';
9966                         templateString[len++] = '\0';
9967                      }
9968                      {
9969                         Context context = SetupTemplatesContext(_class);
9970                         FreeType(exp.expType);
9971                         exp.expType = ProcessTypeString(templateString, false);
9972                         FinishTemplatesContext(context);
9973                      }
9974                   }
9975
9976                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9977                   exp.type = bracketsExp;
9978                   exp.list = MkListOne(MkExpOp(null, '*',
9979                   /*opExp;
9980                   exp.op.op = '*';
9981                   exp.op.exp1 = null;
9982                   exp.op.exp2 = */
9983                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9984                      MkExpBrackets(MkListOne(
9985                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9986                            '+',
9987                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9988                            '+',
9989                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9990
9991                            ));
9992                }
9993             }
9994             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9995                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9996             {
9997                type = ProcessTemplateParameterType(type.templateParameter);
9998             }
9999          }
10000          // TODO: *** This seems to be where we should add method support for all basic types ***
10001          if(type && (type.kind == templateType));
10002          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
10003                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
10004                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
10005                           (type.kind == pointerType && type.type.kind == charType)))
10006          {
10007             Identifier id = exp.member.member;
10008             TypeKind typeKind = type.kind;
10009             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10010             if(typeKind == subClassType && exp.member.exp.type == classExp)
10011             {
10012                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
10013                typeKind = classType;
10014             }
10015
10016             if(id)
10017             {
10018                if(typeKind == intType || typeKind == enumType)
10019                   _class = eSystem_FindClass(privateModule, "int");
10020                else if(!_class)
10021                {
10022                   if(type.kind == classType && type._class && type._class.registered)
10023                   {
10024                      _class = type._class.registered;
10025                   }
10026                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10027                   {
10028                      _class = FindClass("char *").registered;
10029                   }
10030                   else if(type.kind == pointerType)
10031                   {
10032                      _class = eSystem_FindClass(privateModule, "uintptr");
10033                      FreeType(exp.expType);
10034                      exp.expType = ProcessTypeString("uintptr", false);
10035                      exp.byReference = true;
10036                   }
10037                   else
10038                   {
10039                      char string[1024] = "";
10040                      Symbol classSym;
10041                      PrintTypeNoConst(type, string, false, true);
10042                      classSym = FindClass(string);
10043                      if(classSym) _class = classSym.registered;
10044                   }
10045                }
10046             }
10047
10048             if(_class && id)
10049             {
10050                /*bool thisPtr =
10051                   (exp.member.exp.type == identifierExp &&
10052                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10053                Property prop = null;
10054                Method method = null;
10055                DataMember member = null;
10056                Property revConvert = null;
10057                ClassProperty classProp = null;
10058
10059                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10060                   exp.member.memberType = propertyMember;
10061
10062                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10063                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10064
10065                if(typeKind != subClassType)
10066                {
10067                   // Prioritize data members over properties for "this"
10068                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10069                   {
10070                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10071                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10072                      {
10073                         prop = eClass_FindProperty(_class, id.string, privateModule);
10074                         if(prop)
10075                            member = null;
10076                      }
10077                      if(!member && !prop)
10078                         prop = eClass_FindProperty(_class, id.string, privateModule);
10079                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10080                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10081                         exp.member.thisPtr = true;
10082                   }
10083                   // Prioritize properties over data members otherwise
10084                   else
10085                   {
10086                      bool useMemberForNonConst = false;
10087                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10088                      if(!id.classSym)
10089                      {
10090                         prop = eClass_FindProperty(_class, id.string, null);
10091
10092                         useMemberForNonConst = prop && exp.destType &&
10093                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10094                               !strncmp(prop.dataTypeString, "const ", 6);
10095
10096                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10097                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10098                      }
10099
10100                      if((!prop || useMemberForNonConst) && !member)
10101                      {
10102                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10103                         if(!method)
10104                         {
10105                            prop = eClass_FindProperty(_class, id.string, privateModule);
10106
10107                            useMemberForNonConst |= prop && exp.destType &&
10108                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10109                                  !strncmp(prop.dataTypeString, "const ", 6);
10110
10111                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10112                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10113                         }
10114                      }
10115
10116                      if(member && prop)
10117                      {
10118                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10119                            prop = null;
10120                         else
10121                            member = null;
10122                      }
10123                   }
10124                }
10125                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10126                   method = eClass_FindMethod(_class, id.string, privateModule);
10127                if(!prop && !member && !method)
10128                {
10129                   if(typeKind == subClassType)
10130                   {
10131                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10132                      if(classProp)
10133                      {
10134                         exp.member.memberType = classPropertyMember;
10135                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10136                      }
10137                      else
10138                      {
10139                         // Assume this is a class_data member
10140                         char structName[1024];
10141                         Identifier id = exp.member.member;
10142                         Expression classExp = exp.member.exp;
10143                         type.refCount++;
10144
10145                         FreeType(classExp.expType);
10146                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10147
10148                         strcpy(structName, "__ecereClassData_");
10149                         FullClassNameCat(structName, type._class.string, false);
10150                         exp.type = pointerExp;
10151                         exp.member.member = id;
10152
10153                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10154                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10155                               MkExpBrackets(MkListOne(MkExpOp(
10156                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10157                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10158                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10159                                  )));
10160
10161                         FreeType(type);
10162
10163                         ProcessExpressionType(exp);
10164                         return;
10165                      }
10166                   }
10167                   else
10168                   {
10169                      // Check for reverse conversion
10170                      // (Convert in an instantiation later, so that we can use
10171                      //  deep properties system)
10172                      Symbol classSym = FindClass(id.string);
10173                      if(classSym)
10174                      {
10175                         Class convertClass = classSym.registered;
10176                         if(convertClass)
10177                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10178                      }
10179                   }
10180                }
10181
10182                if(!exp.member.exp.destType)
10183                {
10184                   if(method && !method._class.symbol)
10185                      method._class.symbol = FindClass(method._class.fullName);
10186
10187                   exp.member.exp.destType = Type
10188                   {
10189                      refCount = 1;
10190                      kind = classType;
10191                      _class = prop ? prop._class.symbol : method ? method._class.symbol : _class.symbol;
10192                      // wasThisClass = type ? type.wasThisClass : false;
10193                   };
10194                }
10195
10196                if(prop)
10197                {
10198                   exp.member.memberType = propertyMember;
10199                   if(!prop.dataType)
10200                      ProcessPropertyType(prop);
10201                   exp.expType = prop.dataType;
10202                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10203                   {
10204                      Type type { };
10205                      CopyTypeInto(type, exp.expType);
10206                      type.refCount = 1;
10207                      type.constant = true;
10208                      exp.expType = type;
10209                   }
10210                   else if(prop.dataType)
10211                      prop.dataType.refCount++;
10212                }
10213                else if(member)
10214                {
10215                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10216                   {
10217                      FreeExpContents(exp);
10218                      exp.type = identifierExp;
10219                      exp.identifier = MkIdentifier("class");
10220                      ProcessExpressionType(exp);
10221                      return;
10222                   }
10223
10224                   exp.member.memberType = dataMember;
10225                   DeclareStruct(_class.fullName, false);
10226                   if(!member.dataType)
10227                   {
10228                      Context context = SetupTemplatesContext(_class);
10229                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10230                      FinishTemplatesContext(context);
10231                   }
10232                   exp.expType = member.dataType;
10233                   if(member.dataType) member.dataType.refCount++;
10234                }
10235                else if(revConvert)
10236                {
10237                   exp.member.memberType = reverseConversionMember;
10238                   exp.expType = MkClassType(revConvert._class.fullName);
10239                }
10240                else if(method)
10241                {
10242                   //if(inCompiler)
10243                   {
10244                      /*if(id._class)
10245                      {
10246                         exp.type = identifierExp;
10247                         exp.identifier = exp.member.member;
10248                      }
10249                      else*/
10250                         exp.member.memberType = methodMember;
10251                   }
10252                   if(!method.dataType)
10253                      ProcessMethodType(method);
10254                   exp.expType = Type
10255                   {
10256                      refCount = 1;
10257                      kind = methodType;
10258                      method = method;
10259                   };
10260
10261                   // Tricky spot here... To use instance versus class virtual table
10262                   // Put it back to what it was... What did we break?
10263
10264                   // Had to put it back for overriding Main of Thread global instance
10265
10266                   //exp.expType.methodClass = _class;
10267                   exp.expType.methodClass = (id && id._class) ? _class : null;
10268
10269                   // Need the actual class used for templated classes
10270                   exp.expType.usedClass = _class;
10271                }
10272                else if(!classProp)
10273                {
10274                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10275                   {
10276                      FreeExpContents(exp);
10277                      exp.type = identifierExp;
10278                      exp.identifier = MkIdentifier("class");
10279                      FreeType(exp.expType);
10280                      exp.expType = MkClassType("ecere::com::Class");
10281                      return;
10282                   }
10283                   yylloc = exp.member.member.loc;
10284                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10285                   if(inCompiler)
10286                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10287                }
10288
10289                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10290                {
10291                   Class tClass;
10292
10293                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10294                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10295
10296                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10297                   {
10298                      int id = 0;
10299                      ClassTemplateParameter curParam = null;
10300                      Class sClass;
10301
10302                      for(sClass = tClass; sClass; sClass = sClass.base)
10303                      {
10304                         id = 0;
10305                         if(sClass.templateClass) sClass = sClass.templateClass;
10306                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10307                         {
10308                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10309                            {
10310                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10311                                  id += sClass.templateParams.count;
10312                               break;
10313                            }
10314                            id++;
10315                         }
10316                         if(curParam) break;
10317                      }
10318
10319                      if(curParam && tClass.templateArgs[id].dataTypeString)
10320                      {
10321                         ClassTemplateArgument arg = tClass.templateArgs[id];
10322                         Context context = SetupTemplatesContext(tClass);
10323                         bool constant = exp.expType.constant;
10324                         /*if(!arg.dataType)
10325                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10326                         FreeType(exp.expType);
10327
10328                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10329                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10330                         else if(exp.expType.kind == pointerType)
10331                         {
10332                            Type t = exp.expType.type;
10333                            while(t.kind == pointerType) t = t.type;
10334                            if(constant) t.constant = constant;
10335                         }
10336                         if(exp.expType)
10337                         {
10338                            if(exp.expType.kind == thisClassType)
10339                            {
10340                               FreeType(exp.expType);
10341                               exp.expType = ReplaceThisClassType(_class);
10342                            }
10343
10344                            if(tClass.templateClass && (exp.expType.kind != templateType || (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType))))
10345                               exp.expType.passAsTemplate = true;
10346                            //exp.expType.refCount++;
10347                            if(!exp.destType)
10348                            {
10349                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10350                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10351                               else if(exp.destType.kind == pointerType)
10352                               {
10353                                  Type t = exp.destType.type;
10354                                  while(t.kind == pointerType) t = t.type;
10355                                  if(constant) t.constant = constant;
10356                               }
10357
10358                               //exp.destType.refCount++;
10359
10360                               if(exp.destType.kind == thisClassType)
10361                               {
10362                                  FreeType(exp.destType);
10363                                  exp.destType = ReplaceThisClassType(_class);
10364                               }
10365                            }
10366                         }
10367                         FinishTemplatesContext(context);
10368                      }
10369                   }
10370                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10371                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10372                   {
10373                      int id = 0;
10374                      ClassTemplateParameter curParam = null;
10375                      Class sClass;
10376
10377                      for(sClass = tClass; sClass; sClass = sClass.base)
10378                      {
10379                         id = 0;
10380                         if(sClass.templateClass) sClass = sClass.templateClass;
10381                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10382                         {
10383                            if(curParam.type == TemplateParameterType::type &&
10384                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10385                            {
10386                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10387                                  id += sClass.templateParams.count;
10388                               break;
10389                            }
10390                            id++;
10391                         }
10392                         if(curParam) break;
10393                      }
10394
10395                      if(curParam)
10396                      {
10397                         ClassTemplateArgument arg = tClass.templateArgs[id];
10398                         Context context = SetupTemplatesContext(tClass);
10399                         Type basicType;
10400                         /*if(!arg.dataType)
10401                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10402
10403                         basicType = ProcessTypeString(arg.dataTypeString, false);
10404                         if(basicType)
10405                         {
10406                            if(basicType.kind == thisClassType)
10407                            {
10408                               FreeType(basicType);
10409                               basicType = ReplaceThisClassType(_class);
10410                            }
10411
10412                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10413                            if(tClass.templateClass)
10414                               basicType.passAsTemplate = true;
10415                            */
10416
10417                            FreeType(exp.expType);
10418
10419                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10420                            //exp.expType.refCount++;
10421                            if(!exp.destType)
10422                            {
10423                               exp.destType = exp.expType;
10424                               exp.destType.refCount++;
10425                            }
10426
10427                            {
10428                               Expression newExp { };
10429                               OldList * specs = MkList();
10430                               Declarator decl;
10431                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10432                               *newExp = *exp;
10433                               if(exp.destType) exp.destType.refCount++;
10434                               if(exp.expType)  exp.expType.refCount++;
10435                               exp.type = castExp;
10436                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10437                               exp.cast.exp = newExp;
10438                               //FreeType(exp.expType);
10439                               //exp.expType = null;
10440                               //ProcessExpressionType(sourceExp);
10441                            }
10442                         }
10443                         FinishTemplatesContext(context);
10444                      }
10445                   }
10446                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10447                   {
10448                      Class expClass = exp.expType._class.registered;
10449                      if(expClass)
10450                      {
10451                         Class cClass = null;
10452                         int p = 0;
10453                         int paramCount = 0;
10454                         int lastParam = -1;
10455                         char templateString[1024];
10456                         ClassTemplateParameter param;
10457                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10458                         while(cClass != expClass)
10459                         {
10460                            Class sClass;
10461                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10462                            cClass = sClass;
10463
10464                            for(param = cClass.templateParams.first; param; param = param.next)
10465                            {
10466                               Class cClassCur = null;
10467                               int cp = 0;
10468                               ClassTemplateParameter paramCur = null;
10469                               ClassTemplateArgument arg;
10470                               while(cClassCur != tClass && !paramCur)
10471                               {
10472                                  Class sClassCur;
10473                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10474                                  cClassCur = sClassCur;
10475
10476                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10477                                  {
10478                                     if(!strcmp(paramCur.name, param.name))
10479                                     {
10480
10481                                        break;
10482                                     }
10483                                     cp++;
10484                                  }
10485                               }
10486                               if(paramCur && paramCur.type == TemplateParameterType::type)
10487                                  arg = tClass.templateArgs[cp];
10488                               else
10489                                  arg = expClass.templateArgs[p];
10490
10491                               {
10492                                  char argument[256];
10493                                  argument[0] = '\0';
10494                                  /*if(arg.name)
10495                                  {
10496                                     strcat(argument, arg.name.string);
10497                                     strcat(argument, " = ");
10498                                  }*/
10499                                  switch(param.type)
10500                                  {
10501                                     case expression:
10502                                     {
10503                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10504                                        char expString[1024];
10505                                        OldList * specs = MkList();
10506                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10507                                        Expression exp;
10508                                        char * string = PrintHexUInt64(arg.expression.ui64);
10509                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10510                                        delete string;
10511
10512                                        ProcessExpressionType(exp);
10513                                        ComputeExpression(exp);
10514                                        expString[0] = '\0';
10515                                        PrintExpression(exp, expString);
10516                                        strcat(argument, expString);
10517                                        // delete exp;
10518                                        FreeExpression(exp);
10519                                        break;
10520                                     }
10521                                     case identifier:
10522                                     {
10523                                        strcat(argument, arg.member.name);
10524                                        break;
10525                                     }
10526                                     case TemplateParameterType::type:
10527                                     {
10528                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10529                                           strcat(argument, arg.dataTypeString);
10530                                        break;
10531                                     }
10532                                  }
10533                                  if(argument[0])
10534                                  {
10535                                     if(paramCount) strcat(templateString, ", ");
10536                                     if(lastParam != p - 1)
10537                                     {
10538                                        strcat(templateString, param.name);
10539                                        strcat(templateString, " = ");
10540                                     }
10541                                     strcat(templateString, argument);
10542                                     paramCount++;
10543                                     lastParam = p;
10544                                  }
10545                               }
10546                               p++;
10547                            }
10548                         }
10549                         {
10550                            int len = strlen(templateString);
10551                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10552                            templateString[len++] = '>';
10553                            templateString[len++] = '\0';
10554                         }
10555
10556                         FreeType(exp.expType);
10557                         {
10558                            Context context = SetupTemplatesContext(tClass);
10559                            exp.expType = ProcessTypeString(templateString, false);
10560                            FinishTemplatesContext(context);
10561                         }
10562                      }
10563                   }
10564                }
10565             }
10566             else
10567                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10568          }
10569          else if(type && (type.kind == structType || type.kind == unionType))
10570          {
10571             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10572             if(memberType)
10573             {
10574                exp.expType = memberType;
10575                if(memberType)
10576                   memberType.refCount++;
10577             }
10578          }
10579          else
10580          {
10581             char expString[10240];
10582             expString[0] = '\0';
10583             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10584             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10585          }
10586
10587          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10588          {
10589             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10590             {
10591                Identifier id = exp.member.member;
10592                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10593                if(_class)
10594                {
10595                   FreeType(exp.expType);
10596                   exp.expType = ReplaceThisClassType(_class);
10597                }
10598             }
10599          }
10600          yylloc = oldyylloc;
10601          break;
10602       }
10603       // Convert x->y into (*x).y
10604       case pointerExp:
10605       {
10606          Type destType = exp.destType;
10607
10608          // DOING THIS LATER NOW...
10609          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10610          {
10611             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10612             /* TODO: Name Space Fix ups
10613             if(!exp.member.member.classSym)
10614                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10615             */
10616          }
10617
10618          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10619          exp.type = memberExp;
10620          if(destType)
10621             destType.count++;
10622          ProcessExpressionType(exp);
10623          if(destType)
10624             destType.count--;
10625          break;
10626       }
10627       case classSizeExp:
10628       {
10629          //ComputeExpression(exp);
10630
10631          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10632          if(classSym && classSym.registered)
10633          {
10634             if(classSym.registered.type == noHeadClass)
10635             {
10636                char name[1024];
10637                name[0] = '\0';
10638                DeclareStruct(classSym.string, false);
10639                FreeSpecifier(exp._class);
10640                exp.type = typeSizeExp;
10641                FullClassNameCat(name, classSym.string, false);
10642                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10643             }
10644             else
10645             {
10646                if(classSym.registered.fixed)
10647                {
10648                   FreeSpecifier(exp._class);
10649                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10650                   exp.type = constantExp;
10651                }
10652                else
10653                {
10654                   char className[1024];
10655                   strcpy(className, "__ecereClass_");
10656                   FullClassNameCat(className, classSym.string, true);
10657                   //MangleClassName(className);
10658
10659                   DeclareClass(classSym, className);
10660
10661                   FreeExpContents(exp);
10662                   exp.type = pointerExp;
10663                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10664                   exp.member.member = MkIdentifier("structSize");
10665                }
10666             }
10667          }
10668
10669          exp.expType = Type
10670          {
10671             refCount = 1;
10672             kind = intSizeType;
10673          };
10674          // exp.isConstant = true;
10675          break;
10676       }
10677       case typeSizeExp:
10678       {
10679          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10680
10681          exp.expType = Type
10682          {
10683             refCount = 1;
10684             kind = intSizeType;
10685          };
10686          exp.isConstant = true;
10687
10688          DeclareType(type, false, false);
10689          FreeType(type);
10690          break;
10691       }
10692       case castExp:
10693       {
10694          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10695          type.count = 1;
10696          FreeType(exp.cast.exp.destType);
10697          exp.cast.exp.destType = type;
10698          type.refCount++;
10699          type.casted = true;
10700          ProcessExpressionType(exp.cast.exp);
10701          type.casted = false;
10702          type.count = 0;
10703          exp.expType = type;
10704          //type.refCount++;
10705
10706          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10707          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10708          {
10709             void * prev = exp.prev, * next = exp.next;
10710             Type expType = exp.cast.exp.destType;
10711             Expression castExp = exp.cast.exp;
10712             Type destType = exp.destType;
10713
10714             if(expType) expType.refCount++;
10715
10716             //FreeType(exp.destType);
10717             FreeType(exp.expType);
10718             FreeTypeName(exp.cast.typeName);
10719
10720             *exp = *castExp;
10721             FreeType(exp.expType);
10722             FreeType(exp.destType);
10723
10724             exp.expType = expType;
10725             exp.destType = destType;
10726
10727             delete castExp;
10728
10729             exp.prev = prev;
10730             exp.next = next;
10731
10732          }
10733          else
10734          {
10735             exp.isConstant = exp.cast.exp.isConstant;
10736          }
10737          //FreeType(type);
10738          break;
10739       }
10740       case extensionInitializerExp:
10741       {
10742          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10743          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10744          // ProcessInitializer(exp.initializer.initializer, type);
10745          exp.expType = type;
10746          break;
10747       }
10748       case vaArgExp:
10749       {
10750          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10751          ProcessExpressionType(exp.vaArg.exp);
10752          exp.expType = type;
10753          break;
10754       }
10755       case conditionExp:
10756       {
10757          Expression e;
10758          exp.isConstant = true;
10759
10760          FreeType(exp.cond.cond.destType);
10761          exp.cond.cond.destType = MkClassType("bool");
10762          exp.cond.cond.destType.truth = true;
10763          ProcessExpressionType(exp.cond.cond);
10764          if(!exp.cond.cond.isConstant)
10765             exp.isConstant = false;
10766          for(e = exp.cond.exp->first; e; e = e.next)
10767          {
10768             if(!e.next)
10769             {
10770                FreeType(e.destType);
10771                e.destType = exp.destType;
10772                if(e.destType) e.destType.refCount++;
10773             }
10774             ProcessExpressionType(e);
10775             if(!e.next)
10776             {
10777                exp.expType = e.expType;
10778                if(e.expType) e.expType.refCount++;
10779             }
10780             if(!e.isConstant)
10781                exp.isConstant = false;
10782          }
10783
10784          FreeType(exp.cond.elseExp.destType);
10785          // Added this check if we failed to find an expType
10786          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10787
10788          // Reversed it...
10789          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10790
10791          if(exp.cond.elseExp.destType)
10792             exp.cond.elseExp.destType.refCount++;
10793          ProcessExpressionType(exp.cond.elseExp);
10794
10795          // FIXED THIS: Was done before calling process on elseExp
10796          if(!exp.cond.elseExp.isConstant)
10797             exp.isConstant = false;
10798          break;
10799       }
10800       case extensionCompoundExp:
10801       {
10802          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10803          {
10804             Statement last = exp.compound.compound.statements->last;
10805             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10806             {
10807                ((Expression)last.expressions->last).destType = exp.destType;
10808                if(exp.destType)
10809                   exp.destType.refCount++;
10810             }
10811             ProcessStatement(exp.compound);
10812             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10813             if(exp.expType)
10814                exp.expType.refCount++;
10815          }
10816          break;
10817       }
10818       case classExp:
10819       {
10820          Specifier spec = exp._classExp.specifiers->first;
10821          if(spec && spec.type == nameSpecifier)
10822          {
10823             exp.expType = MkClassType(spec.name);
10824             exp.expType.kind = subClassType;
10825             exp.byReference = true;
10826          }
10827          else
10828          {
10829             exp.expType = MkClassType("ecere::com::Class");
10830             exp.byReference = true;
10831          }
10832          break;
10833       }
10834       case classDataExp:
10835       {
10836          Class _class = thisClass ? thisClass : currentClass;
10837          if(_class)
10838          {
10839             Identifier id = exp.classData.id;
10840             char structName[1024];
10841             Expression classExp;
10842             strcpy(structName, "__ecereClassData_");
10843             FullClassNameCat(structName, _class.fullName, false);
10844             exp.type = pointerExp;
10845             exp.member.member = id;
10846             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10847                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10848             else
10849                classExp = MkExpIdentifier(MkIdentifier("class"));
10850
10851             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10852                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10853                   MkExpBrackets(MkListOne(MkExpOp(
10854                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10855                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10856                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10857                      )));
10858
10859             ProcessExpressionType(exp);
10860             return;
10861          }
10862          break;
10863       }
10864       case arrayExp:
10865       {
10866          Type type = null;
10867          const char * typeString = null;
10868          char typeStringBuf[1024];
10869          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10870             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10871          {
10872             Class templateClass = exp.destType._class.registered;
10873             typeString = templateClass.templateArgs[2].dataTypeString;
10874          }
10875          else if(exp.list)
10876          {
10877             // Guess type from expressions in the array
10878             Expression e;
10879             for(e = exp.list->first; e; e = e.next)
10880             {
10881                ProcessExpressionType(e);
10882                if(e.expType)
10883                {
10884                   if(!type) { type = e.expType; type.refCount++; }
10885                   else
10886                   {
10887                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10888                      if(!MatchTypeExpression(e, type, null, false, true))
10889                      {
10890                         FreeType(type);
10891                         type = e.expType;
10892                         e.expType = null;
10893
10894                         e = exp.list->first;
10895                         ProcessExpressionType(e);
10896                         if(e.expType)
10897                         {
10898                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10899                            if(!MatchTypeExpression(e, type, null, false, true))
10900                            {
10901                               FreeType(e.expType);
10902                               e.expType = null;
10903                               FreeType(type);
10904                               type = null;
10905                               break;
10906                            }
10907                         }
10908                      }
10909                   }
10910                   if(e.expType)
10911                   {
10912                      FreeType(e.expType);
10913                      e.expType = null;
10914                   }
10915                }
10916             }
10917             if(type)
10918             {
10919                typeStringBuf[0] = '\0';
10920                PrintTypeNoConst(type, typeStringBuf, false, true);
10921                typeString = typeStringBuf;
10922                FreeType(type);
10923                type = null;
10924             }
10925          }
10926          if(typeString)
10927          {
10928             /*
10929             (Container)& (struct BuiltInContainer)
10930             {
10931                ._vTbl = class(BuiltInContainer)._vTbl,
10932                ._class = class(BuiltInContainer),
10933                .refCount = 0,
10934                .data = (int[]){ 1, 7, 3, 4, 5 },
10935                .count = 5,
10936                .type = class(int),
10937             }
10938             */
10939             char templateString[1024];
10940             OldList * initializers = MkList();
10941             OldList * structInitializers = MkList();
10942             OldList * specs = MkList();
10943             Expression expExt;
10944             Declarator decl = SpecDeclFromString(typeString, specs, null);
10945             sprintf(templateString, "Container<%s>", typeString);
10946
10947             if(exp.list)
10948             {
10949                Expression e;
10950                type = ProcessTypeString(typeString, false);
10951                while((e = exp.list->first))
10952                {
10953                   exp.list->Remove(e);
10954                   e.destType = type;
10955                   type.refCount++;
10956                   ProcessExpressionType(e);
10957                   ListAdd(initializers, MkInitializerAssignment(e));
10958                }
10959                FreeType(type);
10960                delete exp.list;
10961             }
10962
10963             DeclareStruct("ecere::com::BuiltInContainer", false);
10964
10965             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10966                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10967             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10968                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10969             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10970                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10971             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10972                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10973                MkInitializerList(initializers))));
10974                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10975             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10976                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10977             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10978                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10979             exp.expType = ProcessTypeString(templateString, false);
10980             exp.type = bracketsExp;
10981             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10982                MkExpOp(null, '&',
10983                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10984                   MkInitializerList(structInitializers)))));
10985             ProcessExpressionType(expExt);
10986          }
10987          else
10988          {
10989             exp.expType = ProcessTypeString("Container", false);
10990             Compiler_Error($"Couldn't determine type of array elements\n");
10991          }
10992          break;
10993       }
10994    }
10995
10996    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10997    {
10998       FreeType(exp.expType);
10999       exp.expType = ReplaceThisClassType(thisClass);
11000    }
11001
11002    // Resolve structures here
11003    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
11004    {
11005       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
11006       // TODO: Fix members reference...
11007       if(symbol)
11008       {
11009          if(exp.expType.kind != enumType)
11010          {
11011             Type member;
11012             String enumName = CopyString(exp.expType.enumName);
11013
11014             // Fixed a memory leak on self-referencing C structs typedefs
11015             // by instantiating a new type rather than simply copying members
11016             // into exp.expType
11017             FreeType(exp.expType);
11018             exp.expType = Type { };
11019             exp.expType.kind = symbol.type.kind;
11020             exp.expType.refCount++;
11021             exp.expType.enumName = enumName;
11022
11023             exp.expType.members = symbol.type.members;
11024             for(member = symbol.type.members.first; member; member = member.next)
11025                member.refCount++;
11026          }
11027          else
11028          {
11029             NamedLink member;
11030             for(member = symbol.type.members.first; member; member = member.next)
11031             {
11032                NamedLink value { name = CopyString(member.name) };
11033                exp.expType.members.Add(value);
11034             }
11035          }
11036       }
11037    }
11038
11039    yylloc = exp.loc;
11040    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11041    else if(exp.destType && !exp.destType.keepCast)
11042    {
11043       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11044          exp.needTemplateCast = 1;
11045
11046       if(exp.destType.kind == voidType);
11047       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11048       {
11049          if(!exp.destType.count || unresolved)
11050          {
11051             if(!exp.expType)
11052             {
11053                yylloc = exp.loc;
11054                if(exp.destType.kind != ellipsisType)
11055                {
11056                   char type2[1024];
11057                   type2[0] = '\0';
11058                   if(inCompiler)
11059                   {
11060                      char expString[10240];
11061                      expString[0] = '\0';
11062
11063                      PrintType(exp.destType, type2, false, true);
11064
11065                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11066                      if(unresolved)
11067                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11068                      else if(exp.type != dummyExp)
11069                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11070                   }
11071                }
11072                else
11073                {
11074                   char expString[10240] ;
11075                   expString[0] = '\0';
11076                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11077
11078                   if(unresolved)
11079                      Compiler_Error($"unresolved identifier %s\n", expString);
11080                   else if(exp.type != dummyExp)
11081                      Compiler_Error($"couldn't determine type of %s\n", expString);
11082                }
11083             }
11084             else
11085             {
11086                char type1[1024];
11087                char type2[1024];
11088                type1[0] = '\0';
11089                type2[0] = '\0';
11090                if(inCompiler)
11091                {
11092                   PrintType(exp.expType, type1, false, true);
11093                   PrintType(exp.destType, type2, false, true);
11094                }
11095
11096                //CheckExpressionType(exp, exp.destType, false);
11097
11098                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11099                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11100                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11101                else
11102                {
11103                   char expString[10240];
11104                   expString[0] = '\0';
11105                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11106
11107 #ifdef _DEBUG
11108                   CheckExpressionType(exp, exp.destType, false, true);
11109 #endif
11110                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11111                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
11112                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11113
11114                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11115                   FreeType(exp.expType);
11116                   exp.destType.refCount++;
11117                   exp.expType = exp.destType;
11118                }
11119             }
11120          }
11121       }
11122       /*else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
11123       {
11124          Expression newExp { };
11125          char typeString[1024];
11126          OldList * specs = MkList();
11127          Declarator decl;
11128
11129          typeString[0] = '\0';
11130
11131          *newExp = *exp;
11132
11133          if(exp.expType)  exp.expType.refCount++;
11134          if(exp.expType)  exp.expType.refCount++;
11135          exp.type = castExp;
11136          newExp.destType = exp.expType;
11137
11138          PrintType(exp.expType, typeString, false, false);
11139          decl = SpecDeclFromString(typeString, specs, null);
11140
11141          exp.cast.typeName = MkTypeName(specs, decl);
11142          exp.cast.exp = newExp;
11143       }*/
11144    }
11145    else if(unresolved)
11146    {
11147       if(exp.identifier._class && exp.identifier._class.name)
11148          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11149       else if(exp.identifier.string && exp.identifier.string[0])
11150          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11151    }
11152    else if(!exp.expType && exp.type != dummyExp)
11153    {
11154       char expString[10240];
11155       expString[0] = '\0';
11156       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11157       Compiler_Error($"couldn't determine type of %s\n", expString);
11158    }
11159
11160    // Let's try to support any_object & typed_object here:
11161    if(inCompiler)
11162       ApplyAnyObjectLogic(exp);
11163
11164    // Mark nohead classes as by reference, unless we're casting them to an integral type
11165    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11166       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11167          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11168           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11169    {
11170       exp.byReference = true;
11171    }
11172    yylloc = oldyylloc;
11173 }
11174
11175 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11176 {
11177    // THIS CODE WILL FIND NEXT MEMBER...
11178    if(*curMember)
11179    {
11180       *curMember = (*curMember).next;
11181
11182       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11183       {
11184          *curMember = subMemberStack[--(*subMemberStackPos)];
11185          *curMember = (*curMember).next;
11186       }
11187
11188       // SKIP ALL PROPERTIES HERE...
11189       while((*curMember) && (*curMember).isProperty)
11190          *curMember = (*curMember).next;
11191
11192       if(subMemberStackPos)
11193       {
11194          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11195          {
11196             subMemberStack[(*subMemberStackPos)++] = *curMember;
11197
11198             *curMember = (*curMember).members.first;
11199             while(*curMember && (*curMember).isProperty)
11200                *curMember = (*curMember).next;
11201          }
11202       }
11203    }
11204    while(!*curMember)
11205    {
11206       if(!*curMember)
11207       {
11208          if(subMemberStackPos && *subMemberStackPos)
11209          {
11210             *curMember = subMemberStack[--(*subMemberStackPos)];
11211             *curMember = (*curMember).next;
11212          }
11213          else
11214          {
11215             Class lastCurClass = *curClass;
11216
11217             if(*curClass == _class) break;     // REACHED THE END
11218
11219             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11220             *curMember = (*curClass).membersAndProperties.first;
11221          }
11222
11223          while((*curMember) && (*curMember).isProperty)
11224             *curMember = (*curMember).next;
11225          if(subMemberStackPos)
11226          {
11227             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11228             {
11229                subMemberStack[(*subMemberStackPos)++] = *curMember;
11230
11231                *curMember = (*curMember).members.first;
11232                while(*curMember && (*curMember).isProperty)
11233                   *curMember = (*curMember).next;
11234             }
11235          }
11236       }
11237    }
11238 }
11239
11240
11241 static void ProcessInitializer(Initializer init, Type type)
11242 {
11243    switch(init.type)
11244    {
11245       case expInitializer:
11246          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11247          {
11248             // TESTING THIS FOR SHUTTING = 0 WARNING
11249             if(init.exp && !init.exp.destType)
11250             {
11251                FreeType(init.exp.destType);
11252                init.exp.destType = type;
11253                if(type) type.refCount++;
11254             }
11255             if(init.exp)
11256             {
11257                ProcessExpressionType(init.exp);
11258                init.isConstant = init.exp.isConstant;
11259             }
11260             break;
11261          }
11262          else
11263          {
11264             Expression exp = init.exp;
11265             Instantiation inst = exp.instance;
11266             MembersInit members;
11267
11268             init.type = listInitializer;
11269             init.list = MkList();
11270
11271             if(inst.members)
11272             {
11273                for(members = inst.members->first; members; members = members.next)
11274                {
11275                   if(members.type == dataMembersInit)
11276                   {
11277                      MemberInit member;
11278                      for(member = members.dataMembers->first; member; member = member.next)
11279                      {
11280                         ListAdd(init.list, member.initializer);
11281                         member.initializer = null;
11282                      }
11283                   }
11284                   // Discard all MembersInitMethod
11285                }
11286             }
11287             FreeExpression(exp);
11288          }
11289       case listInitializer:
11290       {
11291          Initializer i;
11292          Type initializerType = null;
11293          Class curClass = null;
11294          DataMember curMember = null;
11295          DataMember subMemberStack[256];
11296          int subMemberStackPos = 0;
11297
11298          if(type && type.kind == arrayType)
11299             initializerType = Dereference(type);
11300          else if(type && (type.kind == structType || type.kind == unionType))
11301             initializerType = type.members.first;
11302
11303          for(i = init.list->first; i; i = i.next)
11304          {
11305             if(type && type.kind == classType && type._class && type._class.registered)
11306             {
11307                // 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)
11308                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11309                // TODO: Generate error on initializing a private data member this way from another module...
11310                if(curMember)
11311                {
11312                   if(!curMember.dataType)
11313                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11314                   initializerType = curMember.dataType;
11315                }
11316             }
11317             ProcessInitializer(i, initializerType);
11318             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11319                initializerType = initializerType.next;
11320             if(!i.isConstant)
11321                init.isConstant = false;
11322          }
11323
11324          if(type && type.kind == arrayType)
11325             FreeType(initializerType);
11326
11327          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11328          {
11329             Compiler_Error($"Assigning list initializer to non list\n");
11330          }
11331          break;
11332       }
11333    }
11334 }
11335
11336 static void ProcessSpecifier(Specifier spec, bool declareStruct)
11337 {
11338    switch(spec.type)
11339    {
11340       case baseSpecifier:
11341       {
11342          if(spec.specifier == THISCLASS)
11343          {
11344             if(thisClass)
11345             {
11346                spec.type = nameSpecifier;
11347                spec.name = ReplaceThisClass(thisClass);
11348                spec.symbol = FindClass(spec.name);
11349                ProcessSpecifier(spec, declareStruct);
11350             }
11351          }
11352          break;
11353       }
11354       case nameSpecifier:
11355       {
11356          Symbol symbol = FindType(curContext, spec.name);
11357          if(symbol)
11358             DeclareType(symbol.type, true, true);
11359          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
11360             DeclareStruct(spec.name, false);
11361          break;
11362       }
11363       case enumSpecifier:
11364       {
11365          Enumerator e;
11366          if(spec.list)
11367          {
11368             for(e = spec.list->first; e; e = e.next)
11369             {
11370                if(e.exp)
11371                   ProcessExpressionType(e.exp);
11372             }
11373          }
11374          break;
11375       }
11376       case structSpecifier:
11377       case unionSpecifier:
11378       {
11379          if(spec.definitions)
11380          {
11381             //ClassDef def;
11382             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11383             //if(symbol)
11384                ProcessClass(spec.definitions, symbol);
11385             /*else
11386             {
11387                for(def = spec.definitions->first; def; def = def.next)
11388                {
11389                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11390                      ProcessDeclaration(def.decl);
11391                }
11392             }*/
11393          }
11394          break;
11395       }
11396       /*
11397       case classSpecifier:
11398       {
11399          Symbol classSym = FindClass(spec.name);
11400          if(classSym && classSym.registered && classSym.registered.type == structClass)
11401             DeclareStruct(spec.name, false);
11402          break;
11403       }
11404       */
11405    }
11406 }
11407
11408
11409 static void ProcessDeclarator(Declarator decl)
11410 {
11411    switch(decl.type)
11412    {
11413       case identifierDeclarator:
11414          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11415          {
11416             FreeSpecifier(decl.identifier._class);
11417             decl.identifier._class = null;
11418          }
11419          break;
11420       case arrayDeclarator:
11421          if(decl.array.exp)
11422             ProcessExpressionType(decl.array.exp);
11423       case structDeclarator:
11424       case bracketsDeclarator:
11425       case functionDeclarator:
11426       case pointerDeclarator:
11427       case extendedDeclarator:
11428       case extendedDeclaratorEnd:
11429          if(decl.declarator)
11430             ProcessDeclarator(decl.declarator);
11431          if(decl.type == functionDeclarator)
11432          {
11433             Identifier id = GetDeclId(decl);
11434             if(id && id._class)
11435             {
11436                TypeName param
11437                {
11438                   qualifiers = MkListOne(id._class);
11439                   declarator = null;
11440                };
11441                if(!decl.function.parameters)
11442                   decl.function.parameters = MkList();
11443                decl.function.parameters->Insert(null, param);
11444                id._class = null;
11445             }
11446             if(decl.function.parameters)
11447             {
11448                TypeName param;
11449
11450                for(param = decl.function.parameters->first; param; param = param.next)
11451                {
11452                   if(param.qualifiers && param.qualifiers->first)
11453                   {
11454                      Specifier spec = param.qualifiers->first;
11455                      if(spec && spec.specifier == TYPED_OBJECT)
11456                      {
11457                         Declarator d = param.declarator;
11458                         TypeName newParam
11459                         {
11460                            qualifiers = MkListOne(MkSpecifier(VOID));
11461                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11462                         };
11463                         if(d.type != pointerDeclarator)
11464                            newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11465
11466                         FreeList(param.qualifiers, FreeSpecifier);
11467
11468                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11469                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11470
11471                         decl.function.parameters->Insert(param, newParam);
11472                         param = newParam;
11473                      }
11474                      else if(spec && spec.specifier == ANY_OBJECT)
11475                      {
11476                         Declarator d = param.declarator;
11477
11478                         FreeList(param.qualifiers, FreeSpecifier);
11479
11480                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11481                         if(d.type != pointerDeclarator)
11482                            param.qualifiers->Insert(null, MkSpecifier(CONST));
11483                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11484                      }
11485                      else if(spec.specifier == THISCLASS)
11486                      {
11487                         if(thisClass)
11488                         {
11489                            spec.type = nameSpecifier;
11490                            spec.name = ReplaceThisClass(thisClass);
11491                            spec.symbol = FindClass(spec.name);
11492                            ProcessSpecifier(spec, false);
11493                         }
11494                      }
11495                   }
11496
11497                   if(param.declarator)
11498                      ProcessDeclarator(param.declarator);
11499                }
11500             }
11501          }
11502          break;
11503    }
11504 }
11505
11506 static void ProcessDeclaration(Declaration decl)
11507 {
11508    yylloc = decl.loc;
11509    switch(decl.type)
11510    {
11511       case initDeclaration:
11512       {
11513          bool declareStruct = false;
11514          /*
11515          lineNum = decl.pos.line;
11516          column = decl.pos.col;
11517          */
11518
11519          if(decl.declarators)
11520          {
11521             InitDeclarator d;
11522
11523             for(d = decl.declarators->first; d; d = d.next)
11524             {
11525                Type type, subType;
11526                ProcessDeclarator(d.declarator);
11527
11528                type = ProcessType(decl.specifiers, d.declarator);
11529
11530                if(d.initializer)
11531                {
11532                   ProcessInitializer(d.initializer, type);
11533
11534                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11535
11536                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11537                      d.initializer.exp.type == instanceExp)
11538                   {
11539                      if(type.kind == classType && type._class ==
11540                         d.initializer.exp.expType._class)
11541                      {
11542                         Instantiation inst = d.initializer.exp.instance;
11543                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11544
11545                         d.initializer.exp.instance = null;
11546                         if(decl.specifiers)
11547                            FreeList(decl.specifiers, FreeSpecifier);
11548                         FreeList(decl.declarators, FreeInitDeclarator);
11549
11550                         d = null;
11551
11552                         decl.type = instDeclaration;
11553                         decl.inst = inst;
11554                      }
11555                   }
11556                }
11557                for(subType = type; subType;)
11558                {
11559                   if(subType.kind == classType)
11560                   {
11561                      declareStruct = true;
11562                      break;
11563                   }
11564                   else if(subType.kind == pointerType)
11565                      break;
11566                   else if(subType.kind == arrayType)
11567                      subType = subType.arrayType;
11568                   else
11569                      break;
11570                }
11571
11572                FreeType(type);
11573                if(!d) break;
11574             }
11575          }
11576
11577          if(decl.specifiers)
11578          {
11579             Specifier s;
11580             for(s = decl.specifiers->first; s; s = s.next)
11581             {
11582                ProcessSpecifier(s, declareStruct);
11583             }
11584          }
11585          break;
11586       }
11587       case instDeclaration:
11588       {
11589          ProcessInstantiationType(decl.inst);
11590          break;
11591       }
11592       case structDeclaration:
11593       {
11594          Specifier spec;
11595          Declarator d;
11596          bool declareStruct = false;
11597
11598          if(decl.declarators)
11599          {
11600             for(d = decl.declarators->first; d; d = d.next)
11601             {
11602                Type type = ProcessType(decl.specifiers, d.declarator);
11603                Type subType;
11604                ProcessDeclarator(d);
11605                for(subType = type; subType;)
11606                {
11607                   if(subType.kind == classType)
11608                   {
11609                      declareStruct = true;
11610                      break;
11611                   }
11612                   else if(subType.kind == pointerType)
11613                      break;
11614                   else if(subType.kind == arrayType)
11615                      subType = subType.arrayType;
11616                   else
11617                      break;
11618                }
11619                FreeType(type);
11620             }
11621          }
11622          if(decl.specifiers)
11623          {
11624             for(spec = decl.specifiers->first; spec; spec = spec.next)
11625                ProcessSpecifier(spec, declareStruct);
11626          }
11627          break;
11628       }
11629    }
11630 }
11631
11632 static FunctionDefinition curFunction;
11633
11634 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11635 {
11636    char propName[1024], propNameM[1024];
11637    char getName[1024], setName[1024];
11638    OldList * args;
11639
11640    DeclareProperty(prop, setName, getName);
11641
11642    // eInstance_FireWatchers(object, prop);
11643    strcpy(propName, "__ecereProp_");
11644    FullClassNameCat(propName, prop._class.fullName, false);
11645    strcat(propName, "_");
11646    // strcat(propName, prop.name);
11647    FullClassNameCat(propName, prop.name, true);
11648    //MangleClassName(propName);
11649
11650    strcpy(propNameM, "__ecerePropM_");
11651    FullClassNameCat(propNameM, prop._class.fullName, false);
11652    strcat(propNameM, "_");
11653    // strcat(propNameM, prop.name);
11654    FullClassNameCat(propNameM, prop.name, true);
11655    //MangleClassName(propNameM);
11656
11657    if(prop.isWatchable)
11658    {
11659       args = MkList();
11660       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11661       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11662       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11663
11664       args = MkList();
11665       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11666       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11667       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11668    }
11669
11670
11671    {
11672       args = MkList();
11673       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11674       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11675       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11676
11677       args = MkList();
11678       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11679       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11680       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11681    }
11682
11683    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11684       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11685       curFunction.propSet.fireWatchersDone = true;
11686 }
11687
11688 static void ProcessStatement(Statement stmt)
11689 {
11690    yylloc = stmt.loc;
11691    /*
11692    lineNum = stmt.pos.line;
11693    column = stmt.pos.col;
11694    */
11695    switch(stmt.type)
11696    {
11697       case labeledStmt:
11698          ProcessStatement(stmt.labeled.stmt);
11699          break;
11700       case caseStmt:
11701          // This expression should be constant...
11702          if(stmt.caseStmt.exp)
11703          {
11704             FreeType(stmt.caseStmt.exp.destType);
11705             stmt.caseStmt.exp.destType = curSwitchType;
11706             if(curSwitchType) curSwitchType.refCount++;
11707             ProcessExpressionType(stmt.caseStmt.exp);
11708             ComputeExpression(stmt.caseStmt.exp);
11709          }
11710          if(stmt.caseStmt.stmt)
11711             ProcessStatement(stmt.caseStmt.stmt);
11712          break;
11713       case compoundStmt:
11714       {
11715          if(stmt.compound.context)
11716          {
11717             Declaration decl;
11718             Statement s;
11719
11720             Statement prevCompound = curCompound;
11721             Context prevContext = curContext;
11722
11723             if(!stmt.compound.isSwitch)
11724                curCompound = stmt;
11725             curContext = stmt.compound.context;
11726
11727             if(stmt.compound.declarations)
11728             {
11729                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11730                   ProcessDeclaration(decl);
11731             }
11732             if(stmt.compound.statements)
11733             {
11734                for(s = stmt.compound.statements->first; s; s = s.next)
11735                   ProcessStatement(s);
11736             }
11737
11738             curContext = prevContext;
11739             curCompound = prevCompound;
11740          }
11741          break;
11742       }
11743       case expressionStmt:
11744       {
11745          Expression exp;
11746          if(stmt.expressions)
11747          {
11748             for(exp = stmt.expressions->first; exp; exp = exp.next)
11749                ProcessExpressionType(exp);
11750          }
11751          break;
11752       }
11753       case ifStmt:
11754       {
11755          Expression exp;
11756
11757          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11758          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11759          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11760          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11761          {
11762             ProcessExpressionType(exp);
11763          }
11764          if(stmt.ifStmt.stmt)
11765             ProcessStatement(stmt.ifStmt.stmt);
11766          if(stmt.ifStmt.elseStmt)
11767             ProcessStatement(stmt.ifStmt.elseStmt);
11768          break;
11769       }
11770       case switchStmt:
11771       {
11772          Type oldSwitchType = curSwitchType;
11773          if(stmt.switchStmt.exp)
11774          {
11775             Expression exp;
11776             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11777             {
11778                if(!exp.next)
11779                {
11780                   /*
11781                   Type destType
11782                   {
11783                      kind = intType;
11784                      refCount = 1;
11785                   };
11786                   e.exp.destType = destType;
11787                   */
11788
11789                   ProcessExpressionType(exp);
11790                }
11791                if(!exp.next)
11792                   curSwitchType = exp.expType;
11793             }
11794          }
11795          ProcessStatement(stmt.switchStmt.stmt);
11796          curSwitchType = oldSwitchType;
11797          break;
11798       }
11799       case whileStmt:
11800       {
11801          if(stmt.whileStmt.exp)
11802          {
11803             Expression exp;
11804
11805             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11806             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11807             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11808             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11809             {
11810                ProcessExpressionType(exp);
11811             }
11812          }
11813          if(stmt.whileStmt.stmt)
11814             ProcessStatement(stmt.whileStmt.stmt);
11815          break;
11816       }
11817       case doWhileStmt:
11818       {
11819          if(stmt.doWhile.exp)
11820          {
11821             Expression exp;
11822
11823             if(stmt.doWhile.exp->last)
11824             {
11825                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11826                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11827                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11828             }
11829             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11830             {
11831                ProcessExpressionType(exp);
11832             }
11833          }
11834          if(stmt.doWhile.stmt)
11835             ProcessStatement(stmt.doWhile.stmt);
11836          break;
11837       }
11838       case forStmt:
11839       {
11840          Expression exp;
11841          if(stmt.forStmt.init)
11842             ProcessStatement(stmt.forStmt.init);
11843
11844          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11845          {
11846             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11847             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11848             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11849          }
11850
11851          if(stmt.forStmt.check)
11852             ProcessStatement(stmt.forStmt.check);
11853          if(stmt.forStmt.increment)
11854          {
11855             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11856                ProcessExpressionType(exp);
11857          }
11858
11859          if(stmt.forStmt.stmt)
11860             ProcessStatement(stmt.forStmt.stmt);
11861          break;
11862       }
11863       case forEachStmt:
11864       {
11865          Identifier id = stmt.forEachStmt.id;
11866          OldList * exp = stmt.forEachStmt.exp;
11867          OldList * filter = stmt.forEachStmt.filter;
11868          Statement block = stmt.forEachStmt.stmt;
11869          char iteratorType[1024];
11870          Type source;
11871          Expression e;
11872          bool isBuiltin = exp && exp->last &&
11873             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11874               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11875          Expression arrayExp;
11876          const char * typeString = null;
11877          int builtinCount = 0;
11878
11879          for(e = exp ? exp->first : null; e; e = e.next)
11880          {
11881             if(!e.next)
11882             {
11883                FreeType(e.destType);
11884                e.destType = ProcessTypeString("Container", false);
11885             }
11886             if(!isBuiltin || e.next)
11887                ProcessExpressionType(e);
11888          }
11889
11890          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11891          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11892             eClass_IsDerived(source._class.registered, containerClass)))
11893          {
11894             Class _class = source ? source._class.registered : null;
11895             Symbol symbol;
11896             Expression expIt = null;
11897             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
11898             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11899             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11900             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11901             stmt.type = compoundStmt;
11902
11903             stmt.compound.context = Context { };
11904             stmt.compound.context.parent = curContext;
11905             curContext = stmt.compound.context;
11906
11907             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11908             {
11909                Class mapClass = eSystem_FindClass(privateModule, "Map");
11910                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11911                isCustomAVLTree = true;
11912                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
11913                   isAVLTree = true;
11914                else */if(eClass_IsDerived(source._class.registered, mapClass))
11915                   isMap = true;
11916             }
11917             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11918             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11919             {
11920                Class listClass = eSystem_FindClass(privateModule, "List");
11921                isLinkList = true;
11922                isList = eClass_IsDerived(source._class.registered, listClass);
11923             }
11924
11925             if(isArray)
11926             {
11927                Declarator decl;
11928                OldList * specs = MkList();
11929                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11930                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11931                stmt.compound.declarations = MkListOne(
11932                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11933                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11934                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11935                      MkInitializerAssignment(MkExpBrackets(exp))))));
11936             }
11937             else if(isBuiltin)
11938             {
11939                Type type = null;
11940                char typeStringBuf[1024];
11941
11942                // TODO: Merge this code?
11943                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11944                if(((Expression)exp->last).type == castExp)
11945                {
11946                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11947                   if(typeName)
11948                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11949                }
11950
11951                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11952                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11953                   arrayExp.destType._class.registered.templateArgs)
11954                {
11955                   Class templateClass = arrayExp.destType._class.registered;
11956                   typeString = templateClass.templateArgs[2].dataTypeString;
11957                }
11958                else if(arrayExp.list)
11959                {
11960                   // Guess type from expressions in the array
11961                   Expression e;
11962                   for(e = arrayExp.list->first; e; e = e.next)
11963                   {
11964                      ProcessExpressionType(e);
11965                      if(e.expType)
11966                      {
11967                         if(!type) { type = e.expType; type.refCount++; }
11968                         else
11969                         {
11970                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11971                            if(!MatchTypeExpression(e, type, null, false, true))
11972                            {
11973                               FreeType(type);
11974                               type = e.expType;
11975                               e.expType = null;
11976
11977                               e = arrayExp.list->first;
11978                               ProcessExpressionType(e);
11979                               if(e.expType)
11980                               {
11981                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11982                                  if(!MatchTypeExpression(e, type, null, false, true))
11983                                  {
11984                                     FreeType(e.expType);
11985                                     e.expType = null;
11986                                     FreeType(type);
11987                                     type = null;
11988                                     break;
11989                                  }
11990                               }
11991                            }
11992                         }
11993                         if(e.expType)
11994                         {
11995                            FreeType(e.expType);
11996                            e.expType = null;
11997                         }
11998                      }
11999                   }
12000                   if(type)
12001                   {
12002                      typeStringBuf[0] = '\0';
12003                      PrintType(type, typeStringBuf, false, true);
12004                      typeString = typeStringBuf;
12005                      FreeType(type);
12006                   }
12007                }
12008                if(typeString)
12009                {
12010                   OldList * initializers = MkList();
12011                   Declarator decl;
12012                   OldList * specs = MkList();
12013                   if(arrayExp.list)
12014                   {
12015                      Expression e;
12016
12017                      builtinCount = arrayExp.list->count;
12018                      type = ProcessTypeString(typeString, false);
12019                      while((e = arrayExp.list->first))
12020                      {
12021                         arrayExp.list->Remove(e);
12022                         e.destType = type;
12023                         type.refCount++;
12024                         ProcessExpressionType(e);
12025                         ListAdd(initializers, MkInitializerAssignment(e));
12026                      }
12027                      FreeType(type);
12028                      delete arrayExp.list;
12029                   }
12030                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12031                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12032                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12033
12034                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12035                      PlugDeclarator(
12036                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12037                         ), MkInitializerList(initializers)))));
12038                   FreeList(exp, FreeExpression);
12039                }
12040                else
12041                {
12042                   arrayExp.expType = ProcessTypeString("Container", false);
12043                   Compiler_Error($"Couldn't determine type of array elements\n");
12044                }
12045
12046                /*
12047                Declarator decl;
12048                OldList * specs = MkList();
12049
12050                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12051                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12052                stmt.compound.declarations = MkListOne(
12053                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12054                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12055                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12056                      MkInitializerAssignment(MkExpBrackets(exp))))));
12057                */
12058             }
12059             else if(isLinkList && !isList)
12060             {
12061                Declarator decl;
12062                OldList * specs = MkList();
12063                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12064                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12065                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12066                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12067                      MkInitializerAssignment(MkExpBrackets(exp))))));
12068             }
12069             /*else if(isCustomAVLTree)
12070             {
12071                Declarator decl;
12072                OldList * specs = MkList();
12073                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12074                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12075                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12076                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12077                      MkInitializerAssignment(MkExpBrackets(exp))))));
12078             }*/
12079             else if(_class.templateArgs)
12080             {
12081                if(isMap)
12082                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12083                else
12084                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12085
12086                stmt.compound.declarations = MkListOne(
12087                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12088                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12089                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12090             }
12091             symbol = FindSymbol(id.string, curContext, curContext, false, false);
12092
12093             if(block)
12094             {
12095                // Reparent sub-contexts in this statement
12096                switch(block.type)
12097                {
12098                   case compoundStmt:
12099                      if(block.compound.context)
12100                         block.compound.context.parent = stmt.compound.context;
12101                      break;
12102                   case ifStmt:
12103                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12104                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12105                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12106                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12107                      break;
12108                   case switchStmt:
12109                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12110                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12111                      break;
12112                   case whileStmt:
12113                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12114                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12115                      break;
12116                   case doWhileStmt:
12117                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12118                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12119                      break;
12120                   case forStmt:
12121                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12122                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12123                      break;
12124                   case forEachStmt:
12125                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12126                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12127                      break;
12128                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12129                   case labeledStmt:
12130                   case caseStmt
12131                   case expressionStmt:
12132                   case gotoStmt:
12133                   case continueStmt:
12134                   case breakStmt
12135                   case returnStmt:
12136                   case asmStmt:
12137                   case badDeclarationStmt:
12138                   case fireWatchersStmt:
12139                   case stopWatchingStmt:
12140                   case watchStmt:
12141                   */
12142                }
12143             }
12144             if(filter)
12145             {
12146                block = MkIfStmt(filter, block, null);
12147             }
12148             if(isArray)
12149             {
12150                stmt.compound.statements = MkListOne(MkForStmt(
12151                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12152                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12153                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12154                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12155                   block));
12156               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12157               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12158               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12159             }
12160             else if(isBuiltin)
12161             {
12162                char count[128];
12163                //OldList * specs = MkList();
12164                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12165
12166                sprintf(count, "%d", builtinCount);
12167
12168                stmt.compound.statements = MkListOne(MkForStmt(
12169                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12170                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12171                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12172                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12173                   block));
12174
12175                /*
12176                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12177                stmt.compound.statements = MkListOne(MkForStmt(
12178                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12179                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12180                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12181                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12182                   block));
12183               */
12184               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12185               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12186               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12187             }
12188             else if(isLinkList && !isList)
12189             {
12190                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12191                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12192                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12193                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12194                {
12195                   stmt.compound.statements = MkListOne(MkForStmt(
12196                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12197                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12198                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12199                      block));
12200                }
12201                else
12202                {
12203                   OldList * specs = MkList();
12204                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12205                   stmt.compound.statements = MkListOne(MkForStmt(
12206                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12207                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12208                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12209                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12210                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12211                      block));
12212                }
12213                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12214                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12215                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12216             }
12217             /*else if(isCustomAVLTree)
12218             {
12219                stmt.compound.statements = MkListOne(MkForStmt(
12220                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12221                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12222                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12223                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12224                   block));
12225
12226                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12227                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12228                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12229             }*/
12230             else
12231             {
12232                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12233                   MkIdentifier("Next")), null)), block));
12234             }
12235             ProcessExpressionType(expIt);
12236             if(stmt.compound.declarations->first)
12237                ProcessDeclaration(stmt.compound.declarations->first);
12238
12239             if(symbol)
12240                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12241
12242             ProcessStatement(stmt);
12243             curContext = stmt.compound.context.parent;
12244             break;
12245          }
12246          else
12247          {
12248             Compiler_Error($"Expression is not a container\n");
12249          }
12250          break;
12251       }
12252       case gotoStmt:
12253          break;
12254       case continueStmt:
12255          break;
12256       case breakStmt:
12257          break;
12258       case returnStmt:
12259       {
12260          Expression exp;
12261          if(stmt.expressions)
12262          {
12263             for(exp = stmt.expressions->first; exp; exp = exp.next)
12264             {
12265                if(!exp.next)
12266                {
12267                   if(curFunction && !curFunction.type)
12268                      curFunction.type = ProcessType(
12269                         curFunction.specifiers, curFunction.declarator);
12270                   FreeType(exp.destType);
12271                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12272                   if(exp.destType) exp.destType.refCount++;
12273                }
12274                ProcessExpressionType(exp);
12275             }
12276          }
12277          break;
12278       }
12279       case badDeclarationStmt:
12280       {
12281          ProcessDeclaration(stmt.decl);
12282          break;
12283       }
12284       case asmStmt:
12285       {
12286          AsmField field;
12287          if(stmt.asmStmt.inputFields)
12288          {
12289             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12290                if(field.expression)
12291                   ProcessExpressionType(field.expression);
12292          }
12293          if(stmt.asmStmt.outputFields)
12294          {
12295             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12296                if(field.expression)
12297                   ProcessExpressionType(field.expression);
12298          }
12299          if(stmt.asmStmt.clobberedFields)
12300          {
12301             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12302             {
12303                if(field.expression)
12304                   ProcessExpressionType(field.expression);
12305             }
12306          }
12307          break;
12308       }
12309       case watchStmt:
12310       {
12311          PropertyWatch propWatch;
12312          OldList * watches = stmt._watch.watches;
12313          Expression object = stmt._watch.object;
12314          Expression watcher = stmt._watch.watcher;
12315          if(watcher)
12316             ProcessExpressionType(watcher);
12317          if(object)
12318             ProcessExpressionType(object);
12319
12320          if(inCompiler)
12321          {
12322             if(watcher || thisClass)
12323             {
12324                External external = curExternal;
12325                Context context = curContext;
12326
12327                stmt.type = expressionStmt;
12328                stmt.expressions = MkList();
12329
12330                curExternal = external.prev;
12331
12332                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12333                {
12334                   ClassFunction func;
12335                   char watcherName[1024];
12336                   Class watcherClass = watcher ?
12337                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12338                   External createdExternal;
12339
12340                   // Create a declaration above
12341                   External externalDecl = MkExternalDeclaration(null);
12342                   ast->Insert(curExternal.prev, externalDecl);
12343
12344                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12345                   if(propWatch.deleteWatch)
12346                      strcat(watcherName, "_delete");
12347                   else
12348                   {
12349                      Identifier propID;
12350                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12351                      {
12352                         strcat(watcherName, "_");
12353                         strcat(watcherName, propID.string);
12354                      }
12355                   }
12356
12357                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12358                   {
12359                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
12360                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12361                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
12362                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12363                      ProcessClassFunctionBody(func, propWatch.compound);
12364                      propWatch.compound = null;
12365
12366                      //afterExternal = afterExternal ? afterExternal : curExternal;
12367
12368                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
12369                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12370                      // TESTING THIS...
12371                      createdExternal.symbol.idCode = external.symbol.idCode;
12372
12373                      curExternal = createdExternal;
12374                      ProcessFunction(createdExternal.function);
12375
12376
12377                      // Create a declaration above
12378                      {
12379                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
12380                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
12381                         externalDecl.declaration = decl;
12382                         if(decl.symbol && !decl.symbol.pointerExternal)
12383                            decl.symbol.pointerExternal = externalDecl;
12384                      }
12385
12386                      if(propWatch.deleteWatch)
12387                      {
12388                         OldList * args = MkList();
12389                         ListAdd(args, CopyExpression(object));
12390                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12391                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12392                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12393                      }
12394                      else
12395                      {
12396                         Class _class = object.expType._class.registered;
12397                         Identifier propID;
12398
12399                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12400                         {
12401                            char propName[1024];
12402                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12403                            if(prop)
12404                            {
12405                               char getName[1024], setName[1024];
12406                               OldList * args = MkList();
12407
12408                               DeclareProperty(prop, setName, getName);
12409
12410                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12411                               strcpy(propName, "__ecereProp_");
12412                               FullClassNameCat(propName, prop._class.fullName, false);
12413                               strcat(propName, "_");
12414                               // strcat(propName, prop.name);
12415                               FullClassNameCat(propName, prop.name, true);
12416
12417                               ListAdd(args, CopyExpression(object));
12418                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12419                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12420                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12421
12422                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12423                            }
12424                            else
12425                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12426                         }
12427                      }
12428                   }
12429                   else
12430                      Compiler_Error($"Invalid watched object\n");
12431                }
12432
12433                curExternal = external;
12434                curContext = context;
12435
12436                if(watcher)
12437                   FreeExpression(watcher);
12438                if(object)
12439                   FreeExpression(object);
12440                FreeList(watches, FreePropertyWatch);
12441             }
12442             else
12443                Compiler_Error($"No observer specified and not inside a _class\n");
12444          }
12445          else
12446          {
12447             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12448             {
12449                ProcessStatement(propWatch.compound);
12450             }
12451
12452          }
12453          break;
12454       }
12455       case fireWatchersStmt:
12456       {
12457          OldList * watches = stmt._watch.watches;
12458          Expression object = stmt._watch.object;
12459          Class _class;
12460          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12461          // printf("%X\n", watches);
12462          // printf("%X\n", stmt._watch.watches);
12463          if(object)
12464             ProcessExpressionType(object);
12465
12466          if(inCompiler)
12467          {
12468             _class = object ?
12469                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12470
12471             if(_class)
12472             {
12473                Identifier propID;
12474
12475                stmt.type = expressionStmt;
12476                stmt.expressions = MkList();
12477
12478                // Check if we're inside a property set
12479                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12480                {
12481                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12482                }
12483                else if(!watches)
12484                {
12485                   //Compiler_Error($"No property specified and not inside a property set\n");
12486                }
12487                if(watches)
12488                {
12489                   for(propID = watches->first; propID; propID = propID.next)
12490                   {
12491                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12492                      if(prop)
12493                      {
12494                         CreateFireWatcher(prop, object, stmt);
12495                      }
12496                      else
12497                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12498                   }
12499                }
12500                else
12501                {
12502                   // Fire all properties!
12503                   Property prop;
12504                   Class base;
12505                   for(base = _class; base; base = base.base)
12506                   {
12507                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12508                      {
12509                         if(prop.isProperty && prop.isWatchable)
12510                         {
12511                            CreateFireWatcher(prop, object, stmt);
12512                         }
12513                      }
12514                   }
12515                }
12516
12517                if(object)
12518                   FreeExpression(object);
12519                FreeList(watches, FreeIdentifier);
12520             }
12521             else
12522                Compiler_Error($"Invalid object specified and not inside a class\n");
12523          }
12524          break;
12525       }
12526       case stopWatchingStmt:
12527       {
12528          OldList * watches = stmt._watch.watches;
12529          Expression object = stmt._watch.object;
12530          Expression watcher = stmt._watch.watcher;
12531          Class _class;
12532          if(object)
12533             ProcessExpressionType(object);
12534          if(watcher)
12535             ProcessExpressionType(watcher);
12536          if(inCompiler)
12537          {
12538             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12539
12540             if(watcher || thisClass)
12541             {
12542                if(_class)
12543                {
12544                   Identifier propID;
12545
12546                   stmt.type = expressionStmt;
12547                   stmt.expressions = MkList();
12548
12549                   if(!watches)
12550                   {
12551                      OldList * args;
12552                      // eInstance_StopWatching(object, null, watcher);
12553                      args = MkList();
12554                      ListAdd(args, CopyExpression(object));
12555                      ListAdd(args, MkExpConstant("0"));
12556                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12557                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12558                   }
12559                   else
12560                   {
12561                      for(propID = watches->first; propID; propID = propID.next)
12562                      {
12563                         char propName[1024];
12564                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12565                         if(prop)
12566                         {
12567                            char getName[1024], setName[1024];
12568                            OldList * args = MkList();
12569
12570                            DeclareProperty(prop, setName, getName);
12571
12572                            // eInstance_StopWatching(object, prop, watcher);
12573                            strcpy(propName, "__ecereProp_");
12574                            FullClassNameCat(propName, prop._class.fullName, false);
12575                            strcat(propName, "_");
12576                            // strcat(propName, prop.name);
12577                            FullClassNameCat(propName, prop.name, true);
12578                            //MangleClassName(propName);
12579
12580                            ListAdd(args, CopyExpression(object));
12581                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12582                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12583                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12584                         }
12585                         else
12586                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12587                      }
12588                   }
12589
12590                   if(object)
12591                      FreeExpression(object);
12592                   if(watcher)
12593                      FreeExpression(watcher);
12594                   FreeList(watches, FreeIdentifier);
12595                }
12596                else
12597                   Compiler_Error($"Invalid object specified and not inside a class\n");
12598             }
12599             else
12600                Compiler_Error($"No observer specified and not inside a class\n");
12601          }
12602          break;
12603       }
12604    }
12605 }
12606
12607 static void ProcessFunction(FunctionDefinition function)
12608 {
12609    Identifier id = GetDeclId(function.declarator);
12610    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12611    Type type = symbol ? symbol.type : null;
12612    Class oldThisClass = thisClass;
12613    Context oldTopContext = topContext;
12614
12615    yylloc = function.loc;
12616    // Process thisClass
12617
12618    if(type && type.thisClass)
12619    {
12620       Symbol classSym = type.thisClass;
12621       Class _class = type.thisClass.registered;
12622       char className[1024];
12623       char structName[1024];
12624       Declarator funcDecl;
12625       Symbol thisSymbol;
12626
12627       bool typedObject = false;
12628
12629       if(_class && !_class.base)
12630       {
12631          _class = currentClass;
12632          if(_class && !_class.symbol)
12633             _class.symbol = FindClass(_class.fullName);
12634          classSym = _class ? _class.symbol : null;
12635          typedObject = true;
12636       }
12637
12638       thisClass = _class;
12639
12640       if(inCompiler && _class)
12641       {
12642          if(type.kind == functionType)
12643          {
12644             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12645             {
12646                //TypeName param = symbol.type.params.first;
12647                Type param = symbol.type.params.first;
12648                symbol.type.params.Remove(param);
12649                //FreeTypeName(param);
12650                FreeType(param);
12651             }
12652             if(type.classObjectType != classPointer)
12653             {
12654                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12655                symbol.type.staticMethod = true;
12656                symbol.type.thisClass = null;
12657
12658                // HIGH DANGER: VERIFYING THIS...
12659                symbol.type.extraParam = false;
12660             }
12661          }
12662
12663          strcpy(className, "__ecereClass_");
12664          FullClassNameCat(className, _class.fullName, true);
12665
12666          //MangleClassName(className);
12667
12668          structName[0] = 0;
12669          FullClassNameCat(structName, _class.fullName, false);
12670
12671          // [class] this
12672
12673
12674          funcDecl = GetFuncDecl(function.declarator);
12675          if(funcDecl)
12676          {
12677             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12678             {
12679                TypeName param = funcDecl.function.parameters->first;
12680                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12681                {
12682                   funcDecl.function.parameters->Remove(param);
12683                   FreeTypeName(param);
12684                }
12685             }
12686
12687             // DANGER: Watch for this... Check if it's a Conversion?
12688             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12689
12690             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12691             if(!function.propertyNoThis)
12692             {
12693                TypeName thisParam;
12694
12695                if(type.classObjectType != classPointer)
12696                {
12697                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12698                   if(!funcDecl.function.parameters)
12699                      funcDecl.function.parameters = MkList();
12700                   funcDecl.function.parameters->Insert(null, thisParam);
12701                }
12702
12703                if(typedObject)
12704                {
12705                   if(type.classObjectType != classPointer)
12706                   {
12707                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12708                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12709                   }
12710
12711                   thisParam = TypeName
12712                   {
12713                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12714                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12715                   };
12716                   funcDecl.function.parameters->Insert(null, thisParam);
12717                }
12718             }
12719          }
12720
12721          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12722          {
12723             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12724             funcDecl = GetFuncDecl(initDecl.declarator);
12725             if(funcDecl)
12726             {
12727                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12728                {
12729                   TypeName param = funcDecl.function.parameters->first;
12730                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12731                   {
12732                      funcDecl.function.parameters->Remove(param);
12733                      FreeTypeName(param);
12734                   }
12735                }
12736
12737                if(type.classObjectType != classPointer)
12738                {
12739                   // DANGER: Watch for this... Check if it's a Conversion?
12740                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12741                   {
12742                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12743
12744                      if(!funcDecl.function.parameters)
12745                         funcDecl.function.parameters = MkList();
12746                      funcDecl.function.parameters->Insert(null, thisParam);
12747                   }
12748                }
12749             }
12750          }
12751       }
12752
12753       // Add this to the context
12754       if(function.body)
12755       {
12756          if(type.classObjectType != classPointer)
12757          {
12758             thisSymbol = Symbol
12759             {
12760                string = CopyString("this");
12761                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12762             };
12763             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12764
12765             if(typedObject && thisSymbol.type)
12766             {
12767                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12768                thisSymbol.type.byReference = type.byReference;
12769                thisSymbol.type.typedByReference = type.byReference;
12770                /*
12771                thisSymbol = Symbol { string = CopyString("class") };
12772                function.body.compound.context.symbols.Add(thisSymbol);
12773                */
12774             }
12775          }
12776       }
12777
12778       // Pointer to class data
12779
12780       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12781       {
12782          DataMember member = null;
12783          {
12784             Class base;
12785             for(base = _class; base && base.type != systemClass; base = base.next)
12786             {
12787                for(member = base.membersAndProperties.first; member; member = member.next)
12788                   if(!member.isProperty)
12789                      break;
12790                if(member)
12791                   break;
12792             }
12793          }
12794          for(member = _class.membersAndProperties.first; member; member = member.next)
12795             if(!member.isProperty)
12796                break;
12797          if(member)
12798          {
12799             char pointerName[1024];
12800
12801             Declaration decl;
12802             Initializer initializer;
12803             Expression exp, bytePtr;
12804
12805             strcpy(pointerName, "__ecerePointer_");
12806             FullClassNameCat(pointerName, _class.fullName, false);
12807             {
12808                char className[1024];
12809                strcpy(className, "__ecereClass_");
12810                FullClassNameCat(className, classSym.string, true);
12811                //MangleClassName(className);
12812
12813                // Testing This
12814                DeclareClass(classSym, className);
12815             }
12816
12817             // ((byte *) this)
12818             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12819
12820             if(_class.fixed)
12821             {
12822                char string[256];
12823                sprintf(string, "%d", _class.offset);
12824                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12825             }
12826             else
12827             {
12828                // ([bytePtr] + [className]->offset)
12829                exp = QBrackets(MkExpOp(bytePtr, '+',
12830                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12831             }
12832
12833             // (this ? [exp] : 0)
12834             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12835             exp.expType = Type
12836             {
12837                refCount = 1;
12838                kind = pointerType;
12839                type = Type { refCount = 1, kind = voidType };
12840             };
12841
12842             if(function.body)
12843             {
12844                yylloc = function.body.loc;
12845                // ([structName] *) [exp]
12846                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12847                initializer = MkInitializerAssignment(
12848                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12849
12850                // [structName] * [pointerName] = [initializer];
12851                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12852
12853                {
12854                   Context prevContext = curContext;
12855                   curContext = function.body.compound.context;
12856
12857                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12858                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12859
12860                   curContext = prevContext;
12861                }
12862
12863                // WHY?
12864                decl.symbol = null;
12865
12866                if(!function.body.compound.declarations)
12867                   function.body.compound.declarations = MkList();
12868                function.body.compound.declarations->Insert(null, decl);
12869             }
12870          }
12871       }
12872
12873
12874       // Loop through the function and replace undeclared identifiers
12875       // which are a member of the class (methods, properties or data)
12876       // by "this.[member]"
12877    }
12878    else
12879       thisClass = null;
12880
12881    if(id)
12882    {
12883       FreeSpecifier(id._class);
12884       id._class = null;
12885
12886       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12887       {
12888          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12889          id = GetDeclId(initDecl.declarator);
12890
12891          FreeSpecifier(id._class);
12892          id._class = null;
12893       }
12894    }
12895    if(function.body)
12896       topContext = function.body.compound.context;
12897    {
12898       FunctionDefinition oldFunction = curFunction;
12899       curFunction = function;
12900       if(function.body)
12901          ProcessStatement(function.body);
12902
12903       // If this is a property set and no firewatchers has been done yet, add one here
12904       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12905       {
12906          Statement prevCompound = curCompound;
12907          Context prevContext = curContext;
12908
12909          Statement fireWatchers = MkFireWatchersStmt(null, null);
12910          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12911          ListAdd(function.body.compound.statements, fireWatchers);
12912
12913          curCompound = function.body;
12914          curContext = function.body.compound.context;
12915
12916          ProcessStatement(fireWatchers);
12917
12918          curContext = prevContext;
12919          curCompound = prevCompound;
12920
12921       }
12922
12923       curFunction = oldFunction;
12924    }
12925
12926    if(function.declarator)
12927    {
12928       ProcessDeclarator(function.declarator);
12929    }
12930
12931    topContext = oldTopContext;
12932    thisClass = oldThisClass;
12933 }
12934
12935 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12936 static void ProcessClass(OldList definitions, Symbol symbol)
12937 {
12938    ClassDef def;
12939    External external = curExternal;
12940    Class regClass = symbol ? symbol.registered : null;
12941
12942    // Process all functions
12943    for(def = definitions.first; def; def = def.next)
12944    {
12945       if(def.type == functionClassDef)
12946       {
12947          if(def.function.declarator)
12948             curExternal = def.function.declarator.symbol.pointerExternal;
12949          else
12950             curExternal = external;
12951
12952          ProcessFunction((FunctionDefinition)def.function);
12953       }
12954       else if(def.type == declarationClassDef)
12955       {
12956          if(def.decl.type == instDeclaration)
12957          {
12958             thisClass = regClass;
12959             ProcessInstantiationType(def.decl.inst);
12960             thisClass = null;
12961          }
12962          // Testing this
12963          else
12964          {
12965             Class backThisClass = thisClass;
12966             if(regClass) thisClass = regClass;
12967             ProcessDeclaration(def.decl);
12968             thisClass = backThisClass;
12969          }
12970       }
12971       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12972       {
12973          MemberInit defProperty;
12974
12975          // Add this to the context
12976          Symbol thisSymbol = Symbol
12977          {
12978             string = CopyString("this");
12979             type = regClass ? MkClassType(regClass.fullName) : null;
12980          };
12981          globalContext.symbols.Add((BTNode)thisSymbol);
12982
12983          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12984          {
12985             thisClass = regClass;
12986             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12987             thisClass = null;
12988          }
12989
12990          globalContext.symbols.Remove((BTNode)thisSymbol);
12991          FreeSymbol(thisSymbol);
12992       }
12993       else if(def.type == propertyClassDef && def.propertyDef)
12994       {
12995          PropertyDef prop = def.propertyDef;
12996
12997          // Add this to the context
12998          /*
12999          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
13000          globalContext.symbols.Add(thisSymbol);
13001          */
13002
13003          thisClass = regClass;
13004          if(prop.setStmt)
13005          {
13006             if(regClass)
13007             {
13008                Symbol thisSymbol
13009                {
13010                   string = CopyString("this");
13011                   type = MkClassType(regClass.fullName);
13012                };
13013                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13014             }
13015
13016             curExternal = prop.symbol ? prop.symbol.externalSet : null;
13017             ProcessStatement(prop.setStmt);
13018          }
13019          if(prop.getStmt)
13020          {
13021             if(regClass)
13022             {
13023                Symbol thisSymbol
13024                {
13025                   string = CopyString("this");
13026                   type = MkClassType(regClass.fullName);
13027                };
13028                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13029             }
13030
13031             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13032             ProcessStatement(prop.getStmt);
13033          }
13034          if(prop.issetStmt)
13035          {
13036             if(regClass)
13037             {
13038                Symbol thisSymbol
13039                {
13040                   string = CopyString("this");
13041                   type = MkClassType(regClass.fullName);
13042                };
13043                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13044             }
13045
13046             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13047             ProcessStatement(prop.issetStmt);
13048          }
13049
13050          thisClass = null;
13051
13052          /*
13053          globalContext.symbols.Remove(thisSymbol);
13054          FreeSymbol(thisSymbol);
13055          */
13056       }
13057       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13058       {
13059          PropertyWatch propertyWatch = def.propertyWatch;
13060
13061          thisClass = regClass;
13062          if(propertyWatch.compound)
13063          {
13064             Symbol thisSymbol
13065             {
13066                string = CopyString("this");
13067                type = regClass ? MkClassType(regClass.fullName) : null;
13068             };
13069
13070             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13071
13072             curExternal = null;
13073             ProcessStatement(propertyWatch.compound);
13074          }
13075          thisClass = null;
13076       }
13077    }
13078 }
13079
13080 void DeclareFunctionUtil(const String s)
13081 {
13082    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13083    if(function)
13084    {
13085       char name[1024];
13086       name[0] = 0;
13087       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13088          strcpy(name, "__ecereFunction_");
13089       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13090       DeclareFunction(function, name);
13091    }
13092 }
13093
13094 void ComputeDataTypes()
13095 {
13096    External external;
13097    External temp { };
13098    External after = null;
13099
13100    currentClass = null;
13101
13102    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13103
13104    for(external = ast->first; external; external = external.next)
13105    {
13106       if(external.type == declarationExternal)
13107       {
13108          Declaration decl = external.declaration;
13109          if(decl)
13110          {
13111             OldList * decls = decl.declarators;
13112             if(decls)
13113             {
13114                InitDeclarator initDecl = decls->first;
13115                if(initDecl)
13116                {
13117                   Declarator declarator = initDecl.declarator;
13118                   if(declarator && declarator.type == identifierDeclarator)
13119                   {
13120                      Identifier id = declarator.identifier;
13121                      if(id && id.string)
13122                      {
13123                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
13124                         {
13125                            external.symbol.id = -1001, external.symbol.idCode = -1001;
13126                            after = external;
13127                         }
13128                      }
13129                   }
13130                }
13131             }
13132          }
13133        }
13134    }
13135
13136    {
13137       // Workaround until we have proper toposort for declarations reordering
13138       External e = MkExternalDeclaration(MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Instance"), null)), null));
13139       ast->Insert(after, e);
13140       after = e;
13141    }
13142
13143    temp.symbol = Symbol { id = -1000, idCode = -1000 };
13144    ast->Insert(after, temp);
13145    curExternal = temp;
13146
13147    DeclareFunctionUtil("eSystem_New");
13148    DeclareFunctionUtil("eSystem_New0");
13149    DeclareFunctionUtil("eSystem_Renew");
13150    DeclareFunctionUtil("eSystem_Renew0");
13151    DeclareFunctionUtil("eSystem_Delete");
13152    DeclareFunctionUtil("eClass_GetProperty");
13153    DeclareFunctionUtil("eClass_SetProperty");
13154    DeclareFunctionUtil("eInstance_FireSelfWatchers");
13155    DeclareFunctionUtil("eInstance_SetMethod");
13156    DeclareFunctionUtil("eInstance_IncRef");
13157    DeclareFunctionUtil("eInstance_StopWatching");
13158    DeclareFunctionUtil("eInstance_Watch");
13159    DeclareFunctionUtil("eInstance_FireWatchers");
13160
13161    DeclareStruct("ecere::com::Class", false);
13162    DeclareStruct("ecere::com::Instance", false);
13163    DeclareStruct("ecere::com::Property", false);
13164    DeclareStruct("ecere::com::DataMember", false);
13165    DeclareStruct("ecere::com::Method", false);
13166    DeclareStruct("ecere::com::SerialBuffer", false);
13167    DeclareStruct("ecere::com::ClassTemplateArgument", false);
13168
13169    ast->Remove(temp);
13170
13171    for(external = ast->first; external; external = external.next)
13172    {
13173       afterExternal = curExternal = external;
13174       if(external.type == functionExternal)
13175       {
13176          currentClass = external.function._class;
13177          ProcessFunction(external.function);
13178       }
13179       // There shouldn't be any _class member access here anyways...
13180       else if(external.type == declarationExternal)
13181       {
13182          currentClass = null;
13183          if(external.declaration)
13184             ProcessDeclaration(external.declaration);
13185       }
13186       else if(external.type == classExternal)
13187       {
13188          ClassDefinition _class = external._class;
13189          currentClass = external.symbol.registered;
13190          if(_class.definitions)
13191          {
13192             ProcessClass(_class.definitions, _class.symbol);
13193          }
13194          if(inCompiler)
13195          {
13196             // Free class data...
13197             ast->Remove(external);
13198             delete external;
13199          }
13200       }
13201       else if(external.type == nameSpaceExternal)
13202       {
13203          thisNameSpace = external.id.string;
13204       }
13205    }
13206    currentClass = null;
13207    thisNameSpace = null;
13208    curExternal = null;
13209
13210    delete temp.symbol;
13211    delete temp;
13212 }