compiler/libec: Fixed memory leaks parsing Window.ec
[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
51       if(exp)
52          OutputExpression(exp, f);
53       f.Seek(0, start);
54       count = strlen(string);
55       count += f.Read(string + count, 1, 1023);
56       string[count] = '\0';
57       delete f;
58    }
59 }
60
61 Type ProcessTemplateParameterType(TemplateParameter param)
62 {
63    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
64    {
65       // TOFIX: Will need to free this Type
66       if(!param.baseType)
67       {
68          if(param.dataTypeString)
69             param.baseType = ProcessTypeString(param.dataTypeString, false);
70          else
71             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
72       }
73       return param.baseType;
74    }
75    return null;
76 }
77
78 bool NeedCast(Type type1, Type type2)
79 {
80    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
81
82    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
83    {
84       return false;
85    }
86
87    if(type1.kind == type2.kind)
88    {
89       switch(type1.kind)
90       {
91          case _BoolType:
92          case charType:
93          case shortType:
94          case intType:
95          case int64Type:
96          case intPtrType:
97          case intSizeType:
98             if(type1.passAsTemplate && !type2.passAsTemplate)
99                return true;
100             return type1.isSigned != type2.isSigned;
101          case classType:
102             return type1._class != type2._class;
103          case pointerType:
104             return NeedCast(type1.type, type2.type);
105          default:
106             return true; //false; ????
107       }
108    }
109    return true;
110 }
111
112 static void ReplaceClassMembers(Expression exp, Class _class)
113 {
114    if(exp.type == identifierExp && exp.identifier)
115    {
116       Identifier id = exp.identifier;
117       Context ctx;
118       Symbol symbol = null;
119       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
120       {
121          // First, check if the identifier is declared inside the function
122          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
123          {
124             symbol = (Symbol)ctx.symbols.FindString(id.string);
125             if(symbol) break;
126          }
127       }
128
129       // If it is not, check if it is a member of the _class
130       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
131       {
132          Property prop = eClass_FindProperty(_class, id.string, privateModule);
133          Method method = null;
134          DataMember member = null;
135          ClassProperty classProp = null;
136          if(!prop)
137          {
138             method = eClass_FindMethod(_class, id.string, privateModule);
139          }
140          if(!prop && !method)
141             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
142          if(!prop && !method && !member)
143          {
144             classProp = eClass_FindClassProperty(_class, id.string);
145          }
146          if(prop || method || member || classProp)
147          {
148             // Replace by this.[member]
149             exp.type = memberExp;
150             exp.member.member = id;
151             exp.member.memberType = unresolvedMember;
152             exp.member.exp = QMkExpId("this");
153             //exp.member.exp.loc = exp.loc;
154             exp.addedThis = true;
155          }
156          else if(_class && _class.templateParams.first)
157          {
158             Class sClass;
159             for(sClass = _class; sClass; sClass = sClass.base)
160             {
161                if(sClass.templateParams.first)
162                {
163                   ClassTemplateParameter param;
164                   for(param = sClass.templateParams.first; param; param = param.next)
165                   {
166                      if(param.type == expression && !strcmp(param.name, id.string))
167                      {
168                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
169
170                         if(argExp)
171                         {
172                            Declarator decl;
173                            OldList * specs = MkList();
174
175                            FreeIdentifier(exp.member.member);
176
177                            ProcessExpressionType(argExp);
178
179                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
180
181                            exp.expType = ProcessType(specs, decl);
182
183                            // *[expType] *[argExp]
184                            exp.type = bracketsExp;
185                            exp.list = MkListOne(MkExpOp(null, '*',
186                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
187                         }
188                      }
189                   }
190                }
191             }
192          }
193       }
194    }
195 }
196
197 ////////////////////////////////////////////////////////////////////////
198 // PRINTING ////////////////////////////////////////////////////////////
199 ////////////////////////////////////////////////////////////////////////
200
201 public char * PrintInt(int64 result)
202 {
203    char temp[100];
204    if(result > MAXINT64)
205       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
206    else
207       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
208    return CopyString(temp);
209 }
210
211 public char * PrintUInt(uint64 result)
212 {
213    char temp[100];
214    if(result > MAXDWORD)
215       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
216    else if(result > MAXINT)
217       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
218    else
219       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
220    return CopyString(temp);
221 }
222
223 public char * PrintInt64(int64 result)
224 {
225    char temp[100];
226    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
227    return CopyString(temp);
228 }
229
230 public char * PrintUInt64(uint64 result)
231 {
232    char temp[100];
233    if(result > MAXINT64)
234       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
235    else
236       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
237    return CopyString(temp);
238 }
239
240 public char * PrintHexUInt(uint64 result)
241 {
242    char temp[100];
243    if(result > MAXDWORD)
244       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
245    else
246       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
247    return CopyString(temp);
248 }
249
250 public char * PrintHexUInt64(uint64 result)
251 {
252    char temp[100];
253    if(result > MAXDWORD)
254       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
255    else
256       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
257    return CopyString(temp);
258 }
259
260 public char * PrintShort(short result)
261 {
262    char temp[100];
263    sprintf(temp, "%d", (unsigned short)result);
264    return CopyString(temp);
265 }
266
267 public char * PrintUShort(unsigned short result)
268 {
269    char temp[100];
270    if(result > 32767)
271       sprintf(temp, "0x%X", (int)result);
272    else
273       sprintf(temp, "%d", (int)result);
274    return CopyString(temp);
275 }
276
277 public char * PrintChar(char result)
278 {
279    char temp[100];
280    if(result > 0 && isprint(result))
281       sprintf(temp, "'%c'", result);
282    else if(result < 0)
283       sprintf(temp, "%d", (int)result);
284    else
285       //sprintf(temp, "%#X", result);
286       sprintf(temp, "0x%X", (unsigned char)result);
287    return CopyString(temp);
288 }
289
290 public char * PrintUChar(unsigned char result)
291 {
292    char temp[100];
293    sprintf(temp, "0x%X", result);
294    return CopyString(temp);
295 }
296
297 public char * PrintFloat(float result)
298 {
299    char temp[350];
300    sprintf(temp, "%.16ff", result);
301    return CopyString(temp);
302 }
303
304 public char * PrintDouble(double result)
305 {
306    char temp[350];
307    sprintf(temp, "%.16f", result);
308    return CopyString(temp);
309 }
310
311 ////////////////////////////////////////////////////////////////////////
312 ////////////////////////////////////////////////////////////////////////
313
314 //public Operand GetOperand(Expression exp);
315
316 #define GETVALUE(name, t) \
317    public bool Get##name(Expression exp, t * value2) \
318    {                                                        \
319       Operand op2 = GetOperand(exp);                        \
320       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
321       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
322       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
323       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
324       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
325       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
326       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
327       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
328       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
329       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
330       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
331       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
332       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
333       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
334       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
335       else                                                                          \
336          return false;                                                              \
337       return true;                                                                  \
338    }
339
340 // To help the deubugger currently not preprocessing...
341 #define HELP(x) x
342
343 GETVALUE(Int, HELP(int));
344 GETVALUE(UInt, HELP(unsigned int));
345 GETVALUE(Int64, HELP(int64));
346 GETVALUE(UInt64, HELP(uint64));
347 GETVALUE(IntPtr, HELP(intptr));
348 GETVALUE(UIntPtr, HELP(uintptr));
349 GETVALUE(IntSize, HELP(intsize));
350 GETVALUE(UIntSize, HELP(uintsize));
351 GETVALUE(Short, HELP(short));
352 GETVALUE(UShort, HELP(unsigned short));
353 GETVALUE(Char, HELP(char));
354 GETVALUE(UChar, HELP(unsigned char));
355 GETVALUE(Float, HELP(float));
356 GETVALUE(Double, HELP(double));
357
358 void ComputeExpression(Expression exp);
359
360 void ComputeClassMembers(Class _class, bool isMember)
361 {
362    DataMember member = isMember ? (DataMember) _class : null;
363    Context context = isMember ? null : SetupTemplatesContext(_class);
364    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
365                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
366    {
367       int c;
368       int unionMemberOffset = 0;
369       int bitFields = 0;
370
371       /*
372       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
373          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
374       */
375
376       if(member)
377       {
378          member.memberOffset = 0;
379          if(targetBits < sizeof(void *) * 8)
380             member.structAlignment = 0;
381       }
382       else if(targetBits < sizeof(void *) * 8)
383          _class.structAlignment = 0;
384
385       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
386       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
387          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
388
389       if(!member && _class.destructionWatchOffset)
390          _class.memberOffset += sizeof(OldList);
391
392       // To avoid reentrancy...
393       //_class.structSize = -1;
394
395       {
396          DataMember dataMember;
397          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
398          {
399             if(!dataMember.isProperty)
400             {
401                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
402                {
403                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
404                   /*if(!dataMember.dataType)
405                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
406                      */
407                }
408             }
409          }
410       }
411
412       {
413          DataMember dataMember;
414          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
415          {
416             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
417             {
418                if(!isMember && _class.type == bitClass && dataMember.dataType)
419                {
420                   BitMember bitMember = (BitMember) dataMember;
421                   uint64 mask = 0;
422                   int d;
423
424                   ComputeTypeSize(dataMember.dataType);
425
426                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
427                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
428
429                   _class.memberOffset = bitMember.pos + bitMember.size;
430                   for(d = 0; d<bitMember.size; d++)
431                   {
432                      if(d)
433                         mask <<= 1;
434                      mask |= 1;
435                   }
436                   bitMember.mask = mask << bitMember.pos;
437                }
438                else if(dataMember.type == normalMember && dataMember.dataType)
439                {
440                   int size;
441                   int alignment = 0;
442
443                   // Prevent infinite recursion
444                   if(dataMember.dataType.kind != classType ||
445                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
446                      _class.type != structClass)))
447                      ComputeTypeSize(dataMember.dataType);
448
449                   if(dataMember.dataType.bitFieldCount)
450                   {
451                      bitFields += dataMember.dataType.bitFieldCount;
452                      size = 0;
453                   }
454                   else
455                   {
456                      if(bitFields)
457                      {
458                         int size = (bitFields + 7) / 8;
459
460                         if(isMember)
461                         {
462                            // TESTING THIS PADDING CODE
463                            if(alignment)
464                            {
465                               member.structAlignment = Max(member.structAlignment, alignment);
466
467                               if(member.memberOffset % alignment)
468                                  member.memberOffset += alignment - (member.memberOffset % alignment);
469                            }
470
471                            dataMember.offset = member.memberOffset;
472                            if(member.type == unionMember)
473                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
474                            else
475                            {
476                               member.memberOffset += size;
477                            }
478                         }
479                         else
480                         {
481                            // TESTING THIS PADDING CODE
482                            if(alignment)
483                            {
484                               _class.structAlignment = Max(_class.structAlignment, alignment);
485
486                               if(_class.memberOffset % alignment)
487                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
488                            }
489
490                            dataMember.offset = _class.memberOffset;
491                            _class.memberOffset += size;
492                         }
493                         bitFields = 0;
494                      }
495                      size = dataMember.dataType.size;
496                      alignment = dataMember.dataType.alignment;
497                   }
498
499                   if(isMember)
500                   {
501                      // TESTING THIS PADDING CODE
502                      if(alignment)
503                      {
504                         member.structAlignment = Max(member.structAlignment, alignment);
505
506                         if(member.memberOffset % alignment)
507                            member.memberOffset += alignment - (member.memberOffset % alignment);
508                      }
509
510                      dataMember.offset = member.memberOffset;
511                      if(member.type == unionMember)
512                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
513                      else
514                      {
515                         member.memberOffset += size;
516                      }
517                   }
518                   else
519                   {
520                      // TESTING THIS PADDING CODE
521                      if(alignment)
522                      {
523                         _class.structAlignment = Max(_class.structAlignment, alignment);
524
525                         if(_class.memberOffset % alignment)
526                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
527                      }
528
529                      dataMember.offset = _class.memberOffset;
530                      _class.memberOffset += size;
531                   }
532                }
533                else
534                {
535                   int alignment;
536
537                   ComputeClassMembers((Class)dataMember, true);
538                   alignment = dataMember.structAlignment;
539
540                   if(isMember)
541                   {
542                      if(alignment)
543                      {
544                         if(member.memberOffset % alignment)
545                            member.memberOffset += alignment - (member.memberOffset % alignment);
546
547                         member.structAlignment = Max(member.structAlignment, alignment);
548                      }
549                      dataMember.offset = member.memberOffset;
550                      if(member.type == unionMember)
551                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
552                      else
553                         member.memberOffset += dataMember.memberOffset;
554                   }
555                   else
556                   {
557                      if(alignment)
558                      {
559                         if(_class.memberOffset % alignment)
560                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
561                         _class.structAlignment = Max(_class.structAlignment, alignment);
562                      }
563                      dataMember.offset = _class.memberOffset;
564                      _class.memberOffset += dataMember.memberOffset;
565                   }
566                }
567             }
568          }
569          if(bitFields)
570          {
571             int alignment = 0;
572             int size = (bitFields + 7) / 8;
573
574             if(isMember)
575             {
576                // TESTING THIS PADDING CODE
577                if(alignment)
578                {
579                   member.structAlignment = Max(member.structAlignment, alignment);
580
581                   if(member.memberOffset % alignment)
582                      member.memberOffset += alignment - (member.memberOffset % alignment);
583                }
584
585                if(member.type == unionMember)
586                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
587                else
588                {
589                   member.memberOffset += size;
590                }
591             }
592             else
593             {
594                // TESTING THIS PADDING CODE
595                if(alignment)
596                {
597                   _class.structAlignment = Max(_class.structAlignment, alignment);
598
599                   if(_class.memberOffset % alignment)
600                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
601                }
602                _class.memberOffset += size;
603             }
604             bitFields = 0;
605          }
606       }
607       if(member && member.type == unionMember)
608       {
609          member.memberOffset = unionMemberOffset;
610       }
611
612       if(!isMember)
613       {
614          /*if(_class.type == structClass)
615             _class.size = _class.memberOffset;
616          else
617          */
618
619          if(_class.type != bitClass)
620          {
621             int extra = 0;
622             if(_class.structAlignment)
623             {
624                if(_class.memberOffset % _class.structAlignment)
625                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
626             }
627             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
628             if(!member)
629             {
630                Property prop;
631                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
632                {
633                   if(prop.isProperty && prop.isWatchable)
634                   {
635                      prop.watcherOffset = _class.structSize;
636                      _class.structSize += sizeof(OldList);
637                   }
638                }
639             }
640
641             // Fix Derivatives
642             {
643                OldLink derivative;
644                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
645                {
646                   Class deriv = derivative.data;
647
648                   if(deriv.computeSize)
649                   {
650                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
651                      deriv.offset = /*_class.offset + */_class.structSize;
652                      deriv.memberOffset = 0;
653                      // ----------------------
654
655                      deriv.structSize = deriv.offset;
656
657                      ComputeClassMembers(deriv, false);
658                   }
659                }
660             }
661          }
662       }
663    }
664    if(context)
665       FinishTemplatesContext(context);
666 }
667
668 public void ComputeModuleClasses(Module module)
669 {
670    Class _class;
671    OldLink subModule;
672
673    for(subModule = module.modules.first; subModule; subModule = subModule.next)
674       ComputeModuleClasses(subModule.data);
675    for(_class = module.classes.first; _class; _class = _class.next)
676       ComputeClassMembers(_class, false);
677 }
678
679
680 public int ComputeTypeSize(Type type)
681 {
682    uint size = type ? type.size : 0;
683    if(!size && type && !type.computing)
684    {
685       type.computing = true;
686       switch(type.kind)
687       {
688          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
689          case charType: type.alignment = size = sizeof(char); break;
690          case intType: type.alignment = size = sizeof(int); break;
691          case int64Type: type.alignment = size = sizeof(int64); break;
692          case intPtrType: type.alignment = size = targetBits / 8; break;
693          case intSizeType: type.alignment = size = targetBits / 8; break;
694          case longType: type.alignment = size = sizeof(long); break;
695          case shortType: type.alignment = size = sizeof(short); break;
696          case floatType: type.alignment = size = sizeof(float); break;
697          case doubleType: type.alignment = size = sizeof(double); break;
698          case classType:
699          {
700             Class _class = type._class ? type._class.registered : null;
701
702             if(_class && _class.type == structClass)
703             {
704                // Ensure all members are properly registered
705                ComputeClassMembers(_class, false);
706                type.alignment = _class.structAlignment;
707                size = _class.structSize;
708                if(type.alignment && size % type.alignment)
709                   size += type.alignment - (size % type.alignment);
710
711             }
712             else if(_class && (_class.type == unitClass ||
713                    _class.type == enumClass ||
714                    _class.type == bitClass))
715             {
716                if(!_class.dataType)
717                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
718                size = type.alignment = ComputeTypeSize(_class.dataType);
719             }
720             else
721                size = type.alignment = targetBits / 8; // sizeof(Instance *);
722             break;
723          }
724          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
725          case arrayType:
726             if(type.arraySizeExp)
727             {
728                ProcessExpressionType(type.arraySizeExp);
729                ComputeExpression(type.arraySizeExp);
730                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType &&
731                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
732                {
733                   Location oldLoc = yylloc;
734                   // bool isConstant = type.arraySizeExp.isConstant;
735                   char expression[10240];
736                   expression[0] = '\0';
737                   type.arraySizeExp.expType = null;
738                   yylloc = type.arraySizeExp.loc;
739                   if(inCompiler)
740                      PrintExpression(type.arraySizeExp, expression);
741                   Compiler_Error($"Array size not constant int (%s)\n", expression);
742                   yylloc = oldLoc;
743                }
744                GetInt(type.arraySizeExp, &type.arraySize);
745             }
746             else if(type.enumClass)
747             {
748                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
749                {
750                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
751                }
752                else
753                   type.arraySize = 0;
754             }
755             else
756             {
757                // Unimplemented auto size
758                type.arraySize = 0;
759             }
760
761             size = ComputeTypeSize(type.type) * type.arraySize;
762             if(type.type)
763                type.alignment = type.type.alignment;
764
765             break;
766          case structType:
767          {
768             Type member;
769             for(member = type.members.first; member; member = member.next)
770             {
771                uint addSize = ComputeTypeSize(member);
772
773                member.offset = size;
774                if(member.alignment && size % member.alignment)
775                   member.offset += member.alignment - (size % member.alignment);
776                size = member.offset;
777
778                type.alignment = Max(type.alignment, member.alignment);
779                size += addSize;
780             }
781             if(type.alignment && size % type.alignment)
782                size += type.alignment - (size % type.alignment);
783             break;
784          }
785          case unionType:
786          {
787             Type member;
788             for(member = type.members.first; member; member = member.next)
789             {
790                uint addSize = ComputeTypeSize(member);
791
792                member.offset = size;
793                if(member.alignment && size % member.alignment)
794                   member.offset += member.alignment - (size % member.alignment);
795                size = member.offset;
796
797                type.alignment = Max(type.alignment, member.alignment);
798                size = Max(size, addSize);
799             }
800             if(type.alignment && size % type.alignment)
801                size += type.alignment - (size % type.alignment);
802             break;
803          }
804          case templateType:
805          {
806             TemplateParameter param = type.templateParameter;
807             Type baseType = ProcessTemplateParameterType(param);
808             if(baseType)
809             {
810                size = ComputeTypeSize(baseType);
811                type.alignment = baseType.alignment;
812             }
813             else
814                type.alignment = size = sizeof(uint64);
815             break;
816          }
817          case enumType:
818          {
819             type.alignment = size = sizeof(enum { test });
820             break;
821          }
822          case thisClassType:
823          {
824             type.alignment = size = targetBits / 8; //sizeof(void *);
825             break;
826          }
827       }
828       type.size = size;
829       type.computing = false;
830    }
831    return size;
832 }
833
834
835 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
836 {
837    // This function is in need of a major review when implementing private members etc.
838    DataMember topMember = isMember ? (DataMember) _class : null;
839    uint totalSize = 0;
840    uint maxSize = 0;
841    int alignment, size;
842    DataMember member;
843    Context context = isMember ? null : SetupTemplatesContext(_class);
844    if(addedPadding)
845       *addedPadding = false;
846
847    if(!isMember && _class.base)
848    {
849       maxSize = _class.structSize;
850       //if(_class.base.type != systemClass) // Commented out with new Instance _class
851       {
852          // DANGER: Testing this noHeadClass here...
853          if(_class.type == structClass || _class.type == noHeadClass)
854             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
855          else
856          {
857             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
858             if(maxSize > baseSize)
859                maxSize -= baseSize;
860             else
861                maxSize = 0;
862          }
863       }
864    }
865
866    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
867    {
868       if(!member.isProperty)
869       {
870          switch(member.type)
871          {
872             case normalMember:
873             {
874                if(member.dataTypeString)
875                {
876                   OldList * specs = MkList(), * decls = MkList();
877                   Declarator decl;
878
879                   decl = SpecDeclFromString(member.dataTypeString, specs,
880                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
881                   ListAdd(decls, MkStructDeclarator(decl, null));
882                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
883
884                   if(!member.dataType)
885                      member.dataType = ProcessType(specs, decl);
886
887                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
888
889                   {
890                      Type type = ProcessType(specs, decl);
891                      DeclareType(member.dataType, false, false);
892                      FreeType(type);
893                   }
894                   /*
895                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
896                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
897                      DeclareStruct(member.dataType._class.string, false);
898                   */
899
900                   ComputeTypeSize(member.dataType);
901                   size = member.dataType.size;
902                   alignment = member.dataType.alignment;
903
904                   if(alignment)
905                   {
906                      if(totalSize % alignment)
907                         totalSize += alignment - (totalSize % alignment);
908                   }
909                   totalSize += size;
910                }
911                break;
912             }
913             case unionMember:
914             case structMember:
915             {
916                OldList * specs = MkList(), * list = MkList();
917
918                size = 0;
919                AddMembers(list, (Class)member, true, &size, topClass, null);
920                ListAdd(specs,
921                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
922                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
923                alignment = member.structAlignment;
924
925                if(alignment)
926                {
927                   if(totalSize % alignment)
928                      totalSize += alignment - (totalSize % alignment);
929                }
930                totalSize += size;
931                break;
932             }
933          }
934       }
935    }
936    if(retSize)
937    {
938       if(topMember && topMember.type == unionMember)
939          *retSize = Max(*retSize, totalSize);
940       else
941          *retSize += totalSize;
942    }
943    else if(totalSize < maxSize && _class.type != systemClass)
944    {
945       int autoPadding = 0;
946       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
947          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
948       if(totalSize + autoPadding < maxSize)
949       {
950          char sizeString[50];
951          sprintf(sizeString, "%d", maxSize - totalSize);
952          ListAdd(declarations,
953             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
954             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
955          if(addedPadding)
956             *addedPadding = true;
957       }
958    }
959    if(context)
960       FinishTemplatesContext(context);
961    return topMember ? topMember.memberID : _class.memberID;
962 }
963
964 static int DeclareMembers(Class _class, bool isMember)
965 {
966    DataMember topMember = isMember ? (DataMember) _class : null;
967    uint totalSize = 0;
968    DataMember member;
969    Context context = isMember ? null : SetupTemplatesContext(_class);
970
971    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
972       DeclareMembers(_class.base, false);
973
974    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
975    {
976       if(!member.isProperty)
977       {
978          switch(member.type)
979          {
980             case normalMember:
981             {
982                /*
983                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
984                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
985                   DeclareStruct(member.dataType._class.string, false);
986                   */
987                if(!member.dataType && member.dataTypeString)
988                   member.dataType = ProcessTypeString(member.dataTypeString, false);
989                if(member.dataType)
990                   DeclareType(member.dataType, false, false);
991                break;
992             }
993             case unionMember:
994             case structMember:
995             {
996                DeclareMembers((Class)member, true);
997                break;
998             }
999          }
1000       }
1001    }
1002    if(context)
1003       FinishTemplatesContext(context);
1004
1005    return topMember ? topMember.memberID : _class.memberID;
1006 }
1007
1008 void DeclareStruct(char * name, bool skipNoHead)
1009 {
1010    External external = null;
1011    Symbol classSym = FindClass(name);
1012
1013    if(!inCompiler || !classSym) return;
1014
1015    // We don't need any declaration for bit classes...
1016    if(classSym.registered &&
1017       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1018       return;
1019
1020    /*if(classSym.registered.templateClass)
1021       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1022    */
1023
1024    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1025    {
1026       // Add typedef struct
1027       Declaration decl;
1028       OldList * specifiers, * declarators;
1029       OldList * declarations = null;
1030       char structName[1024];
1031       external = (classSym.registered && classSym.registered.type == structClass) ?
1032          classSym.pointerExternal : classSym.structExternal;
1033
1034       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1035       // Moved this one up because DeclareClass done later will need it
1036
1037       classSym.declaring++;
1038
1039       if(strchr(classSym.string, '<'))
1040       {
1041          if(classSym.registered.templateClass)
1042          {
1043             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1044             classSym.declaring--;
1045          }
1046          return;
1047       }
1048
1049       //if(!skipNoHead)
1050          DeclareMembers(classSym.registered, false);
1051
1052       structName[0] = 0;
1053       FullClassNameCat(structName, name, false);
1054
1055       /*if(!external)
1056          external = MkExternalDeclaration(null);*/
1057
1058       if(!skipNoHead)
1059       {
1060          bool addedPadding = false;
1061          classSym.declaredStructSym = true;
1062
1063          declarations = MkList();
1064
1065          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1066
1067          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1068          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1069
1070          if(!declarations->count || (declarations->count == 1 && addedPadding))
1071          {
1072             FreeList(declarations, FreeClassDef);
1073             declarations = null;
1074          }
1075       }
1076       if(skipNoHead || declarations)
1077       {
1078          if(external && external.declaration)
1079          {
1080             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1081
1082             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1083             {
1084                // TODO: Fix this
1085                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1086
1087                // DANGER
1088                if(classSym.structExternal)
1089                   ast->Move(classSym.structExternal, curExternal.prev);
1090                ast->Move(classSym.pointerExternal, curExternal.prev);
1091
1092                classSym.id = curExternal.symbol.idCode;
1093                classSym.idCode = curExternal.symbol.idCode;
1094                // external = classSym.pointerExternal;
1095                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1096             }
1097          }
1098          else
1099          {
1100             if(!external)
1101                external = MkExternalDeclaration(null);
1102
1103             specifiers = MkList();
1104             declarators = MkList();
1105             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1106
1107             /*
1108             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1109             ListAdd(declarators, MkInitDeclarator(d, null));
1110             */
1111             external.declaration = decl = MkDeclaration(specifiers, declarators);
1112             if(decl.symbol && !decl.symbol.pointerExternal)
1113                decl.symbol.pointerExternal = external;
1114
1115             // For simple classes, keep the declaration as the external to move around
1116             if(classSym.registered && classSym.registered.type == structClass)
1117             {
1118                char className[1024];
1119                strcpy(className, "__ecereClass_");
1120                FullClassNameCat(className, classSym.string, true);
1121                MangleClassName(className);
1122
1123                // Testing This
1124                DeclareClass(classSym, className);
1125
1126                external.symbol = classSym;
1127                classSym.pointerExternal = external;
1128                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1129                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1130             }
1131             else
1132             {
1133                char className[1024];
1134                strcpy(className, "__ecereClass_");
1135                FullClassNameCat(className, classSym.string, true);
1136                MangleClassName(className);
1137
1138                // TOFIX: TESTING THIS...
1139                classSym.structExternal = external;
1140                DeclareClass(classSym, className);
1141                external.symbol = classSym;
1142             }
1143
1144             //if(curExternal)
1145                ast->Insert(curExternal ? curExternal.prev : null, external);
1146          }
1147       }
1148
1149       classSym.declaring--;
1150    }
1151    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1152    {
1153       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1154       // Moved this one up because DeclareClass done later will need it
1155
1156       // TESTING THIS:
1157       classSym.declaring++;
1158
1159       //if(!skipNoHead)
1160       {
1161          if(classSym.registered)
1162             DeclareMembers(classSym.registered, false);
1163       }
1164
1165       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1166       {
1167          // TODO: Fix this
1168          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1169
1170          // DANGER
1171          if(classSym.structExternal)
1172             ast->Move(classSym.structExternal, curExternal.prev);
1173          ast->Move(classSym.pointerExternal, curExternal.prev);
1174
1175          classSym.id = curExternal.symbol.idCode;
1176          classSym.idCode = curExternal.symbol.idCode;
1177          // external = classSym.pointerExternal;
1178          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1179       }
1180
1181       classSym.declaring--;
1182    }
1183    //return external;
1184 }
1185
1186 void DeclareProperty(Property prop, char * setName, char * getName)
1187 {
1188    Symbol symbol = prop.symbol;
1189    char propName[1024];
1190
1191    strcpy(setName, "__ecereProp_");
1192    FullClassNameCat(setName, prop._class.fullName, false);
1193    strcat(setName, "_Set_");
1194    // strcat(setName, prop.name);
1195    FullClassNameCat(setName, prop.name, true);
1196
1197    strcpy(getName, "__ecereProp_");
1198    FullClassNameCat(getName, prop._class.fullName, false);
1199    strcat(getName, "_Get_");
1200    FullClassNameCat(getName, prop.name, true);
1201    // strcat(getName, prop.name);
1202
1203    strcpy(propName, "__ecereProp_");
1204    FullClassNameCat(propName, prop._class.fullName, false);
1205    strcat(propName, "_");
1206    FullClassNameCat(propName, prop.name, true);
1207    // strcat(propName, prop.name);
1208
1209    // To support "char *" property
1210    MangleClassName(getName);
1211    MangleClassName(setName);
1212    MangleClassName(propName);
1213
1214    if(prop._class.type == structClass)
1215       DeclareStruct(prop._class.fullName, false);
1216
1217    if(!symbol || curExternal.symbol.idCode < symbol.id)
1218    {
1219       bool imported = false;
1220       bool dllImport = false;
1221       if(!symbol || symbol._import)
1222       {
1223          if(!symbol)
1224          {
1225             Symbol classSym;
1226             if(!prop._class.symbol)
1227                prop._class.symbol = FindClass(prop._class.fullName);
1228             classSym = prop._class.symbol;
1229             if(classSym && !classSym._import)
1230             {
1231                ModuleImport module;
1232
1233                if(prop._class.module)
1234                   module = FindModule(prop._class.module);
1235                else
1236                   module = mainModule;
1237
1238                classSym._import = ClassImport
1239                {
1240                   name = CopyString(prop._class.fullName);
1241                   isRemote = prop._class.isRemote;
1242                };
1243                module.classes.Add(classSym._import);
1244             }
1245             symbol = prop.symbol = Symbol { };
1246             symbol._import = (ClassImport)PropertyImport
1247             {
1248                name = CopyString(prop.name);
1249                isVirtual = false; //prop.isVirtual;
1250                hasSet = prop.Set ? true : false;
1251                hasGet = prop.Get ? true : false;
1252             };
1253             if(classSym)
1254                classSym._import.properties.Add(symbol._import);
1255          }
1256          imported = true;
1257          if(prop._class.module != privateModule && prop._class.module.importType != staticImport)
1258             dllImport = true;
1259       }
1260
1261       if(!symbol.type)
1262       {
1263          Context context = SetupTemplatesContext(prop._class);
1264          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1265          FinishTemplatesContext(context);
1266       }
1267
1268       // Get
1269       if(prop.Get)
1270       {
1271          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1272          {
1273             Declaration decl;
1274             OldList * specifiers, * declarators;
1275             Declarator d;
1276             OldList * params;
1277             Specifier spec;
1278             External external;
1279             Declarator typeDecl;
1280             bool simple = false;
1281
1282             specifiers = MkList();
1283             declarators = MkList();
1284             params = MkList();
1285
1286             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1287                MkDeclaratorIdentifier(MkIdentifier("this"))));
1288
1289             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1290             //if(imported)
1291             if(dllImport)
1292                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1293
1294             {
1295                Context context = SetupTemplatesContext(prop._class);
1296                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1297                FinishTemplatesContext(context);
1298             }
1299
1300             // Make sure the simple _class's type is declared
1301             for(spec = specifiers->first; spec; spec = spec.next)
1302             {
1303                if(spec.type == nameSpecifier /*SpecifierClass*/)
1304                {
1305                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1306                   {
1307                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1308                      symbol._class = classSym.registered;
1309                      if(classSym.registered && classSym.registered.type == structClass)
1310                      {
1311                         DeclareStruct(spec.name, false);
1312                         simple = true;
1313                      }
1314                   }
1315                }
1316             }
1317
1318             if(!simple)
1319                d = PlugDeclarator(typeDecl, d);
1320             else
1321             {
1322                ListAdd(params, MkTypeName(specifiers,
1323                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1324                specifiers = MkList();
1325             }
1326
1327             d = MkDeclaratorFunction(d, params);
1328
1329             //if(imported)
1330             if(dllImport)
1331                specifiers->Insert(null, MkSpecifier(EXTERN));
1332             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1333                specifiers->Insert(null, MkSpecifier(STATIC));
1334             if(simple)
1335                ListAdd(specifiers, MkSpecifier(VOID));
1336
1337             ListAdd(declarators, MkInitDeclarator(d, null));
1338
1339             decl = MkDeclaration(specifiers, declarators);
1340
1341             external = MkExternalDeclaration(decl);
1342             ast->Insert(curExternal.prev, external);
1343             external.symbol = symbol;
1344             symbol.externalGet = external;
1345
1346             ReplaceThisClassSpecifiers(specifiers, prop._class);
1347
1348             if(typeDecl)
1349                FreeDeclarator(typeDecl);
1350          }
1351          else
1352          {
1353             // Move declaration higher...
1354             ast->Move(symbol.externalGet, curExternal.prev);
1355          }
1356       }
1357
1358       // Set
1359       if(prop.Set)
1360       {
1361          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1362          {
1363             Declaration decl;
1364             OldList * specifiers, * declarators;
1365             Declarator d;
1366             OldList * params;
1367             Specifier spec;
1368             External external;
1369             Declarator typeDecl;
1370
1371             declarators = MkList();
1372             params = MkList();
1373
1374             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1375             if(!prop.conversion || prop._class.type == structClass)
1376             {
1377                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1378                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1379             }
1380
1381             specifiers = MkList();
1382
1383             {
1384                Context context = SetupTemplatesContext(prop._class);
1385                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1386                   MkDeclaratorIdentifier(MkIdentifier("value")));
1387                FinishTemplatesContext(context);
1388             }
1389             ListAdd(params, MkTypeName(specifiers, d));
1390
1391             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1392             //if(imported)
1393             if(dllImport)
1394                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1395             d = MkDeclaratorFunction(d, params);
1396
1397             // Make sure the simple _class's type is declared
1398             for(spec = specifiers->first; spec; spec = spec.next)
1399             {
1400                if(spec.type == nameSpecifier /*SpecifierClass*/)
1401                {
1402                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1403                   {
1404                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1405                      symbol._class = classSym.registered;
1406                      if(classSym.registered && classSym.registered.type == structClass)
1407                         DeclareStruct(spec.name, false);
1408                   }
1409                }
1410             }
1411
1412             ListAdd(declarators, MkInitDeclarator(d, null));
1413
1414             specifiers = MkList();
1415             //if(imported)
1416             if(dllImport)
1417                specifiers->Insert(null, MkSpecifier(EXTERN));
1418             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1419                specifiers->Insert(null, MkSpecifier(STATIC));
1420
1421             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1422             if(!prop.conversion || prop._class.type == structClass)
1423                ListAdd(specifiers, MkSpecifier(VOID));
1424             else
1425                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1426
1427             decl = MkDeclaration(specifiers, declarators);
1428
1429             external = MkExternalDeclaration(decl);
1430             ast->Insert(curExternal.prev, external);
1431             external.symbol = symbol;
1432             symbol.externalSet = external;
1433
1434             ReplaceThisClassSpecifiers(specifiers, prop._class);
1435          }
1436          else
1437          {
1438             // Move declaration higher...
1439             ast->Move(symbol.externalSet, curExternal.prev);
1440          }
1441       }
1442
1443       // Property (for Watchers)
1444       if(!symbol.externalPtr)
1445       {
1446          Declaration decl;
1447          External external;
1448          OldList * specifiers = MkList();
1449
1450          if(imported)
1451             specifiers->Insert(null, MkSpecifier(EXTERN));
1452          else
1453             specifiers->Insert(null, MkSpecifier(STATIC));
1454
1455          ListAdd(specifiers, MkSpecifierName("Property"));
1456
1457          {
1458             OldList * list = MkList();
1459             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1460                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1461
1462             if(!imported)
1463             {
1464                strcpy(propName, "__ecerePropM_");
1465                FullClassNameCat(propName, prop._class.fullName, false);
1466                strcat(propName, "_");
1467                // strcat(propName, prop.name);
1468                FullClassNameCat(propName, prop.name, true);
1469
1470                MangleClassName(propName);
1471
1472                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1473                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1474             }
1475             decl = MkDeclaration(specifiers, list);
1476          }
1477
1478          external = MkExternalDeclaration(decl);
1479          ast->Insert(curExternal.prev, external);
1480          external.symbol = symbol;
1481          symbol.externalPtr = external;
1482       }
1483       else
1484       {
1485          // Move declaration higher...
1486          ast->Move(symbol.externalPtr, curExternal.prev);
1487       }
1488
1489       symbol.id = curExternal.symbol.idCode;
1490    }
1491 }
1492
1493 // ***************** EXPRESSION PROCESSING ***************************
1494 public Type Dereference(Type source)
1495 {
1496    Type type = null;
1497    if(source)
1498    {
1499       if(source.kind == pointerType || source.kind == arrayType)
1500       {
1501          type = source.type;
1502          source.type.refCount++;
1503       }
1504       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1505       {
1506          type = Type
1507          {
1508             kind = charType;
1509             refCount = 1;
1510          };
1511       }
1512       // Support dereferencing of no head classes for now...
1513       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1514       {
1515          type = source;
1516          source.refCount++;
1517       }
1518       else
1519          Compiler_Error($"cannot dereference type\n");
1520    }
1521    return type;
1522 }
1523
1524 static Type Reference(Type source)
1525 {
1526    Type type = null;
1527    if(source)
1528    {
1529       type = Type
1530       {
1531          kind = pointerType;
1532          type = source;
1533          refCount = 1;
1534       };
1535       source.refCount++;
1536    }
1537    return type;
1538 }
1539
1540 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1541 {
1542    Identifier ident = member.identifiers ? member.identifiers->first : null;
1543    bool found = false;
1544    DataMember dataMember = null;
1545    Method method = null;
1546    bool freeType = false;
1547
1548    yylloc = member.loc;
1549
1550    if(!ident)
1551    {
1552       if(curMember)
1553       {
1554          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1555          if(*curMember)
1556          {
1557             found = true;
1558             dataMember = *curMember;
1559          }
1560       }
1561    }
1562    else
1563    {
1564       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1565       DataMember _subMemberStack[256];
1566       int _subMemberStackPos = 0;
1567
1568       // FILL MEMBER STACK
1569       if(!thisMember)
1570          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1571       if(thisMember)
1572       {
1573          dataMember = thisMember;
1574          if(curMember && thisMember.memberAccess == publicAccess)
1575          {
1576             *curMember = thisMember;
1577             *curClass = thisMember._class;
1578             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1579             *subMemberStackPos = _subMemberStackPos;
1580          }
1581          found = true;
1582       }
1583       else
1584       {
1585          // Setting a method
1586          method = eClass_FindMethod(_class, ident.string, privateModule);
1587          if(method && method.type == virtualMethod)
1588             found = true;
1589          else
1590             method = null;
1591       }
1592    }
1593
1594    if(found)
1595    {
1596       Type type = null;
1597       if(dataMember)
1598       {
1599          if(!dataMember.dataType && dataMember.dataTypeString)
1600          {
1601             //Context context = SetupTemplatesContext(dataMember._class);
1602             Context context = SetupTemplatesContext(_class);
1603             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1604             FinishTemplatesContext(context);
1605          }
1606          type = dataMember.dataType;
1607       }
1608       else if(method)
1609       {
1610          // This is for destination type...
1611          if(!method.dataType)
1612             ProcessMethodType(method);
1613          //DeclareMethod(method);
1614          // method.dataType = ((Symbol)method.symbol)->type;
1615          type = method.dataType;
1616       }
1617
1618       if(ident && ident.next)
1619       {
1620          for(ident = ident.next; ident && type; ident = ident.next)
1621          {
1622             if(type.kind == classType)
1623             {
1624                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1625                if(!dataMember)
1626                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1627                if(dataMember)
1628                   type = dataMember.dataType;
1629             }
1630             else if(type.kind == structType || type.kind == unionType)
1631             {
1632                Type memberType;
1633                for(memberType = type.members.first; memberType; memberType = memberType.next)
1634                {
1635                   if(!strcmp(memberType.name, ident.string))
1636                   {
1637                      type = memberType;
1638                      break;
1639                   }
1640                }
1641             }
1642          }
1643       }
1644
1645       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1646       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1647       {
1648          int id = 0;
1649          ClassTemplateParameter curParam = null;
1650          Class sClass;
1651          for(sClass = _class; sClass; sClass = sClass.base)
1652          {
1653             id = 0;
1654             if(sClass.templateClass) sClass = sClass.templateClass;
1655             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1656             {
1657                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1658                {
1659                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1660                   {
1661                      if(sClass.templateClass) sClass = sClass.templateClass;
1662                      id += sClass.templateParams.count;
1663                   }
1664                   break;
1665                }
1666                id++;
1667             }
1668             if(curParam) break;
1669          }
1670
1671          if(curParam)
1672          {
1673             ClassTemplateArgument arg = _class.templateArgs[id];
1674             if(arg.dataTypeString)
1675             {
1676                // FreeType(type);
1677                type = ProcessTypeString(arg.dataTypeString, false);
1678                freeType = true;
1679                if(type && _class.templateClass)
1680                   type.passAsTemplate = true;
1681                if(type)
1682                {
1683                   // type.refCount++;
1684                   /*if(!exp.destType)
1685                   {
1686                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1687                      exp.destType.refCount++;
1688                   }*/
1689                }
1690             }
1691          }
1692       }
1693       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1694       {
1695          Class expClass = type._class.registered;
1696          Class cClass = null;
1697          int c;
1698          int paramCount = 0;
1699          int lastParam = -1;
1700
1701          char templateString[1024];
1702          ClassTemplateParameter param;
1703          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1704          for(cClass = expClass; cClass; cClass = cClass.base)
1705          {
1706             int p = 0;
1707             if(cClass.templateClass) cClass = cClass.templateClass;
1708             for(param = cClass.templateParams.first; param; param = param.next)
1709             {
1710                int id = p;
1711                Class sClass;
1712                ClassTemplateArgument arg;
1713                for(sClass = cClass.base; sClass; sClass = sClass.base)
1714                {
1715                   if(sClass.templateClass) sClass = sClass.templateClass;
1716                   id += sClass.templateParams.count;
1717                }
1718                arg = expClass.templateArgs[id];
1719
1720                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1721                {
1722                   ClassTemplateParameter cParam;
1723                   //int p = numParams - sClass.templateParams.count;
1724                   int p = 0;
1725                   Class nextClass;
1726                   if(sClass.templateClass) sClass = sClass.templateClass;
1727
1728                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1729                   {
1730                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1731                      p += nextClass.templateParams.count;
1732                   }
1733
1734                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1735                   {
1736                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1737                      {
1738                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1739                         {
1740                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1741                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1742                            break;
1743                         }
1744                      }
1745                   }
1746                }
1747
1748                {
1749                   char argument[256];
1750                   argument[0] = '\0';
1751                   /*if(arg.name)
1752                   {
1753                      strcat(argument, arg.name.string);
1754                      strcat(argument, " = ");
1755                   }*/
1756                   switch(param.type)
1757                   {
1758                      case expression:
1759                      {
1760                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1761                         char expString[1024];
1762                         OldList * specs = MkList();
1763                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1764                         Expression exp;
1765                         char * string = PrintHexUInt64(arg.expression.ui64);
1766                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1767                         delete string;
1768
1769                         ProcessExpressionType(exp);
1770                         ComputeExpression(exp);
1771                         expString[0] = '\0';
1772                         PrintExpression(exp, expString);
1773                         strcat(argument, expString);
1774                         //delete exp;
1775                         FreeExpression(exp);
1776                         break;
1777                      }
1778                      case identifier:
1779                      {
1780                         strcat(argument, arg.member.name);
1781                         break;
1782                      }
1783                      case TemplateParameterType::type:
1784                      {
1785                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1786                            strcat(argument, arg.dataTypeString);
1787                         break;
1788                      }
1789                   }
1790                   if(argument[0])
1791                   {
1792                      if(paramCount) strcat(templateString, ", ");
1793                      if(lastParam != p - 1)
1794                      {
1795                         strcat(templateString, param.name);
1796                         strcat(templateString, " = ");
1797                      }
1798                      strcat(templateString, argument);
1799                      paramCount++;
1800                      lastParam = p;
1801                   }
1802                   p++;
1803                }
1804             }
1805          }
1806          {
1807             int len = strlen(templateString);
1808             if(templateString[len-1] == '<')
1809                len--;
1810             else
1811             {
1812                if(templateString[len-1] == '>')
1813                   templateString[len++] = ' ';
1814                templateString[len++] = '>';
1815             }
1816             templateString[len++] = '\0';
1817          }
1818          {
1819             Context context = SetupTemplatesContext(_class);
1820             if(freeType) FreeType(type);
1821             type = ProcessTypeString(templateString, false);
1822             freeType = true;
1823             FinishTemplatesContext(context);
1824          }
1825       }
1826
1827       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1828       {
1829          ProcessExpressionType(member.initializer.exp);
1830          if(!member.initializer.exp.expType)
1831          {
1832             if(inCompiler)
1833             {
1834                char expString[10240];
1835                expString[0] = '\0';
1836                PrintExpression(member.initializer.exp, expString);
1837                ChangeCh(expString, '\n', ' ');
1838                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1839             }
1840          }
1841          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1842          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1843          {
1844             Compiler_Error($"incompatible instance method %s\n", ident.string);
1845          }
1846       }
1847       else if(member.initializer)
1848       {
1849          /*
1850          FreeType(member.exp.destType);
1851          member.exp.destType = type;
1852          if(member.exp.destType)
1853             member.exp.destType.refCount++;
1854          ProcessExpressionType(member.exp);
1855          */
1856
1857          ProcessInitializer(member.initializer, type);
1858       }
1859       if(freeType) FreeType(type);
1860    }
1861    else
1862    {
1863       if(_class && _class.type == unitClass)
1864       {
1865          if(member.initializer)
1866          {
1867             /*
1868             FreeType(member.exp.destType);
1869             member.exp.destType = MkClassType(_class.fullName);
1870             ProcessExpressionType(member.initializer, type);
1871             */
1872             Type type = MkClassType(_class.fullName);
1873             ProcessInitializer(member.initializer, type);
1874             FreeType(type);
1875          }
1876       }
1877       else
1878       {
1879          if(member.initializer)
1880          {
1881             //ProcessExpressionType(member.exp);
1882             ProcessInitializer(member.initializer, null);
1883          }
1884          if(ident)
1885          {
1886             if(method)
1887             {
1888                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1889             }
1890             else if(_class)
1891             {
1892                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1893                if(inCompiler)
1894                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1895             }
1896          }
1897          else if(_class)
1898             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1899       }
1900    }
1901 }
1902
1903 void ProcessInstantiationType(Instantiation inst)
1904 {
1905    yylloc = inst.loc;
1906    if(inst._class)
1907    {
1908       MembersInit members;
1909       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1910       Class _class;
1911
1912       /*if(!inst._class.symbol)
1913          inst._class.symbol = FindClass(inst._class.name);*/
1914       classSym = inst._class.symbol;
1915       _class = classSym ? classSym.registered : null;
1916
1917       // DANGER: Patch for mutex not declaring its struct when not needed
1918       if(!_class || _class.type != noHeadClass)
1919          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1920
1921       afterExternal = afterExternal ? afterExternal : curExternal;
1922
1923       if(inst.exp)
1924          ProcessExpressionType(inst.exp);
1925
1926       inst.isConstant = true;
1927       if(inst.members)
1928       {
1929          DataMember curMember = null;
1930          Class curClass = null;
1931          DataMember subMemberStack[256];
1932          int subMemberStackPos = 0;
1933
1934          for(members = inst.members->first; members; members = members.next)
1935          {
1936             switch(members.type)
1937             {
1938                case methodMembersInit:
1939                {
1940                   char name[1024];
1941                   static uint instMethodID = 0;
1942                   External external = curExternal;
1943                   Context context = curContext;
1944                   Declarator declarator = members.function.declarator;
1945                   Identifier nameID = GetDeclId(declarator);
1946                   char * unmangled = nameID ? nameID.string : null;
1947                   Expression exp;
1948                   External createdExternal = null;
1949
1950                   if(inCompiler)
1951                   {
1952                      char number[16];
1953                      //members.function.dontMangle = true;
1954                      strcpy(name, "__ecereInstMeth_");
1955                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
1956                      strcat(name, "_");
1957                      strcat(name, nameID.string);
1958                      strcat(name, "_");
1959                      sprintf(number, "_%08d", instMethodID++);
1960                      strcat(name, number);
1961                      nameID.string = CopyString(name);
1962                   }
1963
1964                   // Do modifications here...
1965                   if(declarator)
1966                   {
1967                      Symbol symbol = declarator.symbol;
1968                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
1969
1970                      if(method && method.type == virtualMethod)
1971                      {
1972                         symbol.method = method;
1973                         ProcessMethodType(method);
1974
1975                         if(!symbol.type.thisClass)
1976                         {
1977                            if(method.dataType.thisClass && currentClass &&
1978                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
1979                            {
1980                               if(!currentClass.symbol)
1981                                  currentClass.symbol = FindClass(currentClass.fullName);
1982                               symbol.type.thisClass = currentClass.symbol;
1983                            }
1984                            else
1985                            {
1986                               if(!_class.symbol)
1987                                  _class.symbol = FindClass(_class.fullName);
1988                               symbol.type.thisClass = _class.symbol;
1989                            }
1990                         }
1991                         // TESTING THIS HERE:
1992                         DeclareType(symbol.type, true, true);
1993
1994                      }
1995                      else if(classSym)
1996                      {
1997                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
1998                            unmangled, classSym.string);
1999                      }
2000                   }
2001
2002                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2003                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2004
2005                   if(nameID)
2006                   {
2007                      FreeSpecifier(nameID._class);
2008                      nameID._class = null;
2009                   }
2010
2011                   if(inCompiler)
2012                   {
2013
2014                      Type type = declarator.symbol.type;
2015                      External oldExternal = curExternal;
2016
2017                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2018                      // *** It was commented out for problems such as
2019                      /*
2020                            class VirtualDesktop : Window
2021                            {
2022                               clientSize = Size { };
2023                               Timer timer
2024                               {
2025                                  bool DelayExpired()
2026                                  {
2027                                     clientSize.w;
2028                                     return true;
2029                                  }
2030                               };
2031                            }
2032                      */
2033                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2034
2035                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2036
2037                      /*
2038                      if(strcmp(declarator.symbol.string, name))
2039                      {
2040                         printf("TOCHECK: Look out for this\n");
2041                         delete declarator.symbol.string;
2042                         declarator.symbol.string = CopyString(name);
2043                      }
2044
2045                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2046                      {
2047                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2048                         excludedSymbols->Remove(declarator.symbol);
2049                         globalContext.symbols.Add((BTNode)declarator.symbol);
2050                         if(strstr(declarator.symbol.string), "::")
2051                            globalContext.hasNameSpace = true;
2052
2053                      }
2054                      */
2055
2056                      //curExternal = curExternal.prev;
2057                      //afterExternal = afterExternal->next;
2058
2059                      //ProcessFunction(afterExternal->function);
2060
2061                      //curExternal = afterExternal;
2062                      {
2063                         External externalDecl;
2064                         externalDecl = MkExternalDeclaration(null);
2065                         ast->Insert(oldExternal.prev, externalDecl);
2066
2067                         // Which function does this process?
2068                         if(createdExternal.function)
2069                         {
2070                            ProcessFunction(createdExternal.function);
2071
2072                            //curExternal = oldExternal;
2073
2074                            {
2075                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2076
2077                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2078                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2079
2080                               //externalDecl = MkExternalDeclaration(decl);
2081
2082                               //***** ast->Insert(external.prev, externalDecl);
2083                               //ast->Insert(curExternal.prev, externalDecl);
2084                               externalDecl.declaration = decl;
2085                               if(decl.symbol && !decl.symbol.pointerExternal)
2086                                  decl.symbol.pointerExternal = externalDecl;
2087
2088                               // Trying this out...
2089                               declarator.symbol.pointerExternal = externalDecl;
2090                            }
2091                         }
2092                      }
2093                   }
2094                   else if(declarator)
2095                   {
2096                      curExternal = declarator.symbol.pointerExternal;
2097                      ProcessFunction((FunctionDefinition)members.function);
2098                   }
2099                   curExternal = external;
2100                   curContext = context;
2101
2102                   if(inCompiler)
2103                   {
2104                      FreeClassFunction(members.function);
2105
2106                      // In this pass, turn this into a MemberInitData
2107                      exp = QMkExpId(name);
2108                      members.type = dataMembersInit;
2109                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2110
2111                      delete unmangled;
2112                   }
2113                   break;
2114                }
2115                case dataMembersInit:
2116                {
2117                   if(members.dataMembers && classSym)
2118                   {
2119                      MemberInit member;
2120                      Location oldyyloc = yylloc;
2121                      for(member = members.dataMembers->first; member; member = member.next)
2122                      {
2123                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2124                         if(member.initializer && !member.initializer.isConstant)
2125                            inst.isConstant = false;
2126                      }
2127                      yylloc = oldyyloc;
2128                   }
2129                   break;
2130                }
2131             }
2132          }
2133       }
2134    }
2135 }
2136
2137 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2138 {
2139    // OPTIMIZATIONS: TESTING THIS...
2140    if(inCompiler)
2141    {
2142       if(type.kind == functionType)
2143       {
2144          Type param;
2145          if(declareParams)
2146          {
2147             for(param = type.params.first; param; param = param.next)
2148                DeclareType(param, declarePointers, true);
2149          }
2150          DeclareType(type.returnType, declarePointers, true);
2151       }
2152       else if(type.kind == pointerType && declarePointers)
2153          DeclareType(type.type, declarePointers, false);
2154       else if(type.kind == classType)
2155       {
2156          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2157             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2158       }
2159       else if(type.kind == structType || type.kind == unionType)
2160       {
2161          Type member;
2162          for(member = type.members.first; member; member = member.next)
2163             DeclareType(member, false, false);
2164       }
2165       else if(type.kind == arrayType)
2166          DeclareType(type.arrayType, declarePointers, false);
2167    }
2168 }
2169
2170 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2171 {
2172    ClassTemplateArgument * arg = null;
2173    int id = 0;
2174    ClassTemplateParameter curParam = null;
2175    Class sClass;
2176    for(sClass = _class; sClass; sClass = sClass.base)
2177    {
2178       id = 0;
2179       if(sClass.templateClass) sClass = sClass.templateClass;
2180       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2181       {
2182          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2183          {
2184             for(sClass = sClass.base; sClass; sClass = sClass.base)
2185             {
2186                if(sClass.templateClass) sClass = sClass.templateClass;
2187                id += sClass.templateParams.count;
2188             }
2189             break;
2190          }
2191          id++;
2192       }
2193       if(curParam) break;
2194    }
2195    if(curParam)
2196    {
2197       arg = &_class.templateArgs[id];
2198       if(arg && param.type == type)
2199          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2200    }
2201    return arg;
2202 }
2203
2204 public Context SetupTemplatesContext(Class _class)
2205 {
2206    Context context = PushContext();
2207    context.templateTypesOnly = true;
2208    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2209    {
2210       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2211       for(; param; param = param.next)
2212       {
2213          if(param.type == type && param.identifier)
2214          {
2215             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2216             curContext.templateTypes.Add((BTNode)type);
2217          }
2218       }
2219    }
2220    else if(_class)
2221    {
2222       Class sClass;
2223       for(sClass = _class; sClass; sClass = sClass.base)
2224       {
2225          ClassTemplateParameter p;
2226          for(p = sClass.templateParams.first; p; p = p.next)
2227          {
2228             //OldList * specs = MkList();
2229             //Declarator decl = null;
2230             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2231             if(p.type == type)
2232             {
2233                TemplateParameter param = p.param;
2234                TemplatedType type;
2235                if(!param)
2236                {
2237                   // ADD DATA TYPE HERE...
2238                   p.param = param = TemplateParameter
2239                   {
2240                      identifier = MkIdentifier(p.name), type = p.type,
2241                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2242                   };
2243                }
2244                type = TemplatedType { key = (uintptr)p.name, param = param };
2245                curContext.templateTypes.Add((BTNode)type);
2246             }
2247          }
2248       }
2249    }
2250    return context;
2251 }
2252
2253 public void FinishTemplatesContext(Context context)
2254 {
2255    PopContext(context);
2256    FreeContext(context);
2257    delete context;
2258 }
2259
2260 public void ProcessMethodType(Method method)
2261 {
2262    if(!method.dataType)
2263    {
2264       Context context = SetupTemplatesContext(method._class);
2265
2266       method.dataType = ProcessTypeString(method.dataTypeString, false);
2267
2268       FinishTemplatesContext(context);
2269
2270       if(method.type != virtualMethod && method.dataType)
2271       {
2272          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2273          {
2274             if(!method._class.symbol)
2275                method._class.symbol = FindClass(method._class.fullName);
2276             method.dataType.thisClass = method._class.symbol;
2277          }
2278       }
2279
2280       // Why was this commented out? Working fine without now...
2281
2282       /*
2283       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2284          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2285          */
2286    }
2287
2288    /*
2289    if(type)
2290    {
2291       char * par = strstr(type, "(");
2292       char * classOp = null;
2293       int classOpLen = 0;
2294       if(par)
2295       {
2296          int c;
2297          for(c = par-type-1; c >= 0; c++)
2298          {
2299             if(type[c] == ':' && type[c+1] == ':')
2300             {
2301                classOp = type + c - 1;
2302                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2303                {
2304                   classOp--;
2305                   classOpLen++;
2306                }
2307                break;
2308             }
2309             else if(!isspace(type[c]))
2310                break;
2311          }
2312       }
2313       if(classOp)
2314       {
2315          char temp[1024];
2316          int typeLen = strlen(type);
2317          memcpy(temp, classOp, classOpLen);
2318          temp[classOpLen] = '\0';
2319          if(temp[0])
2320             _class = eSystem_FindClass(module, temp);
2321          else
2322             _class = null;
2323          method.dataTypeString = new char[typeLen - classOpLen + 1];
2324          memcpy(method.dataTypeString, type, classOp - type);
2325          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2326       }
2327       else
2328          method.dataTypeString = type;
2329    }
2330    */
2331 }
2332
2333
2334 public void ProcessPropertyType(Property prop)
2335 {
2336    if(!prop.dataType)
2337    {
2338       Context context = SetupTemplatesContext(prop._class);
2339       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2340       FinishTemplatesContext(context);
2341    }
2342 }
2343
2344 public void DeclareMethod(Method method, char * name)
2345 {
2346    Symbol symbol = method.symbol;
2347    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2348    {
2349       bool imported = false;
2350       bool dllImport = false;
2351
2352       if(!method.dataType)
2353          method.dataType = ProcessTypeString(method.dataTypeString, false);
2354
2355       if(!symbol || symbol._import || method.type == virtualMethod)
2356       {
2357          if(!symbol || method.type == virtualMethod)
2358          {
2359             Symbol classSym;
2360             if(!method._class.symbol)
2361                method._class.symbol = FindClass(method._class.fullName);
2362             classSym = method._class.symbol;
2363             if(!classSym._import)
2364             {
2365                ModuleImport module;
2366
2367                if(method._class.module && method._class.module.name)
2368                   module = FindModule(method._class.module);
2369                else
2370                   module = mainModule;
2371                classSym._import = ClassImport
2372                {
2373                   name = CopyString(method._class.fullName);
2374                   isRemote = method._class.isRemote;
2375                };
2376                module.classes.Add(classSym._import);
2377             }
2378             if(!symbol)
2379             {
2380                symbol = method.symbol = Symbol { };
2381             }
2382             if(!symbol._import)
2383             {
2384                symbol._import = (ClassImport)MethodImport
2385                {
2386                   name = CopyString(method.name);
2387                   isVirtual = method.type == virtualMethod;
2388                };
2389                classSym._import.methods.Add(symbol._import);
2390             }
2391             if(!symbol)
2392             {
2393                // Set the symbol type
2394                /*
2395                if(!type.thisClass)
2396                {
2397                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2398                }
2399                else if(type.thisClass == (void *)-1)
2400                {
2401                   type.thisClass = null;
2402                }
2403                */
2404                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2405                symbol.type = method.dataType;
2406                if(symbol.type) symbol.type.refCount++;
2407             }
2408             /*
2409             if(!method.thisClass || strcmp(method.thisClass, "void"))
2410                symbol.type.params.Insert(null,
2411                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2412             */
2413          }
2414          if(!method.dataType.dllExport)
2415          {
2416             imported = true;
2417             if(method._class.module != privateModule && method._class.module.importType != staticImport)
2418                dllImport = true;
2419          }
2420       }
2421
2422       /* MOVING THIS UP
2423       if(!method.dataType)
2424          method.dataType = ((Symbol)method.symbol).type;
2425          //ProcessMethodType(method);
2426       */
2427
2428       if(method.type != virtualMethod && method.dataType)
2429          DeclareType(method.dataType, true, true);
2430
2431       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2432       {
2433          // We need a declaration here :)
2434          Declaration decl;
2435          OldList * specifiers, * declarators;
2436          Declarator d;
2437          Declarator funcDecl;
2438          External external;
2439
2440          specifiers = MkList();
2441          declarators = MkList();
2442
2443          //if(imported)
2444          if(dllImport)
2445             ListAdd(specifiers, MkSpecifier(EXTERN));
2446          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2447             ListAdd(specifiers, MkSpecifier(STATIC));
2448
2449          if(method.type == virtualMethod)
2450          {
2451             ListAdd(specifiers, MkSpecifier(INT));
2452             d = MkDeclaratorIdentifier(MkIdentifier(name));
2453          }
2454          else
2455          {
2456             d = MkDeclaratorIdentifier(MkIdentifier(name));
2457             //if(imported)
2458             if(dllImport)
2459                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2460             {
2461                Context context = SetupTemplatesContext(method._class);
2462                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2463                FinishTemplatesContext(context);
2464             }
2465             funcDecl = GetFuncDecl(d);
2466
2467             if(dllImport)
2468             {
2469                Specifier spec, next;
2470                for(spec = specifiers->first; spec; spec = next)
2471                {
2472                   next = spec.next;
2473                   if(spec.type == extendedSpecifier)
2474                   {
2475                      specifiers->Remove(spec);
2476                      FreeSpecifier(spec);
2477                   }
2478                }
2479             }
2480
2481             // Add this parameter if not a static method
2482             if(method.dataType && !method.dataType.staticMethod)
2483             {
2484                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2485                {
2486                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2487                   TypeName thisParam = MkTypeName(MkListOne(
2488                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2489                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2490                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2491                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2492
2493                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2494                   {
2495                      TypeName param = funcDecl.function.parameters->first;
2496                      funcDecl.function.parameters->Remove(param);
2497                      FreeTypeName(param);
2498                   }
2499
2500                   if(!funcDecl.function.parameters)
2501                      funcDecl.function.parameters = MkList();
2502                   funcDecl.function.parameters->Insert(null, thisParam);
2503                }
2504             }
2505             // Make sure we don't have empty parameter declarations for static methods...
2506             /*
2507             else if(!funcDecl.function.parameters)
2508             {
2509                funcDecl.function.parameters = MkList();
2510                funcDecl.function.parameters->Insert(null,
2511                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2512             }*/
2513          }
2514          // TESTING THIS:
2515          ProcessDeclarator(d);
2516
2517          ListAdd(declarators, MkInitDeclarator(d, null));
2518
2519          decl = MkDeclaration(specifiers, declarators);
2520
2521          ReplaceThisClassSpecifiers(specifiers, method._class);
2522
2523          // Keep a different symbol for the function definition than the declaration...
2524          if(symbol.pointerExternal)
2525          {
2526             Symbol functionSymbol { };
2527
2528             // Copy symbol
2529             {
2530                *functionSymbol = *symbol;
2531                functionSymbol.string = CopyString(symbol.string);
2532                if(functionSymbol.type)
2533                   functionSymbol.type.refCount++;
2534             }
2535
2536             excludedSymbols->Add(functionSymbol);
2537             symbol.pointerExternal.symbol = functionSymbol;
2538          }
2539          external = MkExternalDeclaration(decl);
2540          if(curExternal)
2541             ast->Insert(curExternal ? curExternal.prev : null, external);
2542          external.symbol = symbol;
2543          symbol.pointerExternal = external;
2544       }
2545       else if(ast)
2546       {
2547          // Move declaration higher...
2548          ast->Move(symbol.pointerExternal, curExternal.prev);
2549       }
2550
2551       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2552    }
2553 }
2554
2555 char * ReplaceThisClass(Class _class)
2556 {
2557    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2558    {
2559       bool first = true;
2560       int p = 0;
2561       ClassTemplateParameter param;
2562       int lastParam = -1;
2563
2564       char className[1024];
2565       strcpy(className, _class.fullName);
2566       for(param = _class.templateParams.first; param; param = param.next)
2567       {
2568          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2569          {
2570             if(first) strcat(className, "<");
2571             if(!first) strcat(className, ", ");
2572             if(lastParam + 1 != p)
2573             {
2574                strcat(className, param.name);
2575                strcat(className, " = ");
2576             }
2577             strcat(className, param.name);
2578             first = false;
2579             lastParam = p;
2580          }
2581          p++;
2582       }
2583       if(!first)
2584       {
2585          int len = strlen(className);
2586          if(className[len-1] == '>') className[len++] = ' ';
2587          className[len++] = '>';
2588          className[len++] = '\0';
2589       }
2590       return CopyString(className);
2591    }
2592    else
2593       return CopyString(_class.fullName);
2594 }
2595
2596 Type ReplaceThisClassType(Class _class)
2597 {
2598    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2599    {
2600       bool first = true;
2601       int p = 0;
2602       ClassTemplateParameter param;
2603       int lastParam = -1;
2604       char className[1024];
2605       strcpy(className, _class.fullName);
2606
2607       for(param = _class.templateParams.first; param; param = param.next)
2608       {
2609          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2610          {
2611             if(first) strcat(className, "<");
2612             if(!first) strcat(className, ", ");
2613             if(lastParam + 1 != p)
2614             {
2615                strcat(className, param.name);
2616                strcat(className, " = ");
2617             }
2618             strcat(className, param.name);
2619             first = false;
2620             lastParam = p;
2621          }
2622          p++;
2623       }
2624       if(!first)
2625       {
2626          int len = strlen(className);
2627          if(className[len-1] == '>') className[len++] = ' ';
2628          className[len++] = '>';
2629          className[len++] = '\0';
2630       }
2631       return MkClassType(className);
2632       //return ProcessTypeString(className, false);
2633    }
2634    else
2635    {
2636       return MkClassType(_class.fullName);
2637       //return ProcessTypeString(_class.fullName, false);
2638    }
2639 }
2640
2641 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2642 {
2643    if(specs != null && _class)
2644    {
2645       Specifier spec;
2646       for(spec = specs.first; spec; spec = spec.next)
2647       {
2648          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2649          {
2650             spec.type = nameSpecifier;
2651             spec.name = ReplaceThisClass(_class);
2652             spec.symbol = FindClass(spec.name); //_class.symbol;
2653          }
2654       }
2655    }
2656 }
2657
2658 // Returns imported or not
2659 bool DeclareFunction(GlobalFunction function, char * name)
2660 {
2661    Symbol symbol = function.symbol;
2662    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2663    {
2664       bool imported = false;
2665       bool dllImport = false;
2666
2667       if(!function.dataType)
2668       {
2669          function.dataType = ProcessTypeString(function.dataTypeString, false);
2670          if(!function.dataType.thisClass)
2671             function.dataType.staticMethod = true;
2672       }
2673
2674       if(inCompiler)
2675       {
2676          if(!symbol)
2677          {
2678             ModuleImport module = FindModule(function.module);
2679             // WARNING: This is not added anywhere...
2680             symbol = function.symbol = Symbol {  };
2681
2682             if(module.name)
2683             {
2684                if(!function.dataType.dllExport)
2685                {
2686                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2687                   module.functions.Add(symbol._import);
2688                }
2689             }
2690             // Set the symbol type
2691             {
2692                symbol.type = ProcessTypeString(function.dataTypeString, false);
2693                if(!symbol.type.thisClass)
2694                   symbol.type.staticMethod = true;
2695             }
2696          }
2697          imported = symbol._import ? true : false;
2698          if(imported && function.module != privateModule && function.module.importType != staticImport)
2699             dllImport = true;
2700       }
2701
2702       DeclareType(function.dataType, true, true);
2703
2704       if(inCompiler)
2705       {
2706          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2707          {
2708             // We need a declaration here :)
2709             Declaration decl;
2710             OldList * specifiers, * declarators;
2711             Declarator d;
2712             Declarator funcDecl;
2713             External external;
2714
2715             specifiers = MkList();
2716             declarators = MkList();
2717
2718             //if(imported)
2719                ListAdd(specifiers, MkSpecifier(EXTERN));
2720             /*
2721             else
2722                ListAdd(specifiers, MkSpecifier(STATIC));
2723             */
2724
2725             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2726             //if(imported)
2727             if(dllImport)
2728                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2729
2730             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2731             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2732             if(function.module.importType == staticImport)
2733             {
2734                Specifier spec;
2735                for(spec = specifiers->first; spec; spec = spec.next)
2736                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2737                   {
2738                      specifiers->Remove(spec);
2739                      FreeSpecifier(spec);
2740                      break;
2741                   }
2742             }
2743
2744             funcDecl = GetFuncDecl(d);
2745
2746             // Make sure we don't have empty parameter declarations for static methods...
2747             if(funcDecl && !funcDecl.function.parameters)
2748             {
2749                funcDecl.function.parameters = MkList();
2750                funcDecl.function.parameters->Insert(null,
2751                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2752             }
2753
2754             ListAdd(declarators, MkInitDeclarator(d, null));
2755
2756             {
2757                Context oldCtx = curContext;
2758                curContext = globalContext;
2759                decl = MkDeclaration(specifiers, declarators);
2760                curContext = oldCtx;
2761             }
2762
2763             // Keep a different symbol for the function definition than the declaration...
2764             if(symbol.pointerExternal)
2765             {
2766                Symbol functionSymbol { };
2767                // Copy symbol
2768                {
2769                   *functionSymbol = *symbol;
2770                   functionSymbol.string = CopyString(symbol.string);
2771                   if(functionSymbol.type)
2772                      functionSymbol.type.refCount++;
2773                }
2774
2775                excludedSymbols->Add(functionSymbol);
2776
2777                symbol.pointerExternal.symbol = functionSymbol;
2778             }
2779             external = MkExternalDeclaration(decl);
2780             if(curExternal)
2781                ast->Insert(curExternal.prev, external);
2782             external.symbol = symbol;
2783             symbol.pointerExternal = external;
2784          }
2785          else
2786          {
2787             // Move declaration higher...
2788             ast->Move(symbol.pointerExternal, curExternal.prev);
2789          }
2790
2791          if(curExternal)
2792             symbol.id = curExternal.symbol.idCode;
2793       }
2794    }
2795    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2796 }
2797
2798 void DeclareGlobalData(GlobalData data)
2799 {
2800    Symbol symbol = data.symbol;
2801    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2802    {
2803       if(inCompiler)
2804       {
2805          if(!symbol)
2806             symbol = data.symbol = Symbol { };
2807       }
2808       if(!data.dataType)
2809          data.dataType = ProcessTypeString(data.dataTypeString, false);
2810       DeclareType(data.dataType, true, true);
2811       if(inCompiler)
2812       {
2813          if(!symbol.pointerExternal)
2814          {
2815             // We need a declaration here :)
2816             Declaration decl;
2817             OldList * specifiers, * declarators;
2818             Declarator d;
2819             External external;
2820
2821             specifiers = MkList();
2822             declarators = MkList();
2823
2824             ListAdd(specifiers, MkSpecifier(EXTERN));
2825             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2826             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2827
2828             ListAdd(declarators, MkInitDeclarator(d, null));
2829
2830             decl = MkDeclaration(specifiers, declarators);
2831             external = MkExternalDeclaration(decl);
2832             if(curExternal)
2833                ast->Insert(curExternal.prev, external);
2834             external.symbol = symbol;
2835             symbol.pointerExternal = external;
2836          }
2837          else
2838          {
2839             // Move declaration higher...
2840             ast->Move(symbol.pointerExternal, curExternal.prev);
2841          }
2842
2843          if(curExternal)
2844             symbol.id = curExternal.symbol.idCode;
2845       }
2846    }
2847 }
2848
2849 class Conversion : struct
2850 {
2851    Conversion prev, next;
2852    Property convert;
2853    bool isGet;
2854    Type resultType;
2855 };
2856
2857 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2858 {
2859    if(source && dest)
2860    {
2861       // Property convert;
2862
2863       if(source.kind == templateType && dest.kind != templateType)
2864       {
2865          Type type = ProcessTemplateParameterType(source.templateParameter);
2866          if(type) source = type;
2867       }
2868
2869       if(dest.kind == templateType && source.kind != templateType)
2870       {
2871          Type type = ProcessTemplateParameterType(dest.templateParameter);
2872          if(type) dest = type;
2873       }
2874
2875       if(dest.classObjectType == typedObject)
2876       {
2877          if(source.classObjectType != anyObject)
2878             return true;
2879          else
2880          {
2881             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2882             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2883             {
2884                return true;
2885             }
2886          }
2887       }
2888       else
2889       {
2890          if(source.classObjectType == anyObject)
2891             return true;
2892          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2893             return true;
2894       }
2895
2896       if((dest.kind == structType && source.kind == structType) ||
2897          (dest.kind == unionType && source.kind == unionType))
2898       {
2899          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2900              (source.members.first && source.members.first == dest.members.first))
2901             return true;
2902       }
2903
2904       if(dest.kind == ellipsisType && source.kind != voidType)
2905          return true;
2906
2907       if(dest.kind == pointerType && dest.type.kind == voidType &&
2908          ((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))
2909          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2910
2911          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2912
2913          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2914          return true;
2915       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2916          ((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))
2917          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2918
2919          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2920
2921          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2922          return true;
2923
2924       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2925       {
2926          if(source._class.registered && source._class.registered.type == unitClass)
2927          {
2928             if(conversions != null)
2929             {
2930                if(source._class.registered == dest._class.registered)
2931                   return true;
2932             }
2933             else
2934             {
2935                Class sourceBase, destBase;
2936                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2937                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2938                if(sourceBase == destBase)
2939                   return true;
2940             }
2941          }
2942          // Don't match enum inheriting from other enum if resolving enumeration values
2943          // TESTING: !dest.classObjectType
2944          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2945             (enumBaseType ||
2946                (!source._class.registered || source._class.registered.type != enumClass) ||
2947                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2948             return true;
2949          else
2950          {
2951             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2952             if(enumBaseType &&
2953                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2954                ((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)
2955             {
2956                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2957                {
2958                   return true;
2959                }
2960             }
2961          }
2962       }
2963
2964       // JUST ADDED THIS...
2965       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2966          return true;
2967
2968       if(doConversion)
2969       {
2970          // Just added this for Straight conversion of ColorAlpha => Color
2971          if(source.kind == classType)
2972          {
2973             Class _class;
2974             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
2975             {
2976                Property convert;
2977                for(convert = _class.conversions.first; convert; convert = convert.next)
2978                {
2979                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
2980                   {
2981                      Conversion after = (conversions != null) ? conversions.last : null;
2982
2983                      if(!convert.dataType)
2984                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
2985                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
2986                      {
2987                         if(!conversions && !convert.Get)
2988                            return true;
2989                         else if(conversions != null)
2990                         {
2991                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
2992                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
2993                               (dest.kind != classType || dest._class.registered != _class.base))
2994                               return true;
2995                            else
2996                            {
2997                               Conversion conv { convert = convert, isGet = true };
2998                               // conversions.Add(conv);
2999                               conversions.Insert(after, conv);
3000                               return true;
3001                            }
3002                         }
3003                      }
3004                   }
3005                }
3006             }
3007          }
3008
3009          // MOVING THIS??
3010
3011          if(dest.kind == classType)
3012          {
3013             Class _class;
3014             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3015             {
3016                Property convert;
3017                for(convert = _class.conversions.first; convert; convert = convert.next)
3018                {
3019                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3020                   {
3021                      // Conversion after = (conversions != null) ? conversions.last : null;
3022
3023                      if(!convert.dataType)
3024                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3025                      // Just added this equality check to prevent recursion.... Make it safer?
3026                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3027                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3028                      {
3029                         if(!conversions && !convert.Set)
3030                            return true;
3031                         else if(conversions != null)
3032                         {
3033                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3034                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3035                               (source.kind != classType || source._class.registered != _class.base))
3036                               return true;
3037                            else
3038                            {
3039                               // *** Testing this! ***
3040                               Conversion conv { convert = convert };
3041                               conversions.Add(conv);
3042                               //conversions.Insert(after, conv);
3043                               return true;
3044                            }
3045                         }
3046                      }
3047                   }
3048                }
3049             }
3050             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3051             {
3052                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3053                   (source.kind != classType || source._class.registered.type != structClass))
3054                   return true;
3055             }*/
3056
3057             // TESTING THIS... IS THIS OK??
3058             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3059             {
3060                if(!dest._class.registered.dataType)
3061                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3062                // Only support this for classes...
3063                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3064                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3065                {
3066                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3067                   {
3068                      return true;
3069                   }
3070                }
3071             }
3072          }
3073
3074          // Moved this lower
3075          if(source.kind == classType)
3076          {
3077             Class _class;
3078             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3079             {
3080                Property convert;
3081                for(convert = _class.conversions.first; convert; convert = convert.next)
3082                {
3083                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3084                   {
3085                      Conversion after = (conversions != null) ? conversions.last : null;
3086
3087                      if(!convert.dataType)
3088                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3089                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3090                      {
3091                         if(!conversions && !convert.Get)
3092                            return true;
3093                         else if(conversions != null)
3094                         {
3095                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3096                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3097                               (dest.kind != classType || dest._class.registered != _class.base))
3098                               return true;
3099                            else
3100                            {
3101                               Conversion conv { convert = convert, isGet = true };
3102
3103                               // conversions.Add(conv);
3104                               conversions.Insert(after, conv);
3105                               return true;
3106                            }
3107                         }
3108                      }
3109                   }
3110                }
3111             }
3112
3113             // TESTING THIS... IS THIS OK??
3114             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3115             {
3116                if(!source._class.registered.dataType)
3117                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3118                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3119                {
3120                   return true;
3121                }
3122             }
3123          }
3124       }
3125
3126       if(source.kind == classType || source.kind == subClassType)
3127          ;
3128       else if(dest.kind == source.kind &&
3129          (dest.kind != structType && dest.kind != unionType &&
3130           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3131           return true;
3132       // RECENTLY ADDED THESE
3133       else if(dest.kind == doubleType && source.kind == floatType)
3134          return true;
3135       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3136          return true;
3137       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3138          return true;
3139       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3140          return true;
3141       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3142          return true;
3143       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3144          return true;
3145       else if(source.kind == enumType &&
3146          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3147           return true;
3148       else if(dest.kind == enumType &&
3149          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3150           return true;
3151       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3152               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3153       {
3154          Type paramSource, paramDest;
3155
3156          if(dest.kind == methodType)
3157             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3158          if(source.kind == methodType)
3159             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3160
3161          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3162          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3163          if(dest.kind == methodType)
3164             dest = dest.method.dataType;
3165          if(source.kind == methodType)
3166             source = source.method.dataType;
3167
3168          paramSource = source.params.first;
3169          if(paramSource && paramSource.kind == voidType) paramSource = null;
3170          paramDest = dest.params.first;
3171          if(paramDest && paramDest.kind == voidType) paramDest = null;
3172
3173
3174          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3175             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3176          {
3177             // Source thisClass must be derived from destination thisClass
3178             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3179                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3180             {
3181                if(paramDest && paramDest.kind == classType)
3182                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3183                else
3184                   Compiler_Error($"method class should not take an object\n");
3185                return false;
3186             }
3187             paramDest = paramDest.next;
3188          }
3189          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3190          {
3191             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3192             {
3193                if(dest.thisClass)
3194                {
3195                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3196                   {
3197                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3198                      return false;
3199                   }
3200                }
3201                else
3202                {
3203                   // THIS WAS BACKWARDS:
3204                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3205                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3206                   {
3207                      if(owningClassDest)
3208                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3209                      else
3210                         Compiler_Error($"overriding class expected to be derived from method class\n");
3211                      return false;
3212                   }
3213                }
3214                paramSource = paramSource.next;
3215             }
3216             else
3217             {
3218                if(dest.thisClass)
3219                {
3220                   // Source thisClass must be derived from destination thisClass
3221                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3222                   {
3223                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3224                      return false;
3225                   }
3226                }
3227                else
3228                {
3229                   // THIS WAS BACKWARDS TOO??
3230                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3231                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3232                   {
3233                      //if(owningClass)
3234                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3235                      //else
3236                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3237                      return false;
3238                   }
3239                }
3240             }
3241          }
3242
3243
3244          // Source return type must be derived from destination return type
3245          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3246          {
3247             Compiler_Warning($"incompatible return type for function\n");
3248             return false;
3249          }
3250
3251          // Check parameters
3252
3253          for(; paramDest; paramDest = paramDest.next)
3254          {
3255             if(!paramSource)
3256             {
3257                //Compiler_Warning($"not enough parameters\n");
3258                Compiler_Error($"not enough parameters\n");
3259                return false;
3260             }
3261             {
3262                Type paramDestType = paramDest;
3263                Type paramSourceType = paramSource;
3264                Type type = paramDestType;
3265
3266                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3267                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3268                   paramSource.kind != templateType)
3269                {
3270                   int id = 0;
3271                   ClassTemplateParameter curParam = null;
3272                   Class sClass;
3273                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3274                   {
3275                      id = 0;
3276                      if(sClass.templateClass) sClass = sClass.templateClass;
3277                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3278                      {
3279                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3280                         {
3281                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3282                            {
3283                               if(sClass.templateClass) sClass = sClass.templateClass;
3284                               id += sClass.templateParams.count;
3285                            }
3286                            break;
3287                         }
3288                         id++;
3289                      }
3290                      if(curParam) break;
3291                   }
3292
3293                   if(curParam)
3294                   {
3295                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3296                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3297                   }
3298                }
3299
3300                // paramDest must be derived from paramSource
3301                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3302                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3303                {
3304                   char type[1024];
3305                   type[0] = 0;
3306                   PrintType(paramDest, type, false, true);
3307                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3308
3309                   if(paramDestType != paramDest)
3310                      FreeType(paramDestType);
3311                   return false;
3312                }
3313                if(paramDestType != paramDest)
3314                   FreeType(paramDestType);
3315             }
3316
3317             paramSource = paramSource.next;
3318          }
3319          if(paramSource)
3320          {
3321             Compiler_Error($"too many parameters\n");
3322             return false;
3323          }
3324          return true;
3325       }
3326       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3327       {
3328          return true;
3329       }
3330       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3331          (source.kind == arrayType || source.kind == pointerType))
3332       {
3333          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3334             return true;
3335       }
3336    }
3337    return false;
3338 }
3339
3340 static void FreeConvert(Conversion convert)
3341 {
3342    if(convert.resultType)
3343       FreeType(convert.resultType);
3344 }
3345
3346 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3347                               char * string, OldList conversions)
3348 {
3349    BTNamedLink link;
3350
3351    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3352    {
3353       Class _class = link.data;
3354       if(_class.type == enumClass)
3355       {
3356          OldList converts { };
3357          Type type { };
3358          type.kind = classType;
3359
3360          if(!_class.symbol)
3361             _class.symbol = FindClass(_class.fullName);
3362          type._class = _class.symbol;
3363
3364          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3365          {
3366             NamedLink value;
3367             Class enumClass = eSystem_FindClass(privateModule, "enum");
3368             if(enumClass)
3369             {
3370                Class baseClass;
3371                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3372                {
3373                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3374                   for(value = e.values.first; value; value = value.next)
3375                   {
3376                      if(!strcmp(value.name, string))
3377                         break;
3378                   }
3379                   if(value)
3380                   {
3381                      FreeExpContents(sourceExp);
3382                      FreeType(sourceExp.expType);
3383
3384                      sourceExp.isConstant = true;
3385                      sourceExp.expType = MkClassType(baseClass.fullName);
3386                      //if(inCompiler)
3387                      {
3388                         char constant[256];
3389                         sourceExp.type = constantExp;
3390                         if(!strcmp(baseClass.dataTypeString, "int"))
3391                            sprintf(constant, "%d",(int)value.data);
3392                         else
3393                            sprintf(constant, "0x%X",(int)value.data);
3394                         sourceExp.constant = CopyString(constant);
3395                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3396                      }
3397
3398                      while(converts.first)
3399                      {
3400                         Conversion convert = converts.first;
3401                         converts.Remove(convert);
3402                         conversions.Add(convert);
3403                      }
3404                      delete type;
3405                      return true;
3406                   }
3407                }
3408             }
3409          }
3410          if(converts.first)
3411             converts.Free(FreeConvert);
3412          delete type;
3413       }
3414    }
3415    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3416       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3417          return true;
3418    return false;
3419 }
3420
3421 public bool ModuleVisibility(Module searchIn, Module searchFor)
3422 {
3423    SubModule subModule;
3424
3425    if(searchFor == searchIn)
3426       return true;
3427
3428    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3429    {
3430       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3431       {
3432          if(ModuleVisibility(subModule.module, searchFor))
3433             return true;
3434       }
3435    }
3436    return false;
3437 }
3438
3439 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3440 {
3441    Module module;
3442
3443    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3444       return true;
3445    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3446       return true;
3447    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3448       return true;
3449
3450    for(module = mainModule.application.allModules.first; module; module = module.next)
3451    {
3452       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3453          return true;
3454    }
3455    return false;
3456 }
3457
3458 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3459 {
3460    Type source = sourceExp.expType;
3461    Type realDest = dest;
3462    Type backupSourceExpType = null;
3463
3464    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3465       return true;
3466
3467    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3468    {
3469        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3470        {
3471           Class sourceBase, destBase;
3472           for(sourceBase = source._class.registered;
3473               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3474               sourceBase = sourceBase.base);
3475           for(destBase = dest._class.registered;
3476               destBase && destBase.base && destBase.base.type != systemClass;
3477               destBase = destBase.base);
3478           //if(source._class.registered == dest._class.registered)
3479           if(sourceBase == destBase)
3480              return true;
3481        }
3482    }
3483
3484    if(source)
3485    {
3486       OldList * specs;
3487       bool flag = false;
3488       int64 value = MAXINT;
3489
3490       source.refCount++;
3491       dest.refCount++;
3492
3493       if(sourceExp.type == constantExp)
3494       {
3495          if(source.isSigned)
3496             value = strtoll(sourceExp.constant, null, 0);
3497          else
3498             value = strtoull(sourceExp.constant, null, 0);
3499       }
3500       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3501       {
3502          if(source.isSigned)
3503             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3504          else
3505             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3506       }
3507
3508       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3509          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3510       {
3511          FreeType(source);
3512          source = Type { kind = intType, isSigned = false, refCount = 1 };
3513       }
3514
3515       if(dest.kind == classType)
3516       {
3517          Class _class = dest._class ? dest._class.registered : null;
3518
3519          if(_class && _class.type == unitClass)
3520          {
3521             if(source.kind != classType)
3522             {
3523                Type tempType { };
3524                Type tempDest, tempSource;
3525
3526                for(; _class.base.type != systemClass; _class = _class.base);
3527                tempSource = dest;
3528                tempDest = tempType;
3529
3530                tempType.kind = classType;
3531                if(!_class.symbol)
3532                   _class.symbol = FindClass(_class.fullName);
3533
3534                tempType._class = _class.symbol;
3535                tempType.truth = dest.truth;
3536                if(tempType._class)
3537                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3538
3539                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3540                backupSourceExpType = sourceExp.expType;
3541                sourceExp.expType = dest; dest.refCount++;
3542                //sourceExp.expType = MkClassType(_class.fullName);
3543                flag = true;
3544
3545                delete tempType;
3546             }
3547          }
3548
3549
3550          // Why wasn't there something like this?
3551          if(_class && _class.type == bitClass && source.kind != classType)
3552          {
3553             if(!dest._class.registered.dataType)
3554                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3555             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3556             {
3557                FreeType(source);
3558                FreeType(sourceExp.expType);
3559                source = sourceExp.expType = MkClassType(dest._class.string);
3560                source.refCount++;
3561
3562                //source.kind = classType;
3563                //source._class = dest._class;
3564             }
3565          }
3566
3567          // Adding two enumerations
3568          /*
3569          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3570          {
3571             if(!source._class.registered.dataType)
3572                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3573             if(!dest._class.registered.dataType)
3574                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3575
3576             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3577             {
3578                FreeType(source);
3579                source = sourceExp.expType = MkClassType(dest._class.string);
3580                source.refCount++;
3581
3582                //source.kind = classType;
3583                //source._class = dest._class;
3584             }
3585          }*/
3586
3587          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3588          {
3589             OldList * specs = MkList();
3590             Declarator decl;
3591             char string[1024];
3592
3593             ReadString(string, sourceExp.string);
3594             decl = SpecDeclFromString(string, specs, null);
3595
3596             FreeExpContents(sourceExp);
3597             FreeType(sourceExp.expType);
3598
3599             sourceExp.type = classExp;
3600             sourceExp._classExp.specifiers = specs;
3601             sourceExp._classExp.decl = decl;
3602             sourceExp.expType = dest;
3603             dest.refCount++;
3604
3605             FreeType(source);
3606             FreeType(dest);
3607             if(backupSourceExpType) FreeType(backupSourceExpType);
3608             return true;
3609          }
3610       }
3611       else if(source.kind == classType)
3612       {
3613          Class _class = source._class ? source._class.registered : null;
3614
3615          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3616          {
3617             /*
3618             if(dest.kind != classType)
3619             {
3620                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3621                if(!source._class.registered.dataType)
3622                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3623
3624                FreeType(dest);
3625                dest = MkClassType(source._class.string);
3626                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3627                //   dest = MkClassType(source._class.string);
3628             }
3629             */
3630
3631             if(dest.kind != classType)
3632             {
3633                Type tempType { };
3634                Type tempDest, tempSource;
3635
3636                if(!source._class.registered.dataType)
3637                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3638
3639                for(; _class.base.type != systemClass; _class = _class.base);
3640                tempDest = source;
3641                tempSource = tempType;
3642                tempType.kind = classType;
3643                tempType._class = FindClass(_class.fullName);
3644                tempType.truth = source.truth;
3645                tempType.classObjectType = source.classObjectType;
3646
3647                if(tempType._class)
3648                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3649
3650                // PUT THIS BACK TESTING UNITS?
3651                if(conversions.last)
3652                {
3653                   ((Conversion)(conversions.last)).resultType = dest;
3654                   dest.refCount++;
3655                }
3656
3657                FreeType(sourceExp.expType);
3658                sourceExp.expType = MkClassType(_class.fullName);
3659                sourceExp.expType.truth = source.truth;
3660                sourceExp.expType.classObjectType = source.classObjectType;
3661
3662                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3663
3664                if(!sourceExp.destType)
3665                {
3666                   FreeType(sourceExp.destType);
3667                   sourceExp.destType = sourceExp.expType;
3668                   if(sourceExp.expType)
3669                      sourceExp.expType.refCount++;
3670                }
3671                //flag = true;
3672                //source = _class.dataType;
3673
3674
3675                // TOCHECK: TESTING THIS NEW CODE
3676                if(!_class.dataType)
3677                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3678                FreeType(dest);
3679                dest = MkClassType(source._class.string);
3680                dest.truth = source.truth;
3681                dest.classObjectType = source.classObjectType;
3682
3683                FreeType(source);
3684                source = _class.dataType;
3685                source.refCount++;
3686
3687                delete tempType;
3688             }
3689          }
3690       }
3691
3692       if(!flag)
3693       {
3694          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3695          {
3696             FreeType(source);
3697             FreeType(dest);
3698             return true;
3699          }
3700       }
3701
3702       // Implicit Casts
3703       /*
3704       if(source.kind == classType)
3705       {
3706          Class _class = source._class.registered;
3707          if(_class.type == unitClass)
3708          {
3709             if(!_class.dataType)
3710                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3711             source = _class.dataType;
3712          }
3713       }*/
3714
3715       if(dest.kind == classType)
3716       {
3717          Class _class = dest._class ? dest._class.registered : null;
3718          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") ||
3719             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3720          {
3721             if(_class.type == normalClass || _class.type == noHeadClass)
3722             {
3723                Expression newExp { };
3724                *newExp = *sourceExp;
3725                if(sourceExp.destType) sourceExp.destType.refCount++;
3726                if(sourceExp.expType)  sourceExp.expType.refCount++;
3727                sourceExp.type = castExp;
3728                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3729                sourceExp.cast.exp = newExp;
3730                FreeType(sourceExp.expType);
3731                sourceExp.expType = null;
3732                ProcessExpressionType(sourceExp);
3733
3734                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3735                if(!inCompiler)
3736                {
3737                   FreeType(sourceExp.expType);
3738                   sourceExp.expType = dest;
3739                }
3740
3741                FreeType(source);
3742                if(inCompiler) FreeType(dest);
3743
3744                if(backupSourceExpType) FreeType(backupSourceExpType);
3745                return true;
3746             }
3747
3748             if(!_class.dataType)
3749                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3750             FreeType(dest);
3751             dest = _class.dataType;
3752             dest.refCount++;
3753          }
3754
3755          // Accept lower precision types for units, since we want to keep the unit type
3756          if(dest.kind == doubleType &&
3757             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3758              source.kind == charType || source.kind == _BoolType))
3759          {
3760             specs = MkListOne(MkSpecifier(DOUBLE));
3761          }
3762          else if(dest.kind == floatType &&
3763             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3764             source.kind == _BoolType || source.kind == doubleType))
3765          {
3766             specs = MkListOne(MkSpecifier(FLOAT));
3767          }
3768          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3769             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3770          {
3771             specs = MkList();
3772             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3773             ListAdd(specs, MkSpecifier(INT64));
3774          }
3775          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3776             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3777          {
3778             specs = MkList();
3779             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3780             ListAdd(specs, MkSpecifier(INT));
3781          }
3782          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3783             source.kind == floatType || source.kind == doubleType))
3784          {
3785             specs = MkList();
3786             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3787             ListAdd(specs, MkSpecifier(SHORT));
3788          }
3789          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3790             source.kind == floatType || source.kind == doubleType))
3791          {
3792             specs = MkList();
3793             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3794             ListAdd(specs, MkSpecifier(CHAR));
3795          }
3796          else
3797          {
3798             FreeType(source);
3799             FreeType(dest);
3800             if(backupSourceExpType)
3801             {
3802                // Failed to convert: revert previous exp type
3803                if(sourceExp.expType) FreeType(sourceExp.expType);
3804                sourceExp.expType = backupSourceExpType;
3805             }
3806             return false;
3807          }
3808       }
3809       else if(dest.kind == doubleType &&
3810          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3811           source.kind == _BoolType || source.kind == charType))
3812       {
3813          specs = MkListOne(MkSpecifier(DOUBLE));
3814       }
3815       else if(dest.kind == floatType &&
3816          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3817       {
3818          specs = MkListOne(MkSpecifier(FLOAT));
3819       }
3820       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3821          (value == 1 || value == 0))
3822       {
3823          specs = MkList();
3824          ListAdd(specs, MkSpecifier(BOOL));
3825       }
3826       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3827          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3828       {
3829          specs = MkList();
3830          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3831          ListAdd(specs, MkSpecifier(CHAR));
3832       }
3833       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3834          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3835       {
3836          specs = MkList();
3837          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3838          ListAdd(specs, MkSpecifier(SHORT));
3839       }
3840       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3841       {
3842          specs = MkList();
3843          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3844          ListAdd(specs, MkSpecifier(INT));
3845       }
3846       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3847       {
3848          specs = MkList();
3849          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3850          ListAdd(specs, MkSpecifier(INT64));
3851       }
3852       else if(dest.kind == enumType &&
3853          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3854       {
3855          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3856       }
3857       else
3858       {
3859          FreeType(source);
3860          FreeType(dest);
3861          if(backupSourceExpType)
3862          {
3863             // Failed to convert: revert previous exp type
3864             if(sourceExp.expType) FreeType(sourceExp.expType);
3865             sourceExp.expType = backupSourceExpType;
3866          }
3867          return false;
3868       }
3869
3870       if(!flag)
3871       {
3872          Expression newExp { };
3873          *newExp = *sourceExp;
3874          newExp.prev = null;
3875          newExp.next = null;
3876          if(sourceExp.destType) sourceExp.destType.refCount++;
3877          if(sourceExp.expType)  sourceExp.expType.refCount++;
3878
3879          sourceExp.type = castExp;
3880          if(realDest.kind == classType)
3881          {
3882             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3883             FreeList(specs, FreeSpecifier);
3884          }
3885          else
3886             sourceExp.cast.typeName = MkTypeName(specs, null);
3887          if(newExp.type == opExp)
3888          {
3889             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3890          }
3891          else
3892             sourceExp.cast.exp = newExp;
3893
3894          FreeType(sourceExp.expType);
3895          sourceExp.expType = null;
3896          ProcessExpressionType(sourceExp);
3897       }
3898       else
3899          FreeList(specs, FreeSpecifier);
3900
3901       FreeType(dest);
3902       FreeType(source);
3903       if(backupSourceExpType) FreeType(backupSourceExpType);
3904
3905       return true;
3906    }
3907    else
3908    {
3909       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3910       if(sourceExp.type == identifierExp)
3911       {
3912          Identifier id = sourceExp.identifier;
3913          if(dest.kind == classType)
3914          {
3915             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3916             {
3917                Class _class = dest._class.registered;
3918                Class enumClass = eSystem_FindClass(privateModule, "enum");
3919                if(enumClass)
3920                {
3921                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3922                   {
3923                      NamedLink value;
3924                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3925                      for(value = e.values.first; value; value = value.next)
3926                      {
3927                         if(!strcmp(value.name, id.string))
3928                            break;
3929                      }
3930                      if(value)
3931                      {
3932                         FreeExpContents(sourceExp);
3933                         FreeType(sourceExp.expType);
3934
3935                         sourceExp.isConstant = true;
3936                         sourceExp.expType = MkClassType(_class.fullName);
3937                         //if(inCompiler)
3938                         {
3939                            char constant[256];
3940                            sourceExp.type = constantExp;
3941                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3942                               sprintf(constant, "%d", (int) value.data);
3943                            else
3944                               sprintf(constant, "0x%X", (int) value.data);
3945                            sourceExp.constant = CopyString(constant);
3946                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3947                         }
3948                         return true;
3949                      }
3950                   }
3951                }
3952             }
3953          }
3954
3955          // Loop through all enum classes
3956          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3957             return true;
3958       }
3959    }
3960    return false;
3961 }
3962
3963 #define TERTIARY(o, name, m, t, p) \
3964    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
3965    {                                                              \
3966       exp.type = constantExp;                                    \
3967       exp.string = p(op1.m ? op2.m : op3.m);                     \
3968       if(!exp.expType) \
3969          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3970       return true;                                                \
3971    }
3972
3973 #define BINARY(o, name, m, t, p) \
3974    static bool name(Expression exp, Operand op1, Operand op2)   \
3975    {                                                              \
3976       t value2 = op2.m;                                           \
3977       exp.type = constantExp;                                    \
3978       exp.string = p(op1.m o value2);                     \
3979       if(!exp.expType) \
3980          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3981       return true;                                                \
3982    }
3983
3984 #define BINARY_DIVIDE(o, name, m, t, p) \
3985    static bool name(Expression exp, Operand op1, Operand op2)   \
3986    {                                                              \
3987       t value2 = op2.m;                                           \
3988       exp.type = constantExp;                                    \
3989       exp.string = p(value2 ? (op1.m o value2) : 0);             \
3990       if(!exp.expType) \
3991          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3992       return true;                                                \
3993    }
3994
3995 #define UNARY(o, name, m, t, p) \
3996    static bool name(Expression exp, Operand op1)                \
3997    {                                                              \
3998       exp.type = constantExp;                                    \
3999       exp.string = p((t)(o op1.m));                                   \
4000       if(!exp.expType) \
4001          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4002       return true;                                                \
4003    }
4004
4005 #define OPERATOR_ALL(macro, o, name) \
4006    macro(o, Int##name, i, int, PrintInt) \
4007    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4008    macro(o, Int64##name, i, int, PrintInt64) \
4009    macro(o, UInt64##name, ui, unsigned int, PrintUInt64) \
4010    macro(o, Short##name, s, short, PrintShort) \
4011    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4012    macro(o, Char##name, c, char, PrintChar) \
4013    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4014    macro(o, Float##name, f, float, PrintFloat) \
4015    macro(o, Double##name, d, double, PrintDouble)
4016
4017 #define OPERATOR_INTTYPES(macro, o, name) \
4018    macro(o, Int##name, i, int, PrintInt) \
4019    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4020    macro(o, Int64##name, i, int, PrintInt64) \
4021    macro(o, UInt64##name, ui, unsigned int, PrintUInt64) \
4022    macro(o, Short##name, s, short, PrintShort) \
4023    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4024    macro(o, Char##name, c, char, PrintChar) \
4025    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4026
4027
4028 // binary arithmetic
4029 OPERATOR_ALL(BINARY, +, Add)
4030 OPERATOR_ALL(BINARY, -, Sub)
4031 OPERATOR_ALL(BINARY, *, Mul)
4032 OPERATOR_ALL(BINARY_DIVIDE, /, Div)
4033 OPERATOR_INTTYPES(BINARY_DIVIDE, %, Mod)
4034
4035 // unary arithmetic
4036 OPERATOR_ALL(UNARY, -, Neg)
4037
4038 // unary arithmetic increment and decrement
4039 OPERATOR_ALL(UNARY, ++, Inc)
4040 OPERATOR_ALL(UNARY, --, Dec)
4041
4042 // binary arithmetic assignment
4043 OPERATOR_ALL(BINARY, =, Asign)
4044 OPERATOR_ALL(BINARY, +=, AddAsign)
4045 OPERATOR_ALL(BINARY, -=, SubAsign)
4046 OPERATOR_ALL(BINARY, *=, MulAsign)
4047 OPERATOR_ALL(BINARY_DIVIDE, /=, DivAsign)
4048 OPERATOR_INTTYPES(BINARY_DIVIDE, %=, ModAsign)
4049
4050 // binary bitwise
4051 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4052 OPERATOR_INTTYPES(BINARY, |, BitOr)
4053 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4054 OPERATOR_INTTYPES(BINARY, <<, LShift)
4055 OPERATOR_INTTYPES(BINARY, >>, RShift)
4056
4057 // unary bitwise
4058 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4059
4060 // binary bitwise assignment
4061 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4062 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4063 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4064 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4065 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4066
4067 // unary logical negation
4068 OPERATOR_INTTYPES(UNARY, !, Not)
4069
4070 // binary logical equality
4071 OPERATOR_ALL(BINARY, ==, Equ)
4072 OPERATOR_ALL(BINARY, !=, Nqu)
4073
4074 // binary logical
4075 OPERATOR_ALL(BINARY, &&, And)
4076 OPERATOR_ALL(BINARY, ||, Or)
4077
4078 // binary logical relational
4079 OPERATOR_ALL(BINARY, >, Grt)
4080 OPERATOR_ALL(BINARY, <, Sma)
4081 OPERATOR_ALL(BINARY, >=, GrtEqu)
4082 OPERATOR_ALL(BINARY, <=, SmaEqu)
4083
4084 // tertiary condition operator
4085 OPERATOR_ALL(TERTIARY, ?, Cond)
4086
4087 //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
4088 #define OPERATOR_TABLE_ALL(name, type) \
4089     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4090                           type##Neg, \
4091                           type##Inc, type##Dec, \
4092                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4093                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4094                           type##BitNot, \
4095                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4096                           type##Not, \
4097                           type##Equ, type##Nqu, \
4098                           type##And, type##Or, \
4099                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4100                         }; \
4101
4102 #define OPERATOR_TABLE_INTTYPES(name, type) \
4103     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4104                           type##Neg, \
4105                           type##Inc, type##Dec, \
4106                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4107                           null, null, null, null, null, \
4108                           null, \
4109                           null, null, null, null, null, \
4110                           null, \
4111                           type##Equ, type##Nqu, \
4112                           type##And, type##Or, \
4113                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4114                         }; \
4115
4116 OPERATOR_TABLE_ALL(int, Int)
4117 OPERATOR_TABLE_ALL(uint, UInt)
4118 OPERATOR_TABLE_ALL(int64, Int64)
4119 OPERATOR_TABLE_ALL(uint64, UInt64)
4120 OPERATOR_TABLE_ALL(short, Short)
4121 OPERATOR_TABLE_ALL(ushort, UShort)
4122 OPERATOR_TABLE_INTTYPES(float, Float)
4123 OPERATOR_TABLE_INTTYPES(double, Double)
4124 OPERATOR_TABLE_ALL(char, Char)
4125 OPERATOR_TABLE_ALL(uchar, UChar)
4126
4127 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4128 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4129 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4130 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4131 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4132 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4133 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4134 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4135
4136 public void ReadString(char * output,  char * string)
4137 {
4138    int len = strlen(string);
4139    int c,d = 0;
4140    bool quoted = false, escaped = false;
4141    for(c = 0; c<len; c++)
4142    {
4143       char ch = string[c];
4144       if(escaped)
4145       {
4146          switch(ch)
4147          {
4148             case 'n': output[d] = '\n'; break;
4149             case 't': output[d] = '\t'; break;
4150             case 'a': output[d] = '\a'; break;
4151             case 'b': output[d] = '\b'; break;
4152             case 'f': output[d] = '\f'; break;
4153             case 'r': output[d] = '\r'; break;
4154             case 'v': output[d] = '\v'; break;
4155             case '\\': output[d] = '\\'; break;
4156             case '\"': output[d] = '\"'; break;
4157             default: output[d++] = '\\'; output[d] = ch;
4158             //default: output[d] = ch;
4159          }
4160          d++;
4161          escaped = false;
4162       }
4163       else
4164       {
4165          if(ch == '\"')
4166             quoted ^= true;
4167          else if(quoted)
4168          {
4169             if(ch == '\\')
4170                escaped = true;
4171             else
4172                output[d++] = ch;
4173          }
4174       }
4175    }
4176    output[d] = '\0';
4177 }
4178
4179 public Operand GetOperand(Expression exp)
4180 {
4181    Operand op { };
4182    Type type = exp.expType;
4183    if(type)
4184    {
4185       while(type.kind == classType &&
4186          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4187       {
4188          if(!type._class.registered.dataType)
4189             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4190          type = type._class.registered.dataType;
4191
4192       }
4193       op.kind = type.kind;
4194       op.type = exp.expType;
4195       if(exp.isConstant && exp.type == constantExp)
4196       {
4197          switch(op.kind)
4198          {
4199             case _BoolType:
4200             case charType:
4201             {
4202                if(exp.constant[0] == '\'')
4203                   op.c = exp.constant[1];
4204                else if(type.isSigned)
4205                {
4206                   op.c = (char)strtol(exp.constant, null, 0);
4207                   op.ops = charOps;
4208                }
4209                else
4210                {
4211                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4212                   op.ops = ucharOps;
4213                }
4214                break;
4215             }
4216             case shortType:
4217                if(type.isSigned)
4218                {
4219                   op.s = (short)strtol(exp.constant, null, 0);
4220                   op.ops = shortOps;
4221                }
4222                else
4223                {
4224                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4225                   op.ops = ushortOps;
4226                }
4227                break;
4228             case intType:
4229             case longType:
4230                if(type.isSigned)
4231                {
4232                   op.i = (int)strtol(exp.constant, null, 0);
4233                   op.ops = intOps;
4234                }
4235                else
4236                {
4237                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4238                   op.ops = uintOps;
4239                }
4240                op.kind = intType;
4241                break;
4242             case int64Type:
4243                if(type.isSigned)
4244                {
4245                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4246                   op.ops = intOps;
4247                }
4248                else
4249                {
4250                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4251                   op.ops = uintOps;
4252                }
4253                op.kind = int64Type;
4254                break;
4255             case intPtrType:
4256                if(type.isSigned)
4257                {
4258                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4259                   op.ops = int64Ops;
4260                }
4261                else
4262                {
4263                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4264                   op.ops = uint64Ops;
4265                }
4266                op.kind = int64Type;
4267                break;
4268             case intSizeType:
4269                if(type.isSigned)
4270                {
4271                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4272                   op.ops = int64Ops;
4273                }
4274                else
4275                {
4276                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4277                   op.ops = uint64Ops;
4278                }
4279                op.kind = int64Type;
4280                break;
4281             case floatType:
4282                op.f = (float)strtod(exp.constant, null);
4283                op.ops = floatOps;
4284                break;
4285             case doubleType:
4286                op.d = (double)strtod(exp.constant, null);
4287                op.ops = doubleOps;
4288                break;
4289             //case classType:    For when we have operator overloading...
4290             // Pointer additions
4291             //case functionType:
4292             case arrayType:
4293             case pointerType:
4294             case classType:
4295                op.ui64 = _strtoui64(exp.constant, null, 0);
4296                op.kind = pointerType;
4297                op.ops = uintOps;
4298                // op.ptrSize =
4299                break;
4300          }
4301       }
4302    }
4303    return op;
4304 }
4305
4306 static void UnusedFunction()
4307 {
4308    int a;
4309    a.OnGetString(0,0,0);
4310 }
4311 default:
4312 extern int __ecereVMethodID_class_OnGetString;
4313 public:
4314
4315 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4316 {
4317    DataMember dataMember;
4318    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4319    {
4320       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4321          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4322       else
4323       {
4324          Expression exp { };
4325          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4326          Type type;
4327          void * ptr = inst.data + dataMember.offset + offset;
4328          char * result = null;
4329          exp.loc = member.loc = inst.loc;
4330          ((Identifier)member.identifiers->first).loc = inst.loc;
4331
4332          if(!dataMember.dataType)
4333             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4334          type = dataMember.dataType;
4335          if(type.kind == classType)
4336          {
4337             Class _class = type._class.registered;
4338             if(_class.type == enumClass)
4339             {
4340                Class enumClass = eSystem_FindClass(privateModule, "enum");
4341                if(enumClass)
4342                {
4343                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4344                   NamedLink item;
4345                   for(item = e.values.first; item; item = item.next)
4346                   {
4347                      if((int)item.data == *(int *)ptr)
4348                      {
4349                         result = item.name;
4350                         break;
4351                      }
4352                   }
4353                   if(result)
4354                   {
4355                      exp.identifier = MkIdentifier(result);
4356                      exp.type = identifierExp;
4357                      exp.destType = MkClassType(_class.fullName);
4358                      ProcessExpressionType(exp);
4359                   }
4360                }
4361             }
4362             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4363             {
4364                if(!_class.dataType)
4365                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4366                type = _class.dataType;
4367             }
4368          }
4369          if(!result)
4370          {
4371             switch(type.kind)
4372             {
4373                case floatType:
4374                {
4375                   FreeExpContents(exp);
4376
4377                   exp.constant = PrintFloat(*(float*)ptr);
4378                   exp.type = constantExp;
4379                   break;
4380                }
4381                case doubleType:
4382                {
4383                   FreeExpContents(exp);
4384
4385                   exp.constant = PrintDouble(*(double*)ptr);
4386                   exp.type = constantExp;
4387                   break;
4388                }
4389                case intType:
4390                {
4391                   FreeExpContents(exp);
4392
4393                   exp.constant = PrintInt(*(int*)ptr);
4394                   exp.type = constantExp;
4395                   break;
4396                }
4397                case int64Type:
4398                {
4399                   FreeExpContents(exp);
4400
4401                   exp.constant = PrintInt64(*(int64*)ptr);
4402                   exp.type = constantExp;
4403                   break;
4404                }
4405                case intPtrType:
4406                {
4407                   FreeExpContents(exp);
4408                   // TODO: This should probably use proper type
4409                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4410                   exp.type = constantExp;
4411                   break;
4412                }
4413                case intSizeType:
4414                {
4415                   FreeExpContents(exp);
4416                   // TODO: This should probably use proper type
4417                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4418                   exp.type = constantExp;
4419                   break;
4420                }
4421                default:
4422                   Compiler_Error($"Unhandled type populating instance\n");
4423             }
4424          }
4425          ListAdd(memberList, member);
4426       }
4427
4428       if(parentDataMember.type == unionMember)
4429          break;
4430    }
4431 }
4432
4433 void PopulateInstance(Instantiation inst)
4434 {
4435    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4436    Class _class = classSym.registered;
4437    DataMember dataMember;
4438    OldList * memberList = MkList();
4439    // Added this check and ->Add to prevent memory leaks on bad code
4440    if(!inst.members)
4441       inst.members = MkListOne(MkMembersInitList(memberList));
4442    else
4443       inst.members->Add(MkMembersInitList(memberList));
4444    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4445    {
4446       if(!dataMember.isProperty)
4447       {
4448          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4449             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4450          else
4451          {
4452             Expression exp { };
4453             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4454             Type type;
4455             void * ptr = inst.data + dataMember.offset;
4456             char * result = null;
4457
4458             exp.loc = member.loc = inst.loc;
4459             ((Identifier)member.identifiers->first).loc = inst.loc;
4460
4461             if(!dataMember.dataType)
4462                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4463             type = dataMember.dataType;
4464             if(type.kind == classType)
4465             {
4466                Class _class = type._class.registered;
4467                if(_class.type == enumClass)
4468                {
4469                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4470                   if(enumClass)
4471                   {
4472                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4473                      NamedLink item;
4474                      for(item = e.values.first; item; item = item.next)
4475                      {
4476                         if((int)item.data == *(int *)ptr)
4477                         {
4478                            result = item.name;
4479                            break;
4480                         }
4481                      }
4482                   }
4483                   if(result)
4484                   {
4485                      exp.identifier = MkIdentifier(result);
4486                      exp.type = identifierExp;
4487                      exp.destType = MkClassType(_class.fullName);
4488                      ProcessExpressionType(exp);
4489                   }
4490                }
4491                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4492                {
4493                   if(!_class.dataType)
4494                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4495                   type = _class.dataType;
4496                }
4497             }
4498             if(!result)
4499             {
4500                switch(type.kind)
4501                {
4502                   case floatType:
4503                   {
4504                      exp.constant = PrintFloat(*(float*)ptr);
4505                      exp.type = constantExp;
4506                      break;
4507                   }
4508                   case doubleType:
4509                   {
4510                      exp.constant = PrintDouble(*(double*)ptr);
4511                      exp.type = constantExp;
4512                      break;
4513                   }
4514                   case intType:
4515                   {
4516                      exp.constant = PrintInt(*(int*)ptr);
4517                      exp.type = constantExp;
4518                      break;
4519                   }
4520                   case int64Type:
4521                   {
4522                      exp.constant = PrintInt64(*(int64*)ptr);
4523                      exp.type = constantExp;
4524                      break;
4525                   }
4526                   case intPtrType:
4527                   {
4528                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4529                      exp.type = constantExp;
4530                      break;
4531                   }
4532                   default:
4533                      Compiler_Error($"Unhandled type populating instance\n");
4534                }
4535             }
4536             ListAdd(memberList, member);
4537          }
4538       }
4539    }
4540 }
4541
4542 void ComputeInstantiation(Expression exp)
4543 {
4544    Instantiation inst = exp.instance;
4545    MembersInit members;
4546    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4547    Class _class = classSym ? classSym.registered : null;
4548    DataMember curMember = null;
4549    Class curClass = null;
4550    DataMember subMemberStack[256];
4551    int subMemberStackPos = 0;
4552    uint64 bits = 0;
4553
4554    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4555    {
4556       // Don't recompute the instantiation...
4557       // Non Simple classes will have become constants by now
4558       if(inst.data)
4559          return;
4560
4561       if(_class.type == normalClass || _class.type == noHeadClass)
4562       {
4563          inst.data = (byte *)eInstance_New(_class);
4564          if(_class.type == normalClass)
4565             ((Instance)inst.data)._refCount++;
4566       }
4567       else
4568          inst.data = new0 byte[_class.structSize];
4569    }
4570
4571    if(inst.members)
4572    {
4573       for(members = inst.members->first; members; members = members.next)
4574       {
4575          switch(members.type)
4576          {
4577             case dataMembersInit:
4578             {
4579                if(members.dataMembers)
4580                {
4581                   MemberInit member;
4582                   for(member = members.dataMembers->first; member; member = member.next)
4583                   {
4584                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4585                      bool found = false;
4586
4587                      Property prop = null;
4588                      DataMember dataMember = null;
4589                      Method method = null;
4590                      uint dataMemberOffset;
4591
4592                      if(!ident)
4593                      {
4594                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4595                         if(curMember)
4596                         {
4597                            if(curMember.isProperty)
4598                               prop = (Property)curMember;
4599                            else
4600                            {
4601                               dataMember = curMember;
4602
4603                               // CHANGED THIS HERE
4604                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4605
4606                               // 2013/17/29 -- It seems that this was missing here!
4607                               if(_class.type == normalClass)
4608                                  dataMemberOffset += _class.base.structSize;
4609                               // dataMemberOffset = dataMember.offset;
4610                            }
4611                            found = true;
4612                         }
4613                      }
4614                      else
4615                      {
4616                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4617                         if(prop)
4618                         {
4619                            found = true;
4620                            if(prop.memberAccess == publicAccess)
4621                            {
4622                               curMember = (DataMember)prop;
4623                               curClass = prop._class;
4624                            }
4625                         }
4626                         else
4627                         {
4628                            DataMember _subMemberStack[256];
4629                            int _subMemberStackPos = 0;
4630
4631                            // FILL MEMBER STACK
4632                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4633
4634                            if(dataMember)
4635                            {
4636                               found = true;
4637                               if(dataMember.memberAccess == publicAccess)
4638                               {
4639                                  curMember = dataMember;
4640                                  curClass = dataMember._class;
4641                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4642                                  subMemberStackPos = _subMemberStackPos;
4643                               }
4644                            }
4645                         }
4646                      }
4647
4648                      if(found && member.initializer && member.initializer.type == expInitializer)
4649                      {
4650                         Expression value = member.initializer.exp;
4651                         Type type = null;
4652                         bool deepMember = false;
4653                         if(prop)
4654                         {
4655                            type = prop.dataType;
4656                         }
4657                         else if(dataMember)
4658                         {
4659                            if(!dataMember.dataType)
4660                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4661
4662                            type = dataMember.dataType;
4663                         }
4664
4665                         if(ident && ident.next)
4666                         {
4667                            deepMember = true;
4668
4669                            // for(; ident && type; ident = ident.next)
4670                            for(ident = ident.next; ident && type; ident = ident.next)
4671                            {
4672                               if(type.kind == classType)
4673                               {
4674                                  prop = eClass_FindProperty(type._class.registered,
4675                                     ident.string, privateModule);
4676                                  if(prop)
4677                                     type = prop.dataType;
4678                                  else
4679                                  {
4680                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4681                                        ident.string, &dataMemberOffset, privateModule, null, null);
4682                                     if(dataMember)
4683                                        type = dataMember.dataType;
4684                                  }
4685                               }
4686                               else if(type.kind == structType || type.kind == unionType)
4687                               {
4688                                  Type memberType;
4689                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4690                                  {
4691                                     if(!strcmp(memberType.name, ident.string))
4692                                     {
4693                                        type = memberType;
4694                                        break;
4695                                     }
4696                                  }
4697                               }
4698                            }
4699                         }
4700                         if(value)
4701                         {
4702                            FreeType(value.destType);
4703                            value.destType = type;
4704                            if(type) type.refCount++;
4705                            ComputeExpression(value);
4706                         }
4707                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4708                         {
4709                            if(type.kind == classType)
4710                            {
4711                               Class _class = type._class.registered;
4712                               if(_class.type == bitClass || _class.type == unitClass ||
4713                                  _class.type == enumClass)
4714                               {
4715                                  if(!_class.dataType)
4716                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4717                                  type = _class.dataType;
4718                               }
4719                            }
4720
4721                            if(dataMember)
4722                            {
4723                               void * ptr = inst.data + dataMemberOffset;
4724
4725                               if(value.type == constantExp)
4726                               {
4727                                  switch(type.kind)
4728                                  {
4729                                     case intType:
4730                                     {
4731                                        GetInt(value, (int*)ptr);
4732                                        break;
4733                                     }
4734                                     case int64Type:
4735                                     {
4736                                        GetInt64(value, (int64*)ptr);
4737                                        break;
4738                                     }
4739                                     case intPtrType:
4740                                     {
4741                                        GetIntPtr(value, (intptr*)ptr);
4742                                        break;
4743                                     }
4744                                     case intSizeType:
4745                                     {
4746                                        GetIntSize(value, (intsize*)ptr);
4747                                        break;
4748                                     }
4749                                     case floatType:
4750                                     {
4751                                        GetFloat(value, (float*)ptr);
4752                                        break;
4753                                     }
4754                                     case doubleType:
4755                                     {
4756                                        GetDouble(value, (double *)ptr);
4757                                        break;
4758                                     }
4759                                  }
4760                               }
4761                               else if(value.type == instanceExp)
4762                               {
4763                                  if(type.kind == classType)
4764                                  {
4765                                     Class _class = type._class.registered;
4766                                     if(_class.type == structClass)
4767                                     {
4768                                        ComputeTypeSize(type);
4769                                        if(value.instance.data)
4770                                           memcpy(ptr, value.instance.data, type.size);
4771                                     }
4772                                  }
4773                               }
4774                            }
4775                            else if(prop)
4776                            {
4777                               if(value.type == instanceExp && value.instance.data)
4778                               {
4779                                  if(type.kind == classType)
4780                                  {
4781                                     Class _class = type._class.registered;
4782                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4783                                     {
4784                                        void (*Set)(void *, void *) = (void *)prop.Set;
4785                                        Set(inst.data, value.instance.data);
4786                                        PopulateInstance(inst);
4787                                     }
4788                                  }
4789                               }
4790                               else if(value.type == constantExp)
4791                               {
4792                                  switch(type.kind)
4793                                  {
4794                                     case doubleType:
4795                                     {
4796                                        void (*Set)(void *, double) = (void *)prop.Set;
4797                                        Set(inst.data, strtod(value.constant, null) );
4798                                        break;
4799                                     }
4800                                     case floatType:
4801                                     {
4802                                        void (*Set)(void *, float) = (void *)prop.Set;
4803                                        Set(inst.data, (float)(strtod(value.constant, null)));
4804                                        break;
4805                                     }
4806                                     case intType:
4807                                     {
4808                                        void (*Set)(void *, int) = (void *)prop.Set;
4809                                        Set(inst.data, (int)strtol(value.constant, null, 0));
4810                                        break;
4811                                     }
4812                                     case int64Type:
4813                                     {
4814                                        void (*Set)(void *, int64) = (void *)prop.Set;
4815                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4816                                        break;
4817                                     }
4818                                     case intPtrType:
4819                                     {
4820                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4821                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4822                                        break;
4823                                     }
4824                                     case intSizeType:
4825                                     {
4826                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4827                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4828                                        break;
4829                                     }
4830                                  }
4831                               }
4832                               else if(value.type == stringExp)
4833                               {
4834                                  char temp[1024];
4835                                  ReadString(temp, value.string);
4836                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4837                               }
4838                            }
4839                         }
4840                         else if(!deepMember && type && _class.type == unitClass)
4841                         {
4842                            if(prop)
4843                            {
4844                               // Only support converting units to units for now...
4845                               if(value.type == constantExp)
4846                               {
4847                                  if(type.kind == classType)
4848                                  {
4849                                     Class _class = type._class.registered;
4850                                     if(_class.type == unitClass)
4851                                     {
4852                                        if(!_class.dataType)
4853                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4854                                        type = _class.dataType;
4855                                     }
4856                                  }
4857                                  // TODO: Assuming same base type for units...
4858                                  switch(type.kind)
4859                                  {
4860                                     case floatType:
4861                                     {
4862                                        float fValue;
4863                                        float (*Set)(float) = (void *)prop.Set;
4864                                        GetFloat(member.initializer.exp, &fValue);
4865                                        exp.constant = PrintFloat(Set(fValue));
4866                                        exp.type = constantExp;
4867                                        break;
4868                                     }
4869                                     case doubleType:
4870                                     {
4871                                        double dValue;
4872                                        double (*Set)(double) = (void *)prop.Set;
4873                                        GetDouble(member.initializer.exp, &dValue);
4874                                        exp.constant = PrintDouble(Set(dValue));
4875                                        exp.type = constantExp;
4876                                        break;
4877                                     }
4878                                  }
4879                               }
4880                            }
4881                         }
4882                         else if(!deepMember && type && _class.type == bitClass)
4883                         {
4884                            if(prop)
4885                            {
4886                               if(value.type == instanceExp && value.instance.data)
4887                               {
4888                                  unsigned int (*Set)(void *) = (void *)prop.Set;
4889                                  bits = Set(value.instance.data);
4890                               }
4891                               else if(value.type == constantExp)
4892                               {
4893                               }
4894                            }
4895                            else if(dataMember)
4896                            {
4897                               BitMember bitMember = (BitMember) dataMember;
4898                               Type type;
4899                               int part = 0;
4900                               GetInt(value, &part);
4901                               bits = (bits & ~bitMember.mask);
4902                               if(!bitMember.dataType)
4903                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
4904
4905                               type = bitMember.dataType;
4906
4907                               if(type.kind == classType && type._class && type._class.registered)
4908                               {
4909                                  if(!type._class.registered.dataType)
4910                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4911                                  type = type._class.registered.dataType;
4912                               }
4913
4914                               switch(type.kind)
4915                               {
4916                                  case _BoolType:
4917                                  case charType:
4918                                     if(type.isSigned)
4919                                        bits |= ((char)part << bitMember.pos);
4920                                     else
4921                                        bits |= ((unsigned char)part << bitMember.pos);
4922                                     break;
4923                                  case shortType:
4924                                     if(type.isSigned)
4925                                        bits |= ((short)part << bitMember.pos);
4926                                     else
4927                                        bits |= ((unsigned short)part << bitMember.pos);
4928                                     break;
4929                                  case intType:
4930                                  case longType:
4931                                     if(type.isSigned)
4932                                        bits |= ((int)part << bitMember.pos);
4933                                     else
4934                                        bits |= ((unsigned int)part << bitMember.pos);
4935                                     break;
4936                                  case int64Type:
4937                                     if(type.isSigned)
4938                                        bits |= ((int64)part << bitMember.pos);
4939                                     else
4940                                        bits |= ((uint64)part << bitMember.pos);
4941                                     break;
4942                                  case intPtrType:
4943                                     if(type.isSigned)
4944                                     {
4945                                        bits |= ((intptr)part << bitMember.pos);
4946                                     }
4947                                     else
4948                                     {
4949                                        bits |= ((uintptr)part << bitMember.pos);
4950                                     }
4951                                     break;
4952                                  case intSizeType:
4953                                     if(type.isSigned)
4954                                     {
4955                                        bits |= ((ssize_t)(intsize)part << bitMember.pos);
4956                                     }
4957                                     else
4958                                     {
4959                                        bits |= ((size_t) (uintsize)part << bitMember.pos);
4960                                     }
4961                                     break;
4962                               }
4963                            }
4964                         }
4965                      }
4966                      else
4967                      {
4968                         if(_class && _class.type == unitClass)
4969                         {
4970                            ComputeExpression(member.initializer.exp);
4971                            exp.constant = member.initializer.exp.constant;
4972                            exp.type = constantExp;
4973
4974                            member.initializer.exp.constant = null;
4975                         }
4976                      }
4977                   }
4978                }
4979                break;
4980             }
4981          }
4982       }
4983    }
4984    if(_class && _class.type == bitClass)
4985    {
4986       exp.constant = PrintHexUInt(bits);
4987       exp.type = constantExp;
4988    }
4989    if(exp.type != instanceExp)
4990    {
4991       FreeInstance(inst);
4992    }
4993 }
4994
4995 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
4996 {
4997    if(exp.op.op == SIZEOF)
4998    {
4999       FreeExpContents(exp);
5000       exp.type = constantExp;
5001       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5002    }
5003    else
5004    {
5005       if(!exp.op.exp1)
5006       {
5007          switch(exp.op.op)
5008          {
5009             // unary arithmetic
5010             case '+':
5011             {
5012                // Provide default unary +
5013                Expression exp2 = exp.op.exp2;
5014                exp.op.exp2 = null;
5015                FreeExpContents(exp);
5016                FreeType(exp.expType);
5017                FreeType(exp.destType);
5018                *exp = *exp2;
5019                delete exp2;
5020                break;
5021             }
5022             case '-':
5023                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5024                break;
5025             // unary arithmetic increment and decrement
5026                   //OPERATOR_ALL(UNARY, ++, Inc)
5027                   //OPERATOR_ALL(UNARY, --, Dec)
5028             // unary bitwise
5029             case '~':
5030                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5031                break;
5032             // unary logical negation
5033             case '!':
5034                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5035                break;
5036          }
5037       }
5038       else
5039       {
5040          switch(exp.op.op)
5041          {
5042             // binary arithmetic
5043             case '+':
5044                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5045                break;
5046             case '-':
5047                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5048                break;
5049             case '*':
5050                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5051                break;
5052             case '/':
5053                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5054                break;
5055             case '%':
5056                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5057                break;
5058             // binary arithmetic assignment
5059                   //OPERATOR_ALL(BINARY, =, Asign)
5060                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5061                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5062                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5063                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5064                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5065             // binary bitwise
5066             case '&':
5067                if(exp.op.exp2)
5068                {
5069                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5070                }
5071                break;
5072             case '|':
5073                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5074                break;
5075             case '^':
5076                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5077                break;
5078             case LEFT_OP:
5079                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5080                break;
5081             case RIGHT_OP:
5082                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5083                break;
5084             // binary bitwise assignment
5085                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5086                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5087                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5088                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5089                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5090             // binary logical equality
5091             case EQ_OP:
5092                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5093                break;
5094             case NE_OP:
5095                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5096                break;
5097             // binary logical
5098             case AND_OP:
5099                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5100                break;
5101             case OR_OP:
5102                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5103                break;
5104             // binary logical relational
5105             case '>':
5106                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5107                break;
5108             case '<':
5109                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5110                break;
5111             case GE_OP:
5112                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5113                break;
5114             case LE_OP:
5115                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5116                break;
5117          }
5118       }
5119    }
5120 }
5121
5122 void ComputeExpression(Expression exp)
5123 {
5124    char expString[10240];
5125    expString[0] = '\0';
5126 #ifdef _DEBUG
5127    PrintExpression(exp, expString);
5128 #endif
5129
5130    switch(exp.type)
5131    {
5132       case instanceExp:
5133       {
5134          ComputeInstantiation(exp);
5135          break;
5136       }
5137       /*
5138       case constantExp:
5139          break;
5140       */
5141       case opExp:
5142       {
5143          Expression exp1, exp2 = null;
5144          Operand op1 { };
5145          Operand op2 { };
5146
5147          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5148          if(exp.op.exp2)
5149             ComputeExpression(exp.op.exp2);
5150          if(exp.op.exp1)
5151          {
5152             ComputeExpression(exp.op.exp1);
5153             exp1 = exp.op.exp1;
5154             exp2 = exp.op.exp2;
5155             op1 = GetOperand(exp1);
5156             if(op1.type) op1.type.refCount++;
5157             if(exp2)
5158             {
5159                op2 = GetOperand(exp2);
5160                if(op2.type) op2.type.refCount++;
5161             }
5162          }
5163          else
5164          {
5165             exp1 = exp.op.exp2;
5166             op1 = GetOperand(exp1);
5167             if(op1.type) op1.type.refCount++;
5168          }
5169
5170          CallOperator(exp, exp1, exp2, op1, op2);
5171          /*
5172          switch(exp.op.op)
5173          {
5174             // Unary operators
5175             case '&':
5176                // Also binary
5177                if(exp.op.exp1 && exp.op.exp2)
5178                {
5179                   // Binary And
5180                   if(op1.ops.BitAnd)
5181                   {
5182                      FreeExpContents(exp);
5183                      op1.ops.BitAnd(exp, op1, op2);
5184                   }
5185                }
5186                break;
5187             case '*':
5188                if(exp.op.exp1)
5189                {
5190                   if(op1.ops.Mul)
5191                   {
5192                      FreeExpContents(exp);
5193                      op1.ops.Mul(exp, op1, op2);
5194                   }
5195                }
5196                break;
5197             case '+':
5198                if(exp.op.exp1)
5199                {
5200                   if(op1.ops.Add)
5201                   {
5202                      FreeExpContents(exp);
5203                      op1.ops.Add(exp, op1, op2);
5204                   }
5205                }
5206                else
5207                {
5208                   // Provide default unary +
5209                   Expression exp2 = exp.op.exp2;
5210                   exp.op.exp2 = null;
5211                   FreeExpContents(exp);
5212                   FreeType(exp.expType);
5213                   FreeType(exp.destType);
5214
5215                   *exp = *exp2;
5216                   delete exp2;
5217                }
5218                break;
5219             case '-':
5220                if(exp.op.exp1)
5221                {
5222                   if(op1.ops.Sub)
5223                   {
5224                      FreeExpContents(exp);
5225                      op1.ops.Sub(exp, op1, op2);
5226                   }
5227                }
5228                else
5229                {
5230                   if(op1.ops.Neg)
5231                   {
5232                      FreeExpContents(exp);
5233                      op1.ops.Neg(exp, op1);
5234                   }
5235                }
5236                break;
5237             case '~':
5238                if(op1.ops.BitNot)
5239                {
5240                   FreeExpContents(exp);
5241                   op1.ops.BitNot(exp, op1);
5242                }
5243                break;
5244             case '!':
5245                if(op1.ops.Not)
5246                {
5247                   FreeExpContents(exp);
5248                   op1.ops.Not(exp, op1);
5249                }
5250                break;
5251             // Binary only operators
5252             case '/':
5253                if(op1.ops.Div)
5254                {
5255                   FreeExpContents(exp);
5256                   op1.ops.Div(exp, op1, op2);
5257                }
5258                break;
5259             case '%':
5260                if(op1.ops.Mod)
5261                {
5262                   FreeExpContents(exp);
5263                   op1.ops.Mod(exp, op1, op2);
5264                }
5265                break;
5266             case LEFT_OP:
5267                break;
5268             case RIGHT_OP:
5269                break;
5270             case '<':
5271                if(exp.op.exp1)
5272                {
5273                   if(op1.ops.Sma)
5274                   {
5275                      FreeExpContents(exp);
5276                      op1.ops.Sma(exp, op1, op2);
5277                   }
5278                }
5279                break;
5280             case '>':
5281                if(exp.op.exp1)
5282                {
5283                   if(op1.ops.Grt)
5284                   {
5285                      FreeExpContents(exp);
5286                      op1.ops.Grt(exp, op1, op2);
5287                   }
5288                }
5289                break;
5290             case LE_OP:
5291                if(exp.op.exp1)
5292                {
5293                   if(op1.ops.SmaEqu)
5294                   {
5295                      FreeExpContents(exp);
5296                      op1.ops.SmaEqu(exp, op1, op2);
5297                   }
5298                }
5299                break;
5300             case GE_OP:
5301                if(exp.op.exp1)
5302                {
5303                   if(op1.ops.GrtEqu)
5304                   {
5305                      FreeExpContents(exp);
5306                      op1.ops.GrtEqu(exp, op1, op2);
5307                   }
5308                }
5309                break;
5310             case EQ_OP:
5311                if(exp.op.exp1)
5312                {
5313                   if(op1.ops.Equ)
5314                   {
5315                      FreeExpContents(exp);
5316                      op1.ops.Equ(exp, op1, op2);
5317                   }
5318                }
5319                break;
5320             case NE_OP:
5321                if(exp.op.exp1)
5322                {
5323                   if(op1.ops.Nqu)
5324                   {
5325                      FreeExpContents(exp);
5326                      op1.ops.Nqu(exp, op1, op2);
5327                   }
5328                }
5329                break;
5330             case '|':
5331                if(op1.ops.BitOr)
5332                {
5333                   FreeExpContents(exp);
5334                   op1.ops.BitOr(exp, op1, op2);
5335                }
5336                break;
5337             case '^':
5338                if(op1.ops.BitXor)
5339                {
5340                   FreeExpContents(exp);
5341                   op1.ops.BitXor(exp, op1, op2);
5342                }
5343                break;
5344             case AND_OP:
5345                break;
5346             case OR_OP:
5347                break;
5348             case SIZEOF:
5349                FreeExpContents(exp);
5350                exp.type = constantExp;
5351                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5352                break;
5353          }
5354          */
5355          if(op1.type) FreeType(op1.type);
5356          if(op2.type) FreeType(op2.type);
5357          break;
5358       }
5359       case bracketsExp:
5360       case extensionExpressionExp:
5361       {
5362          Expression e, n;
5363          for(e = exp.list->first; e; e = n)
5364          {
5365             n = e.next;
5366             if(!n)
5367             {
5368                OldList * list = exp.list;
5369                ComputeExpression(e);
5370                //FreeExpContents(exp);
5371                FreeType(exp.expType);
5372                FreeType(exp.destType);
5373                *exp = *e;
5374                delete e;
5375                delete list;
5376             }
5377             else
5378             {
5379                FreeExpression(e);
5380             }
5381          }
5382          break;
5383       }
5384       /*
5385
5386       case ExpIndex:
5387       {
5388          Expression e;
5389          exp.isConstant = true;
5390
5391          ComputeExpression(exp.index.exp);
5392          if(!exp.index.exp.isConstant)
5393             exp.isConstant = false;
5394
5395          for(e = exp.index.index->first; e; e = e.next)
5396          {
5397             ComputeExpression(e);
5398             if(!e.next)
5399             {
5400                // Check if this type is int
5401             }
5402             if(!e.isConstant)
5403                exp.isConstant = false;
5404          }
5405          exp.expType = Dereference(exp.index.exp.expType);
5406          break;
5407       }
5408       */
5409       case memberExp:
5410       {
5411          Expression memberExp = exp.member.exp;
5412          Identifier memberID = exp.member.member;
5413
5414          Type type;
5415          ComputeExpression(exp.member.exp);
5416          type = exp.member.exp.expType;
5417          if(type)
5418          {
5419             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);
5420             Property prop = null;
5421             DataMember member = null;
5422             Class convertTo = null;
5423             if(type.kind == subClassType && exp.member.exp.type == classExp)
5424                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5425
5426             if(!_class)
5427             {
5428                char string[256];
5429                Symbol classSym;
5430                string[0] = '\0';
5431                PrintTypeNoConst(type, string, false, true);
5432                classSym = FindClass(string);
5433                _class = classSym ? classSym.registered : null;
5434             }
5435
5436             if(exp.member.member)
5437             {
5438                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5439                if(!prop)
5440                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5441             }
5442             if(!prop && !member && _class && exp.member.member)
5443             {
5444                Symbol classSym = FindClass(exp.member.member.string);
5445                convertTo = _class;
5446                _class = classSym ? classSym.registered : null;
5447                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5448             }
5449
5450             if(prop)
5451             {
5452                if(prop.compiled)
5453                {
5454                   Type type = prop.dataType;
5455                   // TODO: Assuming same base type for units...
5456                   if(_class.type == unitClass)
5457                   {
5458                      if(type.kind == classType)
5459                      {
5460                         Class _class = type._class.registered;
5461                         if(_class.type == unitClass)
5462                         {
5463                            if(!_class.dataType)
5464                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5465                            type = _class.dataType;
5466                         }
5467                      }
5468                      switch(type.kind)
5469                      {
5470                         case floatType:
5471                         {
5472                            float value;
5473                            float (*Get)(float) = (void *)prop.Get;
5474                            GetFloat(exp.member.exp, &value);
5475                            exp.constant = PrintFloat(Get ? Get(value) : value);
5476                            exp.type = constantExp;
5477                            break;
5478                         }
5479                         case doubleType:
5480                         {
5481                            double value;
5482                            double (*Get)(double);
5483                            GetDouble(exp.member.exp, &value);
5484
5485                            if(convertTo)
5486                               Get = (void *)prop.Set;
5487                            else
5488                               Get = (void *)prop.Get;
5489                            exp.constant = PrintDouble(Get ? Get(value) : value);
5490                            exp.type = constantExp;
5491                            break;
5492                         }
5493                      }
5494                   }
5495                   else
5496                   {
5497                      if(convertTo)
5498                      {
5499                         Expression value = exp.member.exp;
5500                         Type type;
5501                         if(!prop.dataType)
5502                            ProcessPropertyType(prop);
5503
5504                         type = prop.dataType;
5505                         if(!type)
5506                         {
5507                             // printf("Investigate this\n");
5508                         }
5509                         else if(_class.type == structClass)
5510                         {
5511                            switch(type.kind)
5512                            {
5513                               case classType:
5514                               {
5515                                  Class propertyClass = type._class.registered;
5516                                  if(propertyClass.type == structClass && value.type == instanceExp)
5517                                  {
5518                                     void (*Set)(void *, void *) = (void *)prop.Set;
5519                                     exp.instance = Instantiation { };
5520                                     exp.instance.data = new0 byte[_class.structSize];
5521                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5522                                     exp.instance.loc = exp.loc;
5523                                     exp.type = instanceExp;
5524                                     Set(exp.instance.data, value.instance.data);
5525                                     PopulateInstance(exp.instance);
5526                                  }
5527                                  break;
5528                               }
5529                               case intType:
5530                               {
5531                                  int intValue;
5532                                  void (*Set)(void *, int) = (void *)prop.Set;
5533
5534                                  exp.instance = Instantiation { };
5535                                  exp.instance.data = new0 byte[_class.structSize];
5536                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5537                                  exp.instance.loc = exp.loc;
5538                                  exp.type = instanceExp;
5539
5540                                  GetInt(value, &intValue);
5541
5542                                  Set(exp.instance.data, intValue);
5543                                  PopulateInstance(exp.instance);
5544                                  break;
5545                               }
5546                               case int64Type:
5547                               {
5548                                  int64 intValue;
5549                                  void (*Set)(void *, int64) = (void *)prop.Set;
5550
5551                                  exp.instance = Instantiation { };
5552                                  exp.instance.data = new0 byte[_class.structSize];
5553                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5554                                  exp.instance.loc = exp.loc;
5555                                  exp.type = instanceExp;
5556
5557                                  GetInt64(value, &intValue);
5558
5559                                  Set(exp.instance.data, intValue);
5560                                  PopulateInstance(exp.instance);
5561                                  break;
5562                               }
5563                               case intPtrType:
5564                               {
5565                                  // TOFIX:
5566                                  intptr intValue;
5567                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5568
5569                                  exp.instance = Instantiation { };
5570                                  exp.instance.data = new0 byte[_class.structSize];
5571                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5572                                  exp.instance.loc = exp.loc;
5573                                  exp.type = instanceExp;
5574
5575                                  GetIntPtr(value, &intValue);
5576
5577                                  Set(exp.instance.data, intValue);
5578                                  PopulateInstance(exp.instance);
5579                                  break;
5580                               }
5581                               case intSizeType:
5582                               {
5583                                  // TOFIX:
5584                                  intsize intValue;
5585                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5586
5587                                  exp.instance = Instantiation { };
5588                                  exp.instance.data = new0 byte[_class.structSize];
5589                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5590                                  exp.instance.loc = exp.loc;
5591                                  exp.type = instanceExp;
5592
5593                                  GetIntSize(value, &intValue);
5594
5595                                  Set(exp.instance.data, intValue);
5596                                  PopulateInstance(exp.instance);
5597                                  break;
5598                               }
5599                               case doubleType:
5600                               {
5601                                  double doubleValue;
5602                                  void (*Set)(void *, double) = (void *)prop.Set;
5603
5604                                  exp.instance = Instantiation { };
5605                                  exp.instance.data = new0 byte[_class.structSize];
5606                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5607                                  exp.instance.loc = exp.loc;
5608                                  exp.type = instanceExp;
5609
5610                                  GetDouble(value, &doubleValue);
5611
5612                                  Set(exp.instance.data, doubleValue);
5613                                  PopulateInstance(exp.instance);
5614                                  break;
5615                               }
5616                            }
5617                         }
5618                         else if(_class.type == bitClass)
5619                         {
5620                            switch(type.kind)
5621                            {
5622                               case classType:
5623                               {
5624                                  Class propertyClass = type._class.registered;
5625                                  if(propertyClass.type == structClass && value.instance.data)
5626                                  {
5627                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5628                                     unsigned int bits = Set(value.instance.data);
5629                                     exp.constant = PrintHexUInt(bits);
5630                                     exp.type = constantExp;
5631                                     break;
5632                                  }
5633                                  else if(_class.type == bitClass)
5634                                  {
5635                                     unsigned int value;
5636                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5637                                     unsigned int bits;
5638
5639                                     GetUInt(exp.member.exp, &value);
5640                                     bits = Set(value);
5641                                     exp.constant = PrintHexUInt(bits);
5642                                     exp.type = constantExp;
5643                                  }
5644                               }
5645                            }
5646                         }
5647                      }
5648                      else
5649                      {
5650                         if(_class.type == bitClass)
5651                         {
5652                            unsigned int value;
5653                            GetUInt(exp.member.exp, &value);
5654
5655                            switch(type.kind)
5656                            {
5657                               case classType:
5658                               {
5659                                  Class _class = type._class.registered;
5660                                  if(_class.type == structClass)
5661                                  {
5662                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5663
5664                                     exp.instance = Instantiation { };
5665                                     exp.instance.data = new0 byte[_class.structSize];
5666                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5667                                     exp.instance.loc = exp.loc;
5668                                     //exp.instance.fullSet = true;
5669                                     exp.type = instanceExp;
5670                                     Get(value, exp.instance.data);
5671                                     PopulateInstance(exp.instance);
5672                                  }
5673                                  else if(_class.type == bitClass)
5674                                  {
5675                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5676                                     uint64 bits = Get(value);
5677                                     exp.constant = PrintHexUInt64(bits);
5678                                     exp.type = constantExp;
5679                                  }
5680                                  break;
5681                               }
5682                            }
5683                         }
5684                         else if(_class.type == structClass)
5685                         {
5686                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5687                            switch(type.kind)
5688                            {
5689                               case classType:
5690                               {
5691                                  Class _class = type._class.registered;
5692                                  if(_class.type == structClass && value)
5693                                  {
5694                                     void (*Get)(void *, void *) = (void *)prop.Get;
5695
5696                                     exp.instance = Instantiation { };
5697                                     exp.instance.data = new0 byte[_class.structSize];
5698                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5699                                     exp.instance.loc = exp.loc;
5700                                     //exp.instance.fullSet = true;
5701                                     exp.type = instanceExp;
5702                                     Get(value, exp.instance.data);
5703                                     PopulateInstance(exp.instance);
5704                                  }
5705                                  break;
5706                               }
5707                            }
5708                         }
5709                         /*else
5710                         {
5711                            char * value = exp.member.exp.instance.data;
5712                            switch(type.kind)
5713                            {
5714                               case classType:
5715                               {
5716                                  Class _class = type._class.registered;
5717                                  if(_class.type == normalClass)
5718                                  {
5719                                     void *(*Get)(void *) = (void *)prop.Get;
5720
5721                                     exp.instance = Instantiation { };
5722                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5723                                     exp.type = instanceExp;
5724                                     exp.instance.data = Get(value, exp.instance.data);
5725                                  }
5726                                  break;
5727                               }
5728                            }
5729                         }
5730                         */
5731                      }
5732                   }
5733                }
5734                else
5735                {
5736                   exp.isConstant = false;
5737                }
5738             }
5739             else if(member)
5740             {
5741             }
5742          }
5743
5744          if(exp.type != ExpressionType::memberExp)
5745          {
5746             FreeExpression(memberExp);
5747             FreeIdentifier(memberID);
5748          }
5749          break;
5750       }
5751       case typeSizeExp:
5752       {
5753          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5754          FreeExpContents(exp);
5755          exp.constant = PrintUInt(ComputeTypeSize(type));
5756          exp.type = constantExp;
5757          FreeType(type);
5758          break;
5759       }
5760       case classSizeExp:
5761       {
5762          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5763          if(classSym && classSym.registered)
5764          {
5765             if(classSym.registered.fixed)
5766             {
5767                FreeSpecifier(exp._class);
5768                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5769                exp.type = constantExp;
5770             }
5771             else
5772             {
5773                char className[1024];
5774                strcpy(className, "__ecereClass_");
5775                FullClassNameCat(className, classSym.string, true);
5776                MangleClassName(className);
5777
5778                DeclareClass(classSym, className);
5779
5780                FreeExpContents(exp);
5781                exp.type = pointerExp;
5782                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
5783                exp.member.member = MkIdentifier("structSize");
5784             }
5785          }
5786          break;
5787       }
5788       case castExp:
5789       //case constantExp:
5790       {
5791          Type type;
5792          Expression e = exp;
5793          if(exp.type == castExp)
5794          {
5795             if(exp.cast.exp)
5796                ComputeExpression(exp.cast.exp);
5797             e = exp.cast.exp;
5798          }
5799          if(e && exp.expType)
5800          {
5801             /*if(exp.destType)
5802                type = exp.destType;
5803             else*/
5804                type = exp.expType;
5805             if(type.kind == classType)
5806             {
5807                Class _class = type._class.registered;
5808                if(_class && (_class.type == unitClass || _class.type == bitClass))
5809                {
5810                   if(!_class.dataType)
5811                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5812                   type = _class.dataType;
5813                }
5814             }
5815
5816             switch(type.kind)
5817             {
5818                case _BoolType:
5819                case charType:
5820                   if(type.isSigned)
5821                   {
5822                      char value;
5823                      GetChar(e, &value);
5824                      FreeExpContents(exp);
5825                      exp.constant = PrintChar(value);
5826                      exp.type = constantExp;
5827                   }
5828                   else
5829                   {
5830                      unsigned char value;
5831                      GetUChar(e, &value);
5832                      FreeExpContents(exp);
5833                      exp.constant = PrintUChar(value);
5834                      exp.type = constantExp;
5835                   }
5836                   break;
5837                case shortType:
5838                   if(type.isSigned)
5839                   {
5840                      short value;
5841                      GetShort(e, &value);
5842                      FreeExpContents(exp);
5843                      exp.constant = PrintShort(value);
5844                      exp.type = constantExp;
5845                   }
5846                   else
5847                   {
5848                      unsigned short value;
5849                      GetUShort(e, &value);
5850                      FreeExpContents(exp);
5851                      exp.constant = PrintUShort(value);
5852                      exp.type = constantExp;
5853                   }
5854                   break;
5855                case intType:
5856                   if(type.isSigned)
5857                   {
5858                      int value;
5859                      GetInt(e, &value);
5860                      FreeExpContents(exp);
5861                      exp.constant = PrintInt(value);
5862                      exp.type = constantExp;
5863                   }
5864                   else
5865                   {
5866                      unsigned int value;
5867                      GetUInt(e, &value);
5868                      FreeExpContents(exp);
5869                      exp.constant = PrintUInt(value);
5870                      exp.type = constantExp;
5871                   }
5872                   break;
5873                case int64Type:
5874                   if(type.isSigned)
5875                   {
5876                      int64 value;
5877                      GetInt64(e, &value);
5878                      FreeExpContents(exp);
5879                      exp.constant = PrintInt64(value);
5880                      exp.type = constantExp;
5881                   }
5882                   else
5883                   {
5884                      uint64 value;
5885                      GetUInt64(e, &value);
5886                      FreeExpContents(exp);
5887                      exp.constant = PrintUInt64(value);
5888                      exp.type = constantExp;
5889                   }
5890                   break;
5891                case intPtrType:
5892                   if(type.isSigned)
5893                   {
5894                      intptr value;
5895                      GetIntPtr(e, &value);
5896                      FreeExpContents(exp);
5897                      exp.constant = PrintInt64((int64)value);
5898                      exp.type = constantExp;
5899                   }
5900                   else
5901                   {
5902                      uintptr value;
5903                      GetUIntPtr(e, &value);
5904                      FreeExpContents(exp);
5905                      exp.constant = PrintUInt64((uint64)value);
5906                      exp.type = constantExp;
5907                   }
5908                   break;
5909                case intSizeType:
5910                   if(type.isSigned)
5911                   {
5912                      intsize value;
5913                      GetIntSize(e, &value);
5914                      FreeExpContents(exp);
5915                      exp.constant = PrintInt64((int64)value);
5916                      exp.type = constantExp;
5917                   }
5918                   else
5919                   {
5920                      uintsize value;
5921                      GetUIntSize(e, &value);
5922                      FreeExpContents(exp);
5923                      exp.constant = PrintUInt64((uint64)value);
5924                      exp.type = constantExp;
5925                   }
5926                   break;
5927                case floatType:
5928                {
5929                   float value;
5930                   GetFloat(e, &value);
5931                   FreeExpContents(exp);
5932                   exp.constant = PrintFloat(value);
5933                   exp.type = constantExp;
5934                   break;
5935                }
5936                case doubleType:
5937                {
5938                   double value;
5939                   GetDouble(e, &value);
5940                   FreeExpContents(exp);
5941                   exp.constant = PrintDouble(value);
5942                   exp.type = constantExp;
5943                   break;
5944                }
5945             }
5946          }
5947          break;
5948       }
5949       case conditionExp:
5950       {
5951          Operand op1 { };
5952          Operand op2 { };
5953          Operand op3 { };
5954
5955          if(exp.cond.exp)
5956             // Caring only about last expression for now...
5957             ComputeExpression(exp.cond.exp->last);
5958          if(exp.cond.elseExp)
5959             ComputeExpression(exp.cond.elseExp);
5960          if(exp.cond.cond)
5961             ComputeExpression(exp.cond.cond);
5962
5963          op1 = GetOperand(exp.cond.cond);
5964          if(op1.type) op1.type.refCount++;
5965          op2 = GetOperand(exp.cond.exp->last);
5966          if(op2.type) op2.type.refCount++;
5967          op3 = GetOperand(exp.cond.elseExp);
5968          if(op3.type) op3.type.refCount++;
5969
5970          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
5971          if(op1.type) FreeType(op1.type);
5972          if(op2.type) FreeType(op2.type);
5973          if(op3.type) FreeType(op3.type);
5974          break;
5975       }
5976    }
5977 }
5978
5979 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
5980 {
5981    bool result = true;
5982    if(destType)
5983    {
5984       OldList converts { };
5985       Conversion convert;
5986
5987       if(destType.kind == voidType)
5988          return false;
5989
5990       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
5991          result = false;
5992       if(converts.count)
5993       {
5994          // for(convert = converts.last; convert; convert = convert.prev)
5995          for(convert = converts.first; convert; convert = convert.next)
5996          {
5997             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
5998             if(!empty)
5999             {
6000                Expression newExp { };
6001                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6002
6003                // TODO: Check this...
6004                *newExp = *exp;
6005                newExp.destType = null;
6006
6007                if(convert.isGet)
6008                {
6009                   // [exp].ColorRGB
6010                   exp.type = memberExp;
6011                   exp.addedThis = true;
6012                   exp.member.exp = newExp;
6013                   FreeType(exp.member.exp.expType);
6014
6015                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6016                   exp.member.exp.expType.classObjectType = objectType;
6017                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6018                   exp.member.memberType = propertyMember;
6019                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6020                   // TESTING THIS... for (int)degrees
6021                   exp.needCast = true;
6022                   if(exp.expType) exp.expType.refCount++;
6023                   ApplyAnyObjectLogic(exp.member.exp);
6024                }
6025                else
6026                {
6027
6028                   /*if(exp.isConstant)
6029                   {
6030                      // Color { ColorRGB = [exp] };
6031                      exp.type = instanceExp;
6032                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6033                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6034                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6035                   }
6036                   else*/
6037                   {
6038                      // If not constant, don't turn it yet into an instantiation
6039                      // (Go through the deep members system first)
6040                      exp.type = memberExp;
6041                      exp.addedThis = true;
6042                      exp.member.exp = newExp;
6043
6044                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6045                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6046                         newExp.expType._class.registered.type == noHeadClass)
6047                      {
6048                         newExp.byReference = true;
6049                      }
6050
6051                      FreeType(exp.member.exp.expType);
6052                      /*exp.member.exp.expType = convert.convert.dataType;
6053                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6054                      exp.member.exp.expType = null;
6055                      if(convert.convert.dataType)
6056                      {
6057                         exp.member.exp.expType = { };
6058                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6059                         exp.member.exp.expType.refCount = 1;
6060                         exp.member.exp.expType.classObjectType = objectType;
6061                         ApplyAnyObjectLogic(exp.member.exp);
6062                      }
6063
6064                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6065                      exp.member.memberType = reverseConversionMember;
6066                      exp.expType = convert.resultType ? convert.resultType :
6067                         MkClassType(convert.convert._class.fullName);
6068                      exp.needCast = true;
6069                      if(convert.resultType) convert.resultType.refCount++;
6070                   }
6071                }
6072             }
6073             else
6074             {
6075                FreeType(exp.expType);
6076                if(convert.isGet)
6077                {
6078                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6079                   exp.needCast = true;
6080                   if(exp.expType) exp.expType.refCount++;
6081                }
6082                else
6083                {
6084                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6085                   exp.needCast = true;
6086                   if(convert.resultType)
6087                      convert.resultType.refCount++;
6088                }
6089             }
6090          }
6091          if(exp.isConstant && inCompiler)
6092             ComputeExpression(exp);
6093
6094          converts.Free(FreeConvert);
6095       }
6096
6097       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6098       {
6099          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6100       }
6101       if(!result && exp.expType && exp.destType)
6102       {
6103          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6104              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6105             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6106             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6107             result = true;
6108       }
6109    }
6110    // if(result) CheckTemplateTypes(exp);
6111    return result;
6112 }
6113
6114 void CheckTemplateTypes(Expression exp)
6115 {
6116    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6117    {
6118       Expression newExp { };
6119       Statement compound;
6120       Context context;
6121       *newExp = *exp;
6122       if(exp.destType) exp.destType.refCount++;
6123       if(exp.expType)  exp.expType.refCount++;
6124       newExp.prev = null;
6125       newExp.next = null;
6126
6127       switch(exp.expType.kind)
6128       {
6129          case doubleType:
6130             if(exp.destType.classObjectType)
6131             {
6132                // We need to pass the address, just pass it along (Undo what was done above)
6133                if(exp.destType) exp.destType.refCount--;
6134                if(exp.expType)  exp.expType.refCount--;
6135                delete newExp;
6136             }
6137             else
6138             {
6139                // If we're looking for value:
6140                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6141                OldList * specs;
6142                OldList * unionDefs = MkList();
6143                OldList * statements = MkList();
6144                context = PushContext();
6145                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6146                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6147                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6148                exp.type = extensionCompoundExp;
6149                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6150                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6151                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6152                exp.compound.compound.context = context;
6153                PopContext(context);
6154             }
6155             break;
6156          default:
6157             exp.type = castExp;
6158             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6159             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6160             break;
6161       }
6162    }
6163    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6164    {
6165       Expression newExp { };
6166       Statement compound;
6167       Context context;
6168       *newExp = *exp;
6169       if(exp.destType) exp.destType.refCount++;
6170       if(exp.expType)  exp.expType.refCount++;
6171       newExp.prev = null;
6172       newExp.next = null;
6173
6174       switch(exp.expType.kind)
6175       {
6176          case doubleType:
6177             if(exp.destType.classObjectType)
6178             {
6179                // We need to pass the address, just pass it along (Undo what was done above)
6180                if(exp.destType) exp.destType.refCount--;
6181                if(exp.expType)  exp.expType.refCount--;
6182                delete newExp;
6183             }
6184             else
6185             {
6186                // If we're looking for value:
6187                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6188                OldList * specs;
6189                OldList * unionDefs = MkList();
6190                OldList * statements = MkList();
6191                context = PushContext();
6192                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6193                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6194                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6195                exp.type = extensionCompoundExp;
6196                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6197                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6198                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6199                exp.compound.compound.context = context;
6200                PopContext(context);
6201             }
6202             break;
6203          case classType:
6204          {
6205             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6206             {
6207                exp.type = bracketsExp;
6208                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6209                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6210                ProcessExpressionType(exp.list->first);
6211                break;
6212             }
6213             else
6214             {
6215                exp.type = bracketsExp;
6216                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6217                newExp.needCast = true;
6218                ProcessExpressionType(exp.list->first);
6219                break;
6220             }
6221          }
6222          default:
6223          {
6224             if(exp.expType.kind == templateType)
6225             {
6226                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6227                if(type)
6228                {
6229                   FreeType(exp.destType);
6230                   FreeType(exp.expType);
6231                   delete newExp;
6232                   break;
6233                }
6234             }
6235             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6236             {
6237                exp.type = opExp;
6238                exp.op.op = '*';
6239                exp.op.exp1 = null;
6240                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6241                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6242             }
6243             else
6244             {
6245                char typeString[1024];
6246                Declarator decl;
6247                OldList * specs = MkList();
6248                typeString[0] = '\0';
6249                PrintType(exp.expType, typeString, false, false);
6250                decl = SpecDeclFromString(typeString, specs, null);
6251
6252                exp.type = castExp;
6253                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6254                exp.cast.typeName = MkTypeName(specs, decl);
6255                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6256                exp.cast.exp.needCast = true;
6257             }
6258             break;
6259          }
6260       }
6261    }
6262 }
6263 // TODO: The Symbol tree should be reorganized by namespaces
6264 // Name Space:
6265 //    - Tree of all symbols within (stored without namespace)
6266 //    - Tree of sub-namespaces
6267
6268 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6269 {
6270    int nsLen = strlen(nameSpace);
6271    Symbol symbol;
6272    // Start at the name space prefix
6273    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6274    {
6275       char * s = symbol.string;
6276       if(!strncmp(s, nameSpace, nsLen))
6277       {
6278          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6279          int c;
6280          char * namePart;
6281          for(c = strlen(s)-1; c >= 0; c--)
6282             if(s[c] == ':')
6283                break;
6284
6285          namePart = s+c+1;
6286          if(!strcmp(namePart, name))
6287          {
6288             // TODO: Error on ambiguity
6289             return symbol;
6290          }
6291       }
6292       else
6293          break;
6294    }
6295    return null;
6296 }
6297
6298 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6299 {
6300    int c;
6301    char nameSpace[1024];
6302    char * namePart;
6303    bool gotColon = false;
6304
6305    nameSpace[0] = '\0';
6306    for(c = strlen(name)-1; c >= 0; c--)
6307       if(name[c] == ':')
6308       {
6309          gotColon = true;
6310          break;
6311       }
6312
6313    namePart = name+c+1;
6314    while(c >= 0 && name[c] == ':') c--;
6315    if(c >= 0)
6316    {
6317       // Try an exact match first
6318       Symbol symbol = (Symbol)tree.FindString(name);
6319       if(symbol)
6320          return symbol;
6321
6322       // Namespace specified
6323       memcpy(nameSpace, name, c + 1);
6324       nameSpace[c+1] = 0;
6325
6326       return ScanWithNameSpace(tree, nameSpace, namePart);
6327    }
6328    else if(gotColon)
6329    {
6330       // Looking for a global symbol, e.g. ::Sleep()
6331       Symbol symbol = (Symbol)tree.FindString(namePart);
6332       return symbol;
6333    }
6334    else
6335    {
6336       // Name only (no namespace specified)
6337       Symbol symbol = (Symbol)tree.FindString(namePart);
6338       if(symbol)
6339          return symbol;
6340       return ScanWithNameSpace(tree, "", namePart);
6341    }
6342    return null;
6343 }
6344
6345 static void ProcessDeclaration(Declaration decl);
6346
6347 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6348 {
6349 #ifdef _DEBUG
6350    //Time startTime = GetTime();
6351 #endif
6352    // Optimize this later? Do this before/less?
6353    Context ctx;
6354    Symbol symbol = null;
6355    // First, check if the identifier is declared inside the function
6356    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6357
6358    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6359    {
6360       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6361       {
6362          symbol = null;
6363          if(thisNameSpace)
6364          {
6365             char curName[1024];
6366             strcpy(curName, thisNameSpace);
6367             strcat(curName, "::");
6368             strcat(curName, name);
6369             // Try to resolve in current namespace first
6370             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6371          }
6372          if(!symbol)
6373             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6374       }
6375       else
6376          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6377
6378       if(symbol || ctx == endContext) break;
6379    }
6380    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6381    {
6382       if(symbol.pointerExternal.type == functionExternal)
6383       {
6384          FunctionDefinition function = symbol.pointerExternal.function;
6385
6386          // Modified this recently...
6387          Context tmpContext = curContext;
6388          curContext = null;
6389          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6390          curContext = tmpContext;
6391
6392          symbol.pointerExternal.symbol = symbol;
6393
6394          // TESTING THIS:
6395          DeclareType(symbol.type, true, true);
6396
6397          ast->Insert(curExternal.prev, symbol.pointerExternal);
6398
6399          symbol.id = curExternal.symbol.idCode;
6400
6401       }
6402       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6403       {
6404          ast->Move(symbol.pointerExternal, curExternal.prev);
6405          symbol.id = curExternal.symbol.idCode;
6406       }
6407    }
6408 #ifdef _DEBUG
6409    //findSymbolTotalTime += GetTime() - startTime;
6410 #endif
6411    return symbol;
6412 }
6413
6414 static void GetTypeSpecs(Type type, OldList * specs)
6415 {
6416    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6417    switch(type.kind)
6418    {
6419       case classType:
6420       {
6421          if(type._class.registered)
6422          {
6423             if(!type._class.registered.dataType)
6424                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6425             GetTypeSpecs(type._class.registered.dataType, specs);
6426          }
6427          break;
6428       }
6429       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6430       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6431       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6432       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6433       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6434       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6435       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6436       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6437       case intType:
6438       default:
6439          ListAdd(specs, MkSpecifier(INT)); break;
6440    }
6441 }
6442
6443 static void PrintArraySize(Type arrayType, char * string)
6444 {
6445    char size[256];
6446    size[0] = '\0';
6447    strcat(size, "[");
6448    if(arrayType.enumClass)
6449       strcat(size, arrayType.enumClass.string);
6450    else if(arrayType.arraySizeExp)
6451       PrintExpression(arrayType.arraySizeExp, size);
6452    strcat(size, "]");
6453    strcat(string, size);
6454 }
6455
6456 // WARNING : This function expects a null terminated string since it recursively concatenate...
6457 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6458 {
6459    if(type)
6460    {
6461       if(printConst && type.constant)
6462          strcat(string, "const ");
6463       switch(type.kind)
6464       {
6465          case classType:
6466          {
6467             Symbol c = type._class;
6468             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6469             //       look into merging with thisclass ?
6470             if(type.classObjectType == typedObject)
6471                strcat(string, "typed_object");
6472             else if(type.classObjectType == anyObject)
6473                strcat(string, "any_object");
6474             else
6475             {
6476                if(c && c.string)
6477                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6478             }
6479             if(type.byReference)
6480                strcat(string, " &");
6481             break;
6482          }
6483          case voidType: strcat(string, "void"); break;
6484          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6485          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6486          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6487          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6488          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6489          case _BoolType: strcat(string, "_Bool"); break;
6490          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6491          case floatType: strcat(string, "float"); break;
6492          case doubleType: strcat(string, "double"); break;
6493          case structType:
6494             if(type.enumName)
6495             {
6496                strcat(string, "struct ");
6497                strcat(string, type.enumName);
6498             }
6499             else if(type.typeName)
6500                strcat(string, type.typeName);
6501             else
6502             {
6503                Type member;
6504                strcat(string, "struct { ");
6505                for(member = type.members.first; member; member = member.next)
6506                {
6507                   PrintType(member, string, true, fullName);
6508                   strcat(string,"; ");
6509                }
6510                strcat(string,"}");
6511             }
6512             break;
6513          case unionType:
6514             if(type.enumName)
6515             {
6516                strcat(string, "union ");
6517                strcat(string, type.enumName);
6518             }
6519             else if(type.typeName)
6520                strcat(string, type.typeName);
6521             else
6522             {
6523                strcat(string, "union ");
6524                strcat(string,"(unnamed)");
6525             }
6526             break;
6527          case enumType:
6528             if(type.enumName)
6529             {
6530                strcat(string, "enum ");
6531                strcat(string, type.enumName);
6532             }
6533             else if(type.typeName)
6534                strcat(string, type.typeName);
6535             else
6536                strcat(string, "int"); // "enum");
6537             break;
6538          case ellipsisType:
6539             strcat(string, "...");
6540             break;
6541          case subClassType:
6542             strcat(string, "subclass(");
6543             strcat(string, type._class ? type._class.string : "int");
6544             strcat(string, ")");
6545             break;
6546          case templateType:
6547             strcat(string, type.templateParameter.identifier.string);
6548             break;
6549          case thisClassType:
6550             strcat(string, "thisclass");
6551             break;
6552          case vaListType:
6553             strcat(string, "__builtin_va_list");
6554             break;
6555       }
6556    }
6557 }
6558
6559 static void PrintName(Type type, char * string, bool fullName)
6560 {
6561    if(type.name && type.name[0])
6562    {
6563       if(fullName)
6564          strcat(string, type.name);
6565       else
6566       {
6567          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6568          if(name) name += 2; else name = type.name;
6569          strcat(string, name);
6570       }
6571    }
6572 }
6573
6574 static void PrintAttribs(Type type, char * string)
6575 {
6576    if(type)
6577    {
6578       if(type.dllExport)   strcat(string, "dllexport ");
6579       if(type.attrStdcall) strcat(string, "stdcall ");
6580    }
6581 }
6582
6583 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6584 {
6585    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6586    {
6587       Type attrType = null;
6588       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6589          PrintAttribs(type, string);
6590       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6591          strcat(string, " const");
6592       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6593       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6594          strcat(string, " (");
6595       if(type.kind == pointerType)
6596       {
6597          if(type.type.kind == functionType || type.type.kind == methodType)
6598             PrintAttribs(type.type, string);
6599       }
6600       if(type.kind == pointerType)
6601       {
6602          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6603             strcat(string, "*");
6604          else
6605             strcat(string, " *");
6606       }
6607       if(printConst && type.constant && type.kind == pointerType)
6608          strcat(string, " const");
6609    }
6610    else
6611       PrintTypeSpecs(type, string, fullName, printConst);
6612 }
6613
6614 static void PostPrintType(Type type, char * string, bool fullName)
6615 {
6616    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6617       strcat(string, ")");
6618    if(type.kind == arrayType)
6619       PrintArraySize(type, string);
6620    else if(type.kind == functionType)
6621    {
6622       Type param;
6623       strcat(string, "(");
6624       for(param = type.params.first; param; param = param.next)
6625       {
6626          PrintType(param, string, true, fullName);
6627          if(param.next) strcat(string, ", ");
6628       }
6629       strcat(string, ")");
6630    }
6631    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6632       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6633 }
6634
6635 // *****
6636 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6637 // *****
6638 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6639 {
6640    PrePrintType(type, string, fullName, null, printConst);
6641
6642    if(type.thisClass || (printName && type.name && type.name[0]))
6643       strcat(string, " ");
6644    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6645    {
6646       Symbol _class = type.thisClass;
6647       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6648       {
6649          if(type.classObjectType == classPointer)
6650             strcat(string, "class");
6651          else
6652             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6653       }
6654       else if(_class && _class.string)
6655       {
6656          String s = _class.string;
6657          if(fullName)
6658             strcat(string, s);
6659          else
6660          {
6661             char * name = RSearchString(s, "::", strlen(s), true, false);
6662             if(name) name += 2; else name = s;
6663             strcat(string, name);
6664          }
6665       }
6666       strcat(string, "::");
6667    }
6668
6669    if(printName && type.name)
6670       PrintName(type, string, fullName);
6671    PostPrintType(type, string, fullName);
6672    if(type.bitFieldCount)
6673    {
6674       char count[100];
6675       sprintf(count, ":%d", type.bitFieldCount);
6676       strcat(string, count);
6677    }
6678 }
6679
6680 void PrintType(Type type, char * string, bool printName, bool fullName)
6681 {
6682    _PrintType(type, string, printName, fullName, true);
6683 }
6684
6685 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6686 {
6687    _PrintType(type, string, printName, fullName, false);
6688 }
6689
6690 static Type FindMember(Type type, char * string)
6691 {
6692    Type memberType;
6693    for(memberType = type.members.first; memberType; memberType = memberType.next)
6694    {
6695       if(!memberType.name)
6696       {
6697          Type subType = FindMember(memberType, string);
6698          if(subType)
6699             return subType;
6700       }
6701       else if(!strcmp(memberType.name, string))
6702          return memberType;
6703    }
6704    return null;
6705 }
6706
6707 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6708 {
6709    Type memberType;
6710    for(memberType = type.members.first; memberType; memberType = memberType.next)
6711    {
6712       if(!memberType.name)
6713       {
6714          Type subType = FindMember(memberType, string);
6715          if(subType)
6716          {
6717             *offset += memberType.offset;
6718             return subType;
6719          }
6720       }
6721       else if(!strcmp(memberType.name, string))
6722       {
6723          *offset += memberType.offset;
6724          return memberType;
6725       }
6726    }
6727    return null;
6728 }
6729
6730 Expression ParseExpressionString(char * expression)
6731 {
6732    fileInput = TempFile { };
6733    fileInput.Write(expression, 1, strlen(expression));
6734    fileInput.Seek(0, start);
6735
6736    echoOn = false;
6737    parsedExpression = null;
6738    resetScanner();
6739    expression_yyparse();
6740    delete fileInput;
6741
6742    return parsedExpression;
6743 }
6744
6745 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6746 {
6747    Identifier id = exp.identifier;
6748    Method method = null;
6749    Property prop = null;
6750    DataMember member = null;
6751    ClassProperty classProp = null;
6752
6753    if(_class && _class.type == enumClass)
6754    {
6755       NamedLink value = null;
6756       Class enumClass = eSystem_FindClass(privateModule, "enum");
6757       if(enumClass)
6758       {
6759          Class baseClass;
6760          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
6761          {
6762             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
6763             for(value = e.values.first; value; value = value.next)
6764             {
6765                if(!strcmp(value.name, id.string))
6766                   break;
6767             }
6768             if(value)
6769             {
6770                char constant[256];
6771
6772                FreeExpContents(exp);
6773
6774                exp.type = constantExp;
6775                exp.isConstant = true;
6776                if(!strcmp(baseClass.dataTypeString, "int"))
6777                   sprintf(constant, "%d",(int)value.data);
6778                else
6779                   sprintf(constant, "0x%X",(int)value.data);
6780                exp.constant = CopyString(constant);
6781                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
6782                exp.expType = MkClassType(baseClass.fullName);
6783                break;
6784             }
6785          }
6786       }
6787       if(value)
6788          return true;
6789    }
6790    if((method = eClass_FindMethod(_class, id.string, privateModule)))
6791    {
6792       ProcessMethodType(method);
6793       exp.expType = Type
6794       {
6795          refCount = 1;
6796          kind = methodType;
6797          method = method;
6798          // Crash here?
6799          // TOCHECK: Put it back to what it was...
6800          // methodClass = _class;
6801          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
6802       };
6803       //id._class = null;
6804       return true;
6805    }
6806    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
6807    {
6808       if(!prop.dataType)
6809          ProcessPropertyType(prop);
6810       exp.expType = prop.dataType;
6811       if(prop.dataType) prop.dataType.refCount++;
6812       return true;
6813    }
6814    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
6815    {
6816       if(!member.dataType)
6817          member.dataType = ProcessTypeString(member.dataTypeString, false);
6818       exp.expType = member.dataType;
6819       if(member.dataType) member.dataType.refCount++;
6820       return true;
6821    }
6822    else if((classProp = eClass_FindClassProperty(_class, id.string)))
6823    {
6824       if(!classProp.dataType)
6825          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
6826
6827       if(classProp.constant)
6828       {
6829          FreeExpContents(exp);
6830
6831          exp.isConstant = true;
6832          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
6833          {
6834             //char constant[256];
6835             exp.type = stringExp;
6836             exp.constant = QMkString((char *)classProp.Get(_class));
6837          }
6838          else
6839          {
6840             char constant[256];
6841             exp.type = constantExp;
6842             sprintf(constant, "%d", (int)classProp.Get(_class));
6843             exp.constant = CopyString(constant);
6844          }
6845       }
6846       else
6847       {
6848          // TO IMPLEMENT...
6849       }
6850
6851       exp.expType = classProp.dataType;
6852       if(classProp.dataType) classProp.dataType.refCount++;
6853       return true;
6854    }
6855    return false;
6856 }
6857
6858 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
6859 {
6860    BinaryTree * tree = &nameSpace.functions;
6861    GlobalData data = (GlobalData)tree->FindString(name);
6862    NameSpace * child;
6863    if(!data)
6864    {
6865       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
6866       {
6867          data = ScanGlobalData(child, name);
6868          if(data)
6869             break;
6870       }
6871    }
6872    return data;
6873 }
6874
6875 static GlobalData FindGlobalData(char * name)
6876 {
6877    int start = 0, c;
6878    NameSpace * nameSpace;
6879    nameSpace = globalData;
6880    for(c = 0; name[c]; c++)
6881    {
6882       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
6883       {
6884          NameSpace * newSpace;
6885          char * spaceName = new char[c - start + 1];
6886          strncpy(spaceName, name + start, c - start);
6887          spaceName[c-start] = '\0';
6888          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
6889          delete spaceName;
6890          if(!newSpace)
6891             return null;
6892          nameSpace = newSpace;
6893          if(name[c] == ':') c++;
6894          start = c+1;
6895       }
6896    }
6897    if(c - start)
6898    {
6899       return ScanGlobalData(nameSpace, name + start);
6900    }
6901    return null;
6902 }
6903
6904 static int definedExpStackPos;
6905 static void * definedExpStack[512];
6906
6907 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
6908 void ReplaceExpContents(Expression checkedExp, Expression newExp)
6909 {
6910    Expression prev = checkedExp.prev, next = checkedExp.next;
6911
6912    FreeExpContents(checkedExp);
6913    FreeType(checkedExp.expType);
6914    FreeType(checkedExp.destType);
6915
6916    *checkedExp = *newExp;
6917
6918    delete newExp;
6919
6920    checkedExp.prev = prev;
6921    checkedExp.next = next;
6922 }
6923
6924 void ApplyAnyObjectLogic(Expression e)
6925 {
6926    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
6927 #ifdef _DEBUG
6928    char debugExpString[4096];
6929    debugExpString[0] = '\0';
6930    PrintExpression(e, debugExpString);
6931 #endif
6932
6933    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
6934    {
6935       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
6936       //ellipsisDestType = destType;
6937       if(e && e.expType)
6938       {
6939          Type type = e.expType;
6940          Class _class = null;
6941          //Type destType = e.destType;
6942
6943          if(type.kind == classType && type._class && type._class.registered)
6944          {
6945             _class = type._class.registered;
6946          }
6947          else if(type.kind == subClassType)
6948          {
6949             _class = FindClass("ecere::com::Class").registered;
6950          }
6951          else
6952          {
6953             char string[1024] = "";
6954             Symbol classSym;
6955
6956             PrintTypeNoConst(type, string, false, true);
6957             classSym = FindClass(string);
6958             if(classSym) _class = classSym.registered;
6959          }
6960
6961          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...
6962             (!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))) ||
6963             destType.byReference)))
6964          {
6965             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
6966             {
6967                Expression checkedExp = e, newExp;
6968
6969                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
6970                {
6971                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
6972                   {
6973                      if(checkedExp.type == extensionCompoundExp)
6974                      {
6975                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
6976                      }
6977                      else
6978                         checkedExp = checkedExp.list->last;
6979                   }
6980                   else if(checkedExp.type == castExp)
6981                      checkedExp = checkedExp.cast.exp;
6982                }
6983
6984                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
6985                {
6986                   newExp = checkedExp.op.exp2;
6987                   checkedExp.op.exp2 = null;
6988                   FreeExpContents(checkedExp);
6989
6990                   if(e.expType && e.expType.passAsTemplate)
6991                   {
6992                      char size[100];
6993                      ComputeTypeSize(e.expType);
6994                      sprintf(size, "%d", e.expType.size);
6995                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
6996                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
6997                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
6998                   }
6999
7000                   ReplaceExpContents(checkedExp, newExp);
7001                   e.byReference = true;
7002                }
7003                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7004                {
7005                   Expression checkedExp, newExp;
7006
7007                   {
7008                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7009                      bool hasAddress =
7010                         e.type == identifierExp ||
7011                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7012                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7013                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7014                         e.type == indexExp;
7015
7016                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7017                      {
7018                         Context context = PushContext();
7019                         Declarator decl;
7020                         OldList * specs = MkList();
7021                         char typeString[1024];
7022                         Expression newExp { };
7023
7024                         typeString[0] = '\0';
7025                         *newExp = *e;
7026
7027                         //if(e.destType) e.destType.refCount++;
7028                         // if(exp.expType) exp.expType.refCount++;
7029                         newExp.prev = null;
7030                         newExp.next = null;
7031                         newExp.expType = null;
7032
7033                         PrintTypeNoConst(e.expType, typeString, false, true);
7034                         decl = SpecDeclFromString(typeString, specs, null);
7035                         newExp.destType = ProcessType(specs, decl);
7036
7037                         curContext = context;
7038
7039                         // We need a current compound for this
7040                         if(curCompound)
7041                         {
7042                            char name[100];
7043                            OldList * stmts = MkList();
7044                            e.type = extensionCompoundExp;
7045                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7046                            if(!curCompound.compound.declarations)
7047                               curCompound.compound.declarations = MkList();
7048                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7049                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7050                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7051                            e.compound = MkCompoundStmt(null, stmts);
7052                         }
7053                         else
7054                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7055
7056                         /*
7057                         e.compound = MkCompoundStmt(
7058                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7059                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7060
7061                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7062                         */
7063
7064                         {
7065                            Type type = e.destType;
7066                            e.destType = { };
7067                            CopyTypeInto(e.destType, type);
7068                            e.destType.refCount = 1;
7069                            e.destType.classObjectType = none;
7070                            FreeType(type);
7071                         }
7072
7073                         e.compound.compound.context = context;
7074                         PopContext(context);
7075                         curContext = context.parent;
7076                      }
7077                   }
7078
7079                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7080                   checkedExp = e;
7081                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7082                   {
7083                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7084                      {
7085                         if(checkedExp.type == extensionCompoundExp)
7086                         {
7087                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7088                         }
7089                         else
7090                            checkedExp = checkedExp.list->last;
7091                      }
7092                      else if(checkedExp.type == castExp)
7093                         checkedExp = checkedExp.cast.exp;
7094                   }
7095                   {
7096                      Expression operand { };
7097                      operand = *checkedExp;
7098                      checkedExp.destType = null;
7099                      checkedExp.expType = null;
7100                      checkedExp.Clear();
7101                      checkedExp.type = opExp;
7102                      checkedExp.op.op = '&';
7103                      checkedExp.op.exp1 = null;
7104                      checkedExp.op.exp2 = operand;
7105
7106                      //newExp = MkExpOp(null, '&', checkedExp);
7107                   }
7108                   //ReplaceExpContents(checkedExp, newExp);
7109                }
7110             }
7111          }
7112       }
7113    }
7114    {
7115       // If expression type is a simple class, make it an address
7116       // FixReference(e, true);
7117    }
7118 //#if 0
7119    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7120       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7121          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7122    {
7123       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"))
7124       {
7125          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7126       }
7127       else
7128       {
7129          Expression thisExp { };
7130
7131          *thisExp = *e;
7132          thisExp.prev = null;
7133          thisExp.next = null;
7134          e.Clear();
7135
7136          e.type = bracketsExp;
7137          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7138          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7139             ((Expression)e.list->first).byReference = true;
7140
7141          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7142          {
7143             e.expType = thisExp.expType;
7144             e.expType.refCount++;
7145          }
7146          else*/
7147          {
7148             e.expType = { };
7149             CopyTypeInto(e.expType, thisExp.expType);
7150             e.expType.byReference = false;
7151             e.expType.refCount = 1;
7152
7153             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7154                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7155             {
7156                e.expType.classObjectType = none;
7157             }
7158          }
7159       }
7160    }
7161 // TOFIX: Try this for a nice IDE crash!
7162 //#endif
7163    // The other way around
7164    else
7165 //#endif
7166    if(destType && e.expType &&
7167          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7168          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7169          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7170    {
7171       if(destType.kind == ellipsisType)
7172       {
7173          Compiler_Error($"Unspecified type\n");
7174       }
7175       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7176       {
7177          bool byReference = e.expType.byReference;
7178          Expression thisExp { };
7179          Declarator decl;
7180          OldList * specs = MkList();
7181          char typeString[1024]; // Watch buffer overruns
7182          Type type;
7183          ClassObjectType backupClassObjectType;
7184          bool backupByReference;
7185
7186          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7187             type = e.expType;
7188          else
7189             type = destType;
7190
7191          backupClassObjectType = type.classObjectType;
7192          backupByReference = type.byReference;
7193
7194          type.classObjectType = none;
7195          type.byReference = false;
7196
7197          typeString[0] = '\0';
7198          PrintType(type, typeString, false, true);
7199          decl = SpecDeclFromString(typeString, specs, null);
7200
7201          type.classObjectType = backupClassObjectType;
7202          type.byReference = backupByReference;
7203
7204          *thisExp = *e;
7205          thisExp.prev = null;
7206          thisExp.next = null;
7207          e.Clear();
7208
7209          if( ( type.kind == classType && type._class && type._class.registered &&
7210                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7211                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7212              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7213              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7214          {
7215             e.type = opExp;
7216             e.op.op = '*';
7217             e.op.exp1 = null;
7218             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7219
7220             e.expType = { };
7221             CopyTypeInto(e.expType, type);
7222             e.expType.byReference = false;
7223             e.expType.refCount = 1;
7224          }
7225          else
7226          {
7227             e.type = castExp;
7228             e.cast.typeName = MkTypeName(specs, decl);
7229             e.cast.exp = thisExp;
7230             e.byReference = true;
7231             e.expType = type;
7232             type.refCount++;
7233          }
7234          e.destType = destType;
7235          destType.refCount++;
7236       }
7237    }
7238 }
7239
7240 void ProcessExpressionType(Expression exp)
7241 {
7242    bool unresolved = false;
7243    Location oldyylloc = yylloc;
7244    bool notByReference = false;
7245 #ifdef _DEBUG
7246    char debugExpString[4096];
7247    debugExpString[0] = '\0';
7248    PrintExpression(exp, debugExpString);
7249 #endif
7250    if(!exp || exp.expType)
7251       return;
7252
7253    //eSystem_Logf("%s\n", expString);
7254
7255    // Testing this here
7256    yylloc = exp.loc;
7257    switch(exp.type)
7258    {
7259       case identifierExp:
7260       {
7261          Identifier id = exp.identifier;
7262          if(!id) return;
7263
7264          // DOING THIS LATER NOW...
7265          if(id._class && id._class.name)
7266          {
7267             id.classSym = id._class.symbol; // FindClass(id._class.name);
7268             /* TODO: Name Space Fix ups
7269             if(!id.classSym)
7270                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7271             */
7272          }
7273
7274          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7275          {
7276             exp.expType = ProcessTypeString("Module", true);
7277             break;
7278          }
7279          else */if(strstr(id.string, "__ecereClass") == id.string)
7280          {
7281             exp.expType = ProcessTypeString("ecere::com::Class", true);
7282             break;
7283          }
7284          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7285          {
7286             // Added this here as well
7287             ReplaceClassMembers(exp, thisClass);
7288             if(exp.type != identifierExp)
7289             {
7290                ProcessExpressionType(exp);
7291                break;
7292             }
7293
7294             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7295                break;
7296          }
7297          else
7298          {
7299             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7300             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7301             if(!symbol/* && exp.destType*/)
7302             {
7303                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7304                   break;
7305                else
7306                {
7307                   if(thisClass)
7308                   {
7309                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7310                      if(exp.type != identifierExp)
7311                      {
7312                         ProcessExpressionType(exp);
7313                         break;
7314                      }
7315                   }
7316                   // Static methods called from inside the _class
7317                   else if(currentClass && !id._class)
7318                   {
7319                      if(ResolveIdWithClass(exp, currentClass, true))
7320                         break;
7321                   }
7322                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7323                }
7324             }
7325
7326             // If we manage to resolve this symbol
7327             if(symbol)
7328             {
7329                Type type = symbol.type;
7330                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7331
7332                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7333                {
7334                   Context context = SetupTemplatesContext(_class);
7335                   type = ReplaceThisClassType(_class);
7336                   FinishTemplatesContext(context);
7337                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7338                }
7339
7340                FreeSpecifier(id._class);
7341                id._class = null;
7342                delete id.string;
7343                id.string = CopyString(symbol.string);
7344
7345                id.classSym = null;
7346                exp.expType = type;
7347                if(type)
7348                   type.refCount++;
7349                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7350                   // Add missing cases here... enum Classes...
7351                   exp.isConstant = true;
7352
7353                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7354                if(symbol.isParam || !strcmp(id.string, "this"))
7355                {
7356                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7357                      exp.byReference = true;
7358
7359                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7360                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7361                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7362                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7363                   {
7364                      Identifier id = exp.identifier;
7365                      exp.type = bracketsExp;
7366                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7367                   }*/
7368                }
7369
7370                if(symbol.isIterator)
7371                {
7372                   if(symbol.isIterator == 3)
7373                   {
7374                      exp.type = bracketsExp;
7375                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7376                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7377                      exp.expType = null;
7378                      ProcessExpressionType(exp);
7379                   }
7380                   else if(symbol.isIterator != 4)
7381                   {
7382                      exp.type = memberExp;
7383                      exp.member.exp = MkExpIdentifier(exp.identifier);
7384                      exp.member.exp.expType = exp.expType;
7385                      /*if(symbol.isIterator == 6)
7386                         exp.member.member = MkIdentifier("key");
7387                      else*/
7388                         exp.member.member = MkIdentifier("data");
7389                      exp.expType = null;
7390                      ProcessExpressionType(exp);
7391                   }
7392                }
7393                break;
7394             }
7395             else
7396             {
7397                DefinedExpression definedExp = null;
7398                if(thisNameSpace && !(id._class && !id._class.name))
7399                {
7400                   char name[1024];
7401                   strcpy(name, thisNameSpace);
7402                   strcat(name, "::");
7403                   strcat(name, id.string);
7404                   definedExp = eSystem_FindDefine(privateModule, name);
7405                }
7406                if(!definedExp)
7407                   definedExp = eSystem_FindDefine(privateModule, id.string);
7408                if(definedExp)
7409                {
7410                   int c;
7411                   for(c = 0; c<definedExpStackPos; c++)
7412                      if(definedExpStack[c] == definedExp)
7413                         break;
7414                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7415                   {
7416                      Location backupYylloc = yylloc;
7417                      definedExpStack[definedExpStackPos++] = definedExp;
7418                      fileInput = TempFile { };
7419                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7420                      fileInput.Seek(0, start);
7421
7422                      echoOn = false;
7423                      parsedExpression = null;
7424                      resetScanner();
7425                      expression_yyparse();
7426                      delete fileInput;
7427
7428                      yylloc = backupYylloc;
7429
7430                      if(parsedExpression)
7431                      {
7432                         FreeIdentifier(id);
7433                         exp.type = bracketsExp;
7434                         exp.list = MkListOne(parsedExpression);
7435                         parsedExpression.loc = yylloc;
7436                         ProcessExpressionType(exp);
7437                         definedExpStackPos--;
7438                         return;
7439                      }
7440                      definedExpStackPos--;
7441                   }
7442                   else
7443                   {
7444                      if(inCompiler)
7445                      {
7446                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7447                      }
7448                   }
7449                }
7450                else
7451                {
7452                   GlobalData data = null;
7453                   if(thisNameSpace && !(id._class && !id._class.name))
7454                   {
7455                      char name[1024];
7456                      strcpy(name, thisNameSpace);
7457                      strcat(name, "::");
7458                      strcat(name, id.string);
7459                      data = FindGlobalData(name);
7460                   }
7461                   if(!data)
7462                      data = FindGlobalData(id.string);
7463                   if(data)
7464                   {
7465                      DeclareGlobalData(data);
7466                      exp.expType = data.dataType;
7467                      if(data.dataType) data.dataType.refCount++;
7468
7469                      delete id.string;
7470                      id.string = CopyString(data.fullName);
7471                      FreeSpecifier(id._class);
7472                      id._class = null;
7473
7474                      break;
7475                   }
7476                   else
7477                   {
7478                      GlobalFunction function = null;
7479                      if(thisNameSpace && !(id._class && !id._class.name))
7480                      {
7481                         char name[1024];
7482                         strcpy(name, thisNameSpace);
7483                         strcat(name, "::");
7484                         strcat(name, id.string);
7485                         function = eSystem_FindFunction(privateModule, name);
7486                      }
7487                      if(!function)
7488                         function = eSystem_FindFunction(privateModule, id.string);
7489                      if(function)
7490                      {
7491                         char name[1024];
7492                         delete id.string;
7493                         id.string = CopyString(function.name);
7494                         name[0] = 0;
7495
7496                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7497                            strcpy(name, "__ecereFunction_");
7498                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7499                         if(DeclareFunction(function, name))
7500                         {
7501                            delete id.string;
7502                            id.string = CopyString(name);
7503                         }
7504                         exp.expType = function.dataType;
7505                         if(function.dataType) function.dataType.refCount++;
7506
7507                         FreeSpecifier(id._class);
7508                         id._class = null;
7509
7510                         break;
7511                      }
7512                   }
7513                }
7514             }
7515          }
7516          unresolved = true;
7517          break;
7518       }
7519       case instanceExp:
7520       {
7521          Class _class;
7522          // Symbol classSym;
7523
7524          if(!exp.instance._class)
7525          {
7526             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7527             {
7528                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7529             }
7530          }
7531
7532          //classSym = FindClass(exp.instance._class.fullName);
7533          //_class = classSym ? classSym.registered : null;
7534
7535          ProcessInstantiationType(exp.instance);
7536          exp.isConstant = exp.instance.isConstant;
7537
7538          /*
7539          if(_class.type == unitClass && _class.base.type != systemClass)
7540          {
7541             {
7542                Type destType = exp.destType;
7543
7544                exp.destType = MkClassType(_class.base.fullName);
7545                exp.expType = MkClassType(_class.fullName);
7546                CheckExpressionType(exp, exp.destType, true);
7547
7548                exp.destType = destType;
7549             }
7550             exp.expType = MkClassType(_class.fullName);
7551          }
7552          else*/
7553          if(exp.instance._class)
7554          {
7555             exp.expType = MkClassType(exp.instance._class.name);
7556             /*if(exp.expType._class && exp.expType._class.registered &&
7557                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7558                exp.expType.byReference = true;*/
7559          }
7560          break;
7561       }
7562       case constantExp:
7563       {
7564          if(!exp.expType)
7565          {
7566             char * constant = exp.constant;
7567             Type type
7568             {
7569                refCount = 1;
7570                constant = true;
7571             };
7572             exp.expType = type;
7573
7574             if(constant[0] == '\'')
7575             {
7576                if((int)((byte *)constant)[1] > 127)
7577                {
7578                   int nb;
7579                   unichar ch = UTF8GetChar(constant + 1, &nb);
7580                   if(nb < 2) ch = constant[1];
7581                   delete constant;
7582                   exp.constant = PrintUInt(ch);
7583                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7584                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7585                   type._class = FindClass("unichar");
7586
7587                   type.isSigned = false;
7588                }
7589                else
7590                {
7591                   type.kind = charType;
7592                   type.isSigned = true;
7593                }
7594             }
7595             else
7596             {
7597                char * dot = strchr(constant, '.');
7598                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7599                char * exponent;
7600                if(isHex)
7601                {
7602                   exponent = strchr(constant, 'p');
7603                   if(!exponent) exponent = strchr(constant, 'P');
7604                }
7605                else
7606                {
7607                   exponent = strchr(constant, 'e');
7608                   if(!exponent) exponent = strchr(constant, 'E');
7609                }
7610
7611                if(dot || exponent)
7612                {
7613                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7614                      type.kind = floatType;
7615                   else
7616                      type.kind = doubleType;
7617                   type.isSigned = true;
7618                }
7619                else
7620                {
7621                   bool isSigned = constant[0] == '-';
7622                   int64 i64 = strtoll(constant, null, 0);
7623                   uint64 ui64 = strtoull(constant, null, 0);
7624                   bool is64Bit = false;
7625                   if(isSigned)
7626                   {
7627                      if(i64 < MININT)
7628                         is64Bit = true;
7629                   }
7630                   else
7631                   {
7632                      if(ui64 > MAXINT)
7633                      {
7634                         if(ui64 > MAXDWORD)
7635                         {
7636                            is64Bit = true;
7637                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7638                               isSigned = true;
7639                         }
7640                      }
7641                      else if(constant[0] != '0' || !constant[1])
7642                         isSigned = true;
7643                   }
7644                   type.kind = is64Bit ? int64Type : intType;
7645                   type.isSigned = isSigned;
7646                }
7647             }
7648             exp.isConstant = true;
7649             if(exp.destType && exp.destType.kind == doubleType)
7650                type.kind = doubleType;
7651             else if(exp.destType && exp.destType.kind == floatType)
7652                type.kind = floatType;
7653             else if(exp.destType && exp.destType.kind == int64Type)
7654                type.kind = int64Type;
7655          }
7656          break;
7657       }
7658       case stringExp:
7659       {
7660          exp.isConstant = true;      // Why wasn't this constant?
7661          exp.expType = Type
7662          {
7663             refCount = 1;
7664             kind = pointerType;
7665             type = Type
7666             {
7667                refCount = 1;
7668                kind = charType;
7669                constant = true;
7670                isSigned = true;
7671             }
7672          };
7673          break;
7674       }
7675       case newExp:
7676       case new0Exp:
7677          ProcessExpressionType(exp._new.size);
7678          exp.expType = Type
7679          {
7680             refCount = 1;
7681             kind = pointerType;
7682             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7683          };
7684          DeclareType(exp.expType.type, false, false);
7685          break;
7686       case renewExp:
7687       case renew0Exp:
7688          ProcessExpressionType(exp._renew.size);
7689          ProcessExpressionType(exp._renew.exp);
7690          exp.expType = Type
7691          {
7692             refCount = 1;
7693             kind = pointerType;
7694             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7695          };
7696          DeclareType(exp.expType.type, false, false);
7697          break;
7698       case opExp:
7699       {
7700          bool assign = false, boolResult = false, boolOps = false;
7701          Type type1 = null, type2 = null;
7702          bool useDestType = false, useSideType = false;
7703          Location oldyylloc = yylloc;
7704          bool useSideUnit = false;
7705
7706          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7707          Type dummy
7708          {
7709             count = 1;
7710             refCount = 1;
7711          };
7712
7713          switch(exp.op.op)
7714          {
7715             // Assignment Operators
7716             case '=':
7717             case MUL_ASSIGN:
7718             case DIV_ASSIGN:
7719             case MOD_ASSIGN:
7720             case ADD_ASSIGN:
7721             case SUB_ASSIGN:
7722             case LEFT_ASSIGN:
7723             case RIGHT_ASSIGN:
7724             case AND_ASSIGN:
7725             case XOR_ASSIGN:
7726             case OR_ASSIGN:
7727                assign = true;
7728                break;
7729             // boolean Operators
7730             case '!':
7731                // Expect boolean operators
7732                //boolOps = true;
7733                //boolResult = true;
7734                break;
7735             case AND_OP:
7736             case OR_OP:
7737                // Expect boolean operands
7738                boolOps = true;
7739                boolResult = true;
7740                break;
7741             // Comparisons
7742             case EQ_OP:
7743             case '<':
7744             case '>':
7745             case LE_OP:
7746             case GE_OP:
7747             case NE_OP:
7748                // Gives boolean result
7749                boolResult = true;
7750                useSideType = true;
7751                break;
7752             case '+':
7753             case '-':
7754                useSideUnit = true;
7755
7756                // Just added these... testing
7757             case '|':
7758             case '&':
7759             case '^':
7760
7761             // DANGER: Verify units
7762             case '/':
7763             case '%':
7764             case '*':
7765
7766                if(exp.op.op != '*' || exp.op.exp1)
7767                {
7768                   useSideType = true;
7769                   useDestType = true;
7770                }
7771                break;
7772
7773             /*// Implement speed etc.
7774             case '*':
7775             case '/':
7776                break;
7777             */
7778          }
7779          if(exp.op.op == '&')
7780          {
7781             // Added this here earlier for Iterator address as key
7782             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
7783             {
7784                Identifier id = exp.op.exp2.identifier;
7785                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7786                if(symbol && symbol.isIterator == 2)
7787                {
7788                   exp.type = memberExp;
7789                   exp.member.exp = exp.op.exp2;
7790                   exp.member.member = MkIdentifier("key");
7791                   exp.expType = null;
7792                   exp.op.exp2.expType = symbol.type;
7793                   symbol.type.refCount++;
7794                   ProcessExpressionType(exp);
7795                   FreeType(dummy);
7796                   break;
7797                }
7798                // exp.op.exp2.usage.usageRef = true;
7799             }
7800          }
7801
7802          //dummy.kind = TypeDummy;
7803
7804          if(exp.op.exp1)
7805          {
7806             if(exp.destType && exp.destType.kind == classType &&
7807                exp.destType._class && exp.destType._class.registered && useDestType &&
7808
7809               ((exp.destType._class.registered.type == unitClass && useSideUnit) ||
7810                exp.destType._class.registered.type == enumClass ||
7811                exp.destType._class.registered.type == bitClass
7812                ))
7813
7814               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
7815             {
7816                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7817                exp.op.exp1.destType = exp.destType;
7818                if(exp.destType)
7819                   exp.destType.refCount++;
7820             }
7821             else if(!assign)
7822             {
7823                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7824                exp.op.exp1.destType = dummy;
7825                dummy.refCount++;
7826             }
7827
7828             // TESTING THIS HERE...
7829             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
7830             ProcessExpressionType(exp.op.exp1);
7831             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
7832
7833             if(exp.op.exp1.destType == dummy)
7834             {
7835                FreeType(dummy);
7836                exp.op.exp1.destType = null;
7837             }
7838             type1 = exp.op.exp1.expType;
7839          }
7840
7841          if(exp.op.exp2)
7842          {
7843             char expString[10240];
7844             expString[0] = '\0';
7845             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
7846             {
7847                if(exp.op.exp1)
7848                {
7849                   exp.op.exp2.destType = exp.op.exp1.expType;
7850                   if(exp.op.exp1.expType)
7851                      exp.op.exp1.expType.refCount++;
7852                }
7853                else
7854                {
7855                   exp.op.exp2.destType = exp.destType;
7856                   if(exp.destType)
7857                      exp.destType.refCount++;
7858                }
7859
7860                if(type1) type1.refCount++;
7861                exp.expType = type1;
7862             }
7863             else if(assign)
7864             {
7865                if(inCompiler)
7866                   PrintExpression(exp.op.exp2, expString);
7867
7868                if(type1 && type1.kind == pointerType)
7869                {
7870                   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 ||
7871                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
7872                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
7873                   else if(exp.op.op == '=')
7874                   {
7875                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7876                      exp.op.exp2.destType = type1;
7877                      if(type1)
7878                         type1.refCount++;
7879                   }
7880                }
7881                else
7882                {
7883                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
7884                   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/* ||
7885                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
7886                   else
7887                   {
7888                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7889                      exp.op.exp2.destType = type1;
7890                      if(type1)
7891                         type1.refCount++;
7892                   }
7893                }
7894                if(type1) type1.refCount++;
7895                exp.expType = type1;
7896             }
7897             else if(exp.destType && exp.destType.kind == classType &&
7898                exp.destType._class && exp.destType._class.registered &&
7899
7900                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) ||
7901                   (exp.destType._class.registered.type == enumClass && useDestType))
7902                   )
7903             {
7904                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7905                exp.op.exp2.destType = exp.destType;
7906                if(exp.destType)
7907                   exp.destType.refCount++;
7908             }
7909             else
7910             {
7911                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7912                exp.op.exp2.destType = dummy;
7913                dummy.refCount++;
7914             }
7915
7916             // TESTING THIS HERE... (DANGEROUS)
7917             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
7918                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
7919             {
7920                FreeType(exp.op.exp2.destType);
7921                exp.op.exp2.destType = type1;
7922                type1.refCount++;
7923             }
7924             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
7925             ProcessExpressionType(exp.op.exp2);
7926             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
7927
7928             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
7929             {
7930                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)
7931                {
7932                   if(exp.op.op != '=' && type1.type.kind == voidType)
7933                      Compiler_Error($"void *: unknown size\n");
7934                }
7935                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||
7936                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
7937                               (exp.op.exp2.expType._class.registered.type == normalClass ||
7938                               exp.op.exp2.expType._class.registered.type == structClass ||
7939                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
7940                {
7941                   if(exp.op.op == ADD_ASSIGN)
7942                      Compiler_Error($"cannot add two pointers\n");
7943                }
7944                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
7945                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
7946                {
7947                   if(exp.op.op == ADD_ASSIGN)
7948                      Compiler_Error($"cannot add two pointers\n");
7949                }
7950                else if(inCompiler)
7951                {
7952                   char type1String[1024];
7953                   char type2String[1024];
7954                   type1String[0] = '\0';
7955                   type2String[0] = '\0';
7956
7957                   PrintType(exp.op.exp2.expType, type1String, false, true);
7958                   PrintType(type1, type2String, false, true);
7959                   ChangeCh(expString, '\n', ' ');
7960                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
7961                }
7962             }
7963
7964             if(exp.op.exp2.destType == dummy)
7965             {
7966                FreeType(dummy);
7967                exp.op.exp2.destType = null;
7968             }
7969
7970             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
7971             {
7972                type2 = { };
7973                type2.refCount = 1;
7974                CopyTypeInto(type2, exp.op.exp2.expType);
7975                type2.isSigned = true;
7976             }
7977             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
7978             {
7979                type2 = { kind = intType };
7980                type2.refCount = 1;
7981                type2.isSigned = true;
7982             }
7983             else
7984             {
7985                type2 = exp.op.exp2.expType;
7986                if(type2) type2.refCount++;
7987             }
7988          }
7989
7990          dummy.kind = voidType;
7991
7992          if(exp.op.op == SIZEOF)
7993          {
7994             exp.expType = Type
7995             {
7996                refCount = 1;
7997                kind = intType;
7998             };
7999             exp.isConstant = true;
8000          }
8001          // Get type of dereferenced pointer
8002          else if(exp.op.op == '*' && !exp.op.exp1)
8003          {
8004             exp.expType = Dereference(type2);
8005             if(type2 && type2.kind == classType)
8006                notByReference = true;
8007          }
8008          else if(exp.op.op == '&' && !exp.op.exp1)
8009             exp.expType = Reference(type2);
8010          else if(!assign)
8011          {
8012             if(boolOps)
8013             {
8014                if(exp.op.exp1)
8015                {
8016                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8017                   exp.op.exp1.destType = MkClassType("bool");
8018                   exp.op.exp1.destType.truth = true;
8019                   if(!exp.op.exp1.expType)
8020                      ProcessExpressionType(exp.op.exp1);
8021                   else
8022                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8023                   FreeType(exp.op.exp1.expType);
8024                   exp.op.exp1.expType = MkClassType("bool");
8025                   exp.op.exp1.expType.truth = true;
8026                }
8027                if(exp.op.exp2)
8028                {
8029                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8030                   exp.op.exp2.destType = MkClassType("bool");
8031                   exp.op.exp2.destType.truth = true;
8032                   if(!exp.op.exp2.expType)
8033                      ProcessExpressionType(exp.op.exp2);
8034                   else
8035                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8036                   FreeType(exp.op.exp2.expType);
8037                   exp.op.exp2.expType = MkClassType("bool");
8038                   exp.op.exp2.expType.truth = true;
8039                }
8040             }
8041             else if(exp.op.exp1 && exp.op.exp2 &&
8042                ((useSideType /*&&
8043                      (useSideUnit ||
8044                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8045                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8046                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8047                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8048             {
8049                if(type1 && type2 &&
8050                   // If either both are class or both are not class
8051                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8052                {
8053                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8054                   exp.op.exp2.destType = type1;
8055                   type1.refCount++;
8056                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8057                   exp.op.exp1.destType = type2;
8058                   type2.refCount++;
8059                   // Warning here for adding Radians + Degrees with no destination type
8060                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8061                      type1._class.registered && type1._class.registered.type == unitClass &&
8062                      type2._class.registered && type2._class.registered.type == unitClass &&
8063                      type1._class.registered != type2._class.registered)
8064                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8065                         type1._class.string, type2._class.string, type1._class.string);
8066
8067                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8068                   {
8069                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8070                      if(argExp)
8071                      {
8072                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8073
8074                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8075                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8076                            exp.op.exp1)));
8077
8078                         ProcessExpressionType(exp.op.exp1);
8079
8080                         if(type2.kind != pointerType)
8081                         {
8082                            ProcessExpressionType(classExp);
8083
8084                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8085                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8086                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8087                                  // noHeadClass
8088                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8089                                     OR_OP,
8090                                  // normalClass
8091                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8092                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8093                                        MkPointer(null, null), null)))),
8094                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8095
8096                            if(!exp.op.exp2.expType)
8097                            {
8098                               if(type2)
8099                                  FreeType(type2);
8100                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8101                               type2.refCount++;
8102                            }
8103
8104                            ProcessExpressionType(exp.op.exp2);
8105                         }
8106                      }
8107                   }
8108
8109                   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)))
8110                   {
8111                      if(type1.kind != classType && type1.type.kind == voidType)
8112                         Compiler_Error($"void *: unknown size\n");
8113                      exp.expType = type1;
8114                      if(type1) type1.refCount++;
8115                   }
8116                   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)))
8117                   {
8118                      if(type2.kind != classType && type2.type.kind == voidType)
8119                         Compiler_Error($"void *: unknown size\n");
8120                      exp.expType = type2;
8121                      if(type2) type2.refCount++;
8122                   }
8123                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8124                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8125                   {
8126                      Compiler_Warning($"different levels of indirection\n");
8127                   }
8128                   else
8129                   {
8130                      bool success = false;
8131                      if(type1.kind == pointerType && type2.kind == pointerType)
8132                      {
8133                         if(exp.op.op == '+')
8134                            Compiler_Error($"cannot add two pointers\n");
8135                         else if(exp.op.op == '-')
8136                         {
8137                            // Pointer Subtraction gives integer
8138                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8139                            {
8140                               exp.expType = Type
8141                               {
8142                                  kind = intType;
8143                                  refCount = 1;
8144                               };
8145                               success = true;
8146
8147                               if(type1.type.kind == templateType)
8148                               {
8149                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8150                                  if(argExp)
8151                                  {
8152                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8153
8154                                     ProcessExpressionType(classExp);
8155
8156                                     exp.type = bracketsExp;
8157                                     exp.list = MkListOne(MkExpOp(
8158                                        MkExpBrackets(MkListOne(MkExpOp(
8159                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8160                                              , exp.op.op,
8161                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8162
8163                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8164
8165                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8166                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8167                                                 // noHeadClass
8168                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8169                                                    OR_OP,
8170                                                 // normalClass
8171                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8172                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8173                                                       MkPointer(null, null), null)))),
8174                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8175
8176
8177                                              ));
8178
8179                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8180                                     FreeType(dummy);
8181                                     return;
8182                                  }
8183                               }
8184                            }
8185                         }
8186                      }
8187
8188                      if(!success && exp.op.exp1.type == constantExp)
8189                      {
8190                         // If first expression is constant, try to match that first
8191                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8192                         {
8193                            if(exp.expType) FreeType(exp.expType);
8194                            exp.expType = exp.op.exp1.destType;
8195                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8196                            success = true;
8197                         }
8198                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8199                         {
8200                            if(exp.expType) FreeType(exp.expType);
8201                            exp.expType = exp.op.exp2.destType;
8202                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8203                            success = true;
8204                         }
8205                      }
8206                      else if(!success)
8207                      {
8208                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8209                         {
8210                            if(exp.expType) FreeType(exp.expType);
8211                            exp.expType = exp.op.exp2.destType;
8212                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8213                            success = true;
8214                         }
8215                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8216                         {
8217                            if(exp.expType) FreeType(exp.expType);
8218                            exp.expType = exp.op.exp1.destType;
8219                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8220                            success = true;
8221                         }
8222                      }
8223                      if(!success)
8224                      {
8225                         char expString1[10240];
8226                         char expString2[10240];
8227                         char type1[1024];
8228                         char type2[1024];
8229                         expString1[0] = '\0';
8230                         expString2[0] = '\0';
8231                         type1[0] = '\0';
8232                         type2[0] = '\0';
8233                         if(inCompiler)
8234                         {
8235                            PrintExpression(exp.op.exp1, expString1);
8236                            ChangeCh(expString1, '\n', ' ');
8237                            PrintExpression(exp.op.exp2, expString2);
8238                            ChangeCh(expString2, '\n', ' ');
8239                            PrintType(exp.op.exp1.expType, type1, false, true);
8240                            PrintType(exp.op.exp2.expType, type2, false, true);
8241                         }
8242
8243                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8244                      }
8245                   }
8246                }
8247                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8248                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8249                {
8250                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8251                   // Convert e.g. / 4 into / 4.0
8252                   exp.op.exp1.destType = type2._class.registered.dataType;
8253                   if(type2._class.registered.dataType)
8254                      type2._class.registered.dataType.refCount++;
8255                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8256                   exp.expType = type2;
8257                   if(type2) type2.refCount++;
8258                }
8259                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8260                {
8261                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8262                   // Convert e.g. / 4 into / 4.0
8263                   exp.op.exp2.destType = type1._class.registered.dataType;
8264                   if(type1._class.registered.dataType)
8265                      type1._class.registered.dataType.refCount++;
8266                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8267                   exp.expType = type1;
8268                   if(type1) type1.refCount++;
8269                }
8270                else if(type1)
8271                {
8272                   bool valid = false;
8273
8274                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8275                   {
8276                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8277
8278                      if(!type1._class.registered.dataType)
8279                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8280                      exp.op.exp2.destType = type1._class.registered.dataType;
8281                      exp.op.exp2.destType.refCount++;
8282
8283                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8284                      if(type2)
8285                         FreeType(type2);
8286                      type2 = exp.op.exp2.destType;
8287                      if(type2) type2.refCount++;
8288
8289                      exp.expType = type2;
8290                      type2.refCount++;
8291                   }
8292
8293                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8294                   {
8295                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8296
8297                      if(!type2._class.registered.dataType)
8298                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8299                      exp.op.exp1.destType = type2._class.registered.dataType;
8300                      exp.op.exp1.destType.refCount++;
8301
8302                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8303                      type1 = exp.op.exp1.destType;
8304                      exp.expType = type1;
8305                      type1.refCount++;
8306                   }
8307
8308                   // TESTING THIS NEW CODE
8309                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8310                   {
8311                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8312                      {
8313                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8314                         {
8315                            if(exp.expType) FreeType(exp.expType);
8316                            exp.expType = exp.op.exp1.expType;
8317                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8318                            valid = true;
8319                         }
8320                      }
8321
8322                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8323                      {
8324                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8325                         {
8326                            if(exp.expType) FreeType(exp.expType);
8327                            exp.expType = exp.op.exp2.expType;
8328                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8329                            valid = true;
8330                         }
8331                      }
8332                   }
8333
8334                   if(!valid)
8335                   {
8336                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8337                      exp.op.exp2.destType = type1;
8338                      type1.refCount++;
8339
8340                      /*
8341                      // Maybe this was meant to be an enum...
8342                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8343                      {
8344                         Type oldType = exp.op.exp2.expType;
8345                         exp.op.exp2.expType = null;
8346                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8347                            FreeType(oldType);
8348                         else
8349                            exp.op.exp2.expType = oldType;
8350                      }
8351                      */
8352
8353                      /*
8354                      // TESTING THIS HERE... LATEST ADDITION
8355                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8356                      {
8357                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8358                         exp.op.exp2.destType = type2._class.registered.dataType;
8359                         if(type2._class.registered.dataType)
8360                            type2._class.registered.dataType.refCount++;
8361                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8362
8363                         //exp.expType = type2._class.registered.dataType; //type2;
8364                         //if(type2) type2.refCount++;
8365                      }
8366
8367                      // TESTING THIS HERE... LATEST ADDITION
8368                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8369                      {
8370                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8371                         exp.op.exp1.destType = type1._class.registered.dataType;
8372                         if(type1._class.registered.dataType)
8373                            type1._class.registered.dataType.refCount++;
8374                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8375                         exp.expType = type1._class.registered.dataType; //type1;
8376                         if(type1) type1.refCount++;
8377                      }
8378                      */
8379
8380                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8381                      {
8382                         if(exp.expType) FreeType(exp.expType);
8383                         exp.expType = exp.op.exp2.destType;
8384                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8385                      }
8386                      else if(type1 && type2)
8387                      {
8388                         char expString1[10240];
8389                         char expString2[10240];
8390                         char type1String[1024];
8391                         char type2String[1024];
8392                         expString1[0] = '\0';
8393                         expString2[0] = '\0';
8394                         type1String[0] = '\0';
8395                         type2String[0] = '\0';
8396                         if(inCompiler)
8397                         {
8398                            PrintExpression(exp.op.exp1, expString1);
8399                            ChangeCh(expString1, '\n', ' ');
8400                            PrintExpression(exp.op.exp2, expString2);
8401                            ChangeCh(expString2, '\n', ' ');
8402                            PrintType(exp.op.exp1.expType, type1String, false, true);
8403                            PrintType(exp.op.exp2.expType, type2String, false, true);
8404                         }
8405
8406                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8407
8408                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8409                         {
8410                            exp.expType = exp.op.exp1.expType;
8411                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8412                         }
8413                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8414                         {
8415                            exp.expType = exp.op.exp2.expType;
8416                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8417                         }
8418                      }
8419                   }
8420                }
8421                else if(type2)
8422                {
8423                   // Maybe this was meant to be an enum...
8424                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8425                   {
8426                      Type oldType = exp.op.exp1.expType;
8427                      exp.op.exp1.expType = null;
8428                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8429                         FreeType(oldType);
8430                      else
8431                         exp.op.exp1.expType = oldType;
8432                   }
8433
8434                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8435                   exp.op.exp1.destType = type2;
8436                   type2.refCount++;
8437                   /*
8438                   // TESTING THIS HERE... LATEST ADDITION
8439                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8440                   {
8441                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8442                      exp.op.exp1.destType = type1._class.registered.dataType;
8443                      if(type1._class.registered.dataType)
8444                         type1._class.registered.dataType.refCount++;
8445                   }
8446
8447                   // TESTING THIS HERE... LATEST ADDITION
8448                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8449                   {
8450                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8451                      exp.op.exp2.destType = type2._class.registered.dataType;
8452                      if(type2._class.registered.dataType)
8453                         type2._class.registered.dataType.refCount++;
8454                   }
8455                   */
8456
8457                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8458                   {
8459                      if(exp.expType) FreeType(exp.expType);
8460                      exp.expType = exp.op.exp1.destType;
8461                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8462                   }
8463                }
8464             }
8465             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8466             {
8467                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8468                {
8469                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8470                   // Convert e.g. / 4 into / 4.0
8471                   exp.op.exp1.destType = type2._class.registered.dataType;
8472                   if(type2._class.registered.dataType)
8473                      type2._class.registered.dataType.refCount++;
8474                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8475                }
8476                if(exp.op.op == '!')
8477                {
8478                   exp.expType = MkClassType("bool");
8479                   exp.expType.truth = true;
8480                }
8481                else
8482                {
8483                   exp.expType = type2;
8484                   if(type2) type2.refCount++;
8485                }
8486             }
8487             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8488             {
8489                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8490                {
8491                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8492                   // Convert e.g. / 4 into / 4.0
8493                   exp.op.exp2.destType = type1._class.registered.dataType;
8494                   if(type1._class.registered.dataType)
8495                      type1._class.registered.dataType.refCount++;
8496                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8497                }
8498                exp.expType = type1;
8499                if(type1) type1.refCount++;
8500             }
8501          }
8502
8503          yylloc = exp.loc;
8504          if(exp.op.exp1 && !exp.op.exp1.expType)
8505          {
8506             char expString[10000];
8507             expString[0] = '\0';
8508             if(inCompiler)
8509             {
8510                PrintExpression(exp.op.exp1, expString);
8511                ChangeCh(expString, '\n', ' ');
8512             }
8513             if(expString[0])
8514                Compiler_Error($"couldn't determine type of %s\n", expString);
8515          }
8516          if(exp.op.exp2 && !exp.op.exp2.expType)
8517          {
8518             char expString[10240];
8519             expString[0] = '\0';
8520             if(inCompiler)
8521             {
8522                PrintExpression(exp.op.exp2, expString);
8523                ChangeCh(expString, '\n', ' ');
8524             }
8525             if(expString[0])
8526                Compiler_Error($"couldn't determine type of %s\n", expString);
8527          }
8528
8529          if(boolResult)
8530          {
8531             FreeType(exp.expType);
8532             exp.expType = MkClassType("bool");
8533             exp.expType.truth = true;
8534          }
8535
8536          if(exp.op.op != SIZEOF)
8537             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8538                (!exp.op.exp2 || exp.op.exp2.isConstant);
8539
8540          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8541          {
8542             DeclareType(exp.op.exp2.expType, false, false);
8543          }
8544
8545          yylloc = oldyylloc;
8546
8547          FreeType(dummy);
8548          if(type2)
8549             FreeType(type2);
8550          break;
8551       }
8552       case bracketsExp:
8553       case extensionExpressionExp:
8554       {
8555          Expression e;
8556          exp.isConstant = true;
8557          for(e = exp.list->first; e; e = e.next)
8558          {
8559             bool inced = false;
8560             if(!e.next)
8561             {
8562                FreeType(e.destType);
8563                e.destType = exp.destType;
8564                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8565             }
8566             ProcessExpressionType(e);
8567             if(inced)
8568                exp.destType.count--;
8569             if(!exp.expType && !e.next)
8570             {
8571                exp.expType = e.expType;
8572                if(e.expType) e.expType.refCount++;
8573             }
8574             if(!e.isConstant)
8575                exp.isConstant = false;
8576          }
8577
8578          // In case a cast became a member...
8579          e = exp.list->first;
8580          if(!e.next && e.type == memberExp)
8581          {
8582             // Preserve prev, next
8583             Expression next = exp.next, prev = exp.prev;
8584
8585
8586             FreeType(exp.expType);
8587             FreeType(exp.destType);
8588             delete exp.list;
8589
8590             *exp = *e;
8591
8592             exp.prev = prev;
8593             exp.next = next;
8594
8595             delete e;
8596
8597             ProcessExpressionType(exp);
8598          }
8599          break;
8600       }
8601       case indexExp:
8602       {
8603          Expression e;
8604          exp.isConstant = true;
8605
8606          ProcessExpressionType(exp.index.exp);
8607          if(!exp.index.exp.isConstant)
8608             exp.isConstant = false;
8609
8610          if(exp.index.exp.expType)
8611          {
8612             Type source = exp.index.exp.expType;
8613             if(source.kind == classType && source._class && source._class.registered)
8614             {
8615                Class _class = source._class.registered;
8616                Class c = _class.templateClass ? _class.templateClass : _class;
8617                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
8618                {
8619                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8620
8621                   if(exp.index.index && exp.index.index->last)
8622                   {
8623                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8624                   }
8625                }
8626             }
8627          }
8628
8629          for(e = exp.index.index->first; e; e = e.next)
8630          {
8631             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8632             {
8633                if(e.destType) FreeType(e.destType);
8634                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8635             }
8636             ProcessExpressionType(e);
8637             if(!e.next)
8638             {
8639                // Check if this type is int
8640             }
8641             if(!e.isConstant)
8642                exp.isConstant = false;
8643          }
8644
8645          if(!exp.expType)
8646             exp.expType = Dereference(exp.index.exp.expType);
8647          if(exp.expType)
8648             DeclareType(exp.expType, false, false);
8649          break;
8650       }
8651       case callExp:
8652       {
8653          Expression e;
8654          Type functionType;
8655          Type methodType = null;
8656          char name[1024];
8657          name[0] = '\0';
8658
8659          if(inCompiler)
8660          {
8661             PrintExpression(exp.call.exp,  name);
8662             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8663             {
8664                //exp.call.exp.expType = null;
8665                PrintExpression(exp.call.exp,  name);
8666             }
8667          }
8668          if(exp.call.exp.type == identifierExp)
8669          {
8670             Expression idExp = exp.call.exp;
8671             Identifier id = idExp.identifier;
8672             if(!strcmp(id.string, "__builtin_frame_address"))
8673             {
8674                exp.expType = ProcessTypeString("void *", true);
8675                if(exp.call.arguments && exp.call.arguments->first)
8676                   ProcessExpressionType(exp.call.arguments->first);
8677                break;
8678             }
8679             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8680             {
8681                exp.expType = ProcessTypeString("int", true);
8682                if(exp.call.arguments && exp.call.arguments->first)
8683                   ProcessExpressionType(exp.call.arguments->first);
8684                break;
8685             }
8686             else if(!strcmp(id.string, "Max") ||
8687                !strcmp(id.string, "Min") ||
8688                !strcmp(id.string, "Sgn") ||
8689                !strcmp(id.string, "Abs"))
8690             {
8691                Expression a = null;
8692                Expression b = null;
8693                Expression tempExp1 = null, tempExp2 = null;
8694                if((!strcmp(id.string, "Max") ||
8695                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8696                {
8697                   a = exp.call.arguments->first;
8698                   b = exp.call.arguments->last;
8699                   tempExp1 = a;
8700                   tempExp2 = b;
8701                }
8702                else if(exp.call.arguments->count == 1)
8703                {
8704                   a = exp.call.arguments->first;
8705                   tempExp1 = a;
8706                }
8707
8708                if(a)
8709                {
8710                   exp.call.arguments->Clear();
8711                   idExp.identifier = null;
8712
8713                   FreeExpContents(exp);
8714
8715                   ProcessExpressionType(a);
8716                   if(b)
8717                      ProcessExpressionType(b);
8718
8719                   exp.type = bracketsExp;
8720                   exp.list = MkList();
8721
8722                   if(a.expType && (!b || b.expType))
8723                   {
8724                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
8725                      {
8726                         // Use the simpleStruct name/ids for now...
8727                         if(inCompiler)
8728                         {
8729                            OldList * specs = MkList();
8730                            OldList * decls = MkList();
8731                            Declaration decl;
8732                            char temp1[1024], temp2[1024];
8733
8734                            GetTypeSpecs(a.expType, specs);
8735
8736                            if(a && !a.isConstant && a.type != identifierExp)
8737                            {
8738                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
8739                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
8740                               tempExp1 = QMkExpId(temp1);
8741                               tempExp1.expType = a.expType;
8742                               if(a.expType)
8743                                  a.expType.refCount++;
8744                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
8745                            }
8746                            if(b && !b.isConstant && b.type != identifierExp)
8747                            {
8748                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
8749                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
8750                               tempExp2 = QMkExpId(temp2);
8751                               tempExp2.expType = b.expType;
8752                               if(b.expType)
8753                                  b.expType.refCount++;
8754                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
8755                            }
8756
8757                            decl = MkDeclaration(specs, decls);
8758                            if(!curCompound.compound.declarations)
8759                               curCompound.compound.declarations = MkList();
8760                            curCompound.compound.declarations->Insert(null, decl);
8761                         }
8762                      }
8763                   }
8764
8765                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
8766                   {
8767                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
8768                      ListAdd(exp.list,
8769                         MkExpCondition(MkExpBrackets(MkListOne(
8770                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
8771                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
8772                      exp.expType = a.expType;
8773                      if(a.expType)
8774                         a.expType.refCount++;
8775                   }
8776                   else if(!strcmp(id.string, "Abs"))
8777                   {
8778                      ListAdd(exp.list,
8779                         MkExpCondition(MkExpBrackets(MkListOne(
8780                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8781                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
8782                      exp.expType = a.expType;
8783                      if(a.expType)
8784                         a.expType.refCount++;
8785                   }
8786                   else if(!strcmp(id.string, "Sgn"))
8787                   {
8788                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
8789                      ListAdd(exp.list,
8790                         MkExpCondition(MkExpBrackets(MkListOne(
8791                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
8792                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
8793                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8794                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
8795                      exp.expType = ProcessTypeString("int", false);
8796                   }
8797
8798                   FreeExpression(tempExp1);
8799                   if(tempExp2) FreeExpression(tempExp2);
8800
8801                   FreeIdentifier(id);
8802                   break;
8803                }
8804             }
8805          }
8806
8807          {
8808             Type dummy
8809             {
8810                count = 1;
8811                refCount = 1;
8812             };
8813             if(!exp.call.exp.destType)
8814             {
8815                exp.call.exp.destType = dummy;
8816                dummy.refCount++;
8817             }
8818             ProcessExpressionType(exp.call.exp);
8819             if(exp.call.exp.destType == dummy)
8820             {
8821                FreeType(dummy);
8822                exp.call.exp.destType = null;
8823             }
8824             FreeType(dummy);
8825          }
8826
8827          // Check argument types against parameter types
8828          functionType = exp.call.exp.expType;
8829
8830          if(functionType && functionType.kind == TypeKind::methodType)
8831          {
8832             methodType = functionType;
8833             functionType = methodType.method.dataType;
8834
8835             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
8836             // TOCHECK: Instead of doing this here could this be done per param?
8837             if(exp.call.exp.expType.usedClass)
8838             {
8839                char typeString[1024];
8840                typeString[0] = '\0';
8841                {
8842                   Symbol back = functionType.thisClass;
8843                   // Do not output class specifier here (thisclass was added to this)
8844                   functionType.thisClass = null;
8845                   PrintType(functionType, typeString, true, true);
8846                   functionType.thisClass = back;
8847                }
8848                if(strstr(typeString, "thisclass"))
8849                {
8850                   OldList * specs = MkList();
8851                   Declarator decl;
8852                   {
8853                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
8854
8855                      decl = SpecDeclFromString(typeString, specs, null);
8856
8857                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
8858                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
8859                         exp.call.exp.expType.usedClass))
8860                         thisClassParams = false;
8861
8862                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
8863                      {
8864                         Class backupThisClass = thisClass;
8865                         thisClass = exp.call.exp.expType.usedClass;
8866                         ProcessDeclarator(decl);
8867                         thisClass = backupThisClass;
8868                      }
8869
8870                      thisClassParams = true;
8871
8872                      functionType = ProcessType(specs, decl);
8873                      functionType.refCount = 0;
8874                      FinishTemplatesContext(context);
8875                   }
8876
8877                   FreeList(specs, FreeSpecifier);
8878                   FreeDeclarator(decl);
8879                 }
8880             }
8881          }
8882          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
8883          {
8884             Type type = functionType.type;
8885             if(!functionType.refCount)
8886             {
8887                functionType.type = null;
8888                FreeType(functionType);
8889             }
8890             //methodType = functionType;
8891             functionType = type;
8892          }
8893          if(functionType && functionType.kind != TypeKind::functionType)
8894          {
8895             Compiler_Error($"called object %s is not a function\n", name);
8896          }
8897          else if(functionType)
8898          {
8899             bool emptyParams = false, noParams = false;
8900             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
8901             Type type = functionType.params.first;
8902             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
8903             int extra = 0;
8904             Location oldyylloc = yylloc;
8905
8906             if(!type) emptyParams = true;
8907
8908             // WORKING ON THIS:
8909             if(functionType.extraParam && e && functionType.thisClass)
8910             {
8911                e.destType = MkClassType(functionType.thisClass.string);
8912                e = e.next;
8913             }
8914
8915             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
8916             // Fixed #141 by adding '&& !functionType.extraParam'
8917             if(!functionType.staticMethod && !functionType.extraParam)
8918             {
8919                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
8920                   memberExp.member.exp.expType._class)
8921                {
8922                   type = MkClassType(memberExp.member.exp.expType._class.string);
8923                   if(e)
8924                   {
8925                      e.destType = type;
8926                      e = e.next;
8927                      type = functionType.params.first;
8928                   }
8929                   else
8930                      type.refCount = 0;
8931                }
8932                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
8933                {
8934                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
8935                   type.byReference = functionType.byReference;
8936                   type.typedByReference = functionType.typedByReference;
8937                   if(e)
8938                   {
8939                      // Allow manually passing a class for typed object
8940                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
8941                         e = e.next;
8942                      e.destType = type;
8943                      e = e.next;
8944                      type = functionType.params.first;
8945                   }
8946                   else
8947                      type.refCount = 0;
8948                   //extra = 1;
8949                }
8950             }
8951
8952             if(type && type.kind == voidType)
8953             {
8954                noParams = true;
8955                if(!type.refCount) FreeType(type);
8956                type = null;
8957             }
8958
8959             for( ; e; e = e.next)
8960             {
8961                if(!type && !emptyParams)
8962                {
8963                   yylloc = e.loc;
8964                   if(methodType && methodType.methodClass)
8965                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
8966                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
8967                         noParams ? 0 : functionType.params.count);
8968                   else
8969                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
8970                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
8971                         noParams ? 0 : functionType.params.count);
8972                   break;
8973                }
8974
8975                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
8976                {
8977                   Type templatedType = null;
8978                   Class _class = methodType.usedClass;
8979                   ClassTemplateParameter curParam = null;
8980                   int id = 0;
8981                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
8982                   {
8983                      Class sClass;
8984                      for(sClass = _class; sClass; sClass = sClass.base)
8985                      {
8986                         if(sClass.templateClass) sClass = sClass.templateClass;
8987                         id = 0;
8988                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
8989                         {
8990                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
8991                            {
8992                               Class nextClass;
8993                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
8994                               {
8995                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
8996                                  id += nextClass.templateParams.count;
8997                               }
8998                               break;
8999                            }
9000                            id++;
9001                         }
9002                         if(curParam) break;
9003                      }
9004                   }
9005                   if(curParam && _class.templateArgs[id].dataTypeString)
9006                   {
9007                      ClassTemplateArgument arg = _class.templateArgs[id];
9008                      {
9009                         Context context = SetupTemplatesContext(_class);
9010
9011                         /*if(!arg.dataType)
9012                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9013                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9014                         FinishTemplatesContext(context);
9015                      }
9016                      e.destType = templatedType;
9017                      if(templatedType)
9018                      {
9019                         templatedType.passAsTemplate = true;
9020                         // templatedType.refCount++;
9021                      }
9022                   }
9023                   else
9024                   {
9025                      e.destType = type;
9026                      if(type) type.refCount++;
9027                   }
9028                }
9029                else
9030                {
9031                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9032                   {
9033                      e.destType = type.prev;
9034                      e.destType.refCount++;
9035                   }
9036                   else
9037                   {
9038                      e.destType = type;
9039                      if(type) type.refCount++;
9040                   }
9041                }
9042                // Don't reach the end for the ellipsis
9043                if(type && type.kind != ellipsisType)
9044                {
9045                   Type next = type.next;
9046                   if(!type.refCount) FreeType(type);
9047                   type = next;
9048                }
9049             }
9050
9051             if(type && type.kind != ellipsisType)
9052             {
9053                if(methodType && methodType.methodClass)
9054                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9055                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9056                      functionType.params.count + extra);
9057                else
9058                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9059                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9060                      functionType.params.count + extra);
9061             }
9062             yylloc = oldyylloc;
9063             if(type && !type.refCount) FreeType(type);
9064          }
9065          else
9066          {
9067             functionType = Type
9068             {
9069                refCount = 0;
9070                kind = TypeKind::functionType;
9071             };
9072
9073             if(exp.call.exp.type == identifierExp)
9074             {
9075                char * string = exp.call.exp.identifier.string;
9076                if(inCompiler)
9077                {
9078                   Symbol symbol;
9079                   Location oldyylloc = yylloc;
9080
9081                   yylloc = exp.call.exp.identifier.loc;
9082                   if(strstr(string, "__builtin_") == string)
9083                   {
9084                      if(exp.destType)
9085                      {
9086                         functionType.returnType = exp.destType;
9087                         exp.destType.refCount++;
9088                      }
9089                   }
9090                   else
9091                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9092                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9093                   globalContext.symbols.Add((BTNode)symbol);
9094                   if(strstr(symbol.string, "::"))
9095                      globalContext.hasNameSpace = true;
9096
9097                   yylloc = oldyylloc;
9098                }
9099             }
9100             else if(exp.call.exp.type == memberExp)
9101             {
9102                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9103                   exp.call.exp.member.member.string);*/
9104             }
9105             else
9106                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9107
9108             if(!functionType.returnType)
9109             {
9110                functionType.returnType = Type
9111                {
9112                   refCount = 1;
9113                   kind = intType;
9114                };
9115             }
9116          }
9117          if(functionType && functionType.kind == TypeKind::functionType)
9118          {
9119             exp.expType = functionType.returnType;
9120
9121             if(functionType.returnType)
9122                functionType.returnType.refCount++;
9123
9124             if(!functionType.refCount)
9125                FreeType(functionType);
9126          }
9127
9128          if(exp.call.arguments)
9129          {
9130             for(e = exp.call.arguments->first; e; e = e.next)
9131             {
9132                Type destType = e.destType;
9133                ProcessExpressionType(e);
9134             }
9135          }
9136          break;
9137       }
9138       case memberExp:
9139       {
9140          Type type;
9141          Location oldyylloc = yylloc;
9142          bool thisPtr;
9143          Expression checkExp = exp.member.exp;
9144          while(checkExp)
9145          {
9146             if(checkExp.type == castExp)
9147                checkExp = checkExp.cast.exp;
9148             else if(checkExp.type == bracketsExp)
9149                checkExp = checkExp.list ? checkExp.list->first : null;
9150             else
9151                break;
9152          }
9153
9154          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9155          exp.thisPtr = thisPtr;
9156
9157          // DOING THIS LATER NOW...
9158          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9159          {
9160             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9161             /* TODO: Name Space Fix ups
9162             if(!exp.member.member.classSym)
9163                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9164             */
9165          }
9166
9167          ProcessExpressionType(exp.member.exp);
9168          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9169             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9170          {
9171             exp.isConstant = false;
9172          }
9173          else
9174             exp.isConstant = exp.member.exp.isConstant;
9175          type = exp.member.exp.expType;
9176
9177          yylloc = exp.loc;
9178
9179          if(type && (type.kind == templateType))
9180          {
9181             Class _class = thisClass ? thisClass : currentClass;
9182             ClassTemplateParameter param = null;
9183             if(_class)
9184             {
9185                for(param = _class.templateParams.first; param; param = param.next)
9186                {
9187                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9188                      break;
9189                }
9190             }
9191             if(param && param.defaultArg.member)
9192             {
9193                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9194                if(argExp)
9195                {
9196                   Expression expMember = exp.member.exp;
9197                   Declarator decl;
9198                   OldList * specs = MkList();
9199                   char thisClassTypeString[1024];
9200
9201                   FreeIdentifier(exp.member.member);
9202
9203                   ProcessExpressionType(argExp);
9204
9205                   {
9206                      char * colon = strstr(param.defaultArg.memberString, "::");
9207                      if(colon)
9208                      {
9209                         char className[1024];
9210                         Class sClass;
9211
9212                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9213                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9214                      }
9215                      else
9216                         strcpy(thisClassTypeString, _class.fullName);
9217                   }
9218
9219                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9220
9221                   exp.expType = ProcessType(specs, decl);
9222                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9223                   {
9224                      Class expClass = exp.expType._class.registered;
9225                      Class cClass = null;
9226                      int c;
9227                      int paramCount = 0;
9228                      int lastParam = -1;
9229
9230                      char templateString[1024];
9231                      ClassTemplateParameter param;
9232                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9233                      for(cClass = expClass; cClass; cClass = cClass.base)
9234                      {
9235                         int p = 0;
9236                         for(param = cClass.templateParams.first; param; param = param.next)
9237                         {
9238                            int id = p;
9239                            Class sClass;
9240                            ClassTemplateArgument arg;
9241                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9242                            arg = expClass.templateArgs[id];
9243
9244                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9245                            {
9246                               ClassTemplateParameter cParam;
9247                               //int p = numParams - sClass.templateParams.count;
9248                               int p = 0;
9249                               Class nextClass;
9250                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9251
9252                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9253                               {
9254                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9255                                  {
9256                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9257                                     {
9258                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9259                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9260                                        break;
9261                                     }
9262                                  }
9263                               }
9264                            }
9265
9266                            {
9267                               char argument[256];
9268                               argument[0] = '\0';
9269                               /*if(arg.name)
9270                               {
9271                                  strcat(argument, arg.name.string);
9272                                  strcat(argument, " = ");
9273                               }*/
9274                               switch(param.type)
9275                               {
9276                                  case expression:
9277                                  {
9278                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9279                                     char expString[1024];
9280                                     OldList * specs = MkList();
9281                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9282                                     Expression exp;
9283                                     char * string = PrintHexUInt64(arg.expression.ui64);
9284                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9285                                     delete string;
9286
9287                                     ProcessExpressionType(exp);
9288                                     ComputeExpression(exp);
9289                                     expString[0] = '\0';
9290                                     PrintExpression(exp, expString);
9291                                     strcat(argument, expString);
9292                                     // delete exp;
9293                                     FreeExpression(exp);
9294                                     break;
9295                                  }
9296                                  case identifier:
9297                                  {
9298                                     strcat(argument, arg.member.name);
9299                                     break;
9300                                  }
9301                                  case TemplateParameterType::type:
9302                                  {
9303                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9304                                     {
9305                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9306                                           strcat(argument, thisClassTypeString);
9307                                        else
9308                                           strcat(argument, arg.dataTypeString);
9309                                     }
9310                                     break;
9311                                  }
9312                               }
9313                               if(argument[0])
9314                               {
9315                                  if(paramCount) strcat(templateString, ", ");
9316                                  if(lastParam != p - 1)
9317                                  {
9318                                     strcat(templateString, param.name);
9319                                     strcat(templateString, " = ");
9320                                  }
9321                                  strcat(templateString, argument);
9322                                  paramCount++;
9323                                  lastParam = p;
9324                               }
9325                               p++;
9326                            }
9327                         }
9328                      }
9329                      {
9330                         int len = strlen(templateString);
9331                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9332                         templateString[len++] = '>';
9333                         templateString[len++] = '\0';
9334                      }
9335                      {
9336                         Context context = SetupTemplatesContext(_class);
9337                         FreeType(exp.expType);
9338                         exp.expType = ProcessTypeString(templateString, false);
9339                         FinishTemplatesContext(context);
9340                      }
9341                   }
9342
9343                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9344                   exp.type = bracketsExp;
9345                   exp.list = MkListOne(MkExpOp(null, '*',
9346                   /*opExp;
9347                   exp.op.op = '*';
9348                   exp.op.exp1 = null;
9349                   exp.op.exp2 = */
9350                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9351                      MkExpBrackets(MkListOne(
9352                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9353                            '+',
9354                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9355                            '+',
9356                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9357
9358                            ));
9359                }
9360             }
9361             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9362                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9363             {
9364                type = ProcessTemplateParameterType(type.templateParameter);
9365             }
9366          }
9367          // TODO: *** This seems to be where we should add method support for all basic types ***
9368          if(type && (type.kind == templateType));
9369          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9370                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9371                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9372                           (type.kind == pointerType && type.type.kind == charType)))
9373          {
9374             Identifier id = exp.member.member;
9375             TypeKind typeKind = type.kind;
9376             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9377             if(typeKind == subClassType && exp.member.exp.type == classExp)
9378             {
9379                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9380                typeKind = classType;
9381             }
9382
9383             if(id)
9384             {
9385                if(typeKind == intType || typeKind == enumType)
9386                   _class = eSystem_FindClass(privateModule, "int");
9387                else if(!_class)
9388                {
9389                   if(type.kind == classType && type._class && type._class.registered)
9390                   {
9391                      _class = type._class.registered;
9392                   }
9393                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9394                   {
9395                      _class = FindClass("char *").registered;
9396                   }
9397                   else if(type.kind == pointerType)
9398                   {
9399                      _class = eSystem_FindClass(privateModule, "uintptr");
9400                      FreeType(exp.expType);
9401                      exp.expType = ProcessTypeString("uintptr", false);
9402                      exp.byReference = true;
9403                   }
9404                   else
9405                   {
9406                      char string[1024] = "";
9407                      Symbol classSym;
9408                      PrintTypeNoConst(type, string, false, true);
9409                      classSym = FindClass(string);
9410                      if(classSym) _class = classSym.registered;
9411                   }
9412                }
9413             }
9414
9415             if(_class && id)
9416             {
9417                /*bool thisPtr =
9418                   (exp.member.exp.type == identifierExp &&
9419                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9420                Property prop = null;
9421                Method method = null;
9422                DataMember member = null;
9423                Property revConvert = null;
9424                ClassProperty classProp = null;
9425
9426                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9427                   exp.member.memberType = propertyMember;
9428
9429                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9430                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9431
9432                if(typeKind != subClassType)
9433                {
9434                   // Prioritize data members over properties for "this"
9435                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9436                   {
9437                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9438                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9439                      {
9440                         prop = eClass_FindProperty(_class, id.string, privateModule);
9441                         if(prop)
9442                            member = null;
9443                      }
9444                      if(!member && !prop)
9445                         prop = eClass_FindProperty(_class, id.string, privateModule);
9446                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9447                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9448                         exp.member.thisPtr = true;
9449                   }
9450                   // Prioritize properties over data members otherwise
9451                   else
9452                   {
9453                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9454                      if(!id.classSym)
9455                      {
9456                         prop = eClass_FindProperty(_class, id.string, null);
9457                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9458                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9459                      }
9460
9461                      if(!prop && !member)
9462                      {
9463                         method = eClass_FindMethod(_class, id.string, null);
9464                         if(!method)
9465                         {
9466                            prop = eClass_FindProperty(_class, id.string, privateModule);
9467                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9468                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9469                         }
9470                      }
9471
9472                      if(member && prop)
9473                      {
9474                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9475                            prop = null;
9476                         else
9477                            member = null;
9478                      }
9479                   }
9480                }
9481                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9482                   method = eClass_FindMethod(_class, id.string, privateModule);
9483                if(!prop && !member && !method)
9484                {
9485                   if(typeKind == subClassType)
9486                   {
9487                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9488                      if(classProp)
9489                      {
9490                         exp.member.memberType = classPropertyMember;
9491                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9492                      }
9493                      else
9494                      {
9495                         // Assume this is a class_data member
9496                         char structName[1024];
9497                         Identifier id = exp.member.member;
9498                         Expression classExp = exp.member.exp;
9499                         type.refCount++;
9500
9501                         FreeType(classExp.expType);
9502                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9503
9504                         strcpy(structName, "__ecereClassData_");
9505                         FullClassNameCat(structName, type._class.string, false);
9506                         exp.type = pointerExp;
9507                         exp.member.member = id;
9508
9509                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9510                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9511                               MkExpBrackets(MkListOne(MkExpOp(
9512                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9513                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9514                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9515                                  )));
9516
9517                         FreeType(type);
9518
9519                         ProcessExpressionType(exp);
9520                         return;
9521                      }
9522                   }
9523                   else
9524                   {
9525                      // Check for reverse conversion
9526                      // (Convert in an instantiation later, so that we can use
9527                      //  deep properties system)
9528                      Symbol classSym = FindClass(id.string);
9529                      if(classSym)
9530                      {
9531                         Class convertClass = classSym.registered;
9532                         if(convertClass)
9533                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9534                      }
9535                   }
9536                }
9537
9538                if(prop)
9539                {
9540                   exp.member.memberType = propertyMember;
9541                   if(!prop.dataType)
9542                      ProcessPropertyType(prop);
9543                   exp.expType = prop.dataType;
9544                   if(prop.dataType) prop.dataType.refCount++;
9545                }
9546                else if(member)
9547                {
9548                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9549                   {
9550                      FreeExpContents(exp);
9551                      exp.type = identifierExp;
9552                      exp.identifier = MkIdentifier("class");
9553                      ProcessExpressionType(exp);
9554                      return;
9555                   }
9556
9557                   exp.member.memberType = dataMember;
9558                   DeclareStruct(_class.fullName, false);
9559                   if(!member.dataType)
9560                   {
9561                      Context context = SetupTemplatesContext(_class);
9562                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9563                      FinishTemplatesContext(context);
9564                   }
9565                   exp.expType = member.dataType;
9566                   if(member.dataType) member.dataType.refCount++;
9567                }
9568                else if(revConvert)
9569                {
9570                   exp.member.memberType = reverseConversionMember;
9571                   exp.expType = MkClassType(revConvert._class.fullName);
9572                }
9573                else if(method)
9574                {
9575                   //if(inCompiler)
9576                   {
9577                      /*if(id._class)
9578                      {
9579                         exp.type = identifierExp;
9580                         exp.identifier = exp.member.member;
9581                      }
9582                      else*/
9583                         exp.member.memberType = methodMember;
9584                   }
9585                   if(!method.dataType)
9586                      ProcessMethodType(method);
9587                   exp.expType = Type
9588                   {
9589                      refCount = 1;
9590                      kind = methodType;
9591                      method = method;
9592                   };
9593
9594                   // Tricky spot here... To use instance versus class virtual table
9595                   // Put it back to what it was... What did we break?
9596
9597                   // Had to put it back for overriding Main of Thread global instance
9598
9599                   //exp.expType.methodClass = _class;
9600                   exp.expType.methodClass = (id && id._class) ? _class : null;
9601
9602                   // Need the actual class used for templated classes
9603                   exp.expType.usedClass = _class;
9604                }
9605                else if(!classProp)
9606                {
9607                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9608                   {
9609                      FreeExpContents(exp);
9610                      exp.type = identifierExp;
9611                      exp.identifier = MkIdentifier("class");
9612                      ProcessExpressionType(exp);
9613                      return;
9614                   }
9615                   yylloc = exp.member.member.loc;
9616                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9617                   if(inCompiler)
9618                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9619                }
9620
9621                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9622                {
9623                   Class tClass;
9624
9625                   tClass = _class;
9626                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9627
9628                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9629                   {
9630                      int id = 0;
9631                      ClassTemplateParameter curParam = null;
9632                      Class sClass;
9633
9634                      for(sClass = tClass; sClass; sClass = sClass.base)
9635                      {
9636                         id = 0;
9637                         if(sClass.templateClass) sClass = sClass.templateClass;
9638                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9639                         {
9640                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9641                            {
9642                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9643                                  id += sClass.templateParams.count;
9644                               break;
9645                            }
9646                            id++;
9647                         }
9648                         if(curParam) break;
9649                      }
9650
9651                      if(curParam && tClass.templateArgs[id].dataTypeString)
9652                      {
9653                         ClassTemplateArgument arg = tClass.templateArgs[id];
9654                         Context context = SetupTemplatesContext(tClass);
9655                         /*if(!arg.dataType)
9656                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9657                         FreeType(exp.expType);
9658                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9659                         if(exp.expType)
9660                         {
9661                            if(exp.expType.kind == thisClassType)
9662                            {
9663                               FreeType(exp.expType);
9664                               exp.expType = ReplaceThisClassType(_class);
9665                            }
9666
9667                            if(tClass.templateClass)
9668                               exp.expType.passAsTemplate = true;
9669                            //exp.expType.refCount++;
9670                            if(!exp.destType)
9671                            {
9672                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9673                               //exp.destType.refCount++;
9674
9675                               if(exp.destType.kind == thisClassType)
9676                               {
9677                                  FreeType(exp.destType);
9678                                  exp.destType = ReplaceThisClassType(_class);
9679                               }
9680                            }
9681                         }
9682                         FinishTemplatesContext(context);
9683                      }
9684                   }
9685                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9686                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9687                   {
9688                      int id = 0;
9689                      ClassTemplateParameter curParam = null;
9690                      Class sClass;
9691
9692                      for(sClass = tClass; sClass; sClass = sClass.base)
9693                      {
9694                         id = 0;
9695                         if(sClass.templateClass) sClass = sClass.templateClass;
9696                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9697                         {
9698                            if(curParam.type == TemplateParameterType::type &&
9699                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9700                            {
9701                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9702                                  id += sClass.templateParams.count;
9703                               break;
9704                            }
9705                            id++;
9706                         }
9707                         if(curParam) break;
9708                      }
9709
9710                      if(curParam)
9711                      {
9712                         ClassTemplateArgument arg = tClass.templateArgs[id];
9713                         Context context = SetupTemplatesContext(tClass);
9714                         Type basicType;
9715                         /*if(!arg.dataType)
9716                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9717
9718                         basicType = ProcessTypeString(arg.dataTypeString, false);
9719                         if(basicType)
9720                         {
9721                            if(basicType.kind == thisClassType)
9722                            {
9723                               FreeType(basicType);
9724                               basicType = ReplaceThisClassType(_class);
9725                            }
9726
9727                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
9728                            if(tClass.templateClass)
9729                               basicType.passAsTemplate = true;
9730                            */
9731
9732                            FreeType(exp.expType);
9733
9734                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
9735                            //exp.expType.refCount++;
9736                            if(!exp.destType)
9737                            {
9738                               exp.destType = exp.expType;
9739                               exp.destType.refCount++;
9740                            }
9741
9742                            {
9743                               Expression newExp { };
9744                               OldList * specs = MkList();
9745                               Declarator decl;
9746                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
9747                               *newExp = *exp;
9748                               if(exp.destType) exp.destType.refCount++;
9749                               if(exp.expType)  exp.expType.refCount++;
9750                               exp.type = castExp;
9751                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
9752                               exp.cast.exp = newExp;
9753                               //FreeType(exp.expType);
9754                               //exp.expType = null;
9755                               //ProcessExpressionType(sourceExp);
9756                            }
9757                         }
9758                         FinishTemplatesContext(context);
9759                      }
9760                   }
9761                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
9762                   {
9763                      Class expClass = exp.expType._class.registered;
9764                      if(expClass)
9765                      {
9766                         Class cClass = null;
9767                         int c;
9768                         int p = 0;
9769                         int paramCount = 0;
9770                         int lastParam = -1;
9771                         char templateString[1024];
9772                         ClassTemplateParameter param;
9773                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
9774                         while(cClass != expClass)
9775                         {
9776                            Class sClass;
9777                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
9778                            cClass = sClass;
9779
9780                            for(param = cClass.templateParams.first; param; param = param.next)
9781                            {
9782                               Class cClassCur = null;
9783                               int c;
9784                               int cp = 0;
9785                               ClassTemplateParameter paramCur = null;
9786                               ClassTemplateArgument arg;
9787                               while(cClassCur != tClass && !paramCur)
9788                               {
9789                                  Class sClassCur;
9790                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
9791                                  cClassCur = sClassCur;
9792
9793                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
9794                                  {
9795                                     if(!strcmp(paramCur.name, param.name))
9796                                     {
9797
9798                                        break;
9799                                     }
9800                                     cp++;
9801                                  }
9802                               }
9803                               if(paramCur && paramCur.type == TemplateParameterType::type)
9804                                  arg = tClass.templateArgs[cp];
9805                               else
9806                                  arg = expClass.templateArgs[p];
9807
9808                               {
9809                                  char argument[256];
9810                                  argument[0] = '\0';
9811                                  /*if(arg.name)
9812                                  {
9813                                     strcat(argument, arg.name.string);
9814                                     strcat(argument, " = ");
9815                                  }*/
9816                                  switch(param.type)
9817                                  {
9818                                     case expression:
9819                                     {
9820                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9821                                        char expString[1024];
9822                                        OldList * specs = MkList();
9823                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9824                                        Expression exp;
9825                                        char * string = PrintHexUInt64(arg.expression.ui64);
9826                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9827                                        delete string;
9828
9829                                        ProcessExpressionType(exp);
9830                                        ComputeExpression(exp);
9831                                        expString[0] = '\0';
9832                                        PrintExpression(exp, expString);
9833                                        strcat(argument, expString);
9834                                        // delete exp;
9835                                        FreeExpression(exp);
9836                                        break;
9837                                     }
9838                                     case identifier:
9839                                     {
9840                                        strcat(argument, arg.member.name);
9841                                        break;
9842                                     }
9843                                     case TemplateParameterType::type:
9844                                     {
9845                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9846                                           strcat(argument, arg.dataTypeString);
9847                                        break;
9848                                     }
9849                                  }
9850                                  if(argument[0])
9851                                  {
9852                                     if(paramCount) strcat(templateString, ", ");
9853                                     if(lastParam != p - 1)
9854                                     {
9855                                        strcat(templateString, param.name);
9856                                        strcat(templateString, " = ");
9857                                     }
9858                                     strcat(templateString, argument);
9859                                     paramCount++;
9860                                     lastParam = p;
9861                                  }
9862                               }
9863                               p++;
9864                            }
9865                         }
9866                         {
9867                            int len = strlen(templateString);
9868                            if(templateString[len-1] == '>') templateString[len++] = ' ';
9869                            templateString[len++] = '>';
9870                            templateString[len++] = '\0';
9871                         }
9872
9873                         FreeType(exp.expType);
9874                         {
9875                            Context context = SetupTemplatesContext(tClass);
9876                            exp.expType = ProcessTypeString(templateString, false);
9877                            FinishTemplatesContext(context);
9878                         }
9879                      }
9880                   }
9881                }
9882             }
9883             else
9884                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
9885          }
9886          else if(type && (type.kind == structType || type.kind == unionType))
9887          {
9888             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
9889             if(memberType)
9890             {
9891                exp.expType = memberType;
9892                if(memberType)
9893                   memberType.refCount++;
9894             }
9895          }
9896          else
9897          {
9898             char expString[10240];
9899             expString[0] = '\0';
9900             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
9901             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
9902          }
9903
9904          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
9905          {
9906             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
9907             {
9908                Identifier id = exp.member.member;
9909                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9910                if(_class)
9911                {
9912                   FreeType(exp.expType);
9913                   exp.expType = ReplaceThisClassType(_class);
9914                }
9915             }
9916          }
9917          yylloc = oldyylloc;
9918          break;
9919       }
9920       // Convert x->y into (*x).y
9921       case pointerExp:
9922       {
9923          Type destType = exp.destType;
9924
9925          // DOING THIS LATER NOW...
9926          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9927          {
9928             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9929             /* TODO: Name Space Fix ups
9930             if(!exp.member.member.classSym)
9931                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
9932             */
9933          }
9934
9935          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
9936          exp.type = memberExp;
9937          if(destType)
9938             destType.count++;
9939          ProcessExpressionType(exp);
9940          if(destType)
9941             destType.count--;
9942          break;
9943       }
9944       case classSizeExp:
9945       {
9946          //ComputeExpression(exp);
9947
9948          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
9949          if(classSym && classSym.registered)
9950          {
9951             if(classSym.registered.type == noHeadClass)
9952             {
9953                char name[1024];
9954                name[0] = '\0';
9955                DeclareStruct(classSym.string, false);
9956                FreeSpecifier(exp._class);
9957                exp.type = typeSizeExp;
9958                FullClassNameCat(name, classSym.string, false);
9959                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
9960             }
9961             else
9962             {
9963                if(classSym.registered.fixed)
9964                {
9965                   FreeSpecifier(exp._class);
9966                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
9967                   exp.type = constantExp;
9968                }
9969                else
9970                {
9971                   char className[1024];
9972                   strcpy(className, "__ecereClass_");
9973                   FullClassNameCat(className, classSym.string, true);
9974                   MangleClassName(className);
9975
9976                   DeclareClass(classSym, className);
9977
9978                   FreeExpContents(exp);
9979                   exp.type = pointerExp;
9980                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
9981                   exp.member.member = MkIdentifier("structSize");
9982                }
9983             }
9984          }
9985
9986          exp.expType = Type
9987          {
9988             refCount = 1;
9989             kind = intType;
9990          };
9991          // exp.isConstant = true;
9992          break;
9993       }
9994       case typeSizeExp:
9995       {
9996          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
9997
9998          exp.expType = Type
9999          {
10000             refCount = 1;
10001             kind = intType;
10002          };
10003          exp.isConstant = true;
10004
10005          DeclareType(type, false, false);
10006          FreeType(type);
10007          break;
10008       }
10009       case castExp:
10010       {
10011          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10012          type.count = 1;
10013          FreeType(exp.cast.exp.destType);
10014          exp.cast.exp.destType = type;
10015          type.refCount++;
10016          ProcessExpressionType(exp.cast.exp);
10017          type.count = 0;
10018          exp.expType = type;
10019          //type.refCount++;
10020
10021          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10022          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10023          {
10024             void * prev = exp.prev, * next = exp.next;
10025             Type expType = exp.cast.exp.destType;
10026             Expression castExp = exp.cast.exp;
10027             Type destType = exp.destType;
10028
10029             if(expType) expType.refCount++;
10030
10031             //FreeType(exp.destType);
10032             FreeType(exp.expType);
10033             FreeTypeName(exp.cast.typeName);
10034
10035             *exp = *castExp;
10036             FreeType(exp.expType);
10037             FreeType(exp.destType);
10038
10039             exp.expType = expType;
10040             exp.destType = destType;
10041
10042             delete castExp;
10043
10044             exp.prev = prev;
10045             exp.next = next;
10046
10047          }
10048          else
10049          {
10050             exp.isConstant = exp.cast.exp.isConstant;
10051          }
10052          //FreeType(type);
10053          break;
10054       }
10055       case extensionInitializerExp:
10056       {
10057          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10058          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10059          // ProcessInitializer(exp.initializer.initializer, type);
10060          exp.expType = type;
10061          break;
10062       }
10063       case vaArgExp:
10064       {
10065          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10066          ProcessExpressionType(exp.vaArg.exp);
10067          exp.expType = type;
10068          break;
10069       }
10070       case conditionExp:
10071       {
10072          Expression e;
10073          exp.isConstant = true;
10074
10075          FreeType(exp.cond.cond.destType);
10076          exp.cond.cond.destType = MkClassType("bool");
10077          exp.cond.cond.destType.truth = true;
10078          ProcessExpressionType(exp.cond.cond);
10079          if(!exp.cond.cond.isConstant)
10080             exp.isConstant = false;
10081          for(e = exp.cond.exp->first; e; e = e.next)
10082          {
10083             if(!e.next)
10084             {
10085                FreeType(e.destType);
10086                e.destType = exp.destType;
10087                if(e.destType) e.destType.refCount++;
10088             }
10089             ProcessExpressionType(e);
10090             if(!e.next)
10091             {
10092                exp.expType = e.expType;
10093                if(e.expType) e.expType.refCount++;
10094             }
10095             if(!e.isConstant)
10096                exp.isConstant = false;
10097          }
10098
10099          FreeType(exp.cond.elseExp.destType);
10100          // Added this check if we failed to find an expType
10101          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10102
10103          // Reversed it...
10104          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10105
10106          if(exp.cond.elseExp.destType)
10107             exp.cond.elseExp.destType.refCount++;
10108          ProcessExpressionType(exp.cond.elseExp);
10109
10110          // FIXED THIS: Was done before calling process on elseExp
10111          if(!exp.cond.elseExp.isConstant)
10112             exp.isConstant = false;
10113          break;
10114       }
10115       case extensionCompoundExp:
10116       {
10117          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10118          {
10119             Statement last = exp.compound.compound.statements->last;
10120             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10121             {
10122                ((Expression)last.expressions->last).destType = exp.destType;
10123                if(exp.destType)
10124                   exp.destType.refCount++;
10125             }
10126             ProcessStatement(exp.compound);
10127             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10128             if(exp.expType)
10129                exp.expType.refCount++;
10130          }
10131          break;
10132       }
10133       case classExp:
10134       {
10135          Specifier spec = exp._classExp.specifiers->first;
10136          if(spec && spec.type == nameSpecifier)
10137          {
10138             exp.expType = MkClassType(spec.name);
10139             exp.expType.kind = subClassType;
10140             exp.byReference = true;
10141          }
10142          else
10143          {
10144             exp.expType = MkClassType("ecere::com::Class");
10145             exp.byReference = true;
10146          }
10147          break;
10148       }
10149       case classDataExp:
10150       {
10151          Class _class = thisClass ? thisClass : currentClass;
10152          if(_class)
10153          {
10154             Identifier id = exp.classData.id;
10155             char structName[1024];
10156             Expression classExp;
10157             strcpy(structName, "__ecereClassData_");
10158             FullClassNameCat(structName, _class.fullName, false);
10159             exp.type = pointerExp;
10160             exp.member.member = id;
10161             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10162                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10163             else
10164                classExp = MkExpIdentifier(MkIdentifier("class"));
10165
10166             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10167                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10168                   MkExpBrackets(MkListOne(MkExpOp(
10169                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10170                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10171                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10172                      )));
10173
10174             ProcessExpressionType(exp);
10175             return;
10176          }
10177          break;
10178       }
10179       case arrayExp:
10180       {
10181          Type type = null;
10182          char * typeString = null;
10183          char typeStringBuf[1024];
10184          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10185             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10186          {
10187             Class templateClass = exp.destType._class.registered;
10188             typeString = templateClass.templateArgs[2].dataTypeString;
10189          }
10190          else if(exp.list)
10191          {
10192             // Guess type from expressions in the array
10193             Expression e;
10194             for(e = exp.list->first; e; e = e.next)
10195             {
10196                ProcessExpressionType(e);
10197                if(e.expType)
10198                {
10199                   if(!type) { type = e.expType; type.refCount++; }
10200                   else
10201                   {
10202                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10203                      if(!MatchTypeExpression(e, type, null, false))
10204                      {
10205                         FreeType(type);
10206                         type = e.expType;
10207                         e.expType = null;
10208
10209                         e = exp.list->first;
10210                         ProcessExpressionType(e);
10211                         if(e.expType)
10212                         {
10213                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10214                            if(!MatchTypeExpression(e, type, null, false))
10215                            {
10216                               FreeType(e.expType);
10217                               e.expType = null;
10218                               FreeType(type);
10219                               type = null;
10220                               break;
10221                            }
10222                         }
10223                      }
10224                   }
10225                   if(e.expType)
10226                   {
10227                      FreeType(e.expType);
10228                      e.expType = null;
10229                   }
10230                }
10231             }
10232             if(type)
10233             {
10234                typeStringBuf[0] = '\0';
10235                PrintTypeNoConst(type, typeStringBuf, false, true);
10236                typeString = typeStringBuf;
10237                FreeType(type);
10238                type = null;
10239             }
10240          }
10241          if(typeString)
10242          {
10243             /*
10244             (Container)& (struct BuiltInContainer)
10245             {
10246                ._vTbl = class(BuiltInContainer)._vTbl,
10247                ._class = class(BuiltInContainer),
10248                .refCount = 0,
10249                .data = (int[]){ 1, 7, 3, 4, 5 },
10250                .count = 5,
10251                .type = class(int),
10252             }
10253             */
10254             char templateString[1024];
10255             OldList * initializers = MkList();
10256             OldList * structInitializers = MkList();
10257             OldList * specs = MkList();
10258             Expression expExt;
10259             Declarator decl = SpecDeclFromString(typeString, specs, null);
10260             sprintf(templateString, "Container<%s>", typeString);
10261
10262             if(exp.list)
10263             {
10264                Expression e;
10265                type = ProcessTypeString(typeString, false);
10266                while(e = exp.list->first)
10267                {
10268                   exp.list->Remove(e);
10269                   e.destType = type;
10270                   type.refCount++;
10271                   ProcessExpressionType(e);
10272                   ListAdd(initializers, MkInitializerAssignment(e));
10273                }
10274                FreeType(type);
10275                delete exp.list;
10276             }
10277
10278             DeclareStruct("ecere::com::BuiltInContainer", false);
10279
10280             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10281                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10282             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10283                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10284             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10285                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10286             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10287                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10288                MkInitializerList(initializers))));
10289                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10290             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10291                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10292             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10293                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10294             exp.expType = ProcessTypeString(templateString, false);
10295             exp.type = bracketsExp;
10296             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10297                MkExpOp(null, '&',
10298                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10299                   MkInitializerList(structInitializers)))));
10300             ProcessExpressionType(expExt);
10301          }
10302          else
10303          {
10304             exp.expType = ProcessTypeString("Container", false);
10305             Compiler_Error($"Couldn't determine type of array elements\n");
10306          }
10307          break;
10308       }
10309    }
10310
10311    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10312    {
10313       FreeType(exp.expType);
10314       exp.expType = ReplaceThisClassType(thisClass);
10315    }
10316
10317    // Resolve structures here
10318    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10319    {
10320       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10321       // TODO: Fix members reference...
10322       if(symbol)
10323       {
10324          if(exp.expType.kind != enumType)
10325          {
10326             Type member;
10327             String enumName = CopyString(exp.expType.enumName);
10328
10329             // Fixed a memory leak on self-referencing C structs typedefs
10330             // by instantiating a new type rather than simply copying members
10331             // into exp.expType
10332             FreeType(exp.expType);
10333             exp.expType = Type { };
10334             exp.expType.kind = symbol.type.kind;
10335             exp.expType.refCount++;
10336             exp.expType.enumName = enumName;
10337
10338             exp.expType.members = symbol.type.members;
10339             for(member = symbol.type.members.first; member; member = member.next)
10340                member.refCount++;
10341          }
10342          else
10343          {
10344             NamedLink member;
10345             for(member = symbol.type.members.first; member; member = member.next)
10346             {
10347                NamedLink value { name = CopyString(member.name) };
10348                exp.expType.members.Add(value);
10349             }
10350          }
10351       }
10352    }
10353
10354    yylloc = exp.loc;
10355    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10356    else if(exp.destType && !exp.destType.keepCast)
10357    {
10358       if(!CheckExpressionType(exp, exp.destType, false))
10359       {
10360          if(!exp.destType.count || unresolved)
10361          {
10362             if(!exp.expType)
10363             {
10364                yylloc = exp.loc;
10365                if(exp.destType.kind != ellipsisType)
10366                {
10367                   char type2[1024];
10368                   type2[0] = '\0';
10369                   if(inCompiler)
10370                   {
10371                      char expString[10240];
10372                      expString[0] = '\0';
10373
10374                      PrintType(exp.destType, type2, false, true);
10375
10376                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10377                      if(unresolved)
10378                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10379                      else if(exp.type != dummyExp)
10380                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10381                   }
10382                }
10383                else
10384                {
10385                   char expString[10240] ;
10386                   expString[0] = '\0';
10387                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10388
10389                   if(unresolved)
10390                      Compiler_Error($"unresolved identifier %s\n", expString);
10391                   else if(exp.type != dummyExp)
10392                      Compiler_Error($"couldn't determine type of %s\n", expString);
10393                }
10394             }
10395             else
10396             {
10397                char type1[1024];
10398                char type2[1024];
10399                type1[0] = '\0';
10400                type2[0] = '\0';
10401                if(inCompiler)
10402                {
10403                   PrintType(exp.expType, type1, false, true);
10404                   PrintType(exp.destType, type2, false, true);
10405                }
10406
10407                //CheckExpressionType(exp, exp.destType, false);
10408
10409                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10410                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10411                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10412                else
10413                {
10414                   char expString[10240];
10415                   expString[0] = '\0';
10416                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10417
10418 #ifdef _DEBUG
10419                   CheckExpressionType(exp, exp.destType, false);
10420 #endif
10421                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10422                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10423                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10424
10425                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10426                   FreeType(exp.expType);
10427                   exp.destType.refCount++;
10428                   exp.expType = exp.destType;
10429                }
10430             }
10431          }
10432       }
10433       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10434       {
10435          Expression newExp { };
10436          char typeString[1024];
10437          OldList * specs = MkList();
10438          Declarator decl;
10439
10440          typeString[0] = '\0';
10441
10442          *newExp = *exp;
10443
10444          if(exp.expType)  exp.expType.refCount++;
10445          if(exp.expType)  exp.expType.refCount++;
10446          exp.type = castExp;
10447          newExp.destType = exp.expType;
10448
10449          PrintType(exp.expType, typeString, false, false);
10450          decl = SpecDeclFromString(typeString, specs, null);
10451
10452          exp.cast.typeName = MkTypeName(specs, decl);
10453          exp.cast.exp = newExp;
10454       }
10455    }
10456    else if(unresolved)
10457    {
10458       if(exp.identifier._class && exp.identifier._class.name)
10459          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10460       else if(exp.identifier.string && exp.identifier.string[0])
10461          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10462    }
10463    else if(!exp.expType && exp.type != dummyExp)
10464    {
10465       char expString[10240];
10466       expString[0] = '\0';
10467       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10468       Compiler_Error($"couldn't determine type of %s\n", expString);
10469    }
10470
10471    // Let's try to support any_object & typed_object here:
10472    if(inCompiler)
10473       ApplyAnyObjectLogic(exp);
10474
10475    // Mark nohead classes as by reference, unless we're casting them to an integral type
10476    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10477       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10478          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10479           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10480    {
10481       exp.byReference = true;
10482    }
10483    yylloc = oldyylloc;
10484 }
10485
10486 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10487 {
10488    // THIS CODE WILL FIND NEXT MEMBER...
10489    if(*curMember)
10490    {
10491       *curMember = (*curMember).next;
10492
10493       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10494       {
10495          *curMember = subMemberStack[--(*subMemberStackPos)];
10496          *curMember = (*curMember).next;
10497       }
10498
10499       // SKIP ALL PROPERTIES HERE...
10500       while((*curMember) && (*curMember).isProperty)
10501          *curMember = (*curMember).next;
10502
10503       if(subMemberStackPos)
10504       {
10505          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10506          {
10507             subMemberStack[(*subMemberStackPos)++] = *curMember;
10508
10509             *curMember = (*curMember).members.first;
10510             while(*curMember && (*curMember).isProperty)
10511                *curMember = (*curMember).next;
10512          }
10513       }
10514    }
10515    while(!*curMember)
10516    {
10517       if(!*curMember)
10518       {
10519          if(subMemberStackPos && *subMemberStackPos)
10520          {
10521             *curMember = subMemberStack[--(*subMemberStackPos)];
10522             *curMember = (*curMember).next;
10523          }
10524          else
10525          {
10526             Class lastCurClass = *curClass;
10527
10528             if(*curClass == _class) break;     // REACHED THE END
10529
10530             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10531             *curMember = (*curClass).membersAndProperties.first;
10532          }
10533
10534          while((*curMember) && (*curMember).isProperty)
10535             *curMember = (*curMember).next;
10536          if(subMemberStackPos)
10537          {
10538             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10539             {
10540                subMemberStack[(*subMemberStackPos)++] = *curMember;
10541
10542                *curMember = (*curMember).members.first;
10543                while(*curMember && (*curMember).isProperty)
10544                   *curMember = (*curMember).next;
10545             }
10546          }
10547       }
10548    }
10549 }
10550
10551
10552 static void ProcessInitializer(Initializer init, Type type)
10553 {
10554    switch(init.type)
10555    {
10556       case expInitializer:
10557          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10558          {
10559             // TESTING THIS FOR SHUTTING = 0 WARNING
10560             if(init.exp && !init.exp.destType)
10561             {
10562                FreeType(init.exp.destType);
10563                init.exp.destType = type;
10564                if(type) type.refCount++;
10565             }
10566             if(init.exp)
10567             {
10568                ProcessExpressionType(init.exp);
10569                init.isConstant = init.exp.isConstant;
10570             }
10571             break;
10572          }
10573          else
10574          {
10575             Expression exp = init.exp;
10576             Instantiation inst = exp.instance;
10577             MembersInit members;
10578
10579             init.type = listInitializer;
10580             init.list = MkList();
10581
10582             if(inst.members)
10583             {
10584                for(members = inst.members->first; members; members = members.next)
10585                {
10586                   if(members.type == dataMembersInit)
10587                   {
10588                      MemberInit member;
10589                      for(member = members.dataMembers->first; member; member = member.next)
10590                      {
10591                         ListAdd(init.list, member.initializer);
10592                         member.initializer = null;
10593                      }
10594                   }
10595                   // Discard all MembersInitMethod
10596                }
10597             }
10598             FreeExpression(exp);
10599          }
10600       case listInitializer:
10601       {
10602          Initializer i;
10603          Type initializerType = null;
10604          Class curClass = null;
10605          DataMember curMember = null;
10606          DataMember subMemberStack[256];
10607          int subMemberStackPos = 0;
10608
10609          if(type && type.kind == arrayType)
10610             initializerType = Dereference(type);
10611          else if(type && (type.kind == structType || type.kind == unionType))
10612             initializerType = type.members.first;
10613
10614          for(i = init.list->first; i; i = i.next)
10615          {
10616             if(type && type.kind == classType && type._class && type._class.registered)
10617             {
10618                // 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)
10619                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10620                // TODO: Generate error on initializing a private data member this way from another module...
10621                if(curMember)
10622                {
10623                   if(!curMember.dataType)
10624                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10625                   initializerType = curMember.dataType;
10626                }
10627             }
10628             ProcessInitializer(i, initializerType);
10629             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10630                initializerType = initializerType.next;
10631             if(!i.isConstant)
10632                init.isConstant = false;
10633          }
10634
10635          if(type && type.kind == arrayType)
10636             FreeType(initializerType);
10637
10638          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10639          {
10640             Compiler_Error($"Assigning list initializer to non list\n");
10641          }
10642          break;
10643       }
10644    }
10645 }
10646
10647 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10648 {
10649    switch(spec.type)
10650    {
10651       case baseSpecifier:
10652       {
10653          if(spec.specifier == THISCLASS)
10654          {
10655             if(thisClass)
10656             {
10657                spec.type = nameSpecifier;
10658                spec.name = ReplaceThisClass(thisClass);
10659                spec.symbol = FindClass(spec.name);
10660                ProcessSpecifier(spec, declareStruct);
10661             }
10662          }
10663          break;
10664       }
10665       case nameSpecifier:
10666       {
10667          Symbol symbol = FindType(curContext, spec.name);
10668          if(symbol)
10669             DeclareType(symbol.type, true, true);
10670          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10671             DeclareStruct(spec.name, false);
10672          break;
10673       }
10674       case enumSpecifier:
10675       {
10676          Enumerator e;
10677          if(spec.list)
10678          {
10679             for(e = spec.list->first; e; e = e.next)
10680             {
10681                if(e.exp)
10682                   ProcessExpressionType(e.exp);
10683             }
10684          }
10685          break;
10686       }
10687       case structSpecifier:
10688       case unionSpecifier:
10689       {
10690          if(spec.definitions)
10691          {
10692             ClassDef def;
10693             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10694             //if(symbol)
10695                ProcessClass(spec.definitions, symbol);
10696             /*else
10697             {
10698                for(def = spec.definitions->first; def; def = def.next)
10699                {
10700                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10701                      ProcessDeclaration(def.decl);
10702                }
10703             }*/
10704          }
10705          break;
10706       }
10707       /*
10708       case classSpecifier:
10709       {
10710          Symbol classSym = FindClass(spec.name);
10711          if(classSym && classSym.registered && classSym.registered.type == structClass)
10712             DeclareStruct(spec.name, false);
10713          break;
10714       }
10715       */
10716    }
10717 }
10718
10719
10720 static void ProcessDeclarator(Declarator decl)
10721 {
10722    switch(decl.type)
10723    {
10724       case identifierDeclarator:
10725          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
10726          {
10727             FreeSpecifier(decl.identifier._class);
10728             decl.identifier._class = null;
10729          }
10730          break;
10731       case arrayDeclarator:
10732          if(decl.array.exp)
10733             ProcessExpressionType(decl.array.exp);
10734       case structDeclarator:
10735       case bracketsDeclarator:
10736       case functionDeclarator:
10737       case pointerDeclarator:
10738       case extendedDeclarator:
10739       case extendedDeclaratorEnd:
10740          if(decl.declarator)
10741             ProcessDeclarator(decl.declarator);
10742          if(decl.type == functionDeclarator)
10743          {
10744             Identifier id = GetDeclId(decl);
10745             if(id && id._class)
10746             {
10747                TypeName param
10748                {
10749                   qualifiers = MkListOne(id._class);
10750                   declarator = null;
10751                };
10752                if(!decl.function.parameters)
10753                   decl.function.parameters = MkList();
10754                decl.function.parameters->Insert(null, param);
10755                id._class = null;
10756             }
10757             if(decl.function.parameters)
10758             {
10759                TypeName param;
10760
10761                for(param = decl.function.parameters->first; param; param = param.next)
10762                {
10763                   if(param.qualifiers && param.qualifiers->first)
10764                   {
10765                      Specifier spec = param.qualifiers->first;
10766                      if(spec && spec.specifier == TYPED_OBJECT)
10767                      {
10768                         Declarator d = param.declarator;
10769                         TypeName newParam
10770                         {
10771                            qualifiers = MkListOne(MkSpecifier(VOID));
10772                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10773                         };
10774
10775                         FreeList(param.qualifiers, FreeSpecifier);
10776
10777                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
10778                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
10779
10780                         decl.function.parameters->Insert(param, newParam);
10781                         param = newParam;
10782                      }
10783                      else if(spec && spec.specifier == ANY_OBJECT)
10784                      {
10785                         Declarator d = param.declarator;
10786
10787                         FreeList(param.qualifiers, FreeSpecifier);
10788
10789                         param.qualifiers = MkListOne(MkSpecifier(VOID));
10790                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10791                      }
10792                      else if(spec.specifier == THISCLASS)
10793                      {
10794                         if(thisClass)
10795                         {
10796                            spec.type = nameSpecifier;
10797                            spec.name = ReplaceThisClass(thisClass);
10798                            spec.symbol = FindClass(spec.name);
10799                            ProcessSpecifier(spec, false);
10800                         }
10801                      }
10802                   }
10803
10804                   if(param.declarator)
10805                      ProcessDeclarator(param.declarator);
10806                }
10807             }
10808          }
10809          break;
10810    }
10811 }
10812
10813 static void ProcessDeclaration(Declaration decl)
10814 {
10815    yylloc = decl.loc;
10816    switch(decl.type)
10817    {
10818       case initDeclaration:
10819       {
10820          bool declareStruct = false;
10821          /*
10822          lineNum = decl.pos.line;
10823          column = decl.pos.col;
10824          */
10825
10826          if(decl.declarators)
10827          {
10828             InitDeclarator d;
10829
10830             for(d = decl.declarators->first; d; d = d.next)
10831             {
10832                Type type, subType;
10833                ProcessDeclarator(d.declarator);
10834
10835                type = ProcessType(decl.specifiers, d.declarator);
10836
10837                if(d.initializer)
10838                {
10839                   ProcessInitializer(d.initializer, type);
10840
10841                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
10842
10843                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
10844                      d.initializer.exp.type == instanceExp)
10845                   {
10846                      if(type.kind == classType && type._class ==
10847                         d.initializer.exp.expType._class)
10848                      {
10849                         Instantiation inst = d.initializer.exp.instance;
10850                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
10851
10852                         d.initializer.exp.instance = null;
10853                         if(decl.specifiers)
10854                            FreeList(decl.specifiers, FreeSpecifier);
10855                         FreeList(decl.declarators, FreeInitDeclarator);
10856
10857                         d = null;
10858
10859                         decl.type = instDeclaration;
10860                         decl.inst = inst;
10861                      }
10862                   }
10863                }
10864                for(subType = type; subType;)
10865                {
10866                   if(subType.kind == classType)
10867                   {
10868                      declareStruct = true;
10869                      break;
10870                   }
10871                   else if(subType.kind == pointerType)
10872                      break;
10873                   else if(subType.kind == arrayType)
10874                      subType = subType.arrayType;
10875                   else
10876                      break;
10877                }
10878
10879                FreeType(type);
10880                if(!d) break;
10881             }
10882          }
10883
10884          if(decl.specifiers)
10885          {
10886             Specifier s;
10887             for(s = decl.specifiers->first; s; s = s.next)
10888             {
10889                ProcessSpecifier(s, declareStruct);
10890             }
10891          }
10892          break;
10893       }
10894       case instDeclaration:
10895       {
10896          ProcessInstantiationType(decl.inst);
10897          break;
10898       }
10899       case structDeclaration:
10900       {
10901          Specifier spec;
10902          Declarator d;
10903          bool declareStruct = false;
10904
10905          if(decl.declarators)
10906          {
10907             for(d = decl.declarators->first; d; d = d.next)
10908             {
10909                Type type = ProcessType(decl.specifiers, d.declarator);
10910                Type subType;
10911                ProcessDeclarator(d);
10912                for(subType = type; subType;)
10913                {
10914                   if(subType.kind == classType)
10915                   {
10916                      declareStruct = true;
10917                      break;
10918                   }
10919                   else if(subType.kind == pointerType)
10920                      break;
10921                   else if(subType.kind == arrayType)
10922                      subType = subType.arrayType;
10923                   else
10924                      break;
10925                }
10926                FreeType(type);
10927             }
10928          }
10929          if(decl.specifiers)
10930          {
10931             for(spec = decl.specifiers->first; spec; spec = spec.next)
10932                ProcessSpecifier(spec, declareStruct);
10933          }
10934          break;
10935       }
10936    }
10937 }
10938
10939 static FunctionDefinition curFunction;
10940
10941 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
10942 {
10943    char propName[1024], propNameM[1024];
10944    char getName[1024], setName[1024];
10945    OldList * args;
10946
10947    DeclareProperty(prop, setName, getName);
10948
10949    // eInstance_FireWatchers(object, prop);
10950    strcpy(propName, "__ecereProp_");
10951    FullClassNameCat(propName, prop._class.fullName, false);
10952    strcat(propName, "_");
10953    // strcat(propName, prop.name);
10954    FullClassNameCat(propName, prop.name, true);
10955    MangleClassName(propName);
10956
10957    strcpy(propNameM, "__ecerePropM_");
10958    FullClassNameCat(propNameM, prop._class.fullName, false);
10959    strcat(propNameM, "_");
10960    // strcat(propNameM, prop.name);
10961    FullClassNameCat(propNameM, prop.name, true);
10962    MangleClassName(propNameM);
10963
10964    if(prop.isWatchable)
10965    {
10966       args = MkList();
10967       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10968       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10969       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10970
10971       args = MkList();
10972       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10973       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10974       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10975    }
10976
10977
10978    {
10979       args = MkList();
10980       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10981       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10982       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10983
10984       args = MkList();
10985       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10986       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10987       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10988    }
10989
10990    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
10991       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
10992       curFunction.propSet.fireWatchersDone = true;
10993 }
10994
10995 static void ProcessStatement(Statement stmt)
10996 {
10997    yylloc = stmt.loc;
10998    /*
10999    lineNum = stmt.pos.line;
11000    column = stmt.pos.col;
11001    */
11002    switch(stmt.type)
11003    {
11004       case labeledStmt:
11005          ProcessStatement(stmt.labeled.stmt);
11006          break;
11007       case caseStmt:
11008          // This expression should be constant...
11009          if(stmt.caseStmt.exp)
11010          {
11011             FreeType(stmt.caseStmt.exp.destType);
11012             stmt.caseStmt.exp.destType = curSwitchType;
11013             if(curSwitchType) curSwitchType.refCount++;
11014             ProcessExpressionType(stmt.caseStmt.exp);
11015             ComputeExpression(stmt.caseStmt.exp);
11016          }
11017          if(stmt.caseStmt.stmt)
11018             ProcessStatement(stmt.caseStmt.stmt);
11019          break;
11020       case compoundStmt:
11021       {
11022          if(stmt.compound.context)
11023          {
11024             Declaration decl;
11025             Statement s;
11026
11027             Statement prevCompound = curCompound;
11028             Context prevContext = curContext;
11029
11030             if(!stmt.compound.isSwitch)
11031                curCompound = stmt;
11032             curContext = stmt.compound.context;
11033
11034             if(stmt.compound.declarations)
11035             {
11036                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11037                   ProcessDeclaration(decl);
11038             }
11039             if(stmt.compound.statements)
11040             {
11041                for(s = stmt.compound.statements->first; s; s = s.next)
11042                   ProcessStatement(s);
11043             }
11044
11045             curContext = prevContext;
11046             curCompound = prevCompound;
11047          }
11048          break;
11049       }
11050       case expressionStmt:
11051       {
11052          Expression exp;
11053          if(stmt.expressions)
11054          {
11055             for(exp = stmt.expressions->first; exp; exp = exp.next)
11056                ProcessExpressionType(exp);
11057          }
11058          break;
11059       }
11060       case ifStmt:
11061       {
11062          Expression exp;
11063
11064          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11065          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11066          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11067          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11068          {
11069             ProcessExpressionType(exp);
11070          }
11071          if(stmt.ifStmt.stmt)
11072             ProcessStatement(stmt.ifStmt.stmt);
11073          if(stmt.ifStmt.elseStmt)
11074             ProcessStatement(stmt.ifStmt.elseStmt);
11075          break;
11076       }
11077       case switchStmt:
11078       {
11079          Type oldSwitchType = curSwitchType;
11080          if(stmt.switchStmt.exp)
11081          {
11082             Expression exp;
11083             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11084             {
11085                if(!exp.next)
11086                {
11087                   /*
11088                   Type destType
11089                   {
11090                      kind = intType;
11091                      refCount = 1;
11092                   };
11093                   e.exp.destType = destType;
11094                   */
11095
11096                   ProcessExpressionType(exp);
11097                }
11098                if(!exp.next)
11099                   curSwitchType = exp.expType;
11100             }
11101          }
11102          ProcessStatement(stmt.switchStmt.stmt);
11103          curSwitchType = oldSwitchType;
11104          break;
11105       }
11106       case whileStmt:
11107       {
11108          if(stmt.whileStmt.exp)
11109          {
11110             Expression exp;
11111
11112             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11113             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11114             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11115             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11116             {
11117                ProcessExpressionType(exp);
11118             }
11119          }
11120          if(stmt.whileStmt.stmt)
11121             ProcessStatement(stmt.whileStmt.stmt);
11122          break;
11123       }
11124       case doWhileStmt:
11125       {
11126          if(stmt.doWhile.exp)
11127          {
11128             Expression exp;
11129
11130             if(stmt.doWhile.exp->last)
11131             {
11132                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11133                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11134                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11135             }
11136             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11137             {
11138                ProcessExpressionType(exp);
11139             }
11140          }
11141          if(stmt.doWhile.stmt)
11142             ProcessStatement(stmt.doWhile.stmt);
11143          break;
11144       }
11145       case forStmt:
11146       {
11147          Expression exp;
11148          if(stmt.forStmt.init)
11149             ProcessStatement(stmt.forStmt.init);
11150
11151          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11152          {
11153             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11154             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11155             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11156          }
11157
11158          if(stmt.forStmt.check)
11159             ProcessStatement(stmt.forStmt.check);
11160          if(stmt.forStmt.increment)
11161          {
11162             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11163                ProcessExpressionType(exp);
11164          }
11165
11166          if(stmt.forStmt.stmt)
11167             ProcessStatement(stmt.forStmt.stmt);
11168          break;
11169       }
11170       case forEachStmt:
11171       {
11172          Identifier id = stmt.forEachStmt.id;
11173          OldList * exp = stmt.forEachStmt.exp;
11174          OldList * filter = stmt.forEachStmt.filter;
11175          Statement block = stmt.forEachStmt.stmt;
11176          char iteratorType[1024];
11177          Type source;
11178          Expression e;
11179          bool isBuiltin = exp && exp->last &&
11180             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11181               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11182          Expression arrayExp;
11183          char * typeString = null;
11184          int builtinCount = 0;
11185
11186          for(e = exp ? exp->first : null; e; e = e.next)
11187          {
11188             if(!e.next)
11189             {
11190                FreeType(e.destType);
11191                e.destType = ProcessTypeString("Container", false);
11192             }
11193             if(!isBuiltin || e.next)
11194                ProcessExpressionType(e);
11195          }
11196
11197          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11198          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11199             eClass_IsDerived(source._class.registered, containerClass)))
11200          {
11201             Class _class = source ? source._class.registered : null;
11202             Symbol symbol;
11203             Expression expIt = null;
11204             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11205             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11206             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11207             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11208             stmt.type = compoundStmt;
11209
11210             stmt.compound.context = Context { };
11211             stmt.compound.context.parent = curContext;
11212             curContext = stmt.compound.context;
11213
11214             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11215             {
11216                Class mapClass = eSystem_FindClass(privateModule, "Map");
11217                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11218                isCustomAVLTree = true;
11219                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11220                   isAVLTree = true;
11221                else if(eClass_IsDerived(source._class.registered, mapClass))
11222                   isMap = true;
11223             }
11224             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11225             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11226             {
11227                Class listClass = eSystem_FindClass(privateModule, "List");
11228                isLinkList = true;
11229                isList = eClass_IsDerived(source._class.registered, listClass);
11230             }
11231
11232             if(isArray)
11233             {
11234                Declarator decl;
11235                OldList * specs = MkList();
11236                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11237                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11238                stmt.compound.declarations = MkListOne(
11239                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11240                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11241                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11242                      MkInitializerAssignment(MkExpBrackets(exp))))));
11243             }
11244             else if(isBuiltin)
11245             {
11246                Type type = null;
11247                char typeStringBuf[1024];
11248
11249                // TODO: Merge this code?
11250                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11251                if(((Expression)exp->last).type == castExp)
11252                {
11253                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11254                   if(typeName)
11255                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11256                }
11257
11258                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11259                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11260                   arrayExp.destType._class.registered.templateArgs)
11261                {
11262                   Class templateClass = arrayExp.destType._class.registered;
11263                   typeString = templateClass.templateArgs[2].dataTypeString;
11264                }
11265                else if(arrayExp.list)
11266                {
11267                   // Guess type from expressions in the array
11268                   Expression e;
11269                   for(e = arrayExp.list->first; e; e = e.next)
11270                   {
11271                      ProcessExpressionType(e);
11272                      if(e.expType)
11273                      {
11274                         if(!type) { type = e.expType; type.refCount++; }
11275                         else
11276                         {
11277                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11278                            if(!MatchTypeExpression(e, type, null, false))
11279                            {
11280                               FreeType(type);
11281                               type = e.expType;
11282                               e.expType = null;
11283
11284                               e = arrayExp.list->first;
11285                               ProcessExpressionType(e);
11286                               if(e.expType)
11287                               {
11288                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11289                                  if(!MatchTypeExpression(e, type, null, false))
11290                                  {
11291                                     FreeType(e.expType);
11292                                     e.expType = null;
11293                                     FreeType(type);
11294                                     type = null;
11295                                     break;
11296                                  }
11297                               }
11298                            }
11299                         }
11300                         if(e.expType)
11301                         {
11302                            FreeType(e.expType);
11303                            e.expType = null;
11304                         }
11305                      }
11306                   }
11307                   if(type)
11308                   {
11309                      typeStringBuf[0] = '\0';
11310                      PrintType(type, typeStringBuf, false, true);
11311                      typeString = typeStringBuf;
11312                      FreeType(type);
11313                   }
11314                }
11315                if(typeString)
11316                {
11317                   OldList * initializers = MkList();
11318                   Declarator decl;
11319                   OldList * specs = MkList();
11320                   if(arrayExp.list)
11321                   {
11322                      Expression e;
11323
11324                      builtinCount = arrayExp.list->count;
11325                      type = ProcessTypeString(typeString, false);
11326                      while(e = arrayExp.list->first)
11327                      {
11328                         arrayExp.list->Remove(e);
11329                         e.destType = type;
11330                         type.refCount++;
11331                         ProcessExpressionType(e);
11332                         ListAdd(initializers, MkInitializerAssignment(e));
11333                      }
11334                      FreeType(type);
11335                      delete arrayExp.list;
11336                   }
11337                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11338                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11339                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11340
11341                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11342                      PlugDeclarator(
11343                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11344                         ), MkInitializerList(initializers)))));
11345                   FreeList(exp, FreeExpression);
11346                }
11347                else
11348                {
11349                   arrayExp.expType = ProcessTypeString("Container", false);
11350                   Compiler_Error($"Couldn't determine type of array elements\n");
11351                }
11352
11353                /*
11354                Declarator decl;
11355                OldList * specs = MkList();
11356
11357                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11358                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11359                stmt.compound.declarations = MkListOne(
11360                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11361                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11362                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11363                      MkInitializerAssignment(MkExpBrackets(exp))))));
11364                */
11365             }
11366             else if(isLinkList && !isList)
11367             {
11368                Declarator decl;
11369                OldList * specs = MkList();
11370                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11371                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11372                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11373                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11374                      MkInitializerAssignment(MkExpBrackets(exp))))));
11375             }
11376             /*else if(isCustomAVLTree)
11377             {
11378                Declarator decl;
11379                OldList * specs = MkList();
11380                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11381                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11382                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11383                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11384                      MkInitializerAssignment(MkExpBrackets(exp))))));
11385             }*/
11386             else if(_class.templateArgs)
11387             {
11388                if(isMap)
11389                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11390                else
11391                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11392
11393                stmt.compound.declarations = MkListOne(
11394                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11395                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11396                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11397             }
11398             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11399
11400             if(block)
11401             {
11402                // Reparent sub-contexts in this statement
11403                switch(block.type)
11404                {
11405                   case compoundStmt:
11406                      if(block.compound.context)
11407                         block.compound.context.parent = stmt.compound.context;
11408                      break;
11409                   case ifStmt:
11410                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11411                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11412                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11413                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11414                      break;
11415                   case switchStmt:
11416                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11417                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11418                      break;
11419                   case whileStmt:
11420                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11421                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11422                      break;
11423                   case doWhileStmt:
11424                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11425                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11426                      break;
11427                   case forStmt:
11428                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11429                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11430                      break;
11431                   case forEachStmt:
11432                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11433                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11434                      break;
11435                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11436                   case labeledStmt:
11437                   case caseStmt
11438                   case expressionStmt:
11439                   case gotoStmt:
11440                   case continueStmt:
11441                   case breakStmt
11442                   case returnStmt:
11443                   case asmStmt:
11444                   case badDeclarationStmt:
11445                   case fireWatchersStmt:
11446                   case stopWatchingStmt:
11447                   case watchStmt:
11448                   */
11449                }
11450             }
11451             if(filter)
11452             {
11453                block = MkIfStmt(filter, block, null);
11454             }
11455             if(isArray)
11456             {
11457                stmt.compound.statements = MkListOne(MkForStmt(
11458                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11459                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11460                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11461                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11462                   block));
11463               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11464               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11465               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11466             }
11467             else if(isBuiltin)
11468             {
11469                char count[128];
11470                //OldList * specs = MkList();
11471                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11472
11473                sprintf(count, "%d", builtinCount);
11474
11475                stmt.compound.statements = MkListOne(MkForStmt(
11476                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11477                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11478                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11479                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11480                   block));
11481
11482                /*
11483                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11484                stmt.compound.statements = MkListOne(MkForStmt(
11485                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11486                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11487                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11488                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11489                   block));
11490               */
11491               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11492               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11493               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11494             }
11495             else if(isLinkList && !isList)
11496             {
11497                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11498                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11499                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11500                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11501                {
11502                   stmt.compound.statements = MkListOne(MkForStmt(
11503                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11504                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11505                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11506                      block));
11507                }
11508                else
11509                {
11510                   OldList * specs = MkList();
11511                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11512                   stmt.compound.statements = MkListOne(MkForStmt(
11513                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11514                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11515                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11516                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11517                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11518                      block));
11519                }
11520                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11521                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11522                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11523             }
11524             /*else if(isCustomAVLTree)
11525             {
11526                stmt.compound.statements = MkListOne(MkForStmt(
11527                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11528                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11529                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11530                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11531                   block));
11532
11533                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11534                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11535                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11536             }*/
11537             else
11538             {
11539                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11540                   MkIdentifier("Next")), null)), block));
11541             }
11542             ProcessExpressionType(expIt);
11543             if(stmt.compound.declarations->first)
11544                ProcessDeclaration(stmt.compound.declarations->first);
11545
11546             if(symbol)
11547                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11548
11549             ProcessStatement(stmt);
11550             curContext = stmt.compound.context.parent;
11551             break;
11552          }
11553          else
11554          {
11555             Compiler_Error($"Expression is not a container\n");
11556          }
11557          break;
11558       }
11559       case gotoStmt:
11560          break;
11561       case continueStmt:
11562          break;
11563       case breakStmt:
11564          break;
11565       case returnStmt:
11566       {
11567          Expression exp;
11568          if(stmt.expressions)
11569          {
11570             for(exp = stmt.expressions->first; exp; exp = exp.next)
11571             {
11572                if(!exp.next)
11573                {
11574                   if(curFunction && !curFunction.type)
11575                      curFunction.type = ProcessType(
11576                         curFunction.specifiers, curFunction.declarator);
11577                   FreeType(exp.destType);
11578                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11579                   if(exp.destType) exp.destType.refCount++;
11580                }
11581                ProcessExpressionType(exp);
11582             }
11583          }
11584          break;
11585       }
11586       case badDeclarationStmt:
11587       {
11588          ProcessDeclaration(stmt.decl);
11589          break;
11590       }
11591       case asmStmt:
11592       {
11593          AsmField field;
11594          if(stmt.asmStmt.inputFields)
11595          {
11596             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11597                if(field.expression)
11598                   ProcessExpressionType(field.expression);
11599          }
11600          if(stmt.asmStmt.outputFields)
11601          {
11602             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11603                if(field.expression)
11604                   ProcessExpressionType(field.expression);
11605          }
11606          if(stmt.asmStmt.clobberedFields)
11607          {
11608             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11609             {
11610                if(field.expression)
11611                   ProcessExpressionType(field.expression);
11612             }
11613          }
11614          break;
11615       }
11616       case watchStmt:
11617       {
11618          PropertyWatch propWatch;
11619          OldList * watches = stmt._watch.watches;
11620          Expression object = stmt._watch.object;
11621          Expression watcher = stmt._watch.watcher;
11622          if(watcher)
11623             ProcessExpressionType(watcher);
11624          if(object)
11625             ProcessExpressionType(object);
11626
11627          if(inCompiler)
11628          {
11629             if(watcher || thisClass)
11630             {
11631                External external = curExternal;
11632                Context context = curContext;
11633
11634                stmt.type = expressionStmt;
11635                stmt.expressions = MkList();
11636
11637                curExternal = external.prev;
11638
11639                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11640                {
11641                   ClassFunction func;
11642                   char watcherName[1024];
11643                   Class watcherClass = watcher ?
11644                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11645                   External createdExternal;
11646
11647                   // Create a declaration above
11648                   External externalDecl = MkExternalDeclaration(null);
11649                   ast->Insert(curExternal.prev, externalDecl);
11650
11651                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11652                   if(propWatch.deleteWatch)
11653                      strcat(watcherName, "_delete");
11654                   else
11655                   {
11656                      Identifier propID;
11657                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11658                      {
11659                         strcat(watcherName, "_");
11660                         strcat(watcherName, propID.string);
11661                      }
11662                   }
11663
11664                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11665                   {
11666                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11667                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11668                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11669                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11670                      ProcessClassFunctionBody(func, propWatch.compound);
11671                      propWatch.compound = null;
11672
11673                      //afterExternal = afterExternal ? afterExternal : curExternal;
11674
11675                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11676                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11677                      // TESTING THIS...
11678                      createdExternal.symbol.idCode = external.symbol.idCode;
11679
11680                      curExternal = createdExternal;
11681                      ProcessFunction(createdExternal.function);
11682
11683
11684                      // Create a declaration above
11685                      {
11686                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
11687                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11688                         externalDecl.declaration = decl;
11689                         if(decl.symbol && !decl.symbol.pointerExternal)
11690                            decl.symbol.pointerExternal = externalDecl;
11691                      }
11692
11693                      if(propWatch.deleteWatch)
11694                      {
11695                         OldList * args = MkList();
11696                         ListAdd(args, CopyExpression(object));
11697                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11698                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11699                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11700                      }
11701                      else
11702                      {
11703                         Class _class = object.expType._class.registered;
11704                         Identifier propID;
11705
11706                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11707                         {
11708                            char propName[1024];
11709                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11710                            if(prop)
11711                            {
11712                               char getName[1024], setName[1024];
11713                               OldList * args = MkList();
11714
11715                               DeclareProperty(prop, setName, getName);
11716
11717                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
11718                               strcpy(propName, "__ecereProp_");
11719                               FullClassNameCat(propName, prop._class.fullName, false);
11720                               strcat(propName, "_");
11721                               // strcat(propName, prop.name);
11722                               FullClassNameCat(propName, prop.name, true);
11723
11724                               ListAdd(args, CopyExpression(object));
11725                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11726                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11727                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11728
11729                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
11730                            }
11731                            else
11732                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11733                         }
11734                      }
11735                   }
11736                   else
11737                      Compiler_Error($"Invalid watched object\n");
11738                }
11739
11740                curExternal = external;
11741                curContext = context;
11742
11743                if(watcher)
11744                   FreeExpression(watcher);
11745                if(object)
11746                   FreeExpression(object);
11747                FreeList(watches, FreePropertyWatch);
11748             }
11749             else
11750                Compiler_Error($"No observer specified and not inside a _class\n");
11751          }
11752          else
11753          {
11754             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11755             {
11756                ProcessStatement(propWatch.compound);
11757             }
11758
11759          }
11760          break;
11761       }
11762       case fireWatchersStmt:
11763       {
11764          OldList * watches = stmt._watch.watches;
11765          Expression object = stmt._watch.object;
11766          Class _class;
11767          // DEBUGGER BUG: Why doesn't watches evaluate to null??
11768          // printf("%X\n", watches);
11769          // printf("%X\n", stmt._watch.watches);
11770          if(object)
11771             ProcessExpressionType(object);
11772
11773          if(inCompiler)
11774          {
11775             _class = object ?
11776                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
11777
11778             if(_class)
11779             {
11780                Identifier propID;
11781
11782                stmt.type = expressionStmt;
11783                stmt.expressions = MkList();
11784
11785                // Check if we're inside a property set
11786                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11787                {
11788                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
11789                }
11790                else if(!watches)
11791                {
11792                   //Compiler_Error($"No property specified and not inside a property set\n");
11793                }
11794                if(watches)
11795                {
11796                   for(propID = watches->first; propID; propID = propID.next)
11797                   {
11798                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11799                      if(prop)
11800                      {
11801                         CreateFireWatcher(prop, object, stmt);
11802                      }
11803                      else
11804                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
11805                   }
11806                }
11807                else
11808                {
11809                   // Fire all properties!
11810                   Property prop;
11811                   Class base;
11812                   for(base = _class; base; base = base.base)
11813                   {
11814                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
11815                      {
11816                         if(prop.isProperty && prop.isWatchable)
11817                         {
11818                            CreateFireWatcher(prop, object, stmt);
11819                         }
11820                      }
11821                   }
11822                }
11823
11824                if(object)
11825                   FreeExpression(object);
11826                FreeList(watches, FreeIdentifier);
11827             }
11828             else
11829                Compiler_Error($"Invalid object specified and not inside a class\n");
11830          }
11831          break;
11832       }
11833       case stopWatchingStmt:
11834       {
11835          OldList * watches = stmt._watch.watches;
11836          Expression object = stmt._watch.object;
11837          Expression watcher = stmt._watch.watcher;
11838          Class _class;
11839          if(object)
11840             ProcessExpressionType(object);
11841          if(watcher)
11842             ProcessExpressionType(watcher);
11843          if(inCompiler)
11844          {
11845             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
11846
11847             if(watcher || thisClass)
11848             {
11849                if(_class)
11850                {
11851                   Identifier propID;
11852
11853                   stmt.type = expressionStmt;
11854                   stmt.expressions = MkList();
11855
11856                   if(!watches)
11857                   {
11858                      OldList * args;
11859                      // eInstance_StopWatching(object, null, watcher);
11860                      args = MkList();
11861                      ListAdd(args, CopyExpression(object));
11862                      ListAdd(args, MkExpConstant("0"));
11863                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11864                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11865                   }
11866                   else
11867                   {
11868                      for(propID = watches->first; propID; propID = propID.next)
11869                      {
11870                         char propName[1024];
11871                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11872                         if(prop)
11873                         {
11874                            char getName[1024], setName[1024];
11875                            OldList * args = MkList();
11876
11877                            DeclareProperty(prop, setName, getName);
11878
11879                            // eInstance_StopWatching(object, prop, watcher);
11880                            strcpy(propName, "__ecereProp_");
11881                            FullClassNameCat(propName, prop._class.fullName, false);
11882                            strcat(propName, "_");
11883                            // strcat(propName, prop.name);
11884                            FullClassNameCat(propName, prop.name, true);
11885                            MangleClassName(propName);
11886
11887                            ListAdd(args, CopyExpression(object));
11888                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11889                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11890                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11891                         }
11892                         else
11893                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11894                      }
11895                   }
11896
11897                   if(object)
11898                      FreeExpression(object);
11899                   if(watcher)
11900                      FreeExpression(watcher);
11901                   FreeList(watches, FreeIdentifier);
11902                }
11903                else
11904                   Compiler_Error($"Invalid object specified and not inside a class\n");
11905             }
11906             else
11907                Compiler_Error($"No observer specified and not inside a class\n");
11908          }
11909          break;
11910       }
11911    }
11912 }
11913
11914 static void ProcessFunction(FunctionDefinition function)
11915 {
11916    Identifier id = GetDeclId(function.declarator);
11917    Symbol symbol = function.declarator ? function.declarator.symbol : null;
11918    Type type = symbol ? symbol.type : null;
11919    Class oldThisClass = thisClass;
11920    Context oldTopContext = topContext;
11921
11922    yylloc = function.loc;
11923    // Process thisClass
11924
11925    if(type && type.thisClass)
11926    {
11927       Symbol classSym = type.thisClass;
11928       Class _class = type.thisClass.registered;
11929       char className[1024];
11930       char structName[1024];
11931       Declarator funcDecl;
11932       Symbol thisSymbol;
11933
11934       bool typedObject = false;
11935
11936       if(_class && !_class.base)
11937       {
11938          _class = currentClass;
11939          if(_class && !_class.symbol)
11940             _class.symbol = FindClass(_class.fullName);
11941          classSym = _class ? _class.symbol : null;
11942          typedObject = true;
11943       }
11944
11945       thisClass = _class;
11946
11947       if(inCompiler && _class)
11948       {
11949          if(type.kind == functionType)
11950          {
11951             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
11952             {
11953                //TypeName param = symbol.type.params.first;
11954                Type param = symbol.type.params.first;
11955                symbol.type.params.Remove(param);
11956                //FreeTypeName(param);
11957                FreeType(param);
11958             }
11959             if(type.classObjectType != classPointer)
11960             {
11961                symbol.type.params.Insert(null, MkClassType(_class.fullName));
11962                symbol.type.staticMethod = true;
11963                symbol.type.thisClass = null;
11964
11965                // HIGH DANGER: VERIFYING THIS...
11966                symbol.type.extraParam = false;
11967             }
11968          }
11969
11970          strcpy(className, "__ecereClass_");
11971          FullClassNameCat(className, _class.fullName, true);
11972
11973          MangleClassName(className);
11974
11975          structName[0] = 0;
11976          FullClassNameCat(structName, _class.fullName, false);
11977
11978          // [class] this
11979
11980
11981          funcDecl = GetFuncDecl(function.declarator);
11982          if(funcDecl)
11983          {
11984             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
11985             {
11986                TypeName param = funcDecl.function.parameters->first;
11987                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
11988                {
11989                   funcDecl.function.parameters->Remove(param);
11990                   FreeTypeName(param);
11991                }
11992             }
11993
11994             // DANGER: Watch for this... Check if it's a Conversion?
11995             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
11996
11997             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
11998             if(!function.propertyNoThis)
11999             {
12000                TypeName thisParam;
12001
12002                if(type.classObjectType != classPointer)
12003                {
12004                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12005                   if(!funcDecl.function.parameters)
12006                      funcDecl.function.parameters = MkList();
12007                   funcDecl.function.parameters->Insert(null, thisParam);
12008                }
12009
12010                if(typedObject)
12011                {
12012                   if(type.classObjectType != classPointer)
12013                   {
12014                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12015                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12016                   }
12017
12018                   thisParam = TypeName
12019                   {
12020                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12021                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12022                   };
12023                   funcDecl.function.parameters->Insert(null, thisParam);
12024                }
12025             }
12026          }
12027
12028          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12029          {
12030             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12031             funcDecl = GetFuncDecl(initDecl.declarator);
12032             if(funcDecl)
12033             {
12034                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12035                {
12036                   TypeName param = funcDecl.function.parameters->first;
12037                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12038                   {
12039                      funcDecl.function.parameters->Remove(param);
12040                      FreeTypeName(param);
12041                   }
12042                }
12043
12044                if(type.classObjectType != classPointer)
12045                {
12046                   // DANGER: Watch for this... Check if it's a Conversion?
12047                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12048                   {
12049                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12050
12051                      if(!funcDecl.function.parameters)
12052                         funcDecl.function.parameters = MkList();
12053                      funcDecl.function.parameters->Insert(null, thisParam);
12054                   }
12055                }
12056             }
12057          }
12058       }
12059
12060       // Add this to the context
12061       if(function.body)
12062       {
12063          if(type.classObjectType != classPointer)
12064          {
12065             thisSymbol = Symbol
12066             {
12067                string = CopyString("this");
12068                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12069             };
12070             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12071
12072             if(typedObject && thisSymbol.type)
12073             {
12074                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12075                thisSymbol.type.byReference = type.byReference;
12076                thisSymbol.type.typedByReference = type.byReference;
12077                /*
12078                thisSymbol = Symbol { string = CopyString("class") };
12079                function.body.compound.context.symbols.Add(thisSymbol);
12080                */
12081             }
12082          }
12083       }
12084
12085       // Pointer to class data
12086
12087       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12088       {
12089          DataMember member = null;
12090          {
12091             Class base;
12092             for(base = _class; base && base.type != systemClass; base = base.next)
12093             {
12094                for(member = base.membersAndProperties.first; member; member = member.next)
12095                   if(!member.isProperty)
12096                      break;
12097                if(member)
12098                   break;
12099             }
12100          }
12101          for(member = _class.membersAndProperties.first; member; member = member.next)
12102             if(!member.isProperty)
12103                break;
12104          if(member)
12105          {
12106             char pointerName[1024];
12107
12108             Declaration decl;
12109             Initializer initializer;
12110             Expression exp, bytePtr;
12111
12112             strcpy(pointerName, "__ecerePointer_");
12113             FullClassNameCat(pointerName, _class.fullName, false);
12114             {
12115                char className[1024];
12116                strcpy(className, "__ecereClass_");
12117                FullClassNameCat(className, classSym.string, true);
12118                MangleClassName(className);
12119
12120                // Testing This
12121                DeclareClass(classSym, className);
12122             }
12123
12124             // ((byte *) this)
12125             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12126
12127             if(_class.fixed)
12128             {
12129                char string[256];
12130                sprintf(string, "%d", _class.offset);
12131                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12132             }
12133             else
12134             {
12135                // ([bytePtr] + [className]->offset)
12136                exp = QBrackets(MkExpOp(bytePtr, '+',
12137                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12138             }
12139
12140             // (this ? [exp] : 0)
12141             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12142             exp.expType = Type
12143             {
12144                refCount = 1;
12145                kind = pointerType;
12146                type = Type { refCount = 1, kind = voidType };
12147             };
12148
12149             if(function.body)
12150             {
12151                yylloc = function.body.loc;
12152                // ([structName] *) [exp]
12153                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12154                initializer = MkInitializerAssignment(
12155                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12156
12157                // [structName] * [pointerName] = [initializer];
12158                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12159
12160                {
12161                   Context prevContext = curContext;
12162                   curContext = function.body.compound.context;
12163
12164                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12165                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12166
12167                   curContext = prevContext;
12168                }
12169
12170                // WHY?
12171                decl.symbol = null;
12172
12173                if(!function.body.compound.declarations)
12174                   function.body.compound.declarations = MkList();
12175                function.body.compound.declarations->Insert(null, decl);
12176             }
12177          }
12178       }
12179
12180
12181       // Loop through the function and replace undeclared identifiers
12182       // which are a member of the class (methods, properties or data)
12183       // by "this.[member]"
12184    }
12185    else
12186       thisClass = null;
12187
12188    if(id)
12189    {
12190       FreeSpecifier(id._class);
12191       id._class = null;
12192
12193       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12194       {
12195          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12196          id = GetDeclId(initDecl.declarator);
12197
12198          FreeSpecifier(id._class);
12199          id._class = null;
12200       }
12201    }
12202    if(function.body)
12203       topContext = function.body.compound.context;
12204    {
12205       FunctionDefinition oldFunction = curFunction;
12206       curFunction = function;
12207       if(function.body)
12208          ProcessStatement(function.body);
12209
12210       // If this is a property set and no firewatchers has been done yet, add one here
12211       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12212       {
12213          Statement prevCompound = curCompound;
12214          Context prevContext = curContext;
12215
12216          Statement fireWatchers = MkFireWatchersStmt(null, null);
12217          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12218          ListAdd(function.body.compound.statements, fireWatchers);
12219
12220          curCompound = function.body;
12221          curContext = function.body.compound.context;
12222
12223          ProcessStatement(fireWatchers);
12224
12225          curContext = prevContext;
12226          curCompound = prevCompound;
12227
12228       }
12229
12230       curFunction = oldFunction;
12231    }
12232
12233    if(function.declarator)
12234    {
12235       ProcessDeclarator(function.declarator);
12236    }
12237
12238    topContext = oldTopContext;
12239    thisClass = oldThisClass;
12240 }
12241
12242 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12243 static void ProcessClass(OldList definitions, Symbol symbol)
12244 {
12245    ClassDef def;
12246    External external = curExternal;
12247    Class regClass = symbol ? symbol.registered : null;
12248
12249    // Process all functions
12250    for(def = definitions.first; def; def = def.next)
12251    {
12252       if(def.type == functionClassDef)
12253       {
12254          if(def.function.declarator)
12255             curExternal = def.function.declarator.symbol.pointerExternal;
12256          else
12257             curExternal = external;
12258
12259          ProcessFunction((FunctionDefinition)def.function);
12260       }
12261       else if(def.type == declarationClassDef)
12262       {
12263          if(def.decl.type == instDeclaration)
12264          {
12265             thisClass = regClass;
12266             ProcessInstantiationType(def.decl.inst);
12267             thisClass = null;
12268          }
12269          // Testing this
12270          else
12271          {
12272             Class backThisClass = thisClass;
12273             if(regClass) thisClass = regClass;
12274             ProcessDeclaration(def.decl);
12275             thisClass = backThisClass;
12276          }
12277       }
12278       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12279       {
12280          MemberInit defProperty;
12281
12282          // Add this to the context
12283          Symbol thisSymbol = Symbol
12284          {
12285             string = CopyString("this");
12286             type = regClass ? MkClassType(regClass.fullName) : null;
12287          };
12288          globalContext.symbols.Add((BTNode)thisSymbol);
12289
12290          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12291          {
12292             thisClass = regClass;
12293             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12294             thisClass = null;
12295          }
12296
12297          globalContext.symbols.Remove((BTNode)thisSymbol);
12298          FreeSymbol(thisSymbol);
12299       }
12300       else if(def.type == propertyClassDef && def.propertyDef)
12301       {
12302          PropertyDef prop = def.propertyDef;
12303
12304          // Add this to the context
12305          /*
12306          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12307          globalContext.symbols.Add(thisSymbol);
12308          */
12309
12310          thisClass = regClass;
12311          if(prop.setStmt)
12312          {
12313             if(regClass)
12314             {
12315                Symbol thisSymbol
12316                {
12317                   string = CopyString("this");
12318                   type = MkClassType(regClass.fullName);
12319                };
12320                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12321             }
12322
12323             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12324             ProcessStatement(prop.setStmt);
12325          }
12326          if(prop.getStmt)
12327          {
12328             if(regClass)
12329             {
12330                Symbol thisSymbol
12331                {
12332                   string = CopyString("this");
12333                   type = MkClassType(regClass.fullName);
12334                };
12335                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12336             }
12337
12338             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12339             ProcessStatement(prop.getStmt);
12340          }
12341          if(prop.issetStmt)
12342          {
12343             if(regClass)
12344             {
12345                Symbol thisSymbol
12346                {
12347                   string = CopyString("this");
12348                   type = MkClassType(regClass.fullName);
12349                };
12350                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12351             }
12352
12353             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12354             ProcessStatement(prop.issetStmt);
12355          }
12356
12357          thisClass = null;
12358
12359          /*
12360          globalContext.symbols.Remove(thisSymbol);
12361          FreeSymbol(thisSymbol);
12362          */
12363       }
12364       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12365       {
12366          PropertyWatch propertyWatch = def.propertyWatch;
12367
12368          thisClass = regClass;
12369          if(propertyWatch.compound)
12370          {
12371             Symbol thisSymbol
12372             {
12373                string = CopyString("this");
12374                type = regClass ? MkClassType(regClass.fullName) : null;
12375             };
12376
12377             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12378
12379             curExternal = null;
12380             ProcessStatement(propertyWatch.compound);
12381          }
12382          thisClass = null;
12383       }
12384    }
12385 }
12386
12387 void DeclareFunctionUtil(String s)
12388 {
12389    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12390    if(function)
12391    {
12392       char name[1024];
12393       name[0] = 0;
12394       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12395          strcpy(name, "__ecereFunction_");
12396       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12397       DeclareFunction(function, name);
12398    }
12399 }
12400
12401 void ComputeDataTypes()
12402 {
12403    External external;
12404    External temp { };
12405    External after = null;
12406
12407    currentClass = null;
12408
12409    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12410
12411    for(external = ast->first; external; external = external.next)
12412    {
12413       if(external.type == declarationExternal)
12414       {
12415          Declaration decl = external.declaration;
12416          if(decl)
12417          {
12418             OldList * decls = decl.declarators;
12419             if(decls)
12420             {
12421                InitDeclarator initDecl = decls->first;
12422                if(initDecl)
12423                {
12424                   Declarator declarator = initDecl.declarator;
12425                   if(declarator && declarator.type == identifierDeclarator)
12426                   {
12427                      Identifier id = declarator.identifier;
12428                      if(id && id.string)
12429                      {
12430                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12431                         {
12432                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12433                            after = external;
12434                         }
12435                      }
12436                   }
12437                }
12438             }
12439          }
12440        }
12441    }
12442
12443    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12444    ast->Insert(after, temp);
12445    curExternal = temp;
12446
12447    DeclareFunctionUtil("eSystem_New");
12448    DeclareFunctionUtil("eSystem_New0");
12449    DeclareFunctionUtil("eSystem_Renew");
12450    DeclareFunctionUtil("eSystem_Renew0");
12451    DeclareFunctionUtil("eSystem_Delete");
12452    DeclareFunctionUtil("eClass_GetProperty");
12453    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12454
12455    DeclareStruct("ecere::com::Class", false);
12456    DeclareStruct("ecere::com::Instance", false);
12457    DeclareStruct("ecere::com::Property", false);
12458    DeclareStruct("ecere::com::DataMember", false);
12459    DeclareStruct("ecere::com::Method", false);
12460    DeclareStruct("ecere::com::SerialBuffer", false);
12461    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12462
12463    ast->Remove(temp);
12464
12465    for(external = ast->first; external; external = external.next)
12466    {
12467       afterExternal = curExternal = external;
12468       if(external.type == functionExternal)
12469       {
12470          currentClass = external.function._class;
12471          ProcessFunction(external.function);
12472       }
12473       // There shouldn't be any _class member access here anyways...
12474       else if(external.type == declarationExternal)
12475       {
12476          currentClass = null;
12477          ProcessDeclaration(external.declaration);
12478       }
12479       else if(external.type == classExternal)
12480       {
12481          ClassDefinition _class = external._class;
12482          currentClass = external.symbol.registered;
12483          if(_class.definitions)
12484          {
12485             ProcessClass(_class.definitions, _class.symbol);
12486          }
12487          if(inCompiler)
12488          {
12489             // Free class data...
12490             ast->Remove(external);
12491             delete external;
12492          }
12493       }
12494       else if(external.type == nameSpaceExternal)
12495       {
12496          thisNameSpace = external.id.string;
12497       }
12498    }
12499    currentClass = null;
12500    thisNameSpace = null;
12501
12502    delete temp.symbol;
12503    delete temp;
12504 }