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