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