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