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