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