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