compiler/libec: Correction to the previous fix to handle 1 bad warning left
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50
51       if(exp)
52          OutputExpression(exp, f);
53       f.Seek(0, start);
54       count = strlen(string);
55       count += f.Read(string + count, 1, 1023);
56       string[count] = '\0';
57       delete f;
58    }
59 }
60
61 Type ProcessTemplateParameterType(TemplateParameter param)
62 {
63    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
64    {
65       // TOFIX: Will need to free this Type
66       if(!param.baseType)
67       {
68          if(param.dataTypeString)
69             param.baseType = ProcessTypeString(param.dataTypeString, false);
70          else
71             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
72       }
73       return param.baseType;
74    }
75    return null;
76 }
77
78 bool NeedCast(Type type1, Type type2)
79 {
80    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
81
82    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
83    {
84       return false;
85    }
86
87    if(type1.kind == type2.kind)
88    {
89       switch(type1.kind)
90       {
91          case charType:
92          case shortType:
93          case intType:
94          case int64Type:
95          case intPtrType:
96          case intSizeType:
97             if(type1.passAsTemplate && !type2.passAsTemplate)
98                return true;
99             return type1.isSigned != type2.isSigned;
100          case classType:
101             return type1._class != type2._class;
102          case pointerType:
103             return NeedCast(type1.type, type2.type);
104          default:
105             return true; //false; ????
106       }
107    }
108    return true;
109 }
110
111 static void ReplaceClassMembers(Expression exp, Class _class)
112 {
113    if(exp.type == identifierExp && exp.identifier)
114    {
115       Identifier id = exp.identifier;
116       Context ctx;
117       Symbol symbol = null;
118       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
119       {
120          // First, check if the identifier is declared inside the function
121          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
122          {
123             symbol = (Symbol)ctx.symbols.FindString(id.string);
124             if(symbol) break;
125          }
126       }
127
128       // If it is not, check if it is a member of the _class
129       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
130       {
131          Property prop = eClass_FindProperty(_class, id.string, privateModule);
132          Method method = null;
133          DataMember member = null;
134          ClassProperty classProp = null;
135          if(!prop)
136          {
137             method = eClass_FindMethod(_class, id.string, privateModule);
138          }
139          if(!prop && !method)
140             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
141          if(!prop && !method && !member)
142          {
143             classProp = eClass_FindClassProperty(_class, id.string);
144          }
145          if(prop || method || member || classProp)
146          {
147             // Replace by this.[member]
148             exp.type = memberExp;
149             exp.member.member = id;
150             exp.member.memberType = unresolvedMember;
151             exp.member.exp = QMkExpId("this");
152             //exp.member.exp.loc = exp.loc;
153             exp.addedThis = true;
154          }
155          else if(_class && _class.templateParams.first)
156          {
157             Class sClass;
158             for(sClass = _class; sClass; sClass = sClass.base)
159             {
160                if(sClass.templateParams.first)
161                {
162                   ClassTemplateParameter param;
163                   for(param = sClass.templateParams.first; param; param = param.next)
164                   {
165                      if(param.type == expression && !strcmp(param.name, id.string))
166                      {
167                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
168
169                         if(argExp)
170                         {
171                            Declarator decl;
172                            OldList * specs = MkList();
173
174                            FreeIdentifier(exp.member.member);
175
176                            ProcessExpressionType(argExp);
177
178                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
179
180                            exp.expType = ProcessType(specs, decl);
181
182                            // *[expType] *[argExp]
183                            exp.type = bracketsExp;
184                            exp.list = MkListOne(MkExpOp(null, '*',
185                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
186                         }
187                      }
188                   }
189                }
190             }
191          }
192       }
193    }
194 }
195
196 ////////////////////////////////////////////////////////////////////////
197 // PRINTING ////////////////////////////////////////////////////////////
198 ////////////////////////////////////////////////////////////////////////
199
200 public char * PrintInt(int64 result)
201 {
202    char temp[100];
203    if(result > MAXINT64)
204       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
205    else
206       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
207    return CopyString(temp);
208 }
209
210 public char * PrintUInt(uint64 result)
211 {
212    char temp[100];
213    if(result > MAXDWORD)
214       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
215    else if(result > MAXINT)
216       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
217    else
218       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
219    return CopyString(temp);
220 }
221
222 public char * PrintInt64(int64 result)
223 {
224    char temp[100];
225    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
226    return CopyString(temp);
227 }
228
229 public char * PrintUInt64(uint64 result)
230 {
231    char temp[100];
232    if(result > MAXINT64)
233       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
234    else
235       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
236    return CopyString(temp);
237 }
238
239 public char * PrintHexUInt(uint64 result)
240 {
241    char temp[100];
242    if(result > MAXDWORD)
243       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
244    else
245       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
246    return CopyString(temp);
247 }
248
249 public char * PrintHexUInt64(uint64 result)
250 {
251    char temp[100];
252    if(result > MAXDWORD)
253       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
254    else
255       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
256    return CopyString(temp);
257 }
258
259 public char * PrintShort(short result)
260 {
261    char temp[100];
262    sprintf(temp, "%d", (unsigned short)result);
263    return CopyString(temp);
264 }
265
266 public char * PrintUShort(unsigned short result)
267 {
268    char temp[100];
269    if(result > 32767)
270       sprintf(temp, "0x%X", (int)result);
271    else
272       sprintf(temp, "%d", result);
273    return CopyString(temp);
274 }
275
276 public char * PrintChar(char result)
277 {
278    char temp[100];
279    if(result > 0 && isprint(result))
280       sprintf(temp, "'%c'", result);
281    else if(result < 0)
282       sprintf(temp, "%d", result);
283    else
284       //sprintf(temp, "%#X", result);
285       sprintf(temp, "0x%X", (unsigned char)result);
286    return CopyString(temp);
287 }
288
289 public char * PrintUChar(unsigned char result)
290 {
291    char temp[100];
292    sprintf(temp, "0x%X", result);
293    return CopyString(temp);
294 }
295
296 public char * PrintFloat(float result)
297 {
298    char temp[350];
299    sprintf(temp, "%.16ff", result);
300    return CopyString(temp);
301 }
302
303 public char * PrintDouble(double result)
304 {
305    char temp[350];
306    sprintf(temp, "%.16f", result);
307    return CopyString(temp);
308 }
309
310 ////////////////////////////////////////////////////////////////////////
311 ////////////////////////////////////////////////////////////////////////
312
313 //public Operand GetOperand(Expression exp);
314
315 #define GETVALUE(name, t) \
316    public bool Get##name(Expression exp, t * value2) \
317    {                                                        \
318       Operand op2 = GetOperand(exp);                        \
319       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
320       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
321       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
322       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
323       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
324       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
325       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
326       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
327       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
328       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
329       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
330       else if(op2.kind == charType) *value2 = (t) op2.uc;                         \
331       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
332       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
333       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
334       else                                                                          \
335          return false;                                                              \
336       return true;                                                                  \
337    }
338
339 // To help the deubugger currently not preprocessing...
340 #define HELP(x) x
341
342 GETVALUE(Int, HELP(int));
343 GETVALUE(UInt, HELP(unsigned int));
344 GETVALUE(Int64, HELP(int64));
345 GETVALUE(UInt64, HELP(uint64));
346 GETVALUE(IntPtr, HELP(intptr));
347 GETVALUE(UIntPtr, HELP(uintptr));
348 GETVALUE(IntSize, HELP(intsize));
349 GETVALUE(UIntSize, HELP(uintsize));
350 GETVALUE(Short, HELP(short));
351 GETVALUE(UShort, HELP(unsigned short));
352 GETVALUE(Char, HELP(char));
353 GETVALUE(UChar, HELP(unsigned char));
354 GETVALUE(Float, HELP(float));
355 GETVALUE(Double, HELP(double));
356
357 void ComputeExpression(Expression exp);
358
359 void ComputeClassMembers(Class _class, bool isMember)
360 {
361    DataMember member = isMember ? (DataMember) _class : null;
362    Context context = isMember ? null : SetupTemplatesContext(_class);
363    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) && 
364                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
365    {
366       int c;
367       int unionMemberOffset = 0;
368       int bitFields = 0;
369
370       /*
371       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
372          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
373       */
374
375       if(member)
376       {
377          member.memberOffset = 0;
378          if(targetBits < sizeof(void *) * 8)
379             member.structAlignment = 0;
380       }
381       else if(targetBits < sizeof(void *) * 8)
382          _class.structAlignment = 0;
383
384       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
385       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
386          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
387
388       if(!member && _class.destructionWatchOffset)
389          _class.memberOffset += sizeof(OldList);
390
391       // To avoid reentrancy...
392       //_class.structSize = -1;
393
394       {
395          DataMember dataMember;
396          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
397          {
398             if(!dataMember.isProperty)
399             {
400                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
401                {
402                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
403                   /*if(!dataMember.dataType)
404                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
405                      */
406                }
407             }
408          }
409       }
410
411       {
412          DataMember dataMember;
413          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
414          {
415             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
416             {
417                if(!isMember && _class.type == bitClass && dataMember.dataType)
418                {
419                   BitMember bitMember = (BitMember) dataMember;
420                   uint64 mask = 0;
421                   int d;
422
423                   ComputeTypeSize(dataMember.dataType);
424
425                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
426                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
427
428                   _class.memberOffset = bitMember.pos + bitMember.size;
429                   for(d = 0; d<bitMember.size; d++)
430                   {
431                      if(d)
432                         mask <<= 1;
433                      mask |= 1;
434                   }
435                   bitMember.mask = mask << bitMember.pos;
436                }
437                else if(dataMember.type == normalMember && dataMember.dataType)
438                {
439                   int size;
440                   int alignment = 0;
441
442                   // Prevent infinite recursion
443                   if(dataMember.dataType.kind != classType || 
444                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
445                      _class.type != structClass)))
446                      ComputeTypeSize(dataMember.dataType);
447
448                   if(dataMember.dataType.bitFieldCount)
449                   {
450                      bitFields += dataMember.dataType.bitFieldCount;
451                      size = 0;
452                   }
453                   else
454                   {
455                      if(bitFields)
456                      {
457                         int size = (bitFields + 7) / 8;
458
459                         if(isMember)
460                         {
461                            // TESTING THIS PADDING CODE
462                            if(alignment)
463                            {
464                               member.structAlignment = Max(member.structAlignment, alignment);
465
466                               if(member.memberOffset % alignment)
467                                  member.memberOffset += alignment - (member.memberOffset % alignment);
468                            }
469
470                            dataMember.offset = member.memberOffset;
471                            if(member.type == unionMember)
472                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
473                            else
474                            {
475                               member.memberOffset += size;
476                            }
477                         }
478                         else
479                         {
480                            // TESTING THIS PADDING CODE
481                            if(alignment)
482                            {
483                               _class.structAlignment = Max(_class.structAlignment, alignment);
484
485                               if(_class.memberOffset % alignment)
486                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
487                            }
488
489                            dataMember.offset = _class.memberOffset;
490                            _class.memberOffset += size;
491                         }
492                         bitFields = 0;
493                      }
494                      size = dataMember.dataType.size;
495                      alignment = dataMember.dataType.alignment;
496                   }
497
498                   if(isMember)
499                   {
500                      // TESTING THIS PADDING CODE
501                      if(alignment)
502                      {
503                         member.structAlignment = Max(member.structAlignment, alignment);
504
505                         if(member.memberOffset % alignment)
506                            member.memberOffset += alignment - (member.memberOffset % alignment);
507                      }
508
509                      dataMember.offset = member.memberOffset;
510                      if(member.type == unionMember)
511                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
512                      else
513                      {
514                         member.memberOffset += size;
515                      }
516                   }
517                   else
518                   {
519                      // TESTING THIS PADDING CODE
520                      if(alignment)
521                      {
522                         _class.structAlignment = Max(_class.structAlignment, alignment);
523
524                         if(_class.memberOffset % alignment)
525                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
526                      }
527
528                      dataMember.offset = _class.memberOffset;
529                      _class.memberOffset += size;
530                   }
531                }
532                else
533                {
534                   int alignment;
535
536                   ComputeClassMembers((Class)dataMember, true);
537                   alignment = dataMember.structAlignment;
538
539                   if(isMember)
540                   {
541                      if(alignment)
542                      {
543                         if(member.memberOffset % alignment)
544                            member.memberOffset += alignment - (member.memberOffset % alignment);
545
546                         member.structAlignment = Max(member.structAlignment, alignment);
547                      }
548                      dataMember.offset = member.memberOffset;
549                      if(member.type == unionMember)
550                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
551                      else
552                         member.memberOffset += dataMember.memberOffset;
553                   }
554                   else
555                   {
556                      if(alignment)
557                      {
558                         if(_class.memberOffset % alignment)
559                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
560                         _class.structAlignment = Max(_class.structAlignment, alignment);
561                      }
562                      dataMember.offset = _class.memberOffset;
563                      _class.memberOffset += dataMember.memberOffset;
564                   }
565                }
566             }
567          }
568          if(bitFields)
569          {
570             int alignment = 0;
571             int size = (bitFields + 7) / 8;
572
573             if(isMember)
574             {
575                // TESTING THIS PADDING CODE
576                if(alignment)
577                {
578                   member.structAlignment = Max(member.structAlignment, alignment);
579
580                   if(member.memberOffset % alignment)
581                      member.memberOffset += alignment - (member.memberOffset % alignment);
582                }
583
584                if(member.type == unionMember)
585                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
586                else
587                {
588                   member.memberOffset += size;
589                }
590             }
591             else
592             {
593                // TESTING THIS PADDING CODE
594                if(alignment)
595                {
596                   _class.structAlignment = Max(_class.structAlignment, alignment);
597
598                   if(_class.memberOffset % alignment)
599                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
600                }
601                _class.memberOffset += size;
602             }
603             bitFields = 0;
604          }
605       }
606       if(member && member.type == unionMember)
607       {
608          member.memberOffset = unionMemberOffset;
609       }
610       
611       if(!isMember)
612       {
613          /*if(_class.type == structClass)
614             _class.size = _class.memberOffset;
615          else
616          */
617
618          if(_class.type != bitClass)
619          {
620             int extra = 0;
621             if(_class.structAlignment)
622             {
623                if(_class.memberOffset % _class.structAlignment)
624                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
625             }
626             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
627             if(!member)
628             {
629                Property prop;
630                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
631                {
632                   if(prop.isProperty && prop.isWatchable)
633                   {
634                      prop.watcherOffset = _class.structSize;
635                      _class.structSize += sizeof(OldList);
636                   }
637                }
638             }
639
640             // Fix Derivatives
641             {
642                OldLink derivative;
643                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
644                {
645                   Class deriv = derivative.data;
646
647                   if(deriv.computeSize)
648                   {
649                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
650                      deriv.offset = /*_class.offset + */_class.structSize;
651                      deriv.memberOffset = 0;
652                      // ----------------------
653
654                      deriv.structSize = deriv.offset;
655
656                      ComputeClassMembers(deriv, false);
657                   }
658                }
659             }
660          }
661       }
662    }
663    if(context)
664       FinishTemplatesContext(context);
665 }
666
667 public void ComputeModuleClasses(Module module)
668 {
669    Class _class;
670    OldLink subModule;
671    
672    for(subModule = module.modules.first; subModule; subModule = subModule.next)
673       ComputeModuleClasses(subModule.data);
674    for(_class = module.classes.first; _class; _class = _class.next)
675       ComputeClassMembers(_class, false);
676 }
677
678
679 public int ComputeTypeSize(Type type)
680 {
681    uint size = type ? type.size : 0;
682    if(!size && type && !type.computing)
683    {
684       type.computing = true;
685       switch(type.kind)
686       {
687          case charType: type.alignment = size = sizeof(char); break;
688          case intType: type.alignment = size = sizeof(int); break;
689          case int64Type: type.alignment = size = sizeof(int64); break;
690          case intPtrType: type.alignment = size = targetBits / 8; break;
691          case intSizeType: type.alignment = size = targetBits / 8; break;
692          case longType: type.alignment = size = sizeof(long); break;
693          case shortType: type.alignment = size = sizeof(short); break;
694          case floatType: type.alignment = size = sizeof(float); break;
695          case doubleType: type.alignment = size = sizeof(double); break;
696          case classType:
697          {
698             Class _class = type._class ? type._class.registered : null;
699
700             if(_class && _class.type == structClass)
701             {
702                // Ensure all members are properly registered
703                ComputeClassMembers(_class, false);
704                type.alignment = _class.structAlignment;
705                size = _class.structSize;
706                if(type.alignment && size % type.alignment)
707                   size += type.alignment - (size % type.alignment);
708
709             }
710             else if(_class && (_class.type == unitClass || 
711                    _class.type == enumClass || 
712                    _class.type == bitClass))
713             {
714                if(!_class.dataType)
715                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
716                size = type.alignment = ComputeTypeSize(_class.dataType);
717             }
718             else
719                size = type.alignment = targetBits / 8; // sizeof(Instance *);
720             break;
721          }
722          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
723          case arrayType: 
724             if(type.arraySizeExp)
725             {
726                ProcessExpressionType(type.arraySizeExp);
727                ComputeExpression(type.arraySizeExp);
728                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType && 
729                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
730                {
731                   Location oldLoc = yylloc;
732                   // bool isConstant = type.arraySizeExp.isConstant;
733                   char expression[10240];
734                   expression[0] = '\0';
735                   type.arraySizeExp.expType = null;
736                   yylloc = type.arraySizeExp.loc;
737                   if(inCompiler)
738                      PrintExpression(type.arraySizeExp, expression);
739                   Compiler_Error($"Array size not constant int (%s)\n", expression);
740                   yylloc = oldLoc;
741                }
742                GetInt(type.arraySizeExp, &type.arraySize);
743             }
744             else if(type.enumClass)
745             {
746                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
747                {
748                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
749                }
750                else
751                   type.arraySize = 0;
752             }
753             else
754             {
755                // Unimplemented auto size
756                type.arraySize = 0;
757             }
758
759             size = ComputeTypeSize(type.type) * type.arraySize;
760             type.alignment = type.type.alignment;
761             
762             break;
763          case structType:
764          {
765             Type member;
766             for(member = type.members.first; member; member = member.next)
767             {
768                uint addSize = ComputeTypeSize(member);
769
770                member.offset = size;
771                if(member.alignment && size % member.alignment)
772                   member.offset += member.alignment - (size % member.alignment);
773                size = member.offset;
774
775                type.alignment = Max(type.alignment, member.alignment);
776                size += addSize;
777             }
778             if(type.alignment && size % type.alignment)
779                size += type.alignment - (size % type.alignment);
780             break;
781          }
782          case unionType:
783          {
784             Type member;
785             for(member = type.members.first; member; member = member.next)
786             {
787                uint addSize = ComputeTypeSize(member);
788                
789                member.offset = size;
790                if(member.alignment && size % member.alignment)
791                   member.offset += member.alignment - (size % member.alignment);
792                size = member.offset;
793
794                type.alignment = Max(type.alignment, member.alignment);
795                size = Max(size, addSize);
796             }
797             if(type.alignment && size % type.alignment)
798                size += type.alignment - (size % type.alignment);
799             break;
800          }
801          case templateType:
802          {
803             TemplateParameter param = type.templateParameter;
804             Type baseType = ProcessTemplateParameterType(param);
805             if(baseType)
806             {
807                size = ComputeTypeSize(baseType);
808                type.alignment = baseType.alignment;
809             }
810             else
811                type.alignment = size = sizeof(uint64);
812             break;
813          }
814          case enumType:
815          {
816             type.alignment = size = sizeof(enum { test });
817             break;
818          }
819          case thisClassType:
820          {
821             type.alignment = size = targetBits / 8; //sizeof(void *);
822             break;
823          }
824       }
825       type.size = size;
826       type.computing = false;
827    }
828    return size;
829 }
830
831
832 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
833 {
834    // This function is in need of a major review when implementing private members etc.
835    DataMember topMember = isMember ? (DataMember) _class : null;
836    uint totalSize = 0;
837    uint maxSize = 0;
838    int alignment, size;
839    DataMember member;
840    Context context = isMember ? null : SetupTemplatesContext(_class);
841    if(addedPadding)
842       *addedPadding = false;
843
844    if(!isMember && _class.base)
845    {
846       maxSize = _class.structSize;
847       //if(_class.base.type != systemClass) // Commented out with new Instance _class
848       {
849          // DANGER: Testing this noHeadClass here...
850          if(_class.type == structClass || _class.type == noHeadClass)
851             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
852          else
853             maxSize -= _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
854       }
855    }
856
857    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
858    {
859       if(!member.isProperty)
860       {
861          switch(member.type)
862          {
863             case normalMember:
864             {
865                if(member.dataTypeString)
866                {
867                   OldList * specs = MkList(), * decls = MkList();
868                   Declarator decl;
869
870                   decl = SpecDeclFromString(member.dataTypeString, specs, 
871                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
872                   ListAdd(decls, MkStructDeclarator(decl, null));
873                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
874
875                   if(!member.dataType)
876                      member.dataType = ProcessType(specs, decl);
877
878                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
879
880                   {
881                      Type type = ProcessType(specs, decl);
882                      DeclareType(member.dataType, false, false);
883                      FreeType(type);
884                   }
885                   /*
886                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
887                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
888                      DeclareStruct(member.dataType._class.string, false);
889                   */
890
891                   ComputeTypeSize(member.dataType);
892                   size = member.dataType.size;
893                   alignment = member.dataType.alignment;
894
895                   if(alignment)
896                   {
897                      if(totalSize % alignment)
898                         totalSize += alignment - (totalSize % alignment);
899                   }
900                   totalSize += size;
901                }
902                break;
903             }
904             case unionMember:
905             case structMember:
906             {
907                OldList * specs = MkList(), * list = MkList();
908                
909                size = 0;
910                AddMembers(list, (Class)member, true, &size, topClass, null);
911                ListAdd(specs, 
912                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
913                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
914                alignment = member.structAlignment;
915
916                if(alignment)
917                {
918                   if(totalSize % alignment)
919                      totalSize += alignment - (totalSize % alignment);
920                }
921                totalSize += size;
922                break;
923             }
924          }
925       }
926    }
927    if(retSize)
928    {
929       if(topMember && topMember.type == unionMember)
930          *retSize = Max(*retSize, totalSize);
931       else
932          *retSize += totalSize;
933    }
934    else if(totalSize < maxSize && _class.type != systemClass)
935    {
936       int autoPadding = 0;
937       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
938          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
939       if(totalSize + autoPadding < maxSize)
940       {
941          char sizeString[50];
942          sprintf(sizeString, "%d", maxSize - totalSize);
943          ListAdd(declarations, 
944             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)), 
945             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
946          if(addedPadding)
947             *addedPadding = true;
948       }
949    }
950    if(context)
951       FinishTemplatesContext(context);
952    return topMember ? topMember.memberID : _class.memberID;
953 }
954
955 static int DeclareMembers(Class _class, bool isMember)
956 {
957    DataMember topMember = isMember ? (DataMember) _class : null;
958    uint totalSize = 0;
959    DataMember member;
960    Context context = isMember ? null : SetupTemplatesContext(_class);
961    
962    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
963       DeclareMembers(_class.base, false);
964
965    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
966    {
967       if(!member.isProperty)
968       {
969          switch(member.type)
970          {
971             case normalMember:
972             {
973                /*
974                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
975                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
976                   DeclareStruct(member.dataType._class.string, false);
977                   */
978                if(!member.dataType && member.dataTypeString)
979                   member.dataType = ProcessTypeString(member.dataTypeString, false);
980                if(member.dataType)
981                   DeclareType(member.dataType, false, false);
982                break;
983             }
984             case unionMember:
985             case structMember:
986             {
987                DeclareMembers((Class)member, true);
988                break;
989             }
990          }
991       }
992    }
993    if(context)
994       FinishTemplatesContext(context);
995
996    return topMember ? topMember.memberID : _class.memberID;
997 }
998
999 void DeclareStruct(char * name, bool skipNoHead)
1000 {
1001    External external = null;
1002    Symbol classSym = FindClass(name);
1003
1004    if(!inCompiler || !classSym) return;
1005
1006    // We don't need any declaration for bit classes...
1007    if(classSym.registered && 
1008       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1009       return;
1010
1011    /*if(classSym.registered.templateClass)
1012       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1013    */
1014
1015    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1016    {
1017       // Add typedef struct
1018       Declaration decl;
1019       OldList * specifiers, * declarators;
1020       OldList * declarations = null;
1021       char structName[1024];
1022       external = (classSym.registered && classSym.registered.type == structClass) ? 
1023          classSym.pointerExternal : classSym.structExternal;
1024
1025       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1026       // Moved this one up because DeclareClass done later will need it
1027
1028       classSym.declaring++;
1029
1030       if(strchr(classSym.string, '<'))
1031       {
1032          if(classSym.registered.templateClass)
1033          {
1034             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1035             classSym.declaring--;
1036          }
1037          return;
1038       }
1039       
1040       //if(!skipNoHead)
1041          DeclareMembers(classSym.registered, false);
1042
1043       structName[0] = 0;
1044       FullClassNameCat(structName, name, false);
1045
1046       /*if(!external)      
1047          external = MkExternalDeclaration(null);*/
1048
1049       if(!skipNoHead)
1050       {
1051          bool addedPadding = false;
1052          classSym.declaredStructSym = true;
1053
1054          declarations = MkList();
1055
1056          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1057
1058          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1059          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1060
1061          if(!declarations->count || (declarations->count == 1 && addedPadding))
1062          {
1063             FreeList(declarations, FreeClassDef);
1064             declarations = null;
1065          }
1066       }
1067       if(skipNoHead || declarations)
1068       {
1069          if(external && external.declaration)
1070          {
1071             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1072
1073             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1074             {
1075                // TODO: Fix this
1076                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1077
1078                // DANGER
1079                if(classSym.structExternal)
1080                   ast->Move(classSym.structExternal, curExternal.prev);
1081                ast->Move(classSym.pointerExternal, curExternal.prev);
1082
1083                classSym.id = curExternal.symbol.idCode;
1084                classSym.idCode = curExternal.symbol.idCode;
1085                // external = classSym.pointerExternal;
1086                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1087             }
1088          }
1089          else
1090          {
1091             if(!external)      
1092                external = MkExternalDeclaration(null);
1093
1094             specifiers = MkList();
1095             declarators = MkList();
1096             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1097
1098             /*
1099             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1100             ListAdd(declarators, MkInitDeclarator(d, null));
1101             */
1102             external.declaration = decl = MkDeclaration(specifiers, declarators);
1103             if(decl.symbol && !decl.symbol.pointerExternal)
1104                decl.symbol.pointerExternal = external;
1105
1106             // For simple classes, keep the declaration as the external to move around
1107             if(classSym.registered && classSym.registered.type == structClass)
1108             {
1109                char className[1024];
1110                strcpy(className, "__ecereClass_");
1111                FullClassNameCat(className, classSym.string, true);
1112                MangleClassName(className);
1113
1114                // Testing This
1115                DeclareClass(classSym, className);
1116
1117                external.symbol = classSym;
1118                classSym.pointerExternal = external;
1119                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1120                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1121             }
1122             else
1123             {
1124                char className[1024];
1125                strcpy(className, "__ecereClass_");
1126                FullClassNameCat(className, classSym.string, true);
1127                MangleClassName(className);
1128
1129                // TOFIX: TESTING THIS...
1130                classSym.structExternal = external;
1131                DeclareClass(classSym, className);
1132                external.symbol = classSym;
1133             }
1134
1135             //if(curExternal)
1136                ast->Insert(curExternal ? curExternal.prev : null, external);
1137          }
1138       }
1139
1140       classSym.declaring--;
1141    }
1142    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1143    {
1144       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1145       // Moved this one up because DeclareClass done later will need it
1146
1147       // TESTING THIS:
1148       classSym.declaring++;
1149
1150       //if(!skipNoHead)
1151       {
1152          if(classSym.registered)
1153             DeclareMembers(classSym.registered, false);
1154       }
1155
1156       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1157       {
1158          // TODO: Fix this
1159          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1160
1161          // DANGER
1162          if(classSym.structExternal)
1163             ast->Move(classSym.structExternal, curExternal.prev);
1164          ast->Move(classSym.pointerExternal, curExternal.prev);
1165
1166          classSym.id = curExternal.symbol.idCode;
1167          classSym.idCode = curExternal.symbol.idCode;
1168          // external = classSym.pointerExternal;
1169          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1170       }
1171
1172       classSym.declaring--;
1173    }
1174    //return external;
1175 }
1176
1177 void DeclareProperty(Property prop, char * setName, char * getName)
1178 {
1179    Symbol symbol = prop.symbol;
1180    char propName[1024];
1181
1182    strcpy(setName, "__ecereProp_");
1183    FullClassNameCat(setName, prop._class.fullName, false);
1184    strcat(setName, "_Set_");
1185    // strcat(setName, prop.name);
1186    FullClassNameCat(setName, prop.name, true);
1187
1188    strcpy(getName, "__ecereProp_");
1189    FullClassNameCat(getName, prop._class.fullName, false);
1190    strcat(getName, "_Get_");
1191    FullClassNameCat(getName, prop.name, true);
1192    // strcat(getName, prop.name);
1193
1194    strcpy(propName, "__ecereProp_");
1195    FullClassNameCat(propName, prop._class.fullName, false);
1196    strcat(propName, "_");
1197    FullClassNameCat(propName, prop.name, true);
1198    // strcat(propName, prop.name);
1199
1200    // To support "char *" property
1201    MangleClassName(getName);
1202    MangleClassName(setName);
1203    MangleClassName(propName);
1204
1205    if(prop._class.type == structClass)
1206       DeclareStruct(prop._class.fullName, false);
1207
1208    if(!symbol || curExternal.symbol.idCode < symbol.id)
1209    {
1210       bool imported = false;
1211       bool dllImport = false;
1212       if(!symbol || symbol._import)
1213       {
1214          if(!symbol)
1215          {
1216             Symbol classSym;
1217             if(!prop._class.symbol)
1218                prop._class.symbol = FindClass(prop._class.fullName);
1219             classSym = prop._class.symbol;
1220             if(classSym && !classSym._import)
1221             {
1222                ModuleImport module;
1223
1224                if(prop._class.module)
1225                   module = FindModule(prop._class.module);
1226                else
1227                   module = mainModule;
1228
1229                classSym._import = ClassImport
1230                {
1231                   name = CopyString(prop._class.fullName);
1232                   isRemote = prop._class.isRemote;
1233                };
1234                module.classes.Add(classSym._import);
1235             }
1236             symbol = prop.symbol = Symbol { };
1237             symbol._import = (ClassImport)PropertyImport
1238             {
1239                name = CopyString(prop.name);
1240                isVirtual = false; //prop.isVirtual;
1241                hasSet = prop.Set ? true : false;
1242                hasGet = prop.Get ? true : false;
1243             };
1244             if(classSym)
1245                classSym._import.properties.Add(symbol._import);
1246          }
1247          imported = true;
1248          if(prop._class.module != privateModule && prop._class.module.importType != staticImport)
1249             dllImport = true;
1250       }
1251
1252       if(!symbol.type)
1253       {
1254          Context context = SetupTemplatesContext(prop._class);
1255          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1256          FinishTemplatesContext(context);
1257       }
1258
1259       // Get
1260       if(prop.Get)
1261       {
1262          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1263          {
1264             Declaration decl;
1265             OldList * specifiers, * declarators;
1266             Declarator d;
1267             OldList * params;
1268             Specifier spec;
1269             External external;
1270             Declarator typeDecl;
1271             bool simple = false;
1272
1273             specifiers = MkList();
1274             declarators = MkList();
1275             params = MkList();
1276
1277             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)), 
1278                MkDeclaratorIdentifier(MkIdentifier("this"))));
1279
1280             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1281             //if(imported)
1282             if(dllImport)
1283                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1284
1285             {
1286                Context context = SetupTemplatesContext(prop._class);
1287                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1288                FinishTemplatesContext(context);
1289             }
1290
1291             // Make sure the simple _class's type is declared
1292             for(spec = specifiers->first; spec; spec = spec.next)
1293             {
1294                if(spec.type == nameSpecifier /*SpecifierClass*/)
1295                {
1296                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1297                   {
1298                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1299                      symbol._class = classSym.registered;
1300                      if(classSym.registered && classSym.registered.type == structClass)
1301                      {
1302                         DeclareStruct(spec.name, false);
1303                         simple = true;
1304                      }
1305                   }
1306                }
1307             }
1308
1309             if(!simple)
1310                d = PlugDeclarator(typeDecl, d);
1311             else
1312             {
1313                ListAdd(params, MkTypeName(specifiers, 
1314                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1315                specifiers = MkList();
1316             }
1317
1318             d = MkDeclaratorFunction(d, params);
1319  
1320             //if(imported)
1321             if(dllImport)
1322                specifiers->Insert(null, MkSpecifier(EXTERN));
1323             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1324                specifiers->Insert(null, MkSpecifier(STATIC));
1325             if(simple)
1326                ListAdd(specifiers, MkSpecifier(VOID));
1327
1328             ListAdd(declarators, MkInitDeclarator(d, null));
1329
1330             decl = MkDeclaration(specifiers, declarators);
1331
1332             external = MkExternalDeclaration(decl);
1333             ast->Insert(curExternal.prev, external);
1334             external.symbol = symbol;
1335             symbol.externalGet = external;
1336
1337             ReplaceThisClassSpecifiers(specifiers, prop._class);
1338
1339             if(typeDecl)
1340                FreeDeclarator(typeDecl);
1341          }
1342          else
1343          {
1344             // Move declaration higher...
1345             ast->Move(symbol.externalGet, curExternal.prev);
1346          }
1347       }
1348
1349       // Set
1350       if(prop.Set)
1351       {
1352          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1353          {
1354             Declaration decl;
1355             OldList * specifiers, * declarators;
1356             Declarator d;
1357             OldList * params;
1358             Specifier spec;
1359             External external;
1360             Declarator typeDecl;
1361
1362             declarators = MkList();
1363             params = MkList();
1364
1365             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1366             if(!prop.conversion || prop._class.type == structClass)
1367             {
1368                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)), 
1369                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1370             }
1371
1372             specifiers = MkList();
1373
1374             {
1375                Context context = SetupTemplatesContext(prop._class);
1376                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1377                   MkDeclaratorIdentifier(MkIdentifier("value")));
1378                FinishTemplatesContext(context);
1379             }
1380             ListAdd(params, MkTypeName(specifiers, d));
1381
1382             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1383             //if(imported)
1384             if(dllImport)
1385                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1386             d = MkDeclaratorFunction(d, params);
1387
1388             // Make sure the simple _class's type is declared
1389             for(spec = specifiers->first; spec; spec = spec.next)
1390             {
1391                if(spec.type == nameSpecifier /*SpecifierClass*/)
1392                {
1393                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1394                   {
1395                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1396                      symbol._class = classSym.registered;
1397                      if(classSym.registered && classSym.registered.type == structClass)
1398                         DeclareStruct(spec.name, false);
1399                   }
1400                }
1401             }
1402
1403             ListAdd(declarators, MkInitDeclarator(d, null));
1404
1405             specifiers = MkList();
1406             //if(imported)
1407             if(dllImport)
1408                specifiers->Insert(null, MkSpecifier(EXTERN));
1409             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1410                specifiers->Insert(null, MkSpecifier(STATIC));
1411
1412             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1413             if(!prop.conversion || prop._class.type == structClass)
1414                ListAdd(specifiers, MkSpecifier(VOID));
1415             else
1416                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1417
1418             decl = MkDeclaration(specifiers, declarators);
1419
1420             external = MkExternalDeclaration(decl);
1421             ast->Insert(curExternal.prev, external);
1422             external.symbol = symbol;
1423             symbol.externalSet = external;
1424
1425             ReplaceThisClassSpecifiers(specifiers, prop._class);
1426          }
1427          else
1428          {
1429             // Move declaration higher...
1430             ast->Move(symbol.externalSet, curExternal.prev);
1431          }
1432       }
1433
1434       // Property (for Watchers)
1435       if(!symbol.externalPtr)
1436       {
1437          Declaration decl;
1438          External external;
1439          OldList * specifiers = MkList();
1440
1441          if(imported)
1442             specifiers->Insert(null, MkSpecifier(EXTERN));
1443          else
1444             specifiers->Insert(null, MkSpecifier(STATIC));
1445
1446          ListAdd(specifiers, MkSpecifierName("Property"));
1447
1448          {
1449             OldList * list = MkList();
1450             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), 
1451                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1452
1453             if(!imported)
1454             {
1455                strcpy(propName, "__ecerePropM_");
1456                FullClassNameCat(propName, prop._class.fullName, false);
1457                strcat(propName, "_");
1458                // strcat(propName, prop.name);
1459                FullClassNameCat(propName, prop.name, true);
1460
1461                MangleClassName(propName);
1462
1463                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), 
1464                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1465             }
1466             decl = MkDeclaration(specifiers, list);
1467          }
1468
1469          external = MkExternalDeclaration(decl);
1470          ast->Insert(curExternal.prev, external);
1471          external.symbol = symbol;
1472          symbol.externalPtr = external;
1473       }
1474       else
1475       {
1476          // Move declaration higher...
1477          ast->Move(symbol.externalPtr, curExternal.prev);
1478       }
1479
1480       symbol.id = curExternal.symbol.idCode;
1481    }
1482 }
1483
1484 // ***************** EXPRESSION PROCESSING ***************************
1485 public Type Dereference(Type source)
1486 {
1487    Type type = null;
1488    if(source)
1489    {
1490       if(source.kind == pointerType || source.kind == arrayType)
1491       {
1492          type = source.type;
1493          source.type.refCount++;
1494       }
1495       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1496       {
1497          type = Type
1498          {
1499             kind = charType;
1500             refCount = 1;
1501          };
1502       }
1503       // Support dereferencing of no head classes for now...
1504       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1505       {
1506          type = source;
1507          source.refCount++;
1508       }
1509       else
1510          Compiler_Error($"cannot dereference type\n");
1511    }
1512    return type;
1513 }
1514
1515 static Type Reference(Type source)
1516 {
1517    Type type = null;
1518    if(source)
1519    {
1520       type = Type
1521       {
1522          kind = pointerType;
1523          type = source;
1524          refCount = 1;
1525       };
1526       source.refCount++;
1527    }
1528    return type;
1529 }
1530
1531 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1532 {
1533    Identifier ident = member.identifiers ? member.identifiers->first : null;
1534    bool found = false;
1535    DataMember dataMember = null;
1536    Method method = null;
1537    bool freeType = false;
1538
1539    yylloc = member.loc;
1540
1541    if(!ident)
1542    {
1543       if(curMember)
1544       {
1545          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1546          if(*curMember)
1547          {
1548             found = true;
1549             dataMember = *curMember;
1550          }
1551       }
1552    }
1553    else
1554    {
1555       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1556       DataMember _subMemberStack[256];
1557       int _subMemberStackPos = 0;
1558
1559       // FILL MEMBER STACK
1560       if(!thisMember)
1561          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1562       if(thisMember)
1563       {
1564          dataMember = thisMember;
1565          if(curMember && thisMember.memberAccess == publicAccess)
1566          {
1567             *curMember = thisMember;
1568             *curClass = thisMember._class;
1569             memcpy(subMemberStack, _subMemberStack, sizeof(int) * _subMemberStackPos);
1570             *subMemberStackPos = _subMemberStackPos;
1571          }
1572          found = true;
1573       }
1574       else
1575       {
1576          // Setting a method
1577          method = eClass_FindMethod(_class, ident.string, privateModule);
1578          if(method && method.type == virtualMethod)
1579             found = true;
1580          else
1581             method = null;
1582       }
1583    }
1584
1585    if(found)
1586    {
1587       Type type = null;
1588       if(dataMember)
1589       {
1590          if(!dataMember.dataType && dataMember.dataTypeString)
1591          {
1592             //Context context = SetupTemplatesContext(dataMember._class);
1593             Context context = SetupTemplatesContext(_class);
1594             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1595             FinishTemplatesContext(context);
1596          }
1597          type = dataMember.dataType;
1598       }
1599       else if(method)
1600       {
1601          // This is for destination type...
1602          if(!method.dataType)
1603             ProcessMethodType(method);
1604          //DeclareMethod(method);
1605          // method.dataType = ((Symbol)method.symbol)->type;
1606          type = method.dataType;
1607       }
1608
1609       if(ident && ident.next)
1610       {
1611          for(ident = ident.next; ident && type; ident = ident.next)
1612          {
1613             if(type.kind == classType)
1614             {
1615                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1616                if(!dataMember)
1617                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1618                if(dataMember)
1619                   type = dataMember.dataType;
1620             }
1621             else if(type.kind == structType || type.kind == unionType)
1622             {
1623                Type memberType;
1624                for(memberType = type.members.first; memberType; memberType = memberType.next)
1625                {
1626                   if(!strcmp(memberType.name, ident.string))
1627                   {
1628                      type = memberType;
1629                      break;
1630                   }
1631                }
1632             }
1633          }
1634       }
1635
1636       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1637       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1638       {
1639          int id = 0;
1640          ClassTemplateParameter curParam = null;
1641          Class sClass;
1642          for(sClass = _class; sClass; sClass = sClass.base)
1643          {
1644             id = 0;
1645             if(sClass.templateClass) sClass = sClass.templateClass;
1646             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1647             {
1648                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1649                {
1650                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1651                   {
1652                      if(sClass.templateClass) sClass = sClass.templateClass;
1653                      id += sClass.templateParams.count;
1654                   }
1655                   break;
1656                }
1657                id++;
1658             }
1659             if(curParam) break;
1660          }
1661
1662          if(curParam)
1663          {
1664             ClassTemplateArgument arg = _class.templateArgs[id];
1665             if(arg.dataTypeString)
1666             {
1667                // FreeType(type);
1668                type = ProcessTypeString(arg.dataTypeString, false);
1669                freeType = true;
1670                if(type && _class.templateClass)
1671                   type.passAsTemplate = true;
1672                if(type)
1673                {
1674                   // type.refCount++;
1675                   /*if(!exp.destType)
1676                   {
1677                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1678                      exp.destType.refCount++;
1679                   }*/
1680                }
1681             }
1682          }
1683       }
1684       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1685       {
1686          Class expClass = type._class.registered;
1687          Class cClass = null;
1688          int c;
1689          int paramCount = 0;
1690          int lastParam = -1;
1691          
1692          char templateString[1024];
1693          ClassTemplateParameter param;
1694          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1695          for(cClass = expClass; cClass; cClass = cClass.base)
1696          {
1697             int p = 0;
1698             if(cClass.templateClass) cClass = cClass.templateClass;
1699             for(param = cClass.templateParams.first; param; param = param.next)
1700             {
1701                int id = p;
1702                Class sClass;
1703                ClassTemplateArgument arg;
1704                for(sClass = cClass.base; sClass; sClass = sClass.base) 
1705                {
1706                   if(sClass.templateClass) sClass = sClass.templateClass;
1707                   id += sClass.templateParams.count;
1708                }
1709                arg = expClass.templateArgs[id];
1710
1711                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1712                {
1713                   ClassTemplateParameter cParam;
1714                   //int p = numParams - sClass.templateParams.count;
1715                   int p = 0;
1716                   Class nextClass;
1717                   if(sClass.templateClass) sClass = sClass.templateClass;
1718
1719                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) 
1720                   {
1721                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1722                      p += nextClass.templateParams.count;
1723                   }
1724                   
1725                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1726                   {
1727                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1728                      {
1729                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1730                         {
1731                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1732                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1733                            break;
1734                         }
1735                      }
1736                   }
1737                }
1738
1739                {
1740                   char argument[256];
1741                   argument[0] = '\0';
1742                   /*if(arg.name)
1743                   {
1744                      strcat(argument, arg.name.string);
1745                      strcat(argument, " = ");
1746                   }*/
1747                   switch(param.type)
1748                   {
1749                      case expression:
1750                      {
1751                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1752                         char expString[1024];
1753                         OldList * specs = MkList();
1754                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1755                         Expression exp;
1756                         char * string = PrintHexUInt64(arg.expression.ui64);
1757                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1758
1759                         ProcessExpressionType(exp);
1760                         ComputeExpression(exp);
1761                         expString[0] = '\0';
1762                         PrintExpression(exp, expString);
1763                         strcat(argument, expString);
1764                         //delete exp;
1765                         FreeExpression(exp);
1766                         break;
1767                      }
1768                      case identifier:
1769                      {
1770                         strcat(argument, arg.member.name);
1771                         break;
1772                      }
1773                      case TemplateParameterType::type:
1774                      {
1775                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1776                            strcat(argument, arg.dataTypeString);
1777                         break;
1778                      }
1779                   }
1780                   if(argument[0])
1781                   {
1782                      if(paramCount) strcat(templateString, ", ");
1783                      if(lastParam != p - 1)
1784                      {
1785                         strcat(templateString, param.name);
1786                         strcat(templateString, " = ");
1787                      }
1788                      strcat(templateString, argument);
1789                      paramCount++;
1790                      lastParam = p;
1791                   }
1792                   p++;
1793                }               
1794             }
1795          }
1796          {
1797             int len = strlen(templateString);
1798             if(templateString[len-1] == '<')
1799                len--;
1800             else
1801             {
1802                if(templateString[len-1] == '>')
1803                   templateString[len++] = ' ';
1804                templateString[len++] = '>';
1805             }
1806             templateString[len++] = '\0';
1807          }
1808          {
1809             Context context = SetupTemplatesContext(_class);
1810             if(freeType) FreeType(type);
1811             type = ProcessTypeString(templateString, false);
1812             freeType = true;
1813             FinishTemplatesContext(context);
1814          }
1815       }
1816
1817       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1818       {
1819          ProcessExpressionType(member.initializer.exp);
1820          if(!member.initializer.exp.expType)
1821          {
1822             if(inCompiler)
1823             {
1824                char expString[10240];
1825                expString[0] = '\0';
1826                PrintExpression(member.initializer.exp, expString);
1827                ChangeCh(expString, '\n', ' ');
1828                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1829             }
1830          }
1831          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1832          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1833          {
1834             Compiler_Error($"incompatible instance method %s\n", ident.string);
1835          }
1836       }
1837       else if(member.initializer)
1838       {
1839          /*
1840          FreeType(member.exp.destType);
1841          member.exp.destType = type;
1842          if(member.exp.destType)
1843             member.exp.destType.refCount++;
1844          ProcessExpressionType(member.exp);
1845          */
1846
1847          ProcessInitializer(member.initializer, type);
1848       }
1849       if(freeType) FreeType(type);
1850    }
1851    else
1852    {
1853       if(_class && _class.type == unitClass)
1854       {
1855          if(member.initializer)
1856          {
1857             /*
1858             FreeType(member.exp.destType);
1859             member.exp.destType = MkClassType(_class.fullName);
1860             ProcessExpressionType(member.initializer, type);
1861             */
1862             Type type = MkClassType(_class.fullName);
1863             ProcessInitializer(member.initializer, type);
1864             FreeType(type);
1865          }
1866       }
1867       else
1868       {
1869          if(member.initializer)
1870          {
1871             //ProcessExpressionType(member.exp);
1872             ProcessInitializer(member.initializer, null);
1873          }
1874          if(ident)
1875          {
1876             if(method)
1877             {
1878                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1879             }
1880             else if(_class)
1881             {
1882                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1883                if(inCompiler)
1884                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1885             }
1886          }
1887          else if(_class)
1888             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1889       }
1890    }
1891 }
1892
1893 void ProcessInstantiationType(Instantiation inst)
1894 {
1895    yylloc = inst.loc;
1896    if(inst._class)
1897    {
1898       MembersInit members;
1899       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1900       Class _class;
1901       
1902       /*if(!inst._class.symbol)
1903          inst._class.symbol = FindClass(inst._class.name);*/
1904       classSym = inst._class.symbol;
1905       _class = classSym ? classSym.registered : null;
1906
1907       // DANGER: Patch for mutex not declaring its struct when not needed
1908       if(!_class || _class.type != noHeadClass)
1909          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1910
1911       afterExternal = afterExternal ? afterExternal : curExternal;
1912
1913       if(inst.exp)
1914          ProcessExpressionType(inst.exp);
1915
1916       inst.isConstant = true;
1917       if(inst.members)
1918       {
1919          DataMember curMember = null;
1920          Class curClass = null;
1921          DataMember subMemberStack[256];
1922          int subMemberStackPos = 0;
1923
1924          for(members = inst.members->first; members; members = members.next)
1925          {
1926             switch(members.type)
1927             {
1928                case methodMembersInit:
1929                {
1930                   char name[1024];
1931                   static uint instMethodID = 0;
1932                   External external = curExternal;
1933                   Context context = curContext;
1934                   Declarator declarator = members.function.declarator;
1935                   Identifier nameID = GetDeclId(declarator);
1936                   char * unmangled = nameID ? nameID.string : null;
1937                   Expression exp;
1938                   External createdExternal = null;
1939
1940                   if(inCompiler)
1941                   {
1942                      char number[16];
1943                      //members.function.dontMangle = true;
1944                      strcpy(name, "__ecereInstMeth_");
1945                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
1946                      strcat(name, "_");
1947                      strcat(name, nameID.string);
1948                      strcat(name, "_");
1949                      sprintf(number, "_%08d", instMethodID++);
1950                      strcat(name, number);                     
1951                      nameID.string = CopyString(name);
1952                   }
1953
1954                   // Do modifications here...
1955                   if(declarator)
1956                   {
1957                      Symbol symbol = declarator.symbol;
1958                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
1959                                     
1960                      if(method && method.type == virtualMethod)
1961                      {
1962                         symbol.method = method;
1963                         ProcessMethodType(method);
1964
1965                         if(!symbol.type.thisClass)
1966                         {
1967                            if(method.dataType.thisClass && currentClass && 
1968                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
1969                            {
1970                               if(!currentClass.symbol)
1971                                  currentClass.symbol = FindClass(currentClass.fullName);
1972                               symbol.type.thisClass = currentClass.symbol;
1973                            }
1974                            else
1975                            {
1976                               if(!_class.symbol)
1977                                  _class.symbol = FindClass(_class.fullName);
1978                               symbol.type.thisClass = _class.symbol;
1979                            }
1980                         }
1981                         // TESTING THIS HERE:
1982                         DeclareType(symbol.type, true, true);
1983
1984                      }
1985                      else if(classSym)
1986                      {
1987                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
1988                            unmangled, classSym.string);
1989                      }
1990                   }
1991
1992                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
1993                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
1994
1995                   if(nameID)
1996                   {
1997                      FreeSpecifier(nameID._class);
1998                      nameID._class = null;
1999                   }
2000
2001                   if(inCompiler)
2002                   {
2003
2004                      Type type = declarator.symbol.type;
2005                      External oldExternal = curExternal;
2006
2007                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2008                      // *** It was commented out for problems such as
2009                      /*
2010                            class VirtualDesktop : Window
2011                            {
2012                               clientSize = Size { };
2013                               Timer timer
2014                               {
2015                                  bool DelayExpired()
2016                                  {
2017                                     clientSize.w;
2018                                     return true;
2019                                  }
2020                               };
2021                            }
2022                      */
2023                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2024
2025                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2026
2027                      /*
2028                      if(strcmp(declarator.symbol.string, name))
2029                      {
2030                         printf("TOCHECK: Look out for this\n");
2031                         delete declarator.symbol.string;
2032                         declarator.symbol.string = CopyString(name);
2033                      }
2034                      
2035                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2036                      {
2037                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2038                         excludedSymbols->Remove(declarator.symbol);
2039                         globalContext.symbols.Add((BTNode)declarator.symbol);
2040                         if(strstr(declarator.symbol.string), "::")
2041                            globalContext.hasNameSpace = true;
2042
2043                      }
2044                      */
2045                   
2046                      //curExternal = curExternal.prev;
2047                      //afterExternal = afterExternal->next;
2048
2049                      //ProcessFunction(afterExternal->function);
2050
2051                      //curExternal = afterExternal;
2052                      {
2053                         External externalDecl;
2054                         externalDecl = MkExternalDeclaration(null);
2055                         ast->Insert(oldExternal.prev, externalDecl);
2056
2057                         // Which function does this process?
2058                         if(createdExternal.function)
2059                         {
2060                            ProcessFunction(createdExternal.function);
2061
2062                            //curExternal = oldExternal;
2063
2064                            {
2065                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2066
2067                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier), 
2068                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2069                      
2070                               //externalDecl = MkExternalDeclaration(decl);
2071                         
2072                               //***** ast->Insert(external.prev, externalDecl);
2073                               //ast->Insert(curExternal.prev, externalDecl);
2074                               externalDecl.declaration = decl;
2075                               if(decl.symbol && !decl.symbol.pointerExternal)
2076                                  decl.symbol.pointerExternal = externalDecl;
2077
2078                               // Trying this out...
2079                               declarator.symbol.pointerExternal = externalDecl;
2080                            }
2081                         }
2082                      }
2083                   }
2084                   else if(declarator)
2085                   {
2086                      curExternal = declarator.symbol.pointerExternal;
2087                      ProcessFunction((FunctionDefinition)members.function);
2088                   }
2089                   curExternal = external;
2090                   curContext = context;
2091
2092                   if(inCompiler)
2093                   {
2094                      FreeClassFunction(members.function);
2095
2096                      // In this pass, turn this into a MemberInitData
2097                      exp = QMkExpId(name);
2098                      members.type = dataMembersInit;
2099                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2100
2101                      delete unmangled;
2102                   }
2103                   break;
2104                }
2105                case dataMembersInit:
2106                {
2107                   if(members.dataMembers && classSym)
2108                   {
2109                      MemberInit member;
2110                      Location oldyyloc = yylloc;
2111                      for(member = members.dataMembers->first; member; member = member.next)
2112                      {
2113                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2114                         if(member.initializer && !member.initializer.isConstant)
2115                            inst.isConstant = false;
2116                      }
2117                      yylloc = oldyyloc;
2118                   }
2119                   break;
2120                }
2121             }
2122          }
2123       }
2124    }
2125 }
2126
2127 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2128 {
2129    // OPTIMIZATIONS: TESTING THIS...
2130    if(inCompiler)
2131    {
2132       if(type.kind == functionType)
2133       {
2134          Type param;
2135          if(declareParams)
2136          {
2137             for(param = type.params.first; param; param = param.next)
2138                DeclareType(param, declarePointers, true);
2139          }
2140          DeclareType(type.returnType, declarePointers, true);
2141       }
2142       else if(type.kind == pointerType && declarePointers)
2143          DeclareType(type.type, declarePointers, false);
2144       else if(type.kind == classType)
2145       {
2146          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2147             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2148       }
2149       else if(type.kind == structType || type.kind == unionType)
2150       {
2151          Type member;
2152          for(member = type.members.first; member; member = member.next)
2153             DeclareType(member, false, false);
2154       }
2155       else if(type.kind == arrayType)
2156          DeclareType(type.arrayType, declarePointers, false);
2157    }
2158 }
2159
2160 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2161 {
2162    ClassTemplateArgument * arg = null;
2163    int id = 0;
2164    ClassTemplateParameter curParam = null;
2165    Class sClass;
2166    for(sClass = _class; sClass; sClass = sClass.base)
2167    {
2168       id = 0;
2169       if(sClass.templateClass) sClass = sClass.templateClass;
2170       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2171       {
2172          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2173          {
2174             for(sClass = sClass.base; sClass; sClass = sClass.base)
2175             {
2176                if(sClass.templateClass) sClass = sClass.templateClass;
2177                id += sClass.templateParams.count;
2178             }
2179             break;
2180          }
2181          id++;
2182       }
2183       if(curParam) break;
2184    }
2185    if(curParam)
2186    {
2187       arg = &_class.templateArgs[id];
2188       if(arg && param.type == type)
2189          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2190    }
2191    return arg;
2192 }
2193
2194 public Context SetupTemplatesContext(Class _class)
2195 {
2196    Context context = PushContext();
2197    context.templateTypesOnly = true;
2198    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2199    {
2200       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2201       for(; param; param = param.next)
2202       {
2203          if(param.type == type && param.identifier)
2204          {
2205             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2206             curContext.templateTypes.Add((BTNode)type);
2207          }
2208       }
2209    }
2210    else if(_class)
2211    {
2212       Class sClass;
2213       for(sClass = _class; sClass; sClass = sClass.base)
2214       {
2215          ClassTemplateParameter p;
2216          for(p = sClass.templateParams.first; p; p = p.next)
2217          {
2218             //OldList * specs = MkList();
2219             //Declarator decl = null;
2220             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2221             if(p.type == type)
2222             {
2223                TemplateParameter param = p.param;
2224                TemplatedType type;
2225                if(!param)
2226                {
2227                   // ADD DATA TYPE HERE...
2228                   p.param = param = TemplateParameter
2229                   {
2230                      identifier = MkIdentifier(p.name), type = p.type, 
2231                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2232                   };
2233                }
2234                type = TemplatedType { key = (uintptr)p.name, param = param };
2235                curContext.templateTypes.Add((BTNode)type);
2236             }
2237          }
2238       }
2239    }
2240    return context;
2241 }
2242
2243 public void FinishTemplatesContext(Context context)
2244 {
2245    PopContext(context);
2246    FreeContext(context);
2247    delete context;
2248 }
2249
2250 public void ProcessMethodType(Method method)
2251 {
2252    if(!method.dataType)
2253    {
2254       Context context = SetupTemplatesContext(method._class);
2255
2256       method.dataType = ProcessTypeString(method.dataTypeString, false);
2257
2258       FinishTemplatesContext(context);
2259
2260       if(method.type != virtualMethod && method.dataType)
2261       {
2262          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2263          {
2264             if(!method._class.symbol)
2265                method._class.symbol = FindClass(method._class.fullName);
2266             method.dataType.thisClass = method._class.symbol;
2267          }
2268       }
2269
2270       // Why was this commented out? Working fine without now...
2271
2272       /*
2273       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2274          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2275          */
2276    }
2277
2278    /*
2279    if(type)
2280    {
2281       char * par = strstr(type, "(");
2282       char * classOp = null;
2283       int classOpLen = 0;
2284       if(par)
2285       {
2286          int c;
2287          for(c = par-type-1; c >= 0; c++)
2288          {
2289             if(type[c] == ':' && type[c+1] == ':')
2290             {
2291                classOp = type + c - 1;
2292                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2293                {
2294                   classOp--;
2295                   classOpLen++;
2296                }
2297                break;
2298             }
2299             else if(!isspace(type[c]))
2300                break;
2301          }
2302       }
2303       if(classOp)
2304       {
2305          char temp[1024];
2306          int typeLen = strlen(type);
2307          memcpy(temp, classOp, classOpLen);
2308          temp[classOpLen] = '\0';
2309          if(temp[0])
2310             _class = eSystem_FindClass(module, temp);
2311          else
2312             _class = null;
2313          method.dataTypeString = new char[typeLen - classOpLen + 1];
2314          memcpy(method.dataTypeString, type, classOp - type);
2315          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2316       }
2317       else
2318          method.dataTypeString = type;
2319    }
2320    */
2321 }
2322
2323
2324 public void ProcessPropertyType(Property prop)
2325 {
2326    if(!prop.dataType)
2327    {
2328       Context context = SetupTemplatesContext(prop._class);
2329       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2330       FinishTemplatesContext(context);
2331    }
2332 }
2333
2334 public void DeclareMethod(Method method, char * name)
2335 {
2336    Symbol symbol = method.symbol;
2337    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2338    {
2339       bool imported = false;
2340       bool dllImport = false;
2341
2342       if(!method.dataType)
2343          method.dataType = ProcessTypeString(method.dataTypeString, false);
2344
2345       if(!symbol || symbol._import || method.type == virtualMethod)
2346       {
2347          if(!symbol || method.type == virtualMethod)
2348          {
2349             Symbol classSym;
2350             if(!method._class.symbol)
2351                method._class.symbol = FindClass(method._class.fullName);
2352             classSym = method._class.symbol;
2353             if(!classSym._import)
2354             {
2355                ModuleImport module;
2356                
2357                if(method._class.module && method._class.module.name)
2358                   module = FindModule(method._class.module);
2359                else
2360                   module = mainModule;
2361                classSym._import = ClassImport
2362                {
2363                   name = CopyString(method._class.fullName);
2364                   isRemote = method._class.isRemote;
2365                };
2366                module.classes.Add(classSym._import);
2367             }
2368             if(!symbol)
2369             {
2370                symbol = method.symbol = Symbol { };
2371             }
2372             if(!symbol._import)
2373             {
2374                symbol._import = (ClassImport)MethodImport
2375                {
2376                   name = CopyString(method.name);
2377                   isVirtual = method.type == virtualMethod;
2378                };
2379                classSym._import.methods.Add(symbol._import);
2380             }
2381             if(!symbol)
2382             {
2383                // Set the symbol type
2384                /*
2385                if(!type.thisClass)
2386                {
2387                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2388                }
2389                else if(type.thisClass == (void *)-1)
2390                {
2391                   type.thisClass = null;
2392                }
2393                */
2394                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2395                symbol.type = method.dataType;
2396                if(symbol.type) symbol.type.refCount++;
2397             }
2398             /*
2399             if(!method.thisClass || strcmp(method.thisClass, "void"))
2400                symbol.type.params.Insert(null, 
2401                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2402             */
2403          }
2404          if(!method.dataType.dllExport)
2405          {
2406             imported = true;
2407             if(method._class.module != privateModule && method._class.module.importType != staticImport)
2408                dllImport = true;
2409          }
2410       }
2411
2412       /* MOVING THIS UP
2413       if(!method.dataType)
2414          method.dataType = ((Symbol)method.symbol).type;
2415          //ProcessMethodType(method);
2416       */
2417
2418       if(method.type != virtualMethod && method.dataType)
2419          DeclareType(method.dataType, true, true);
2420
2421       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2422       {
2423          // We need a declaration here :)
2424          Declaration decl;
2425          OldList * specifiers, * declarators;
2426          Declarator d;
2427          Declarator funcDecl;
2428          External external;
2429
2430          specifiers = MkList();
2431          declarators = MkList();
2432
2433          //if(imported)
2434          if(dllImport)
2435             ListAdd(specifiers, MkSpecifier(EXTERN));
2436          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2437             ListAdd(specifiers, MkSpecifier(STATIC));
2438
2439          if(method.type == virtualMethod)
2440          {
2441             ListAdd(specifiers, MkSpecifier(INT));
2442             d = MkDeclaratorIdentifier(MkIdentifier(name));
2443          }
2444          else
2445          {
2446             d = MkDeclaratorIdentifier(MkIdentifier(name));
2447             //if(imported)
2448             if(dllImport)
2449                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2450             {
2451                Context context = SetupTemplatesContext(method._class);
2452                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2453                FinishTemplatesContext(context);
2454             }
2455             funcDecl = GetFuncDecl(d);
2456
2457             if(dllImport)
2458             {
2459                Specifier spec, next;
2460                for(spec = specifiers->first; spec; spec = next)
2461                {
2462                   next = spec.next;
2463                   if(spec.type == extendedSpecifier)
2464                   {
2465                      specifiers->Remove(spec);
2466                      FreeSpecifier(spec);
2467                   }
2468                }
2469             }
2470
2471             // Add this parameter if not a static method
2472             if(method.dataType && !method.dataType.staticMethod)
2473             {
2474                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2475                {
2476                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2477                   TypeName thisParam = MkTypeName(MkListOne(
2478                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)), 
2479                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2480                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2481                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2482
2483                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2484                   {
2485                      TypeName param = funcDecl.function.parameters->first;
2486                      funcDecl.function.parameters->Remove(param);
2487                      FreeTypeName(param);
2488                   }
2489
2490                   if(!funcDecl.function.parameters)
2491                      funcDecl.function.parameters = MkList();
2492                   funcDecl.function.parameters->Insert(null, thisParam);
2493                }
2494             }
2495             // Make sure we don't have empty parameter declarations for static methods...
2496             /*
2497             else if(!funcDecl.function.parameters)
2498             {
2499                funcDecl.function.parameters = MkList();
2500                funcDecl.function.parameters->Insert(null, 
2501                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2502             }*/
2503          }
2504          // TESTING THIS:
2505          ProcessDeclarator(d);
2506
2507          ListAdd(declarators, MkInitDeclarator(d, null));
2508
2509          decl = MkDeclaration(specifiers, declarators);
2510
2511          ReplaceThisClassSpecifiers(specifiers, method._class);
2512
2513          // Keep a different symbol for the function definition than the declaration...
2514          if(symbol.pointerExternal)
2515          {
2516             Symbol functionSymbol { };
2517
2518             // Copy symbol
2519             {
2520                *functionSymbol = *symbol;
2521                functionSymbol.string = CopyString(symbol.string);
2522                if(functionSymbol.type)
2523                   functionSymbol.type.refCount++;
2524             }
2525
2526             excludedSymbols->Add(functionSymbol);
2527             symbol.pointerExternal.symbol = functionSymbol;
2528          }
2529          external = MkExternalDeclaration(decl);
2530          if(curExternal)
2531             ast->Insert(curExternal ? curExternal.prev : null, external);
2532          external.symbol = symbol;
2533          symbol.pointerExternal = external;
2534       }
2535       else if(ast)
2536       {
2537          // Move declaration higher...
2538          ast->Move(symbol.pointerExternal, curExternal.prev);
2539       }
2540
2541       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2542    }
2543 }
2544
2545 char * ReplaceThisClass(Class _class)
2546 {
2547    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2548    {
2549       bool first = true;
2550       int p = 0;
2551       ClassTemplateParameter param;
2552       int lastParam = -1;
2553       
2554       char className[1024];
2555       strcpy(className, _class.fullName);
2556       for(param = _class.templateParams.first; param; param = param.next)
2557       {
2558          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2559          {
2560             if(first) strcat(className, "<");
2561             if(!first) strcat(className, ", ");
2562             if(lastParam + 1 != p)
2563             {
2564                strcat(className, param.name);
2565                strcat(className, " = ");
2566             }
2567             strcat(className, param.name);
2568             first = false;
2569             lastParam = p;
2570          }
2571          p++;
2572       }
2573       if(!first)
2574       {
2575          int len = strlen(className);
2576          if(className[len-1] == '>') className[len++] = ' ';
2577          className[len++] = '>';
2578          className[len++] = '\0';
2579       }
2580       return CopyString(className);
2581    }
2582    else
2583       return CopyString(_class.fullName);   
2584 }
2585
2586 Type ReplaceThisClassType(Class _class)
2587 {
2588    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2589    {
2590       bool first = true;
2591       int p = 0;
2592       ClassTemplateParameter param;
2593       int lastParam = -1;
2594       char className[1024];
2595       strcpy(className, _class.fullName);
2596       
2597       for(param = _class.templateParams.first; param; param = param.next)
2598       {
2599          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2600          {
2601             if(first) strcat(className, "<");
2602             if(!first) strcat(className, ", ");
2603             if(lastParam + 1 != p)
2604             {
2605                strcat(className, param.name);
2606                strcat(className, " = ");
2607             }
2608             strcat(className, param.name);
2609             first = false;
2610             lastParam = p;
2611          }
2612          p++;
2613       }
2614       if(!first)
2615       {
2616          int len = strlen(className);
2617          if(className[len-1] == '>') className[len++] = ' ';
2618          className[len++] = '>';
2619          className[len++] = '\0';
2620       }
2621       return MkClassType(className);
2622       //return ProcessTypeString(className, false);
2623    }
2624    else
2625    {
2626       return MkClassType(_class.fullName);
2627       //return ProcessTypeString(_class.fullName, false);
2628    }
2629 }
2630
2631 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2632 {
2633    if(specs != null && _class)
2634    {
2635       Specifier spec;
2636       for(spec = specs.first; spec; spec = spec.next)
2637       {
2638          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2639          {
2640             spec.type = nameSpecifier;
2641             spec.name = ReplaceThisClass(_class);
2642             spec.symbol = FindClass(spec.name); //_class.symbol;
2643          }
2644       }
2645    }
2646 }
2647
2648 // Returns imported or not
2649 bool DeclareFunction(GlobalFunction function, char * name)
2650 {
2651    Symbol symbol = function.symbol;
2652    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2653    {
2654       bool imported = false;
2655       bool dllImport = false;
2656
2657       if(!function.dataType)
2658       {
2659          function.dataType = ProcessTypeString(function.dataTypeString, false);
2660          if(!function.dataType.thisClass)
2661             function.dataType.staticMethod = true;
2662       }
2663
2664       if(inCompiler)
2665       {
2666          if(!symbol)
2667          {
2668             ModuleImport module = FindModule(function.module);
2669             // WARNING: This is not added anywhere...
2670             symbol = function.symbol = Symbol {  };
2671
2672             if(module.name)
2673             {
2674                if(!function.dataType.dllExport)
2675                {
2676                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2677                   module.functions.Add(symbol._import);
2678                }
2679             }
2680             // Set the symbol type
2681             {
2682                symbol.type = ProcessTypeString(function.dataTypeString, false);
2683                if(!symbol.type.thisClass)
2684                   symbol.type.staticMethod = true;
2685             }
2686          }
2687          imported = symbol._import ? true : false;
2688          if(imported && function.module != privateModule && function.module.importType != staticImport)
2689             dllImport = true;
2690       }
2691
2692       DeclareType(function.dataType, true, true);
2693
2694       if(inCompiler)
2695       {
2696          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2697          {
2698             // We need a declaration here :)
2699             Declaration decl;
2700             OldList * specifiers, * declarators;
2701             Declarator d;
2702             Declarator funcDecl;
2703             External external;
2704
2705             specifiers = MkList();
2706             declarators = MkList();
2707
2708             //if(imported)
2709                ListAdd(specifiers, MkSpecifier(EXTERN));
2710             /*
2711             else
2712                ListAdd(specifiers, MkSpecifier(STATIC));
2713             */
2714
2715             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2716             //if(imported)
2717             if(dllImport)
2718                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2719
2720             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2721             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2722             if(function.module.importType == staticImport)
2723             {
2724                Specifier spec;
2725                for(spec = specifiers->first; spec; spec = spec.next)
2726                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2727                   {
2728                      specifiers->Remove(spec);
2729                      FreeSpecifier(spec);
2730                      break;
2731                   }
2732             }
2733
2734             funcDecl = GetFuncDecl(d);
2735
2736             // Make sure we don't have empty parameter declarations for static methods...
2737             if(funcDecl && !funcDecl.function.parameters)
2738             {
2739                funcDecl.function.parameters = MkList();
2740                funcDecl.function.parameters->Insert(null, 
2741                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2742             }
2743
2744             ListAdd(declarators, MkInitDeclarator(d, null));
2745
2746             {
2747                Context oldCtx = curContext;
2748                curContext = globalContext;
2749                decl = MkDeclaration(specifiers, declarators);
2750                curContext = oldCtx;
2751             }
2752
2753             // Keep a different symbol for the function definition than the declaration...
2754             if(symbol.pointerExternal)
2755             {
2756                Symbol functionSymbol { };
2757                // Copy symbol
2758                {
2759                   *functionSymbol = *symbol;
2760                   functionSymbol.string = CopyString(symbol.string);
2761                   if(functionSymbol.type)
2762                      functionSymbol.type.refCount++;
2763                }
2764
2765                excludedSymbols->Add(functionSymbol);
2766
2767                symbol.pointerExternal.symbol = functionSymbol;
2768             }
2769             external = MkExternalDeclaration(decl);
2770             if(curExternal)
2771                ast->Insert(curExternal.prev, external);
2772             external.symbol = symbol;
2773             symbol.pointerExternal = external;
2774          }
2775          else
2776          {
2777             // Move declaration higher...
2778             ast->Move(symbol.pointerExternal, curExternal.prev);
2779          }
2780
2781          if(curExternal)
2782             symbol.id = curExternal.symbol.idCode;
2783       }
2784    }
2785    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2786 }
2787
2788 void DeclareGlobalData(GlobalData data)
2789 {
2790    Symbol symbol = data.symbol;
2791    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2792    {
2793       if(inCompiler)
2794       {
2795          if(!symbol)
2796             symbol = data.symbol = Symbol { };
2797       }
2798       if(!data.dataType)
2799          data.dataType = ProcessTypeString(data.dataTypeString, false);
2800       DeclareType(data.dataType, true, true);
2801       if(inCompiler)
2802       {
2803          if(!symbol.pointerExternal)
2804          {
2805             // We need a declaration here :)
2806             Declaration decl;
2807             OldList * specifiers, * declarators;
2808             Declarator d;
2809             External external;
2810
2811             specifiers = MkList();
2812             declarators = MkList();
2813
2814             ListAdd(specifiers, MkSpecifier(EXTERN));
2815             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2816             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2817
2818             ListAdd(declarators, MkInitDeclarator(d, null));
2819
2820             decl = MkDeclaration(specifiers, declarators);
2821             external = MkExternalDeclaration(decl);
2822             if(curExternal)
2823                ast->Insert(curExternal.prev, external);
2824             external.symbol = symbol;
2825             symbol.pointerExternal = external;
2826          }
2827          else
2828          {
2829             // Move declaration higher...
2830             ast->Move(symbol.pointerExternal, curExternal.prev);
2831          }
2832
2833          if(curExternal)
2834             symbol.id = curExternal.symbol.idCode;
2835       }
2836    }
2837 }
2838
2839 class Conversion : struct
2840 {
2841    Conversion prev, next;
2842    Property convert;
2843    bool isGet;
2844    Type resultType;
2845 };
2846
2847 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2848 {
2849    if(source && dest)
2850    {
2851       // Property convert;
2852
2853       if(source.kind == templateType && dest.kind != templateType)
2854       {
2855          Type type = ProcessTemplateParameterType(source.templateParameter);
2856          if(type) source = type;
2857       }
2858
2859       if(dest.kind == templateType && source.kind != templateType)
2860       {
2861          Type type = ProcessTemplateParameterType(dest.templateParameter);
2862          if(type) dest = type;
2863       }
2864
2865       if((dest.classObjectType == typedObject && source.classObjectType != anyObject) || (dest.classObjectType == anyObject && source.classObjectType != typedObject))
2866       {
2867          return true;
2868       }
2869       
2870       if(source.classObjectType == anyObject && dest.classObjectType != typedObject)
2871       {
2872          return true;
2873       }
2874       
2875       if((dest.kind == structType && source.kind == structType) ||
2876          (dest.kind == unionType && source.kind == unionType))
2877       {
2878          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2879              (source.members.first && source.members.first == dest.members.first))
2880             return true;
2881       }
2882
2883       if(dest.kind == ellipsisType && source.kind != voidType)
2884          return true;
2885
2886       if(dest.kind == pointerType && dest.type.kind == voidType &&
2887          ((source.kind == classType && (!source._class || !source._class.registered || source._class.registered.type == structClass || source._class.registered.type == normalClass || source._class.registered.type == noHeadClass || source._class.registered.type == systemClass))
2888          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2889
2890          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2891       
2892          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2893          return true;
2894       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2895          ((dest.kind == classType && (!dest._class || !dest._class.registered || dest._class.registered.type == structClass || dest._class.registered.type == normalClass || dest._class.registered.type == noHeadClass || dest._class.registered.type == systemClass))
2896          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2897
2898          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2899
2900          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2901          return true;
2902
2903       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2904       {
2905          if(source._class.registered && source._class.registered.type == unitClass)
2906          {
2907             if(conversions != null)
2908             {
2909                if(source._class.registered == dest._class.registered)
2910                   return true;
2911             }
2912             else
2913             {
2914                Class sourceBase, destBase;
2915                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2916                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2917                if(sourceBase == destBase)
2918                   return true;
2919             }
2920          }
2921          // Don't match enum inheriting from other enum if resolving enumeration values
2922          // TESTING: !dest.classObjectType
2923          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2924             (enumBaseType || 
2925                (!source._class.registered || source._class.registered.type != enumClass) || 
2926                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2927             return true;
2928          else
2929          {
2930             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2931             if(enumBaseType && 
2932                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2933                source._class && source._class.registered && source._class.registered.type != enumClass)
2934             {
2935                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2936                {
2937                   return true;
2938                }
2939             }
2940          }
2941       }
2942
2943       // JUST ADDED THIS...
2944       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2945          return true;
2946
2947       if(doConversion)
2948       {
2949          // Just added this for Straight conversion of ColorAlpha => Color
2950          if(source.kind == classType)
2951          {
2952             Class _class;
2953             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
2954             {
2955                Property convert;
2956                for(convert = _class.conversions.first; convert; convert = convert.next)
2957                {
2958                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
2959                   {
2960                      Conversion after = (conversions != null) ? conversions.last : null;
2961
2962                      if(!convert.dataType)
2963                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
2964                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
2965                      {
2966                         if(!conversions && !convert.Get)
2967                            return true;
2968                         else if(conversions != null)
2969                         {
2970                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class && 
2971                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base && 
2972                               (dest.kind != classType || dest._class.registered != _class.base))
2973                               return true;
2974                            else
2975                            {
2976                               Conversion conv { convert = convert, isGet = true };
2977                               // conversions.Add(conv);
2978                               conversions.Insert(after, conv);
2979                               return true;
2980                            }
2981                         }
2982                      }
2983                   }
2984                }
2985             }
2986          }
2987
2988          // MOVING THIS??
2989
2990          if(dest.kind == classType)
2991          {
2992             Class _class;
2993             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
2994             {
2995                Property convert;
2996                for(convert = _class.conversions.first; convert; convert = convert.next)
2997                {
2998                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
2999                   {
3000                      // Conversion after = (conversions != null) ? conversions.last : null;
3001
3002                      if(!convert.dataType)
3003                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3004                      // Just added this equality check to prevent recursion.... Make it safer?
3005                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3006                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3007                      {
3008                         if(!conversions && !convert.Set)
3009                            return true;
3010                         else if(conversions != null)
3011                         {
3012                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class && 
3013                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base && 
3014                               (source.kind != classType || source._class.registered != _class.base))
3015                               return true;
3016                            else
3017                            {
3018                               // *** Testing this! ***
3019                               Conversion conv { convert = convert };
3020                               conversions.Add(conv);
3021                               //conversions.Insert(after, conv);
3022                               return true;
3023                            }
3024                         }
3025                      }
3026                   }
3027                }
3028             }
3029             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3030             {
3031                if(source.kind != voidType && source.kind != structType && source.kind != unionType && 
3032                   (source.kind != classType || source._class.registered.type != structClass))
3033                   return true;
3034             }*/
3035
3036             // TESTING THIS... IS THIS OK??
3037             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3038             {
3039                if(!dest._class.registered.dataType)
3040                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3041                // Only support this for classes...
3042                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3043                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3044                {
3045                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3046                   {
3047                      return true;
3048                   }
3049                }
3050             }
3051          }
3052
3053          // Moved this lower
3054          if(source.kind == classType)
3055          {
3056             Class _class;
3057             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3058             {
3059                Property convert;
3060                for(convert = _class.conversions.first; convert; convert = convert.next)
3061                {
3062                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3063                   {
3064                      Conversion after = (conversions != null) ? conversions.last : null;
3065
3066                      if(!convert.dataType)
3067                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3068                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3069                      {
3070                         if(!conversions && !convert.Get)
3071                            return true;
3072                         else if(conversions != null)
3073                         {
3074                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class && 
3075                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base && 
3076                               (dest.kind != classType || dest._class.registered != _class.base))
3077                               return true;
3078                            else
3079                            {
3080                               Conversion conv { convert = convert, isGet = true };
3081
3082                               // conversions.Add(conv);
3083                               conversions.Insert(after, conv);
3084                               return true;
3085                            }
3086                         }
3087                      }
3088                   }
3089                }
3090             }
3091
3092             // TESTING THIS... IS THIS OK??
3093             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3094             {
3095                if(!source._class.registered.dataType)
3096                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3097                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3098                {
3099                   return true;
3100                }
3101             }
3102          }
3103       }
3104
3105       if(source.kind == classType || source.kind == subClassType)
3106          ;
3107       else if(dest.kind == source.kind && 
3108          (dest.kind != structType && dest.kind != unionType &&
3109           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3110           return true;
3111       // RECENTLY ADDED THESE
3112       else if(dest.kind == doubleType && source.kind == floatType)
3113          return true;
3114       else if(dest.kind == shortType && source.kind == charType)
3115          return true;
3116       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == intSizeType /* Exception here for size_t */))
3117          return true;
3118       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3119          return true;
3120       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3121          return true;
3122       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3123          return true;
3124       else if(source.kind == enumType &&
3125          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3126           return true;
3127       else if(dest.kind == enumType &&
3128          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3129           return true;
3130       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && 
3131               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3132       {
3133          Type paramSource, paramDest;
3134
3135          if(dest.kind == methodType)     
3136             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3137          if(source.kind == methodType)   
3138             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3139
3140          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3141          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3142          if(dest.kind == methodType) 
3143             dest = dest.method.dataType;
3144          if(source.kind == methodType) 
3145             source = source.method.dataType;
3146
3147          paramSource = source.params.first;
3148          if(paramSource && paramSource.kind == voidType) paramSource = null;
3149          paramDest = dest.params.first;
3150          if(paramDest && paramDest.kind == voidType) paramDest = null;
3151
3152      
3153          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) && 
3154             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3155          {
3156             // Source thisClass must be derived from destination thisClass
3157             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3158                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3159             {
3160                if(paramDest && paramDest.kind == classType)
3161                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3162                else
3163                   Compiler_Error($"method class should not take an object\n");
3164                return false;
3165             }
3166             paramDest = paramDest.next;
3167          }
3168          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3169          {
3170             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3171             {
3172                if(dest.thisClass)
3173                {
3174                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3175                   {
3176                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3177                      return false;
3178                   }
3179                }
3180                else
3181                {
3182                   // THIS WAS BACKWARDS:
3183                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3184                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3185                   {
3186                      if(owningClassDest)
3187                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3188                      else
3189                         Compiler_Error($"overriding class expected to be derived from method class\n");      
3190                      return false;
3191                   }
3192                }
3193                paramSource = paramSource.next;
3194             }
3195             else
3196             {
3197                if(dest.thisClass)
3198                {
3199                   // Source thisClass must be derived from destination thisClass
3200                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3201                   {
3202                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3203                      return false;
3204                   }
3205                }
3206                else
3207                {
3208                   // THIS WAS BACKWARDS TOO??
3209                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3210                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3211                   {
3212                      //if(owningClass)
3213                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3214                      //else
3215                         //Compiler_Error($"overriding class expected to be derived from method class\n");      
3216                      return false;
3217                   }
3218                }
3219             }
3220          }
3221
3222
3223          // Source return type must be derived from destination return type
3224          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3225          {
3226             Compiler_Warning($"incompatible return type for function\n");
3227             return false;
3228          }
3229
3230          // Check parameters
3231       
3232          for(; paramDest; paramDest = paramDest.next)
3233          {
3234             if(!paramSource)
3235             {
3236                //Compiler_Warning($"not enough parameters\n");
3237                Compiler_Error($"not enough parameters\n");
3238                return false;
3239             }
3240             {
3241                Type paramDestType = paramDest;
3242                Type paramSourceType = paramSource;
3243                Type type = paramDestType;
3244
3245                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3246                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3247                   paramSource.kind != templateType)
3248                {
3249                   int id = 0;
3250                   ClassTemplateParameter curParam = null;
3251                   Class sClass;
3252                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3253                   {
3254                      id = 0;
3255                      if(sClass.templateClass) sClass = sClass.templateClass;
3256                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3257                      {
3258                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3259                         {
3260                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3261                            {
3262                               if(sClass.templateClass) sClass = sClass.templateClass;
3263                               id += sClass.templateParams.count;
3264                            }
3265                            break;
3266                         }
3267                         id++;
3268                      }
3269                      if(curParam) break;
3270                   }
3271
3272                   if(curParam)
3273                   {
3274                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3275                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3276                   }
3277                }
3278
3279                // paramDest must be derived from paramSource
3280                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) && 
3281                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3282                {
3283                   char type[1024];
3284                   type[0] = 0;
3285                   PrintType(paramDest, type, false, true);
3286                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3287                   
3288                   if(paramDestType != paramDest)
3289                      FreeType(paramDestType);
3290                   return false;
3291                }
3292                if(paramDestType != paramDest)
3293                   FreeType(paramDestType);
3294             }
3295          
3296             paramSource = paramSource.next;
3297          }
3298          if(paramSource)
3299          {
3300             Compiler_Error($"too many parameters\n");
3301             return false;
3302          }
3303          return true;
3304       }
3305       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3306       {
3307          return true;
3308       }
3309       else if((dest.kind == pointerType || dest.kind == arrayType) && 
3310          (source.kind == arrayType || source.kind == pointerType))
3311       {
3312          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3313             return true;
3314       }
3315    }
3316    return false;
3317 }
3318
3319 static void FreeConvert(Conversion convert)
3320 {
3321    if(convert.resultType)
3322       FreeType(convert.resultType);
3323 }
3324
3325 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest, 
3326                               char * string, OldList conversions)
3327 {
3328    BTNamedLink link;
3329
3330    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3331    {
3332       Class _class = link.data;
3333       if(_class.type == enumClass)
3334       {
3335          OldList converts { };
3336          Type type { };
3337          type.kind = classType;
3338
3339          if(!_class.symbol)
3340             _class.symbol = FindClass(_class.fullName);
3341          type._class = _class.symbol;
3342
3343          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3344          {
3345             NamedLink value;
3346             Class enumClass = eSystem_FindClass(privateModule, "enum");
3347             if(enumClass)
3348             {
3349                Class baseClass;
3350                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3351                {
3352                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3353                   for(value = e.values.first; value; value = value.next)
3354                   {
3355                      if(!strcmp(value.name, string))
3356                         break;
3357                   }
3358                   if(value)
3359                   {
3360                      FreeExpContents(sourceExp);
3361                      FreeType(sourceExp.expType);
3362
3363                      sourceExp.isConstant = true;
3364                      sourceExp.expType = MkClassType(baseClass.fullName);
3365                      //if(inCompiler)
3366                      {
3367                         char constant[256];
3368                         sourceExp.type = constantExp;
3369                         if(!strcmp(baseClass.dataTypeString, "int"))
3370                            sprintf(constant, "%d",value.data);
3371                         else
3372                            sprintf(constant, "0x%X",value.data);
3373                         sourceExp.constant = CopyString(constant);
3374                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3375                      }
3376                   
3377                      while(converts.first)
3378                      {
3379                         Conversion convert = converts.first;
3380                         converts.Remove(convert);
3381                         conversions.Add(convert);
3382                      }
3383                      delete type;
3384                      return true;
3385                   }
3386                }
3387             }
3388          }
3389          if(converts.first)
3390             converts.Free(FreeConvert);
3391          delete type;
3392       }
3393    }
3394    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3395       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3396          return true;
3397    return false;
3398 }
3399
3400 public bool ModuleVisibility(Module searchIn, Module searchFor)
3401 {
3402    SubModule subModule;
3403    
3404    if(searchFor == searchIn)
3405       return true;
3406
3407    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3408    {
3409       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3410       {
3411          if(ModuleVisibility(subModule.module, searchFor))
3412             return true;
3413       }
3414    }
3415    return false;
3416 }
3417
3418 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3419 {
3420    Module module;
3421
3422    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3423       return true;
3424    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3425       return true;
3426    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3427       return true;
3428
3429    for(module = mainModule.application.allModules.first; module; module = module.next)
3430    {
3431       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3432          return true;
3433    }
3434    return false;
3435 }
3436
3437 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3438 {
3439    Type source = sourceExp.expType;
3440    Type realDest = dest;
3441    Type backupSourceExpType = null;
3442
3443    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3444       return true;
3445
3446    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3447    {
3448        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3449        {
3450           Class sourceBase, destBase;
3451           for(sourceBase = source._class.registered; 
3452               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3453               sourceBase = sourceBase.base);
3454           for(destBase = dest._class.registered; 
3455               destBase && destBase.base && destBase.base.type != systemClass;
3456               destBase = destBase.base);
3457           //if(source._class.registered == dest._class.registered)
3458           if(sourceBase == destBase)
3459              return true;
3460        }
3461    }
3462
3463    if(source)
3464    {
3465       OldList * specs;
3466       bool flag = false;
3467       int64 value = MAXINT;
3468
3469       source.refCount++;
3470       dest.refCount++;
3471
3472       if(sourceExp.type == constantExp)
3473       {
3474          if(source.isSigned)
3475             value = strtoll(sourceExp.constant, null, 0);
3476          else
3477             value = strtoull(sourceExp.constant, null, 0);
3478       }
3479       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3480       {
3481          if(source.isSigned)
3482             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3483          else
3484             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3485       }
3486
3487       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered && 
3488          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3489       {
3490          FreeType(source);
3491          source = Type { kind = intType, isSigned = false, refCount = 1 };
3492       }
3493
3494       if(dest.kind == classType)
3495       {
3496          Class _class = dest._class ? dest._class.registered : null;
3497
3498          if(_class && _class.type == unitClass)
3499          {
3500             if(source.kind != classType)
3501             {
3502                Type tempType { };
3503                Type tempDest, tempSource;
3504
3505                for(; _class.base.type != systemClass; _class = _class.base);
3506                tempSource = dest;
3507                tempDest = tempType;
3508
3509                tempType.kind = classType;
3510                if(!_class.symbol)
3511                   _class.symbol = FindClass(_class.fullName);
3512
3513                tempType._class = _class.symbol;
3514                tempType.truth = dest.truth;
3515                if(tempType._class)
3516                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3517
3518                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3519                backupSourceExpType = sourceExp.expType;
3520                sourceExp.expType = dest; dest.refCount++;
3521                //sourceExp.expType = MkClassType(_class.fullName);
3522                flag = true;            
3523
3524                delete tempType;
3525             }
3526          }
3527
3528
3529          // Why wasn't there something like this?
3530          if(_class && _class.type == bitClass && source.kind != classType)
3531          {
3532             if(!dest._class.registered.dataType)
3533                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3534             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3535             {
3536                FreeType(source);
3537                FreeType(sourceExp.expType);
3538                source = sourceExp.expType = MkClassType(dest._class.string);
3539                source.refCount++;
3540                
3541                //source.kind = classType;
3542                //source._class = dest._class;
3543             }
3544          }
3545
3546          // Adding two enumerations
3547          /*
3548          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3549          {
3550             if(!source._class.registered.dataType)
3551                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3552             if(!dest._class.registered.dataType)
3553                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3554
3555             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3556             {
3557                FreeType(source);
3558                source = sourceExp.expType = MkClassType(dest._class.string);
3559                source.refCount++;
3560                
3561                //source.kind = classType;
3562                //source._class = dest._class;
3563             }
3564          }*/
3565
3566          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3567          {
3568             OldList * specs = MkList();
3569             Declarator decl;
3570             char string[1024];
3571
3572             ReadString(string, sourceExp.string);
3573             decl = SpecDeclFromString(string, specs, null);
3574
3575             FreeExpContents(sourceExp);
3576             FreeType(sourceExp.expType);
3577
3578             sourceExp.type = classExp;
3579             sourceExp._classExp.specifiers = specs;
3580             sourceExp._classExp.decl = decl;
3581             sourceExp.expType = dest;
3582             dest.refCount++;
3583
3584             FreeType(source);
3585             FreeType(dest);
3586             if(backupSourceExpType) FreeType(backupSourceExpType);
3587             return true;
3588          }
3589       }
3590       else if(source.kind == classType)
3591       {
3592          Class _class = source._class ? source._class.registered : null;
3593
3594          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3595          {
3596             /*
3597             if(dest.kind != classType)
3598             {
3599                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3600                if(!source._class.registered.dataType)
3601                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3602                
3603                FreeType(dest);
3604                dest = MkClassType(source._class.string);
3605                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3606                //   dest = MkClassType(source._class.string);
3607             }
3608             */
3609             
3610             if(dest.kind != classType)
3611             {
3612                Type tempType { };
3613                Type tempDest, tempSource;
3614
3615                if(!source._class.registered.dataType)
3616                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3617
3618                for(; _class.base.type != systemClass; _class = _class.base);
3619                tempDest = source;
3620                tempSource = tempType;
3621                tempType.kind = classType;
3622                tempType._class = FindClass(_class.fullName);
3623                tempType.truth = source.truth;
3624                tempType.classObjectType = source.classObjectType;
3625
3626                if(tempType._class)
3627                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3628                
3629                // PUT THIS BACK TESTING UNITS?
3630                if(conversions.last)
3631                {
3632                   ((Conversion)(conversions.last)).resultType = dest;
3633                   dest.refCount++;
3634                }
3635                
3636                FreeType(sourceExp.expType);
3637                sourceExp.expType = MkClassType(_class.fullName);
3638                sourceExp.expType.truth = source.truth;
3639                sourceExp.expType.classObjectType = source.classObjectType;
3640
3641                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3642
3643                if(!sourceExp.destType)
3644                {
3645                   FreeType(sourceExp.destType);
3646                   sourceExp.destType = sourceExp.expType;
3647                   if(sourceExp.expType)
3648                      sourceExp.expType.refCount++;
3649                }
3650                //flag = true;
3651                //source = _class.dataType;
3652
3653
3654                // TOCHECK: TESTING THIS NEW CODE
3655                if(!_class.dataType)
3656                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3657                FreeType(dest);
3658                dest = MkClassType(source._class.string);
3659                dest.truth = source.truth;
3660                dest.classObjectType = source.classObjectType;
3661                
3662                FreeType(source);
3663                source = _class.dataType;
3664                source.refCount++;
3665
3666                delete tempType;
3667             }
3668          }
3669       }
3670
3671       if(!flag)
3672       {
3673          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3674          {
3675             FreeType(source);
3676             FreeType(dest);
3677             return true;
3678          }
3679       }
3680
3681       // Implicit Casts
3682       /*
3683       if(source.kind == classType)
3684       {
3685          Class _class = source._class.registered;
3686          if(_class.type == unitClass)
3687          {
3688             if(!_class.dataType)
3689                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3690             source = _class.dataType;
3691          }
3692       }*/
3693
3694       if(dest.kind == classType)
3695       {
3696          Class _class = dest._class ? dest._class.registered : null;
3697          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || 
3698             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3699          {
3700             if(_class.type == normalClass || _class.type == noHeadClass)
3701             {
3702                Expression newExp { };
3703                *newExp = *sourceExp;
3704                if(sourceExp.destType) sourceExp.destType.refCount++;
3705                if(sourceExp.expType)  sourceExp.expType.refCount++;
3706                sourceExp.type = castExp;
3707                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3708                sourceExp.cast.exp = newExp;
3709                FreeType(sourceExp.expType);
3710                sourceExp.expType = null;
3711                ProcessExpressionType(sourceExp);
3712
3713                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3714                if(!inCompiler)
3715                {
3716                   FreeType(sourceExp.expType);
3717                   sourceExp.expType = dest;
3718                }
3719
3720                FreeType(source);
3721                if(inCompiler) FreeType(dest);
3722
3723                if(backupSourceExpType) FreeType(backupSourceExpType);
3724                return true;
3725             }
3726
3727             if(!_class.dataType)
3728                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3729             FreeType(dest);
3730             dest = _class.dataType;
3731             dest.refCount++;
3732          }
3733
3734          // Accept lower precision types for units, since we want to keep the unit type
3735          if(dest.kind == doubleType && 
3736             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3737              source.kind == charType))
3738          {
3739             specs = MkListOne(MkSpecifier(DOUBLE));
3740          }
3741          else if(dest.kind == floatType && 
3742             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3743             source.kind == doubleType))
3744          {
3745             specs = MkListOne(MkSpecifier(FLOAT));
3746          }
3747          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3748             source.kind == floatType || source.kind == doubleType))
3749          {
3750             specs = MkList();
3751             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3752             ListAdd(specs, MkSpecifier(INT64));
3753          }
3754          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3755             source.kind == floatType || source.kind == doubleType))
3756          {
3757             specs = MkList();
3758             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3759             ListAdd(specs, MkSpecifier(INT));
3760          }
3761          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == intType ||
3762             source.kind == floatType || source.kind == doubleType))
3763          {
3764             specs = MkList();
3765             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3766             ListAdd(specs, MkSpecifier(SHORT));
3767          }
3768          else if(dest.kind == charType && (source.kind == charType || source.kind == shortType || source.kind == intType ||
3769             source.kind == floatType || source.kind == doubleType))
3770          {
3771             specs = MkList();
3772             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3773             ListAdd(specs, MkSpecifier(CHAR));
3774          }
3775          else
3776          {
3777             FreeType(source);
3778             FreeType(dest);
3779             if(backupSourceExpType)
3780             {
3781                // Failed to convert: revert previous exp type
3782                if(sourceExp.expType) FreeType(sourceExp.expType);
3783                sourceExp.expType = backupSourceExpType;
3784             }
3785             return false;
3786          }
3787       }
3788       else if(dest.kind == doubleType && 
3789          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3790           source.kind == charType))
3791       {
3792          specs = MkListOne(MkSpecifier(DOUBLE));
3793       }
3794       else if(dest.kind == floatType && 
3795          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType))
3796       {
3797          specs = MkListOne(MkSpecifier(FLOAT));
3798       }
3799       else if(dest.kind == charType && (source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) && 
3800          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3801       {
3802          specs = MkList();
3803          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3804          ListAdd(specs, MkSpecifier(CHAR));
3805       }
3806       else if(dest.kind == shortType && (source.kind == enumType || source.kind == charType || source.kind == shortType || 
3807          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3808       {
3809          specs = MkList();
3810          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3811          ListAdd(specs, MkSpecifier(SHORT));
3812       }
3813       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == charType || source.kind == intType))
3814       {
3815          specs = MkList();
3816          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3817          ListAdd(specs, MkSpecifier(INT));
3818       }
3819       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3820       {
3821          specs = MkList();
3822          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3823          ListAdd(specs, MkSpecifier(INT64));
3824       }
3825       else if(dest.kind == enumType && 
3826          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType))
3827       {
3828          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3829       }
3830       else
3831       {
3832          FreeType(source);
3833          FreeType(dest);
3834          if(backupSourceExpType)
3835          {
3836             // Failed to convert: revert previous exp type
3837             if(sourceExp.expType) FreeType(sourceExp.expType);
3838             sourceExp.expType = backupSourceExpType;
3839          }
3840          return false;
3841       }
3842
3843       if(!flag)
3844       {
3845          Expression newExp { };
3846          *newExp = *sourceExp;
3847          newExp.prev = null;
3848          newExp.next = null;
3849          if(sourceExp.destType) sourceExp.destType.refCount++;
3850          if(sourceExp.expType)  sourceExp.expType.refCount++;
3851
3852          sourceExp.type = castExp;
3853          if(realDest.kind == classType)
3854          {
3855             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3856             FreeList(specs, FreeSpecifier);
3857          }
3858          else
3859             sourceExp.cast.typeName = MkTypeName(specs, null);
3860          if(newExp.type == opExp)
3861          {
3862             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3863          }
3864          else
3865             sourceExp.cast.exp = newExp;
3866          
3867          FreeType(sourceExp.expType);
3868          sourceExp.expType = null;
3869          ProcessExpressionType(sourceExp);
3870       }
3871       else
3872          FreeList(specs, FreeSpecifier);
3873
3874       FreeType(dest);
3875       FreeType(source);
3876       if(backupSourceExpType) FreeType(backupSourceExpType);
3877
3878       return true;
3879    }
3880    else
3881    {
3882       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3883       if(sourceExp.type == identifierExp)
3884       {
3885          Identifier id = sourceExp.identifier;
3886          if(dest.kind == classType)
3887          {
3888             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3889             {
3890                Class _class = dest._class.registered;
3891                Class enumClass = eSystem_FindClass(privateModule, "enum");
3892                if(enumClass)
3893                {
3894                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3895                   {
3896                      NamedLink value;
3897                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3898                      for(value = e.values.first; value; value = value.next)
3899                      {
3900                         if(!strcmp(value.name, id.string))
3901                            break;
3902                      }
3903                      if(value)
3904                      {
3905                         FreeExpContents(sourceExp);
3906                         FreeType(sourceExp.expType);
3907
3908                         sourceExp.isConstant = true;
3909                         sourceExp.expType = MkClassType(_class.fullName);
3910                         //if(inCompiler)
3911                         {
3912                            char constant[256];
3913                            sourceExp.type = constantExp;
3914                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3915                               sprintf(constant, "%d",value.data);
3916                            else
3917                               sprintf(constant, "0x%X",value.data);
3918                            sourceExp.constant = CopyString(constant);
3919                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3920                         }
3921                         return true;
3922                      }
3923                   }
3924                }
3925             }
3926          }
3927
3928          // Loop through all enum classes
3929          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3930             return true;
3931       }
3932    }
3933    return false;
3934 }
3935
3936 #define TERTIARY(o, name, m, t, p) \
3937    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
3938    {                                                              \
3939       exp.type = constantExp;                                    \
3940       exp.string = p(op1.m ? op2.m : op3.m);                     \
3941       if(!exp.expType) \
3942          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3943       return true;                                                \
3944    }
3945
3946 #define BINARY(o, name, m, t, p) \
3947    static bool name(Expression exp, Operand op1, Operand op2)   \
3948    {                                                              \
3949       t value2 = op2.m;                                           \
3950       exp.type = constantExp;                                    \
3951       exp.string = p(op1.m o value2);                     \
3952       if(!exp.expType) \
3953          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3954       return true;                                                \
3955    }
3956
3957 #define BINARY_DIVIDE(o, name, m, t, p) \
3958    static bool name(Expression exp, Operand op1, Operand op2)   \
3959    {                                                              \
3960       t value2 = op2.m;                                           \
3961       exp.type = constantExp;                                    \
3962       exp.string = p(value2 ? (op1.m o value2) : 0);             \
3963       if(!exp.expType) \
3964          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3965       return true;                                                \
3966    }
3967
3968 #define UNARY(o, name, m, t, p) \
3969    static bool name(Expression exp, Operand op1)                \
3970    {                                                              \
3971       exp.type = constantExp;                                    \
3972       exp.string = p(o op1.m);                                   \
3973       if(!exp.expType) \
3974          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3975       return true;                                                \
3976    }
3977
3978 #define OPERATOR_ALL(macro, o, name) \
3979    macro(o, Int##name, i, int, PrintInt) \
3980    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
3981    macro(o, Short##name, s, short, PrintShort) \
3982    macro(o, UShort##name, us, unsigned short, PrintUShort) \
3983    macro(o, Char##name, c, char, PrintChar) \
3984    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
3985    macro(o, Float##name, f, float, PrintFloat) \
3986    macro(o, Double##name, d, double, PrintDouble)
3987
3988 #define OPERATOR_INTTYPES(macro, o, name) \
3989    macro(o, Int##name, i, int, PrintInt) \
3990    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
3991    macro(o, Short##name, s, short, PrintShort) \
3992    macro(o, UShort##name, us, unsigned short, PrintUShort) \
3993    macro(o, Char##name, c, char, PrintChar) \
3994    macro(o, UChar##name, uc, unsigned char, PrintUChar)
3995
3996
3997 // binary arithmetic
3998 OPERATOR_ALL(BINARY, +, Add)
3999 OPERATOR_ALL(BINARY, -, Sub)
4000 OPERATOR_ALL(BINARY, *, Mul)
4001 OPERATOR_ALL(BINARY_DIVIDE, /, Div)
4002 OPERATOR_INTTYPES(BINARY_DIVIDE, %, Mod)
4003
4004 // unary arithmetic
4005 OPERATOR_ALL(UNARY, -, Neg)
4006
4007 // unary arithmetic increment and decrement
4008 OPERATOR_ALL(UNARY, ++, Inc)
4009 OPERATOR_ALL(UNARY, --, Dec)
4010
4011 // binary arithmetic assignment
4012 OPERATOR_ALL(BINARY, =, Asign)
4013 OPERATOR_ALL(BINARY, +=, AddAsign)
4014 OPERATOR_ALL(BINARY, -=, SubAsign)
4015 OPERATOR_ALL(BINARY, *=, MulAsign)
4016 OPERATOR_ALL(BINARY_DIVIDE, /=, DivAsign)
4017 OPERATOR_INTTYPES(BINARY_DIVIDE, %=, ModAsign)
4018
4019 // binary bitwise
4020 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4021 OPERATOR_INTTYPES(BINARY, |, BitOr)
4022 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4023 OPERATOR_INTTYPES(BINARY, <<, LShift)
4024 OPERATOR_INTTYPES(BINARY, >>, RShift)
4025
4026 // unary bitwise
4027 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4028
4029 // binary bitwise assignment
4030 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4031 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4032 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4033 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4034 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4035
4036 // unary logical negation
4037 OPERATOR_INTTYPES(UNARY, !, Not)
4038
4039 // binary logical equality
4040 OPERATOR_ALL(BINARY, ==, Equ)
4041 OPERATOR_ALL(BINARY, !=, Nqu)
4042
4043 // binary logical
4044 OPERATOR_ALL(BINARY, &&, And)
4045 OPERATOR_ALL(BINARY, ||, Or)
4046
4047 // binary logical relational
4048 OPERATOR_ALL(BINARY, >, Grt)
4049 OPERATOR_ALL(BINARY, <, Sma)
4050 OPERATOR_ALL(BINARY, >=, GrtEqu)
4051 OPERATOR_ALL(BINARY, <=, SmaEqu)
4052
4053 // tertiary condition operator
4054 OPERATOR_ALL(TERTIARY, ?, Cond)
4055
4056 //Add, Sub, Mul, Div, Mod,     , Neg,     Inc, Dec,    Asign, AddAsign, SubAsign, MulAsign, DivAsign, ModAsign,     BitAnd, BitOr, BitXor, LShift, RShift, BitNot,     AndAsign, OrAsign, XorAsign, LShiftAsign, RShiftAsign,     Not,     Equ, Nqu,     And, Or,     Grt, Sma, GrtEqu, SmaEqu
4057 #define OPERATOR_TABLE_ALL(name, type) \
4058     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4059                           type##Neg, \
4060                           type##Inc, type##Dec, \
4061                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4062                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4063                           type##BitNot, \
4064                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4065                           type##Not, \
4066                           type##Equ, type##Nqu, \
4067                           type##And, type##Or, \
4068                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4069                         }; \
4070
4071 #define OPERATOR_TABLE_INTTYPES(name, type) \
4072     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4073                           type##Neg, \
4074                           type##Inc, type##Dec, \
4075                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4076                           null, null, null, null, null, \
4077                           null, \
4078                           null, null, null, null, null, \
4079                           null, \
4080                           type##Equ, type##Nqu, \
4081                           type##And, type##Or, \
4082                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4083                         }; \
4084
4085 OPERATOR_TABLE_ALL(int, Int)
4086 OPERATOR_TABLE_ALL(uint, UInt)
4087 OPERATOR_TABLE_ALL(short, Short)
4088 OPERATOR_TABLE_ALL(ushort, UShort)
4089 OPERATOR_TABLE_INTTYPES(float, Float)
4090 OPERATOR_TABLE_INTTYPES(double, Double)
4091 OPERATOR_TABLE_ALL(char, Char)
4092 OPERATOR_TABLE_ALL(uchar, UChar)
4093
4094 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4095 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4096 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4097 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4098 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4099 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4100 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4101 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4102
4103 public void ReadString(char * output,  char * string)
4104 {
4105    int len = strlen(string);
4106    int c,d = 0;
4107    bool quoted = false, escaped = false;
4108    for(c = 0; c<len; c++)
4109    {
4110       char ch = string[c];
4111       if(escaped)
4112       {
4113          switch(ch)
4114          {
4115             case 'n': output[d] = '\n'; break;
4116             case 't': output[d] = '\t'; break;
4117             case 'a': output[d] = '\a'; break;
4118             case 'b': output[d] = '\b'; break;
4119             case 'f': output[d] = '\f'; break;
4120             case 'r': output[d] = '\r'; break;
4121             case 'v': output[d] = '\v'; break;
4122             case '\\': output[d] = '\\'; break;
4123             case '\"': output[d] = '\"'; break;
4124             default: output[d++] = '\\'; output[d] = ch;
4125             //default: output[d] = ch;
4126          }
4127          d++;
4128          escaped = false;
4129       }
4130       else 
4131       {
4132          if(ch == '\"') 
4133             quoted ^= true;
4134          else if(quoted)
4135          {
4136             if(ch == '\\')
4137                escaped = true;
4138             else
4139                output[d++] = ch;
4140          }
4141       }
4142    }
4143    output[d] = '\0';
4144 }
4145
4146 public Operand GetOperand(Expression exp)
4147 {
4148    Operand op { };
4149    Type type = exp.expType;
4150    if(type)
4151    {
4152       while(type.kind == classType && 
4153          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4154       {
4155          if(!type._class.registered.dataType)
4156             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4157          type = type._class.registered.dataType;
4158          
4159       }
4160       op.kind = type.kind;
4161       op.type = exp.expType;
4162       if(exp.isConstant && exp.type == constantExp)
4163       {
4164          switch(op.kind)
4165          {
4166             case charType:
4167             {
4168                if(exp.constant[0] == '\'')
4169                   op.c = exp.constant[1];
4170                else if(type.isSigned)
4171                {
4172                   op.c = (char)strtol(exp.constant, null, 0);
4173                   op.ops = charOps;
4174                }
4175                else
4176                {
4177                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4178                   op.ops = ucharOps;
4179                }
4180                break;
4181             }
4182             case shortType:
4183                if(type.isSigned)
4184                {
4185                   op.s = (short)strtol(exp.constant, null, 0);
4186                   op.ops = shortOps;
4187                }
4188                else
4189                {
4190                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4191                   op.ops = ushortOps;
4192                }
4193                break;
4194             case intType:
4195             case longType:
4196                if(type.isSigned)
4197                {
4198                   op.i = (int)strtol(exp.constant, null, 0);
4199                   op.ops = intOps;
4200                }
4201                else
4202                {
4203                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4204                   op.ops = uintOps;
4205                }
4206                op.kind = intType;
4207                break;
4208             case int64Type:
4209                if(type.isSigned)
4210                {
4211                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4212                   op.ops = intOps;
4213                }
4214                else
4215                {
4216                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4217                   op.ops = uintOps;
4218                }
4219                op.kind = intType;
4220                break;
4221             case intPtrType:
4222                if(type.isSigned)
4223                {
4224                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4225                   op.ops = intOps;
4226                }
4227                else
4228                {
4229                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4230                   op.ops = uintOps;
4231                }
4232                op.kind = intType;
4233                break;
4234             case intSizeType:
4235                if(type.isSigned)
4236                {
4237                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4238                   op.ops = intOps;
4239                }
4240                else
4241                {
4242                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4243                   op.ops = uintOps;
4244                }
4245                op.kind = intType;
4246                break;
4247             case floatType:
4248                op.f = (float)strtod(exp.constant, null);
4249                op.ops = floatOps;
4250                break;
4251             case doubleType:
4252                op.d = (double)strtod(exp.constant, null);
4253                op.ops = doubleOps;
4254                break;
4255             //case classType:    For when we have operator overloading...
4256             // Pointer additions
4257             //case functionType:
4258             case arrayType:
4259             case pointerType:
4260             case classType:
4261                op.ui64 = _strtoui64(exp.constant, null, 0);
4262                op.kind = pointerType;
4263                op.ops = uintOps;
4264                // op.ptrSize = 
4265                break;
4266          }
4267       }
4268    }
4269    return op;
4270 }
4271
4272 static void UnusedFunction()
4273 {
4274    int a;
4275    a.OnGetString(0,0,0);
4276 }
4277 default:
4278 extern int __ecereVMethodID_class_OnGetString;
4279 public:
4280
4281 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4282 {
4283    DataMember dataMember;
4284    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4285    {
4286       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4287          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4288       else
4289       {
4290          Expression exp { };
4291          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4292          Type type;
4293          void * ptr = inst.data + dataMember.offset + offset;
4294          char * result = null;
4295          exp.loc = member.loc = inst.loc;
4296          ((Identifier)member.identifiers->first).loc = inst.loc;
4297
4298          if(!dataMember.dataType)
4299             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4300          type = dataMember.dataType;
4301          if(type.kind == classType)
4302          {
4303             Class _class = type._class.registered;
4304             if(_class.type == enumClass)
4305             {
4306                Class enumClass = eSystem_FindClass(privateModule, "enum");
4307                if(enumClass)
4308                {
4309                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4310                   NamedLink item;
4311                   for(item = e.values.first; item; item = item.next)
4312                   {
4313                      if((int)item.data == *(int *)ptr)
4314                      {
4315                         result = item.name;
4316                         break;
4317                      }
4318                   }
4319                   if(result)
4320                   {
4321                      exp.identifier = MkIdentifier(result);
4322                      exp.type = identifierExp;
4323                      exp.destType = MkClassType(_class.fullName);
4324                      ProcessExpressionType(exp);
4325                   }
4326                }
4327             }
4328             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4329             {
4330                if(!_class.dataType)
4331                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4332                type = _class.dataType;
4333             }
4334          }
4335          if(!result)
4336          {
4337             switch(type.kind)
4338             {
4339                case floatType:
4340                {
4341                   FreeExpContents(exp);
4342
4343                   exp.constant = PrintFloat(*(float*)ptr);
4344                   exp.type = constantExp;
4345                   break;
4346                }
4347                case doubleType:
4348                {
4349                   FreeExpContents(exp);
4350
4351                   exp.constant = PrintDouble(*(double*)ptr);
4352                   exp.type = constantExp;
4353                   break;
4354                }
4355                case intType:
4356                {
4357                   FreeExpContents(exp);
4358
4359                   exp.constant = PrintInt(*(int*)ptr);
4360                   exp.type = constantExp;
4361                   break;
4362                }
4363                case int64Type:
4364                {
4365                   FreeExpContents(exp);
4366
4367                   exp.constant = PrintInt64(*(int64*)ptr);
4368                   exp.type = constantExp;
4369                   break;
4370                }
4371                case intPtrType:
4372                {
4373                   FreeExpContents(exp);
4374                   // TODO: This should probably use proper type
4375                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4376                   exp.type = constantExp;
4377                   break;
4378                }
4379                case intSizeType:
4380                {
4381                   FreeExpContents(exp);
4382                   // TODO: This should probably use proper type
4383                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4384                   exp.type = constantExp;
4385                   break;
4386                }
4387                default:
4388                   Compiler_Error($"Unhandled type populating instance\n");
4389             }
4390          }
4391          ListAdd(memberList, member);
4392       }
4393
4394       if(parentDataMember.type == unionMember)
4395          break;
4396    }
4397 }
4398
4399 void PopulateInstance(Instantiation inst)
4400 {
4401    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4402    Class _class = classSym.registered;
4403    DataMember dataMember;
4404    OldList * memberList = MkList();
4405    inst.members = MkListOne(MkMembersInitList(memberList));
4406    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4407    {
4408       if(!dataMember.isProperty)
4409       {
4410          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4411             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4412          else
4413          {
4414             Expression exp { };
4415             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4416             Type type;
4417             void * ptr = inst.data + dataMember.offset;
4418             char * result = null;
4419
4420             exp.loc = member.loc = inst.loc;
4421             ((Identifier)member.identifiers->first).loc = inst.loc;
4422
4423             if(!dataMember.dataType)
4424                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4425             type = dataMember.dataType;
4426             if(type.kind == classType)
4427             {
4428                Class _class = type._class.registered;
4429                if(_class.type == enumClass)
4430                {
4431                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4432                   if(enumClass)
4433                   {
4434                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4435                      NamedLink item;
4436                      for(item = e.values.first; item; item = item.next)
4437                      {
4438                         if((int)item.data == *(int *)ptr)
4439                         {
4440                            result = item.name;
4441                            break;
4442                         }
4443                      }
4444                   }
4445                   if(result)
4446                   {
4447                      exp.identifier = MkIdentifier(result);
4448                      exp.type = identifierExp;
4449                      exp.destType = MkClassType(_class.fullName);
4450                      ProcessExpressionType(exp);
4451                   }                     
4452                }
4453                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4454                {
4455                   if(!_class.dataType)
4456                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4457                   type = _class.dataType;
4458                }
4459             }
4460             if(!result)
4461             {
4462                switch(type.kind)
4463                {
4464                   case floatType:
4465                   {
4466                      exp.constant = PrintFloat(*(float*)ptr);
4467                      exp.type = constantExp;
4468                      break;
4469                   }
4470                   case doubleType:
4471                   {
4472                      exp.constant = PrintDouble(*(double*)ptr);
4473                      exp.type = constantExp;
4474                      break;
4475                   }
4476                   case intType:
4477                   {
4478                      exp.constant = PrintInt(*(int*)ptr);
4479                      exp.type = constantExp;
4480                      break;
4481                   }
4482                   case int64Type:
4483                   {
4484                      exp.constant = PrintInt64(*(int64*)ptr);
4485                      exp.type = constantExp;
4486                      break;
4487                   }
4488                   case intPtrType:
4489                   {
4490                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4491                      exp.type = constantExp;
4492                      break;
4493                   }
4494                   default:
4495                      Compiler_Error($"Unhandled type populating instance\n");
4496                }
4497             }
4498             ListAdd(memberList, member);
4499          }
4500       }
4501    }
4502 }
4503
4504 void ComputeInstantiation(Expression exp)
4505 {
4506    Instantiation inst = exp.instance;
4507    MembersInit members;
4508    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4509    Class _class = classSym ? classSym.registered : null;
4510    DataMember curMember = null;
4511    Class curClass = null;
4512    DataMember subMemberStack[256];
4513    int subMemberStackPos = 0;
4514    uint64 bits = 0;
4515
4516    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4517    {
4518       // Don't recompute the instantiation... 
4519       // Non Simple classes will have become constants by now
4520       if(inst.data) 
4521          return;
4522
4523       if(_class.type == normalClass || _class.type == noHeadClass)
4524          inst.data = (byte *)eInstance_New(_class);
4525       else
4526          inst.data = new0 byte[_class.structSize];
4527    }
4528
4529    if(inst.members)
4530    {
4531       for(members = inst.members->first; members; members = members.next)
4532       {
4533          switch(members.type)
4534          {
4535             case dataMembersInit:
4536             {
4537                if(members.dataMembers)
4538                {
4539                   MemberInit member;
4540                   for(member = members.dataMembers->first; member; member = member.next)
4541                   {
4542                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4543                      bool found = false;
4544
4545                      Property prop = null;
4546                      DataMember dataMember = null;
4547                      Method method = null;
4548                      uint dataMemberOffset;
4549
4550                      if(!ident)
4551                      {
4552                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4553                         if(curMember)
4554                         {
4555                            if(curMember.isProperty)
4556                               prop = (Property)curMember;
4557                            else
4558                            {
4559                               dataMember = curMember;
4560                               
4561                               // CHANGED THIS HERE
4562                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4563                               // dataMemberOffset = dataMember.offset;
4564                            }
4565                            found = true;
4566                         }
4567                      }
4568                      else
4569                      {
4570                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4571                         if(prop)
4572                         {
4573                            found = true;
4574                            if(prop.memberAccess == publicAccess)
4575                            {
4576                               curMember = (DataMember)prop;
4577                               curClass = prop._class;
4578                            }
4579                         }
4580                         else
4581                         {
4582                            DataMember _subMemberStack[256];
4583                            int _subMemberStackPos = 0;
4584
4585                            // FILL MEMBER STACK
4586                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4587
4588                            if(dataMember)
4589                            {
4590                               found = true;
4591                               if(dataMember.memberAccess == publicAccess)
4592                               {
4593                                  curMember = dataMember;
4594                                  curClass = dataMember._class;
4595                                  memcpy(subMemberStack, _subMemberStack, sizeof(int) * _subMemberStackPos);
4596                                  subMemberStackPos = _subMemberStackPos;
4597                               }
4598                            }
4599                         }
4600                      }
4601
4602                      if(found && member.initializer && member.initializer.type == expInitializer)
4603                      {
4604                         Expression value = member.initializer.exp;
4605                         Type type = null;
4606                         if(prop)
4607                         {
4608                            type = prop.dataType;
4609                         }
4610                         else if(dataMember)
4611                         {
4612                            if(!dataMember.dataType)
4613                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4614                            
4615                            type = dataMember.dataType;
4616                         }
4617
4618                         if(ident && ident.next)
4619                         {
4620                            // for(; ident && type; ident = ident.next)
4621                            for(ident = ident.next; ident && type; ident = ident.next)
4622                            {
4623                               if(type.kind == classType)
4624                               {
4625                                  prop = eClass_FindProperty(type._class.registered,
4626                                     ident.string, privateModule);
4627                                  if(prop)
4628                                     type = prop.dataType;
4629                                  else
4630                                  {
4631                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered, 
4632                                        ident.string, &dataMemberOffset, privateModule, null, null);
4633                                     if(dataMember)
4634                                        type = dataMember.dataType;
4635                                  }
4636                               }
4637                               else if(type.kind == structType || type.kind == unionType)
4638                               {
4639                                  Type memberType;
4640                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4641                                  {
4642                                     if(!strcmp(memberType.name, ident.string))
4643                                     {
4644                                        type = memberType;
4645                                        break;
4646                                     }
4647                                  }
4648                               }
4649                            }
4650                         }
4651                         if(value)
4652                         {
4653                            FreeType(value.destType);
4654                            value.destType = type;
4655                            if(type) type.refCount++;
4656                            ComputeExpression(value);
4657                         }
4658                         if(value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4659                         {
4660                            if(type.kind == classType)
4661                            {
4662                               Class _class = type._class.registered;
4663                               if(_class.type == bitClass || _class.type == unitClass ||
4664                                  _class.type == enumClass)
4665                               {
4666                                  if(!_class.dataType)
4667                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4668                                  type = _class.dataType;
4669                               }
4670                            }
4671
4672                            if(dataMember)
4673                            {
4674                               void * ptr = inst.data + dataMemberOffset;
4675                               
4676                               if(value.type == constantExp)
4677                               {
4678                                  switch(type.kind)
4679                                  {
4680                                     case intType:
4681                                     {
4682                                        GetInt(value, (int*)ptr);
4683                                        break;
4684                                     }
4685                                     case int64Type:
4686                                     {
4687                                        GetInt64(value, (int64*)ptr);
4688                                        break;
4689                                     }
4690                                     case intPtrType:
4691                                     {
4692                                        GetIntPtr(value, (intptr*)ptr);
4693                                        break;
4694                                     }
4695                                     case intSizeType:
4696                                     {
4697                                        GetIntSize(value, (intsize*)ptr);
4698                                        break;
4699                                     }
4700                                     case floatType:
4701                                     {
4702                                        GetFloat(value, (float*)ptr);
4703                                        break;
4704                                     }
4705                                     case doubleType:
4706                                     {
4707                                        GetDouble(value, (double *)ptr);
4708                                        break;
4709                                     }
4710                                  }
4711                               }
4712                               else if(value.type == instanceExp)
4713                               {
4714                                  if(type.kind == classType)
4715                                  {
4716                                     Class _class = type._class.registered;
4717                                     if(_class.type == structClass)
4718                                     {
4719                                        ComputeTypeSize(type);
4720                                        if(value.instance.data)
4721                                           memcpy(ptr, value.instance.data, type.size);
4722                                     }
4723                                  }
4724                               }
4725                            }
4726                            else if(prop)
4727                            {
4728                               if(value.type == instanceExp && value.instance.data)
4729                               {
4730                                  void (*Set)(void *, void *) = (void *)prop.Set;
4731                                  Set(inst.data, value.instance.data);
4732                                  PopulateInstance(inst);
4733                               }
4734                               else if(value.type == constantExp)
4735                               {
4736                                  switch(type.kind)
4737                                  {
4738                                     case doubleType:
4739                                     {
4740                                        void (*Set)(void *, double) = (void *)prop.Set;
4741                                        Set(inst.data, strtod(value.constant, null) );
4742                                        break;
4743                                     }
4744                                     case floatType:
4745                                     {
4746                                        void (*Set)(void *, float) = (void *)prop.Set;
4747                                        Set(inst.data, (float)(strtod(value.constant, null)));
4748                                        break;
4749                                     }
4750                                     case intType:
4751                                     {
4752                                        void (*Set)(void *, int) = (void *)prop.Set;
4753                                        Set(inst.data, strtol(value.constant, null, 0));
4754                                        break;
4755                                     }
4756                                     case int64Type:
4757                                     {
4758                                        void (*Set)(void *, int64) = (void *)prop.Set;
4759                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4760                                        break;
4761                                     }
4762                                     case intPtrType:
4763                                     {
4764                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4765                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4766                                        break;
4767                                     }
4768                                     case intSizeType:
4769                                     {
4770                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4771                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4772                                        break;
4773                                     }
4774                                  }
4775                               }
4776                               else if(value.type == stringExp)
4777                               {
4778                                  char temp[1024];
4779                                  ReadString(temp, value.string);
4780                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4781                               }
4782                            }
4783                         }
4784                         else if(_class.type == unitClass)
4785                         {
4786                            if(prop)
4787                            {
4788                               // Only support converting units to units for now...
4789                               if(value.type == constantExp)
4790                               {
4791                                  if(type.kind == classType)
4792                                  {
4793                                     Class _class = type._class.registered;
4794                                     if(_class.type == unitClass)
4795                                     {
4796                                        if(!_class.dataType)
4797                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4798                                        type = _class.dataType;
4799                                     }
4800                                  }
4801                                  // TODO: Assuming same base type for units...
4802                                  switch(type.kind)
4803                                  {
4804                                     case floatType:
4805                                     {
4806                                        float fValue;
4807                                        float (*Set)(float) = (void *)prop.Set;
4808                                        GetFloat(member.initializer.exp, &fValue);
4809                                        exp.constant = PrintFloat(Set(fValue));
4810                                        exp.type = constantExp;
4811                                        break;
4812                                     }
4813                                     case doubleType:
4814                                     {
4815                                        double dValue;
4816                                        double (*Set)(double) = (void *)prop.Set;
4817                                        GetDouble(member.initializer.exp, &dValue);
4818                                        exp.constant = PrintDouble(Set(dValue));
4819                                        exp.type = constantExp;
4820                                        break;
4821                                     }
4822                                  }
4823                               }
4824                            }
4825                         }
4826                         else if(_class.type == bitClass)
4827                         {
4828                            if(prop)
4829                            {
4830                               if(value.type == instanceExp && value.instance.data)
4831                               {
4832                                  unsigned int (*Set)(void *) = (void *)prop.Set;
4833                                  bits = Set(value.instance.data);
4834                               }
4835                               else if(value.type == constantExp)
4836                               {
4837                               }
4838                            }
4839                            else if(dataMember)
4840                            {
4841                               BitMember bitMember = (BitMember) dataMember;
4842                               Type type;
4843                               int part = 0;
4844                               GetInt(value, &part);
4845                               bits = (bits & ~bitMember.mask);
4846                               if(!bitMember.dataType)
4847                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
4848
4849                               type = bitMember.dataType;
4850
4851                               if(type.kind == classType && type._class && type._class.registered)
4852                               {
4853                                  if(!type._class.registered.dataType)
4854                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);                                    
4855                                  type = type._class.registered.dataType;
4856                               }
4857
4858                               switch(type.kind)
4859                               {
4860                                  case charType:
4861                                     if(type.isSigned)
4862                                        bits |= ((char)part << bitMember.pos);
4863                                     else
4864                                        bits |= ((unsigned char)part << bitMember.pos);
4865                                     break;
4866                                  case shortType:
4867                                     if(type.isSigned)
4868                                        bits |= ((short)part << bitMember.pos);
4869                                     else
4870                                        bits |= ((unsigned short)part << bitMember.pos);
4871                                     break;
4872                                  case intType:
4873                                  case longType:
4874                                     if(type.isSigned)
4875                                        bits |= ((int)part << bitMember.pos);
4876                                     else
4877                                        bits |= ((unsigned int)part << bitMember.pos);
4878                                     break;
4879                                  case int64Type:
4880                                     if(type.isSigned)
4881                                        bits |= ((int64)part << bitMember.pos);
4882                                     else
4883                                        bits |= ((uint64)part << bitMember.pos);
4884                                     break;
4885                                  case intPtrType:
4886                                     if(type.isSigned)
4887                                     {
4888                                        bits |= ((intptr)part << bitMember.pos);
4889                                     }
4890                                     else
4891                                     {
4892                                        bits |= ((uintptr)part << bitMember.pos);
4893                                     }
4894                                     break;
4895                                  case intSizeType:
4896                                     if(type.isSigned)
4897                                     {
4898                                        bits |= ((ssize_t)(intsize)part << bitMember.pos);
4899                                     }
4900                                     else
4901                                     {
4902                                        bits |= ((size_t) (uintsize)part << bitMember.pos);
4903                                     }
4904                                     break;
4905                               }
4906                            }
4907                         }
4908                      }
4909                      else
4910                      {
4911                         if(_class && _class.type == unitClass)
4912                         {
4913                            ComputeExpression(member.initializer.exp);
4914                            exp.constant = member.initializer.exp.constant;
4915                            exp.type = constantExp;
4916                            
4917                            member.initializer.exp.constant = null;
4918                         }
4919                      }
4920                   }
4921                }
4922                break;
4923             }
4924          }
4925       }
4926    }
4927    if(_class && _class.type == bitClass)
4928    {
4929       exp.constant = PrintHexUInt(bits);
4930       exp.type = constantExp;
4931    }
4932    if(exp.type != instanceExp)
4933    {
4934       FreeInstance(inst);
4935    }
4936 }
4937
4938 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
4939 {
4940    if(exp.op.op == SIZEOF)
4941    {
4942       FreeExpContents(exp);
4943       exp.type = constantExp;
4944       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
4945    }
4946    else
4947    {
4948       if(!exp.op.exp1)
4949       {
4950          switch(exp.op.op)
4951          {
4952             // unary arithmetic
4953             case '+':
4954             {
4955                // Provide default unary +
4956                Expression exp2 = exp.op.exp2;
4957                exp.op.exp2 = null;
4958                FreeExpContents(exp);
4959                FreeType(exp.expType);
4960                FreeType(exp.destType);
4961                *exp = *exp2;
4962                delete exp2;
4963                break;
4964             }
4965             case '-':
4966                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
4967                break;
4968             // unary arithmetic increment and decrement
4969                   //OPERATOR_ALL(UNARY, ++, Inc)
4970                   //OPERATOR_ALL(UNARY, --, Dec)
4971             // unary bitwise
4972             case '~':
4973                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
4974                break;
4975             // unary logical negation
4976             case '!':
4977                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
4978                break;
4979          }
4980       }
4981       else
4982       {
4983          switch(exp.op.op)
4984          {
4985             // binary arithmetic
4986             case '+':
4987                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
4988                break;
4989             case '-':
4990                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
4991                break;
4992             case '*':
4993                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
4994                break;
4995             case '/':
4996                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
4997                break;
4998             case '%':
4999                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5000                break;
5001             // binary arithmetic assignment
5002                   //OPERATOR_ALL(BINARY, =, Asign)
5003                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5004                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5005                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5006                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5007                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5008             // binary bitwise
5009             case '&':
5010                if(exp.op.exp2)
5011                {
5012                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5013                }
5014                break;
5015             case '|':
5016                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5017                break;
5018             case '^':
5019                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5020                break;
5021             case LEFT_OP:
5022                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5023                break;
5024             case RIGHT_OP:
5025                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5026                break;
5027             // binary bitwise assignment
5028                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5029                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5030                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5031                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5032                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5033             // binary logical equality
5034             case EQ_OP:
5035                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5036                break;
5037             case NE_OP:
5038                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5039                break;
5040             // binary logical
5041             case AND_OP:
5042                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5043                break;
5044             case OR_OP:
5045                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5046                break;
5047             // binary logical relational
5048             case '>':
5049                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5050                break;
5051             case '<':
5052                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5053                break;
5054             case GE_OP:
5055                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5056                break;
5057             case LE_OP:
5058                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5059                break;
5060          }
5061       }
5062    }
5063 }
5064
5065 void ComputeExpression(Expression exp)
5066 {
5067    char expString[10240];
5068    expString[0] = '\0';
5069 #ifdef _DEBUG
5070    PrintExpression(exp, expString);
5071 #endif
5072
5073    switch(exp.type)
5074    {
5075       case instanceExp:
5076       {
5077          ComputeInstantiation(exp);
5078          break;
5079       }
5080       /*
5081       case constantExp:
5082          break;
5083       */
5084       case opExp:
5085       {
5086          Expression exp1, exp2 = null;
5087          Operand op1 { };
5088          Operand op2 { };
5089
5090          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5091          if(exp.op.exp2)
5092             ComputeExpression(exp.op.exp2);
5093          if(exp.op.exp1)
5094          {
5095             ComputeExpression(exp.op.exp1);
5096             exp1 = exp.op.exp1;
5097             exp2 = exp.op.exp2;
5098             op1 = GetOperand(exp1);
5099             if(op1.type) op1.type.refCount++;
5100             if(exp2)
5101             {
5102                op2 = GetOperand(exp2);
5103                if(op2.type) op2.type.refCount++;
5104             }
5105          }
5106          else 
5107          {
5108             exp1 = exp.op.exp2;
5109             op1 = GetOperand(exp1);
5110             if(op1.type) op1.type.refCount++;
5111          }
5112
5113          CallOperator(exp, exp1, exp2, op1, op2);
5114          /*
5115          switch(exp.op.op)
5116          {
5117             // Unary operators
5118             case '&':
5119                // Also binary
5120                if(exp.op.exp1 && exp.op.exp2)
5121                {
5122                   // Binary And
5123                   if(op1.ops.BitAnd)
5124                   {
5125                      FreeExpContents(exp);
5126                      op1.ops.BitAnd(exp, op1, op2);
5127                   }
5128                }
5129                break;
5130             case '*':
5131                if(exp.op.exp1)
5132                {
5133                   if(op1.ops.Mul)
5134                   {
5135                      FreeExpContents(exp);
5136                      op1.ops.Mul(exp, op1, op2);
5137                   }
5138                }
5139                break;
5140             case '+':
5141                if(exp.op.exp1)
5142                {
5143                   if(op1.ops.Add)
5144                   {
5145                      FreeExpContents(exp);
5146                      op1.ops.Add(exp, op1, op2);
5147                   }
5148                }
5149                else
5150                {
5151                   // Provide default unary +
5152                   Expression exp2 = exp.op.exp2;
5153                   exp.op.exp2 = null;
5154                   FreeExpContents(exp);
5155                   FreeType(exp.expType);
5156                   FreeType(exp.destType);
5157
5158                   *exp = *exp2;
5159                   delete exp2;
5160                }
5161                break;
5162             case '-':
5163                if(exp.op.exp1)
5164                {
5165                   if(op1.ops.Sub) 
5166                   {
5167                      FreeExpContents(exp);
5168                      op1.ops.Sub(exp, op1, op2);
5169                   }
5170                }
5171                else
5172                {
5173                   if(op1.ops.Neg) 
5174                   {
5175                      FreeExpContents(exp);
5176                      op1.ops.Neg(exp, op1);
5177                   }
5178                }
5179                break;
5180             case '~':
5181                if(op1.ops.BitNot)
5182                {
5183                   FreeExpContents(exp);
5184                   op1.ops.BitNot(exp, op1);
5185                }
5186                break;
5187             case '!':
5188                if(op1.ops.Not)
5189                {
5190                   FreeExpContents(exp);
5191                   op1.ops.Not(exp, op1);
5192                }
5193                break;
5194             // Binary only operators
5195             case '/':
5196                if(op1.ops.Div) 
5197                {
5198                   FreeExpContents(exp);
5199                   op1.ops.Div(exp, op1, op2);
5200                }
5201                break;
5202             case '%':
5203                if(op1.ops.Mod)
5204                {
5205                   FreeExpContents(exp);
5206                   op1.ops.Mod(exp, op1, op2);
5207                }
5208                break;
5209             case LEFT_OP:
5210                break;
5211             case RIGHT_OP:
5212                break;
5213             case '<':
5214                if(exp.op.exp1)
5215                {
5216                   if(op1.ops.Sma)
5217                   {
5218                      FreeExpContents(exp);
5219                      op1.ops.Sma(exp, op1, op2);
5220                   }
5221                }
5222                break;
5223             case '>':
5224                if(exp.op.exp1)
5225                {
5226                   if(op1.ops.Grt)
5227                   {
5228                      FreeExpContents(exp);
5229                      op1.ops.Grt(exp, op1, op2);
5230                   }
5231                }
5232                break;
5233             case LE_OP:
5234                if(exp.op.exp1)
5235                {
5236                   if(op1.ops.SmaEqu)
5237                   {
5238                      FreeExpContents(exp);
5239                      op1.ops.SmaEqu(exp, op1, op2);
5240                   }
5241                }
5242                break;
5243             case GE_OP:
5244                if(exp.op.exp1)
5245                {
5246                   if(op1.ops.GrtEqu)
5247                   {
5248                      FreeExpContents(exp);
5249                      op1.ops.GrtEqu(exp, op1, op2);
5250                   }
5251                }
5252                break;
5253             case EQ_OP:
5254                if(exp.op.exp1)
5255                {
5256                   if(op1.ops.Equ)
5257                   {
5258                      FreeExpContents(exp);
5259                      op1.ops.Equ(exp, op1, op2);
5260                   }
5261                }
5262                break;
5263             case NE_OP:
5264                if(exp.op.exp1)
5265                {
5266                   if(op1.ops.Nqu)
5267                   {
5268                      FreeExpContents(exp);
5269                      op1.ops.Nqu(exp, op1, op2);
5270                   }
5271                }
5272                break;
5273             case '|':
5274                if(op1.ops.BitOr) 
5275                {
5276                   FreeExpContents(exp);
5277                   op1.ops.BitOr(exp, op1, op2);
5278                }
5279                break;
5280             case '^':
5281                if(op1.ops.BitXor) 
5282                {
5283                   FreeExpContents(exp);
5284                   op1.ops.BitXor(exp, op1, op2);
5285                }
5286                break;
5287             case AND_OP:
5288                break;
5289             case OR_OP:
5290                break;
5291             case SIZEOF:
5292                FreeExpContents(exp);
5293                exp.type = constantExp;
5294                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5295                break;
5296          }
5297          */
5298          if(op1.type) FreeType(op1.type);
5299          if(op2.type) FreeType(op2.type);
5300          break;
5301       }
5302       case bracketsExp:
5303       case extensionExpressionExp:
5304       {
5305          Expression e, n;
5306          for(e = exp.list->first; e; e = n)
5307          {
5308             n = e.next;
5309             if(!n)
5310             {
5311                OldList * list = exp.list;
5312                ComputeExpression(e);
5313                //FreeExpContents(exp);
5314                FreeType(exp.expType);
5315                FreeType(exp.destType);
5316                *exp = *e;
5317                delete e;
5318                delete list;
5319             }
5320             else
5321             {
5322                FreeExpression(e);
5323             }
5324          }
5325          break;
5326       }
5327       /*
5328
5329       case ExpIndex:
5330       {
5331          Expression e;
5332          exp.isConstant = true;
5333
5334          ComputeExpression(exp.index.exp);
5335          if(!exp.index.exp.isConstant)
5336             exp.isConstant = false;
5337
5338          for(e = exp.index.index->first; e; e = e.next)
5339          {
5340             ComputeExpression(e);
5341             if(!e.next)
5342             {
5343                // Check if this type is int
5344             }
5345             if(!e.isConstant)
5346                exp.isConstant = false;
5347          }
5348          exp.expType = Dereference(exp.index.exp.expType);
5349          break;
5350       }
5351       */
5352       case memberExp:
5353       {
5354          Expression memberExp = exp.member.exp;
5355          Identifier memberID = exp.member.member;
5356
5357          Type type;
5358          ComputeExpression(exp.member.exp);
5359          type = exp.member.exp.expType;
5360          if(type)
5361          {
5362             Class _class = (exp.member.member && exp.member.member.classSym) ? exp.member.member.classSym.registered : (((type.kind == classType || type.kind == subClassType) && type._class) ? type._class.registered : null);
5363             Property prop = null;
5364             DataMember member = null;
5365             Class convertTo = null;
5366             if(type.kind == subClassType && exp.member.exp.type == classExp)
5367                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5368
5369             if(!_class)
5370             {
5371                char string[256];
5372                Symbol classSym;
5373                string[0] = '\0';
5374                PrintType(type, string, false, true);
5375                classSym = FindClass(string);
5376                _class = classSym ? classSym.registered : null;
5377             }
5378
5379             if(exp.member.member)
5380             {
5381                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5382                if(!prop)
5383                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5384             }
5385             if(!prop && !member && _class && exp.member.member)
5386             {
5387                Symbol classSym = FindClass(exp.member.member.string);
5388                convertTo = _class;
5389                _class = classSym ? classSym.registered : null;
5390                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5391             }
5392       
5393             if(prop)
5394             {
5395                if(prop.compiled)
5396                {
5397                   Type type = prop.dataType;
5398                   // TODO: Assuming same base type for units...
5399                   if(_class.type == unitClass)
5400                   {
5401                      if(type.kind == classType)
5402                      {
5403                         Class _class = type._class.registered;
5404                         if(_class.type == unitClass)
5405                         {
5406                            if(!_class.dataType)
5407                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5408                            type = _class.dataType;
5409                         }
5410                      }
5411                      switch(type.kind)
5412                      {
5413                         case floatType:
5414                         {
5415                            float value;
5416                            float (*Get)(float) = (void *)prop.Get;
5417                            GetFloat(exp.member.exp, &value);
5418                            exp.constant = PrintFloat(Get ? Get(value) : value);
5419                            exp.type = constantExp;
5420                            break;
5421                         }
5422                         case doubleType:
5423                         {
5424                            double value;
5425                            double (*Get)(double);
5426                            GetDouble(exp.member.exp, &value);
5427                      
5428                            if(convertTo)
5429                               Get = (void *)prop.Set;
5430                            else
5431                               Get = (void *)prop.Get;
5432                            exp.constant = PrintDouble(Get ? Get(value) : value);
5433                            exp.type = constantExp;
5434                            break;
5435                         }
5436                      }
5437                   }
5438                   else
5439                   {
5440                      if(convertTo)
5441                      {
5442                         Expression value = exp.member.exp;
5443                         Type type;
5444                         if(!prop.dataType)
5445                            ProcessPropertyType(prop);
5446
5447                         type = prop.dataType;
5448                         if(!type)
5449                         {
5450                             // printf("Investigate this\n");
5451                         }
5452                         else if(_class.type == structClass)
5453                         {
5454                            switch(type.kind)
5455                            {
5456                               case classType:
5457                               {
5458                                  Class propertyClass = type._class.registered;
5459                                  if(propertyClass.type == structClass && value.type == instanceExp)
5460                                  {
5461                                     void (*Set)(void *, void *) = (void *)prop.Set;
5462                                     exp.instance = Instantiation { };
5463                                     exp.instance.data = new0 byte[_class.structSize];
5464                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5465                                     exp.instance.loc = exp.loc;
5466                                     exp.type = instanceExp;
5467                                     Set(exp.instance.data, value.instance.data);
5468                                     PopulateInstance(exp.instance);
5469                                  }
5470                                  break;
5471                               }
5472                               case intType:
5473                               {
5474                                  int intValue;
5475                                  void (*Set)(void *, int) = (void *)prop.Set;
5476
5477                                  exp.instance = Instantiation { };
5478                                  exp.instance.data = new0 byte[_class.structSize];
5479                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5480                                  exp.instance.loc = exp.loc;
5481                                  exp.type = instanceExp;
5482                               
5483                                  GetInt(value, &intValue);
5484
5485                                  Set(exp.instance.data, intValue);
5486                                  PopulateInstance(exp.instance);
5487                                  break;
5488                               }
5489                               case int64Type:
5490                               {
5491                                  int64 intValue;
5492                                  void (*Set)(void *, int64) = (void *)prop.Set;
5493
5494                                  exp.instance = Instantiation { };
5495                                  exp.instance.data = new0 byte[_class.structSize];
5496                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5497                                  exp.instance.loc = exp.loc;
5498                                  exp.type = instanceExp;
5499                               
5500                                  GetInt64(value, &intValue);
5501
5502                                  Set(exp.instance.data, intValue);
5503                                  PopulateInstance(exp.instance);
5504                                  break;
5505                               }
5506                               case intPtrType:
5507                               {
5508                                  // TOFIX:
5509                                  intptr intValue;
5510                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5511
5512                                  exp.instance = Instantiation { };
5513                                  exp.instance.data = new0 byte[_class.structSize];
5514                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5515                                  exp.instance.loc = exp.loc;
5516                                  exp.type = instanceExp;
5517                               
5518                                  GetIntPtr(value, &intValue);
5519
5520                                  Set(exp.instance.data, intValue);
5521                                  PopulateInstance(exp.instance);
5522                                  break;
5523                               }
5524                               case intSizeType:
5525                               {
5526                                  // TOFIX:
5527                                  intsize intValue;
5528                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5529
5530                                  exp.instance = Instantiation { };
5531                                  exp.instance.data = new0 byte[_class.structSize];
5532                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5533                                  exp.instance.loc = exp.loc;
5534                                  exp.type = instanceExp;
5535
5536                                  GetIntSize(value, &intValue);
5537
5538                                  Set(exp.instance.data, intValue);
5539                                  PopulateInstance(exp.instance);
5540                                  break;
5541                               }
5542                               case doubleType:
5543                               {
5544                                  double doubleValue;
5545                                  void (*Set)(void *, double) = (void *)prop.Set;
5546
5547                                  exp.instance = Instantiation { };
5548                                  exp.instance.data = new0 byte[_class.structSize];
5549                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5550                                  exp.instance.loc = exp.loc;
5551                                  exp.type = instanceExp;
5552                               
5553                                  GetDouble(value, &doubleValue);
5554
5555                                  Set(exp.instance.data, doubleValue);
5556                                  PopulateInstance(exp.instance);
5557                                  break;
5558                               }
5559                            }
5560                         }
5561                         else if(_class.type == bitClass)
5562                         {
5563                            switch(type.kind)
5564                            {
5565                               case classType:
5566                               {
5567                                  Class propertyClass = type._class.registered;
5568                                  if(propertyClass.type == structClass && value.instance.data)
5569                                  {
5570                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5571                                     unsigned int bits = Set(value.instance.data);
5572                                     exp.constant = PrintHexUInt(bits);
5573                                     exp.type = constantExp;
5574                                     break;
5575                                  }
5576                                  else if(_class.type == bitClass)
5577                                  {
5578                                     unsigned int value;
5579                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5580                                     unsigned int bits;
5581
5582                                     GetUInt(exp.member.exp, &value);
5583                                     bits = Set(value);
5584                                     exp.constant = PrintHexUInt(bits);
5585                                     exp.type = constantExp;
5586                                  }
5587                               }
5588                            }
5589                         }
5590                      }
5591                      else
5592                      {
5593                         if(_class.type == bitClass)
5594                         {
5595                            unsigned int value;
5596                            GetUInt(exp.member.exp, &value);
5597
5598                            switch(type.kind)
5599                            {
5600                               case classType:
5601                               {
5602                                  Class _class = type._class.registered;
5603                                  if(_class.type == structClass)
5604                                  {
5605                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5606
5607                                     exp.instance = Instantiation { };
5608                                     exp.instance.data = new0 byte[_class.structSize];
5609                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5610                                     exp.instance.loc = exp.loc;
5611                                     //exp.instance.fullSet = true;
5612                                     exp.type = instanceExp;
5613                                     Get(value, exp.instance.data);
5614                                     PopulateInstance(exp.instance);
5615                                  }
5616                                  else if(_class.type == bitClass)
5617                                  {
5618                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5619                                     uint64 bits = Get(value);
5620                                     exp.constant = PrintHexUInt64(bits);
5621                                     exp.type = constantExp;
5622                                  }
5623                                  break;
5624                               }
5625                            }
5626                         }
5627                         else if(_class.type == structClass)
5628                         {
5629                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5630                            switch(type.kind)
5631                            {
5632                               case classType:
5633                               {
5634                                  Class _class = type._class.registered;
5635                                  if(_class.type == structClass && value)
5636                                  {
5637                                     void (*Get)(void *, void *) = (void *)prop.Get;
5638
5639                                     exp.instance = Instantiation { };
5640                                     exp.instance.data = new0 byte[_class.structSize];
5641                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5642                                     exp.instance.loc = exp.loc;
5643                                     //exp.instance.fullSet = true;
5644                                     exp.type = instanceExp;
5645                                     Get(value, exp.instance.data);
5646                                     PopulateInstance(exp.instance);
5647                                  }
5648                                  break;
5649                               }
5650                            }
5651                         }
5652                         /*else
5653                         {
5654                            char * value = exp.member.exp.instance.data;
5655                            switch(type.kind)
5656                            {
5657                               case classType:
5658                               {
5659                                  Class _class = type._class.registered;
5660                                  if(_class.type == normalClass)
5661                                  {
5662                                     void *(*Get)(void *) = (void *)prop.Get;
5663
5664                                     exp.instance = Instantiation { };
5665                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5666                                     exp.type = instanceExp;
5667                                     exp.instance.data = Get(value, exp.instance.data);
5668                                  }
5669                                  break;
5670                               }
5671                            }
5672                         }
5673                         */
5674                      }
5675                   }
5676                }
5677                else
5678                {
5679                   exp.isConstant = false;
5680                }
5681             }
5682             else if(member)
5683             {
5684             }
5685          }
5686
5687          if(exp.type != ExpressionType::memberExp)
5688          {
5689             FreeExpression(memberExp);
5690             FreeIdentifier(memberID);
5691          }
5692          break;
5693       }
5694       case typeSizeExp:
5695       {
5696          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5697          FreeExpContents(exp);
5698          exp.constant = PrintUInt(ComputeTypeSize(type));
5699          exp.type = constantExp;         
5700          FreeType(type);
5701          break;
5702       }
5703       case classSizeExp:
5704       {
5705          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5706          if(classSym && classSym.registered)
5707          {
5708             if(classSym.registered.fixed)
5709             {
5710                FreeSpecifier(exp._class);
5711                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5712                exp.type = constantExp;
5713             }
5714             else
5715             {
5716                char className[1024];
5717                strcpy(className, "__ecereClass_");
5718                FullClassNameCat(className, classSym.string, true);
5719                MangleClassName(className);
5720
5721                FreeExpContents(exp);
5722                exp.type = pointerExp;
5723                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
5724                exp.member.member = MkIdentifier("structSize");
5725             }
5726          }
5727          break;
5728       }
5729       case castExp:
5730       //case constantExp:
5731       {
5732          Type type;
5733          Expression e = exp;
5734          if(exp.type == castExp)
5735          {
5736             if(exp.cast.exp)
5737                ComputeExpression(exp.cast.exp);
5738             e = exp.cast.exp;
5739          }
5740          if(e && exp.expType)
5741          {
5742             /*if(exp.destType)
5743                type = exp.destType;
5744             else*/
5745                type = exp.expType;
5746             if(type.kind == classType)
5747             {
5748                Class _class = type._class.registered;
5749                if(_class && (_class.type == unitClass || _class.type == bitClass))
5750                {
5751                   if(!_class.dataType)
5752                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5753                   type = _class.dataType;
5754                }
5755             }
5756             
5757             switch(type.kind)
5758             {
5759                case charType:
5760                   if(type.isSigned)
5761                   {
5762                      char value;
5763                      GetChar(e, &value);
5764                      FreeExpContents(exp);
5765                      exp.constant = PrintChar(value);
5766                      exp.type = constantExp;
5767                   }
5768                   else
5769                   {
5770                      unsigned char value;
5771                      GetUChar(e, &value);
5772                      FreeExpContents(exp);
5773                      exp.constant = PrintUChar(value);
5774                      exp.type = constantExp;
5775                   }
5776                   break;
5777                case shortType:
5778                   if(type.isSigned)
5779                   {
5780                      short value;
5781                      GetShort(e, &value);
5782                      FreeExpContents(exp);
5783                      exp.constant = PrintShort(value);
5784                      exp.type = constantExp;
5785                   }
5786                   else
5787                   {
5788                      unsigned short value;
5789                      GetUShort(e, &value);
5790                      FreeExpContents(exp);
5791                      exp.constant = PrintUShort(value);
5792                      exp.type = constantExp;
5793                   }
5794                   break;
5795                case intType:
5796                   if(type.isSigned)
5797                   {
5798                      int value;
5799                      GetInt(e, &value);
5800                      FreeExpContents(exp);
5801                      exp.constant = PrintInt(value);
5802                      exp.type = constantExp;
5803                   }
5804                   else
5805                   {
5806                      unsigned int value;
5807                      GetUInt(e, &value);
5808                      FreeExpContents(exp);
5809                      exp.constant = PrintUInt(value);
5810                      exp.type = constantExp;
5811                   }
5812                   break;
5813                case int64Type:
5814                   if(type.isSigned)
5815                   {
5816                      int64 value;
5817                      GetInt64(e, &value);
5818                      FreeExpContents(exp);
5819                      exp.constant = PrintInt64(value);
5820                      exp.type = constantExp;
5821                   }
5822                   else
5823                   {
5824                      uint64 value;
5825                      GetUInt64(e, &value);
5826                      FreeExpContents(exp);
5827                      exp.constant = PrintUInt64(value);
5828                      exp.type = constantExp;
5829                   }
5830                   break;
5831                case intPtrType:
5832                   if(type.isSigned)
5833                   {
5834                      intptr value;
5835                      GetIntPtr(e, &value);
5836                      FreeExpContents(exp);
5837                      exp.constant = PrintInt64((int64)value);
5838                      exp.type = constantExp;
5839                   }
5840                   else
5841                   {
5842                      uintptr value;
5843                      GetUIntPtr(e, &value);
5844                      FreeExpContents(exp);
5845                      exp.constant = PrintUInt64((uint64)value);
5846                      exp.type = constantExp;
5847                   }
5848                   break;
5849                case intSizeType:
5850                   if(type.isSigned)
5851                   {
5852                      intsize value;
5853                      GetIntSize(e, &value);
5854                      FreeExpContents(exp);
5855                      exp.constant = PrintInt64((int64)value);
5856                      exp.type = constantExp;
5857                   }
5858                   else
5859                   {
5860                      uintsize value;
5861                      GetUIntSize(e, &value);
5862                      FreeExpContents(exp);
5863                      exp.constant = PrintUInt64((uint64)value);
5864                      exp.type = constantExp;
5865                   }
5866                   break;
5867                case floatType:
5868                {
5869                   float value;
5870                   GetFloat(e, &value);
5871                   FreeExpContents(exp);
5872                   exp.constant = PrintFloat(value);
5873                   exp.type = constantExp;
5874                   break;
5875                }
5876                case doubleType:
5877                {  
5878                   double value;
5879                   GetDouble(e, &value);
5880                   FreeExpContents(exp);
5881                   exp.constant = PrintDouble(value);
5882                   exp.type = constantExp;
5883                   break;
5884                }
5885             }
5886          }
5887          break;
5888       }
5889       case conditionExp:
5890       {
5891          Operand op1 { };
5892          Operand op2 { };
5893          Operand op3 { };
5894
5895          if(exp.cond.exp)
5896             // Caring only about last expression for now...
5897             ComputeExpression(exp.cond.exp->last);
5898          if(exp.cond.elseExp)
5899             ComputeExpression(exp.cond.elseExp);
5900          if(exp.cond.cond)
5901             ComputeExpression(exp.cond.cond);
5902
5903          op1 = GetOperand(exp.cond.cond);
5904          if(op1.type) op1.type.refCount++;
5905          op2 = GetOperand(exp.cond.exp->last);
5906          if(op2.type) op2.type.refCount++;
5907          op3 = GetOperand(exp.cond.elseExp);
5908          if(op3.type) op3.type.refCount++;
5909
5910          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
5911          if(op1.type) FreeType(op1.type);
5912          if(op2.type) FreeType(op2.type);
5913          if(op3.type) FreeType(op3.type);
5914          break;
5915       }  
5916    }
5917 }
5918
5919 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
5920 {
5921    bool result = true;
5922    if(destType)
5923    {
5924       OldList converts { };
5925       Conversion convert;
5926
5927       if(destType.kind == voidType)
5928          return false;
5929
5930       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
5931          result = false;
5932       if(converts.count)
5933       {
5934          // for(convert = converts.last; convert; convert = convert.prev)
5935          for(convert = converts.first; convert; convert = convert.next)
5936          {
5937             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
5938             if(!empty)
5939             {
5940                Expression newExp { };
5941                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
5942
5943                // TODO: Check this...
5944                *newExp = *exp;
5945                newExp.destType = null;
5946
5947                if(convert.isGet)
5948                {
5949                   // [exp].ColorRGB
5950                   exp.type = memberExp;
5951                   exp.addedThis = true;
5952                   exp.member.exp = newExp;
5953                   FreeType(exp.member.exp.expType);
5954
5955                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
5956                   exp.member.exp.expType.classObjectType = objectType;
5957                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
5958                   exp.member.memberType = propertyMember;
5959                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
5960                   // TESTING THIS... for (int)degrees
5961                   exp.needCast = true;
5962                   if(exp.expType) exp.expType.refCount++;
5963                   ApplyAnyObjectLogic(exp.member.exp);
5964                }
5965                else
5966                {
5967                
5968                   /*if(exp.isConstant)
5969                   {
5970                      // Color { ColorRGB = [exp] };
5971                      exp.type = instanceExp;
5972                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
5973                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
5974                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
5975                   }
5976                   else*/
5977                   {
5978                      // If not constant, don't turn it yet into an instantiation
5979                      // (Go through the deep members system first)
5980                      exp.type = memberExp;
5981                      exp.addedThis = true;
5982                      exp.member.exp = newExp;
5983
5984                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
5985                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
5986                         newExp.expType._class.registered.type == noHeadClass)
5987                      {
5988                         newExp.byReference = true;
5989                      }
5990
5991                      FreeType(exp.member.exp.expType);
5992                      /*exp.member.exp.expType = convert.convert.dataType;
5993                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
5994                      exp.member.exp.expType = null;
5995                      if(convert.convert.dataType)
5996                      {
5997                         exp.member.exp.expType = { };
5998                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
5999                         exp.member.exp.expType.refCount = 1;
6000                         exp.member.exp.expType.classObjectType = objectType;
6001                         ApplyAnyObjectLogic(exp.member.exp);
6002                      }
6003
6004                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6005                      exp.member.memberType = reverseConversionMember;
6006                      exp.expType = convert.resultType ? convert.resultType :
6007                         MkClassType(convert.convert._class.fullName);
6008                      exp.needCast = true;
6009                      if(convert.resultType) convert.resultType.refCount++;
6010                   }
6011                }
6012             }
6013             else
6014             {
6015                FreeType(exp.expType);
6016                if(convert.isGet)
6017                {
6018                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6019                   exp.needCast = true;
6020                   if(exp.expType) exp.expType.refCount++;
6021                }
6022                else
6023                {
6024                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6025                   exp.needCast = true;
6026                   if(convert.resultType)
6027                      convert.resultType.refCount++;
6028                }
6029             }
6030          }
6031          if(exp.isConstant && inCompiler)
6032             ComputeExpression(exp);
6033
6034          converts.Free(FreeConvert);
6035       }
6036
6037       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6038       {
6039          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6040       }
6041       if(!result && exp.expType && exp.destType)
6042       {
6043          if((exp.destType.kind == classType && exp.expType.kind == pointerType && 
6044              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6045             (exp.expType.kind == classType && exp.destType.kind == pointerType && 
6046             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6047             result = true;
6048       }
6049    }
6050    // if(result) CheckTemplateTypes(exp);
6051    return result;
6052 }
6053
6054 void CheckTemplateTypes(Expression exp)
6055 {
6056    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6057    {
6058       Expression newExp { };
6059       Statement compound;
6060       Context context;
6061       *newExp = *exp;
6062       if(exp.destType) exp.destType.refCount++;
6063       if(exp.expType)  exp.expType.refCount++;
6064       newExp.prev = null;
6065       newExp.next = null;
6066
6067       switch(exp.expType.kind)
6068       {
6069          case doubleType:
6070             if(exp.destType.classObjectType)
6071             {
6072                // We need to pass the address, just pass it along (Undo what was done above)
6073                if(exp.destType) exp.destType.refCount--;
6074                if(exp.expType)  exp.expType.refCount--;
6075                delete newExp;
6076             }
6077             else
6078             {
6079                // If we're looking for value:
6080                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6081                OldList * specs;
6082                OldList * unionDefs = MkList();
6083                OldList * statements = MkList();
6084                context = PushContext();
6085                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null))); 
6086                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6087                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6088                exp.type = extensionCompoundExp;
6089                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6090                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6091                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6092                exp.compound.compound.context = context;
6093                PopContext(context);
6094             }
6095             break;
6096          default:
6097             exp.type = castExp;
6098             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6099             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6100             break;
6101       }
6102    }
6103    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6104    {
6105       Expression newExp { };
6106       Statement compound;
6107       Context context;
6108       *newExp = *exp;
6109       if(exp.destType) exp.destType.refCount++;
6110       if(exp.expType)  exp.expType.refCount++;
6111       newExp.prev = null;
6112       newExp.next = null;
6113
6114       switch(exp.expType.kind)
6115       {
6116          case doubleType:
6117             if(exp.destType.classObjectType)
6118             {
6119                // We need to pass the address, just pass it along (Undo what was done above)
6120                if(exp.destType) exp.destType.refCount--;
6121                if(exp.expType)  exp.expType.refCount--;
6122                delete newExp;
6123             }
6124             else
6125             {
6126                // If we're looking for value:
6127                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6128                OldList * specs;
6129                OldList * unionDefs = MkList();
6130                OldList * statements = MkList();
6131                context = PushContext();
6132                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null))); 
6133                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6134                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6135                exp.type = extensionCompoundExp;
6136                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6137                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6138                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6139                exp.compound.compound.context = context;
6140                PopContext(context);
6141             }
6142             break;
6143          case classType:
6144          {
6145             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6146             {
6147                exp.type = bracketsExp;
6148                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6149                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6150                ProcessExpressionType(exp.list->first);
6151                break;
6152             }
6153             else
6154             {
6155                exp.type = bracketsExp;
6156                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6157                newExp.needCast = true;
6158                ProcessExpressionType(exp.list->first);
6159                break;
6160             }
6161          }
6162          default:
6163          {
6164             if(exp.expType.kind == templateType)
6165             {
6166                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6167                if(type)
6168                {
6169                   FreeType(exp.destType);
6170                   FreeType(exp.expType);
6171                   delete newExp;
6172                   break;
6173                }
6174             }
6175             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6176             {
6177                exp.type = opExp;
6178                exp.op.op = '*';
6179                exp.op.exp1 = null;
6180                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6181                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6182             }
6183             else
6184             {
6185                char typeString[1024];
6186                Declarator decl;
6187                OldList * specs = MkList();
6188                typeString[0] = '\0';
6189                PrintType(exp.expType, typeString, false, false);
6190                decl = SpecDeclFromString(typeString, specs, null);
6191                
6192                exp.type = castExp;
6193                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6194                exp.cast.typeName = MkTypeName(specs, decl);
6195                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6196                exp.cast.exp.needCast = true;
6197             }
6198             break;
6199          }
6200       }
6201    }
6202 }
6203 // TODO: The Symbol tree should be reorganized by namespaces
6204 // Name Space:
6205 //    - Tree of all symbols within (stored without namespace)
6206 //    - Tree of sub-namespaces
6207
6208 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6209 {
6210    int nsLen = strlen(nameSpace);
6211    Symbol symbol;
6212    // Start at the name space prefix
6213    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6214    {
6215       char * s = symbol.string;
6216       if(!strncmp(s, nameSpace, nsLen))
6217       {
6218          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6219          int c;
6220          char * namePart;
6221          for(c = strlen(s)-1; c >= 0; c--)
6222             if(s[c] == ':')
6223                break;
6224
6225          namePart = s+c+1;
6226          if(!strcmp(namePart, name))
6227          {
6228             // TODO: Error on ambiguity
6229             return symbol;
6230          }
6231       }
6232       else
6233          break;
6234    }
6235    return null;
6236 }
6237
6238 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6239 {
6240    int c;
6241    char nameSpace[1024];
6242    char * namePart;
6243    bool gotColon = false;
6244
6245    nameSpace[0] = '\0';
6246    for(c = strlen(name)-1; c >= 0; c--)
6247       if(name[c] == ':')
6248       {
6249          gotColon = true;
6250          break;
6251       }
6252
6253    namePart = name+c+1;
6254    while(c >= 0 && name[c] == ':') c--;
6255    if(c >= 0)
6256    {
6257       // Try an exact match first
6258       Symbol symbol = (Symbol)tree.FindString(name);
6259       if(symbol)
6260          return symbol;
6261
6262       // Namespace specified
6263       memcpy(nameSpace, name, c + 1);
6264       nameSpace[c+1] = 0;
6265
6266       return ScanWithNameSpace(tree, nameSpace, namePart);
6267    }
6268    else if(gotColon)
6269    {
6270       // Looking for a global symbol, e.g. ::Sleep()
6271       Symbol symbol = (Symbol)tree.FindString(namePart);
6272       return symbol;
6273    }
6274    else
6275    {
6276       // Name only (no namespace specified)
6277       Symbol symbol = (Symbol)tree.FindString(namePart);
6278       if(symbol)
6279          return symbol;
6280       return ScanWithNameSpace(tree, "", namePart);
6281    }
6282    return null;
6283 }
6284
6285 static void ProcessDeclaration(Declaration decl);
6286
6287 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6288 {
6289 #ifdef _DEBUG
6290    //Time startTime = GetTime();
6291 #endif
6292    // Optimize this later? Do this before/less?
6293    Context ctx;
6294    Symbol symbol = null;
6295    // First, check if the identifier is declared inside the function
6296    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6297
6298    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6299    {
6300       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6301       {
6302          symbol = null;
6303          if(thisNameSpace)
6304          {
6305             char curName[1024];
6306             strcpy(curName, thisNameSpace);
6307             strcat(curName, "::");
6308             strcat(curName, name);
6309             // Try to resolve in current namespace first
6310             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6311          }
6312          if(!symbol)
6313             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6314       }
6315       else
6316          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6317
6318       if(symbol || ctx == endContext) break;
6319    }
6320    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6321    {
6322       if(symbol.pointerExternal.type == functionExternal)
6323       {
6324          FunctionDefinition function = symbol.pointerExternal.function;
6325
6326          // Modified this recently...
6327          Context tmpContext = curContext;
6328          curContext = null;         
6329          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6330          curContext = tmpContext;
6331
6332          symbol.pointerExternal.symbol = symbol;
6333
6334          // TESTING THIS:
6335          DeclareType(symbol.type, true, true);
6336
6337          ast->Insert(curExternal.prev, symbol.pointerExternal);
6338
6339          symbol.id = curExternal.symbol.idCode;
6340
6341       }
6342       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6343       {
6344          ast->Move(symbol.pointerExternal, curExternal.prev);
6345          symbol.id = curExternal.symbol.idCode;
6346       }
6347    }
6348 #ifdef _DEBUG
6349    //findSymbolTotalTime += GetTime() - startTime;
6350 #endif
6351    return symbol;
6352 }
6353
6354 static void GetTypeSpecs(Type type, OldList * specs)
6355 {
6356    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6357    switch(type.kind)
6358    {
6359       case classType: 
6360       {
6361          if(type._class.registered)
6362          {
6363             if(!type._class.registered.dataType)
6364                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6365             GetTypeSpecs(type._class.registered.dataType, specs);
6366          }
6367          break;
6368       }
6369       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6370       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6371       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6372       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6373       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6374       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6375       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6376       case intType: 
6377       default:
6378          ListAdd(specs, MkSpecifier(INT)); break;
6379    }
6380 }
6381
6382 // WARNING : This function expects a null terminated string since it recursively concatenate...
6383 static void _PrintType(Type type, char * string, bool printName, bool printFunction, bool fullName)
6384 {
6385    if(type)
6386    {
6387       switch(type.kind)
6388       {
6389          case classType:
6390             if(type._class && type._class.string)
6391             {
6392                // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6393                //       look into merging with thisclass ?
6394                if(type.classObjectType == typedObject)
6395                   strcat(string, "typed_object");
6396                else if(fullName)
6397                   strcat(string, type._class.string);
6398                else
6399                {
6400                   if(type._class.registered)
6401                      strcat(string, type._class.registered.name);
6402                   else
6403                      strcat(string, type._class.string);                     
6404                }
6405             }
6406             break;
6407          case pointerType:
6408          {
6409             /*Type funcType;
6410             for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
6411             if(funcType && funcType.kind == functionType)
6412             {
6413                Type param;
6414                PrintType(funcType.returnType, string, false, fullName);
6415                strcat(string, "(*");
6416                if(printName || funcType.thisClass)
6417                {
6418                   strcat(string, " ");
6419                   if(funcType.thisClass)
6420                   {
6421                      strcat(string, funcType.thisClass.string);
6422                      strcat(string, "::");
6423                   }
6424                   if(type.name)
6425                      strcat(string, type.name);
6426                }
6427                strcat(string, ")(");
6428                for(param = funcType.params.first; param; param = param.next)
6429                {
6430                   PrintType(param, string, false, fullName);
6431                   if(param.next) strcat(string, ", ");
6432                }
6433                strcat(string, ")");               
6434             }
6435             else*/
6436             {
6437                _PrintType(type.type, string, false /*printName*/, printFunction, fullName);
6438                strcat(string, " *");
6439             }
6440             break;
6441          }
6442          case voidType: strcat(string, "void"); break;
6443          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6444          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6445          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6446          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6447          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6448          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6449          case floatType: strcat(string, "float"); break;
6450          case doubleType: strcat(string, "double"); break;
6451          case structType:
6452             if(type.enumName)
6453             {
6454                strcat(string, "struct ");
6455                strcat(string, type.enumName);
6456             }
6457             else if(type.typeName)
6458             {
6459                strcat(string, type.typeName);
6460             }
6461             else
6462             {
6463                /*
6464                strcat(string, "struct ");
6465                strcat(string,"(unnamed)");
6466                */
6467                Type member;
6468                strcat(string, "struct {");
6469                for(member = type.members.first; member; member = member.next)
6470                {
6471                   PrintType(member, string, true, fullName);
6472                   strcat(string,"; ");
6473                }
6474                strcat(string,"}");
6475             }
6476             break;
6477          case unionType:
6478             if(type.enumName)
6479             {
6480                strcat(string, "union ");
6481                strcat(string, type.enumName);
6482             }
6483             else if(type.typeName)
6484             {
6485                strcat(string, type.typeName);
6486             }
6487             else
6488             {
6489                strcat(string, "union ");
6490                strcat(string,"(unnamed)");
6491             }
6492             break;
6493          case enumType:
6494             if(type.enumName)
6495             {
6496                strcat(string, "enum ");
6497                strcat(string, type.enumName);
6498             }
6499             else if(type.typeName)
6500             {
6501                strcat(string, type.typeName);
6502             }
6503             else
6504                strcat(string, "enum");
6505             break;
6506          case functionType:
6507          {
6508             if(printFunction)
6509             {
6510                if(type.dllExport)
6511                   strcat(string, "dllexport ");
6512                PrintType(type.returnType, string, false, fullName);
6513                strcat(string, " ");
6514             }
6515             
6516             // DANGER: Testing This
6517             if(printName)
6518             {
6519                if(type.name)
6520                {
6521                   if(fullName)
6522                      strcat(string, type.name);
6523                   else
6524                   {
6525                      char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6526                      if(name) name += 2; else name = type.name;
6527                      strcat(string, name);
6528                   }
6529                }
6530             }
6531
6532             if(printFunction)
6533             {
6534                Type param;
6535                strcat(string, "(");
6536                for(param = type.params.first; param; param = param.next)
6537                {
6538                   PrintType(param, string, true, fullName);
6539                   if(param.next) strcat(string, ", ");
6540                }
6541                strcat(string, ")");
6542             }
6543             break;
6544          }
6545          case arrayType:
6546          {
6547             /*Type funcType;
6548             for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
6549             if(funcType && funcType.kind == functionType)
6550             {
6551                Type param;
6552                PrintType(funcType.returnType, string, false, fullName);
6553                strcat(string, "(*");
6554                if(printName || funcType.thisClass)
6555                {
6556                   strcat(string, " ");
6557                   if(funcType.thisClass)
6558                   {
6559                      strcat(string, funcType.thisClass.string);
6560                      strcat(string, "::");
6561                   }
6562                   if(type.name)
6563                      strcat(string, type.name);
6564                }
6565                strcat(string, ")(");
6566                for(param = funcType.params.first; param; param = param.next)
6567                {
6568                   PrintType(param, string, false, fullName);
6569                   if(param.next) strcat(string, ", ");
6570                }
6571                strcat(string, ")");               
6572             }
6573             else*/
6574             {
6575                char baseType[1024], size[256];
6576                Type arrayType = type;
6577                baseType[0] = '\0';
6578                size[0] = '\0';
6579
6580                while(arrayType.kind == TypeKind::arrayType)
6581                {
6582                   strcat(size, "[");
6583                   if(arrayType.enumClass)
6584                      strcat(size, arrayType.enumClass.string);
6585                   else if(arrayType.arraySizeExp)
6586                      PrintExpression(arrayType.arraySizeExp, size);
6587                   //sprintf(string, "%s[%s]", baseType, size); 
6588                   strcat(size, "]");
6589
6590                   arrayType = arrayType.arrayType;
6591                }
6592                _PrintType(arrayType, baseType, printName, printFunction, fullName);
6593                strcat(string, baseType);
6594                strcat(string, size);
6595             }
6596
6597             /*
6598                PrintType(type.arrayType, baseType, printName, fullName);
6599                if(type.enumClass)
6600                   strcpy(size, type.enumClass.string);
6601                else if(type.arraySizeExp)
6602                   PrintExpression(type.arraySizeExp, size);
6603                //sprintf(string, "%s[%s]", baseType, size); 
6604                strcat(string, baseType);
6605                strcat(string, "[");
6606                strcat(string, size); 
6607                strcat(string, "]");
6608                */
6609
6610             printName = false;
6611             break;
6612          }
6613          case ellipsisType:
6614             strcat(string, "...");
6615             break;
6616          case methodType:
6617             _PrintType(type.method.dataType, string, false, printFunction, fullName);
6618             break;
6619          case subClassType:
6620             strcat(string, "subclass(");
6621             strcat(string, type._class ? type._class.string : "int");
6622             strcat(string, ")");                  
6623             break;
6624          case templateType:
6625             strcat(string, type.templateParameter.identifier.string);
6626             break;
6627          case thisClassType:
6628             strcat(string, "thisclass");
6629             break;
6630          case vaListType:
6631          strcat(string, "__builtin_va_list");
6632             break;
6633       }
6634       if(type.name && printName && type.kind != functionType && (type.kind != pointerType || type.type.kind != functionType))
6635       {
6636          strcat(string, " ");
6637          strcat(string, type.name);
6638       }
6639    }
6640 }
6641
6642 // *****
6643 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6644 // *****
6645 void PrintType(Type type, char * string, bool printName, bool fullName)
6646 {
6647    Type funcType;
6648    for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
6649    if(funcType && funcType.kind == functionType && type != funcType)
6650    {
6651       char typeString[1024];
6652       Type param;
6653
6654       PrintType(funcType.returnType, string, false, fullName);
6655       strcat(string, "(");
6656       _PrintType(type, string, printName, false, fullName);
6657       strcat(string, ")");
6658
6659       strcat(string, "(");
6660       for(param = funcType.params.first; param; param = param.next)
6661       {
6662          PrintType(param, string, true, fullName);
6663          if(param.next) strcat(string, ", ");
6664       }
6665       strcat(string, ")");
6666    }
6667    else
6668       _PrintType(type, string, printName, true, fullName);
6669    if(type.bitFieldCount)
6670    {
6671       char count[100];
6672       sprintf(count, ":%d", type.bitFieldCount);
6673       strcat(string, count);
6674    }
6675 }
6676
6677 static Type FindMember(Type type, char * string)
6678 {
6679    Type memberType;
6680    for(memberType = type.members.first; memberType; memberType = memberType.next)
6681    {
6682       if(!memberType.name)
6683       {
6684          Type subType = FindMember(memberType, string);
6685          if(subType)
6686             return subType;
6687       }
6688       else if(!strcmp(memberType.name, string))
6689          return memberType;
6690    }
6691    return null;
6692 }
6693
6694 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6695 {
6696    Type memberType;
6697    for(memberType = type.members.first; memberType; memberType = memberType.next)
6698    {
6699       if(!memberType.name)
6700       {
6701          Type subType = FindMember(memberType, string);
6702          if(subType)
6703          {
6704             *offset += memberType.offset;
6705             return subType;
6706          }
6707       }
6708       else if(!strcmp(memberType.name, string))
6709       {
6710          *offset += memberType.offset;
6711          return memberType;
6712       }
6713    }
6714    return null;
6715 }
6716
6717 Expression ParseExpressionString(char * expression)
6718 {
6719    fileInput = TempFile { };
6720    fileInput.Write(expression, 1, strlen(expression));
6721    fileInput.Seek(0, start);
6722
6723    echoOn = false;
6724    parsedExpression = null;
6725    resetScanner();
6726    expression_yyparse();
6727    delete fileInput;
6728
6729    return parsedExpression;
6730 }
6731
6732 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6733 {
6734    Identifier id = exp.identifier;
6735    Method method = null;
6736    Property prop = null;
6737    DataMember member = null;
6738    ClassProperty classProp = null;
6739
6740    if(_class && _class.type == enumClass)
6741    {
6742       NamedLink value = null;
6743       Class enumClass = eSystem_FindClass(privateModule, "enum");
6744       if(enumClass)
6745       {
6746          Class baseClass;
6747          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
6748          {
6749             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
6750             for(value = e.values.first; value; value = value.next)
6751             {
6752                if(!strcmp(value.name, id.string))
6753                   break;
6754             }
6755             if(value)
6756             {
6757                char constant[256];
6758
6759                FreeExpContents(exp);
6760
6761                exp.type = constantExp;
6762                exp.isConstant = true;
6763                if(!strcmp(baseClass.dataTypeString, "int"))
6764                   sprintf(constant, "%d",value.data);
6765                else
6766                   sprintf(constant, "0x%X",value.data);
6767                exp.constant = CopyString(constant);
6768                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
6769                exp.expType = MkClassType(baseClass.fullName);
6770                break;
6771             }
6772          }
6773       }
6774       if(value)
6775          return true;
6776    }
6777    if((method = eClass_FindMethod(_class, id.string, privateModule)))
6778    {
6779       ProcessMethodType(method);
6780       exp.expType = Type
6781       {
6782          refCount = 1;
6783          kind = methodType;
6784          method = method;
6785          // Crash here?
6786          // TOCHECK: Put it back to what it was...
6787          // methodClass = _class;
6788          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
6789       };
6790       //id._class = null;
6791       return true;
6792    }
6793    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
6794    {
6795       if(!prop.dataType)
6796          ProcessPropertyType(prop);
6797       exp.expType = prop.dataType;
6798       if(prop.dataType) prop.dataType.refCount++;
6799       return true;
6800    }
6801    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
6802    {
6803       if(!member.dataType)
6804          member.dataType = ProcessTypeString(member.dataTypeString, false);
6805       exp.expType = member.dataType;
6806       if(member.dataType) member.dataType.refCount++;
6807       return true;
6808    }
6809    else if((classProp = eClass_FindClassProperty(_class, id.string)))
6810    {
6811       if(!classProp.dataType)
6812          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
6813
6814       if(classProp.constant)
6815       {
6816          FreeExpContents(exp);
6817
6818          exp.isConstant = true;
6819          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
6820          {
6821             //char constant[256];
6822             exp.type = stringExp;
6823             exp.constant = QMkString((char *)classProp.Get(_class));
6824          }
6825          else
6826          {
6827             char constant[256];
6828             exp.type = constantExp;
6829             sprintf(constant, "%d", (int)classProp.Get(_class));
6830             exp.constant = CopyString(constant);
6831          }
6832       }
6833       else
6834       {
6835          // TO IMPLEMENT...
6836       }
6837
6838       exp.expType = classProp.dataType;
6839       if(classProp.dataType) classProp.dataType.refCount++;
6840       return true;
6841    }
6842    return false;
6843 }
6844
6845 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
6846 {
6847    BinaryTree * tree = &nameSpace.functions;
6848    GlobalData data = (GlobalData)tree->FindString(name);
6849    NameSpace * child;
6850    if(!data)
6851    {
6852       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
6853       {
6854          data = ScanGlobalData(child, name);
6855          if(data)
6856             break;
6857       }
6858    }
6859    return data;
6860 }
6861
6862 static GlobalData FindGlobalData(char * name)
6863 {
6864    int start = 0, c;
6865    NameSpace * nameSpace;
6866    nameSpace = globalData;
6867    for(c = 0; name[c]; c++)
6868    {
6869       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
6870       {
6871          NameSpace * newSpace;
6872          char * spaceName = new char[c - start + 1];
6873          strncpy(spaceName, name + start, c - start);
6874          spaceName[c-start] = '\0';
6875          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
6876          delete spaceName;
6877          if(!newSpace)
6878             return null;
6879          nameSpace = newSpace;
6880          if(name[c] == ':') c++;
6881          start = c+1;
6882       }
6883    }
6884    if(c - start)
6885    {
6886       return ScanGlobalData(nameSpace, name + start);
6887    }
6888    return null;
6889 }
6890
6891 static int definedExpStackPos;
6892 static void * definedExpStack[512];
6893
6894 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
6895 void ReplaceExpContents(Expression checkedExp, Expression newExp)
6896 {
6897    Expression prev = checkedExp.prev, next = checkedExp.next;
6898
6899    FreeExpContents(checkedExp);
6900    FreeType(checkedExp.expType);
6901    FreeType(checkedExp.destType);
6902
6903    *checkedExp = *newExp;
6904
6905    delete newExp;
6906
6907    checkedExp.prev = prev;
6908    checkedExp.next = next;
6909 }
6910
6911 void ApplyAnyObjectLogic(Expression e)
6912 {
6913    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
6914    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
6915    {
6916       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
6917       //ellipsisDestType = destType;
6918       if(e && e.expType)
6919       {
6920          Type type = e.expType;
6921          Class _class = null;
6922          //Type destType = e.destType;
6923
6924          if(type.kind == classType && type._class && type._class.registered)
6925          {
6926             _class = type._class.registered;
6927          }
6928          else if(type.kind == subClassType)
6929          {
6930             _class = FindClass("ecere::com::Class").registered;
6931          }
6932          else
6933          {
6934             char string[1024] = "";
6935             Symbol classSym;
6936
6937             PrintType(type, string, false, true);
6938             classSym = FindClass(string);
6939             if(classSym) _class = classSym.registered;
6940          }
6941
6942          if((_class && (_class.type == enumClass || _class.type == unitClass || _class.type == bitClass || _class.type == systemClass) && strcmp(_class.fullName, "class") && strcmp(_class.fullName, "ecere::com::Class")) || // Patched so that class isn't considered SYSTEM...
6943             (!e.expType.classObjectType && (((type.kind != pointerType && type.kind != subClassType && (type.kind != classType || !type._class || !type._class.registered || type._class.registered.type == structClass))) ||
6944             destType.byReference)))
6945          {
6946             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
6947             {
6948                Expression checkedExp = e, newExp;
6949
6950                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
6951                {
6952                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
6953                   {
6954                      if(checkedExp.type == extensionCompoundExp)
6955                      {
6956                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
6957                      }
6958                      else
6959                         checkedExp = checkedExp.list->last;
6960                   }
6961                   else if(checkedExp.type == castExp)
6962                      checkedExp = checkedExp.cast.exp;
6963                }
6964
6965                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
6966                {
6967                   newExp = checkedExp.op.exp2;
6968                   checkedExp.op.exp2 = null;
6969                   FreeExpContents(checkedExp);
6970                   
6971                   if(e.expType && e.expType.passAsTemplate)
6972                   {
6973                      char size[100];
6974                      ComputeTypeSize(e.expType);
6975                      sprintf(size, "%d", e.expType.size);
6976                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
6977                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
6978                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
6979                   }
6980
6981                   ReplaceExpContents(checkedExp, newExp);
6982                   e.byReference = true;
6983                }
6984                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
6985                {
6986                   Expression checkedExp, newExp;
6987
6988                   {
6989                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
6990                      bool hasAddress =
6991                         e.type == identifierExp ||
6992                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
6993                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
6994                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
6995                         e.type == indexExp;
6996
6997                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
6998                      {
6999                         Context context = PushContext();
7000                         Declarator decl;
7001                         OldList * specs = MkList();
7002                         char typeString[1024];
7003                         Expression newExp { };
7004
7005                         typeString[0] = '\0';
7006                         *newExp = *e;
7007
7008                         //if(e.destType) e.destType.refCount++;
7009                         // if(exp.expType) exp.expType.refCount++;
7010                         newExp.prev = null;
7011                         newExp.next = null;
7012                         newExp.expType = null;
7013
7014                         PrintType(e.expType, typeString, false, true);
7015                         decl = SpecDeclFromString(typeString, specs, null);
7016                         newExp.destType = ProcessType(specs, decl);
7017
7018                         curContext = context;
7019                         e.type = extensionCompoundExp;
7020
7021                         // We need a current compound for this
7022                         if(curCompound)
7023                         {
7024                            char name[100];
7025                            OldList * stmts = MkList();
7026                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7027                            if(!curCompound.compound.declarations)
7028                               curCompound.compound.declarations = MkList();
7029                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7030                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7031                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7032                            e.compound = MkCompoundStmt(null, stmts);
7033                         }
7034                         else
7035                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7036
7037                         /*
7038                         e.compound = MkCompoundStmt(
7039                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7040                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))), 
7041
7042                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7043                         */
7044                         
7045                         {
7046                            Type type = e.destType;
7047                            e.destType = { };
7048                            CopyTypeInto(e.destType, type);
7049                            e.destType.refCount = 1;
7050                            e.destType.classObjectType = none;
7051                            FreeType(type);
7052                         }
7053
7054                         e.compound.compound.context = context;
7055                         PopContext(context);
7056                         curContext = context.parent;
7057                      }
7058                   }
7059
7060                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7061                   checkedExp = e;
7062                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7063                   {
7064                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7065                      {
7066                         if(checkedExp.type == extensionCompoundExp)
7067                         {
7068                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7069                         }
7070                         else
7071                            checkedExp = checkedExp.list->last;
7072                      }
7073                      else if(checkedExp.type == castExp)
7074                         checkedExp = checkedExp.cast.exp;
7075                   }
7076                   {
7077                      Expression operand { };
7078                      operand = *checkedExp;
7079                      checkedExp.destType = null;
7080                      checkedExp.expType = null;
7081                      checkedExp.Clear();
7082                      checkedExp.type = opExp;
7083                      checkedExp.op.op = '&';
7084                      checkedExp.op.exp1 = null;
7085                      checkedExp.op.exp2 = operand;
7086
7087                      //newExp = MkExpOp(null, '&', checkedExp);
7088                   }
7089                   //ReplaceExpContents(checkedExp, newExp);
7090                }
7091             }
7092          }
7093       }
7094    }
7095    {
7096       // If expression type is a simple class, make it an address
7097       // FixReference(e, true);
7098    }
7099 //#if 0
7100    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) && 
7101       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7102          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7103    {
7104       if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class"))
7105       {
7106          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7107       }
7108       else
7109       {
7110          Expression thisExp { };
7111
7112          *thisExp = *e;
7113          thisExp.prev = null;
7114          thisExp.next = null;
7115          e.Clear();
7116
7117          e.type = bracketsExp;
7118          e.list = MkListOne(MkExpOp(null, '*', MkExpBrackets(MkListOne(thisExp))));
7119          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7120             ((Expression)e.list->first).byReference = true;
7121
7122          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7123          {
7124             e.expType = thisExp.expType;
7125             e.expType.refCount++;
7126          }
7127          else*/
7128          {
7129             e.expType = { };
7130             CopyTypeInto(e.expType, thisExp.expType);
7131             e.expType.byReference = false;
7132             e.expType.refCount = 1;
7133
7134             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7135                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7136             {
7137                e.expType.classObjectType = none;
7138             }
7139          }
7140       }
7141    }
7142 // TOFIX: Try this for a nice IDE crash!
7143 //#endif
7144    // The other way around
7145    else 
7146 //#endif
7147    if(destType && e.expType && 
7148          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7149          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) && 
7150          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7151    {
7152       if(destType.kind == ellipsisType)
7153       {
7154          Compiler_Error($"Unspecified type\n");
7155       }
7156       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7157       {
7158          bool byReference = e.expType.byReference;
7159          Expression thisExp { };
7160          Declarator decl;
7161          OldList * specs = MkList();
7162          char typeString[1024]; // Watch buffer overruns
7163          Type type;
7164          ClassObjectType backupClassObjectType;
7165
7166          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7167             type = e.expType;
7168          else
7169             type = destType;            
7170
7171          backupClassObjectType = type.classObjectType;
7172
7173          type.classObjectType = none;
7174
7175          typeString[0] = '\0';
7176          PrintType(type, typeString, false, true);
7177          decl = SpecDeclFromString(typeString, specs, null);
7178
7179          type.classObjectType = backupClassObjectType;
7180
7181          *thisExp = *e;
7182          thisExp.prev = null;
7183          thisExp.next = null;
7184          e.Clear();
7185
7186          if( ( type.kind == classType && type._class && type._class.registered && strcmp(type._class.registered.fullName, "ecere::com::Instance") &&
7187                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass || 
7188                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7189              (type.kind != pointerType && type.kind != arrayType && type.kind != classType) ||
7190              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7191          {
7192             e.type = opExp;
7193             e.op.op = '*';
7194             e.op.exp1 = null;
7195             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7196          }
7197          else
7198          {
7199             e.type = castExp;
7200             e.cast.typeName = MkTypeName(specs, decl);
7201             e.cast.exp = thisExp;
7202             e.byReference = true;
7203          }
7204          e.expType = type;
7205          e.destType = destType;
7206          type.refCount++;
7207          destType.refCount++;
7208       }
7209    }
7210 }
7211
7212 void ProcessExpressionType(Expression exp)
7213 {
7214    bool unresolved = false;
7215    Location oldyylloc = yylloc;
7216    bool notByReference = false;
7217 #ifdef _DEBUG   
7218    char debugExpString[4096];
7219    debugExpString[0] = '\0';
7220    PrintExpression(exp, debugExpString);
7221 #endif
7222    if(!exp || exp.expType) 
7223       return;
7224
7225    //eSystem_Logf("%s\n", expString);
7226    
7227    // Testing this here
7228    yylloc = exp.loc;
7229    switch(exp.type)
7230    {
7231       case identifierExp:
7232       {
7233          Identifier id = exp.identifier;
7234          if(!id) return;
7235
7236          // DOING THIS LATER NOW...
7237          if(id._class && id._class.name)
7238          {
7239             id.classSym = id._class.symbol; // FindClass(id._class.name);
7240             /* TODO: Name Space Fix ups
7241             if(!id.classSym)
7242                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7243             */
7244          }
7245
7246          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7247          {
7248             exp.expType = ProcessTypeString("Module", true);
7249             break;
7250          }
7251          else */if(strstr(id.string, "__ecereClass") == id.string)
7252          {
7253             exp.expType = ProcessTypeString("ecere::com::Class", true);
7254             break;
7255          }
7256          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7257          {
7258             // Added this here as well
7259             ReplaceClassMembers(exp, thisClass);
7260             if(exp.type != identifierExp)
7261             {
7262                ProcessExpressionType(exp);
7263                break;
7264             }
7265
7266             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7267                break;
7268          }
7269          else
7270          {
7271             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7272             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7273             if(!symbol/* && exp.destType*/)
7274             {
7275                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7276                   break;
7277                else
7278                {
7279                   if(thisClass)
7280                   {
7281                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7282                      if(exp.type != identifierExp)
7283                      {
7284                         ProcessExpressionType(exp);
7285                         break;
7286                      }
7287                   }
7288                   // Static methods called from inside the _class
7289                   else if(currentClass && !id._class)
7290                   {
7291                      if(ResolveIdWithClass(exp, currentClass, true))
7292                         break;
7293                   }
7294                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7295                }
7296             }
7297
7298             // If we manage to resolve this symbol
7299             if(symbol)
7300             {
7301                Type type = symbol.type;
7302                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7303
7304                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7305                {
7306                   Context context = SetupTemplatesContext(_class);
7307                   type = ReplaceThisClassType(_class);
7308                   FinishTemplatesContext(context);
7309                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7310                }
7311
7312                FreeSpecifier(id._class);
7313                id._class = null;
7314                delete id.string;
7315                id.string = CopyString(symbol.string);
7316
7317                id.classSym = null;
7318                exp.expType = type;
7319                if(type)
7320                   type.refCount++;
7321                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7322                   // Add missing cases here... enum Classes...
7323                   exp.isConstant = true;
7324
7325                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7326                if(symbol.isParam || !strcmp(id.string, "this"))
7327                {
7328                   if(_class && _class.type == structClass)
7329                      exp.byReference = true;
7330                   
7331                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7332                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) && 
7333                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) || 
7334                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7335                   {
7336                      Identifier id = exp.identifier;
7337                      exp.type = bracketsExp;
7338                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7339                   }*/
7340                }
7341
7342                if(symbol.isIterator)
7343                {
7344                   if(symbol.isIterator == 3)
7345                   {
7346                      exp.type = bracketsExp;
7347                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7348                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7349                      exp.expType = null;
7350                      ProcessExpressionType(exp);                     
7351                   }
7352                   else if(symbol.isIterator != 4)
7353                   {
7354                      exp.type = memberExp;
7355                      exp.member.exp = MkExpIdentifier(exp.identifier);
7356                      exp.member.exp.expType = exp.expType;
7357                      /*if(symbol.isIterator == 6)
7358                         exp.member.member = MkIdentifier("key");
7359                      else*/
7360                         exp.member.member = MkIdentifier("data");
7361                      exp.expType = null;
7362                      ProcessExpressionType(exp);
7363                   }
7364                }
7365                break;
7366             }
7367             else
7368             {
7369                DefinedExpression definedExp = null;
7370                if(thisNameSpace && !(id._class && !id._class.name))
7371                {
7372                   char name[1024];
7373                   strcpy(name, thisNameSpace);
7374                   strcat(name, "::");
7375                   strcat(name, id.string);
7376                   definedExp = eSystem_FindDefine(privateModule, name);
7377                }
7378                if(!definedExp)
7379                   definedExp = eSystem_FindDefine(privateModule, id.string);
7380                if(definedExp)
7381                {
7382                   int c;
7383                   for(c = 0; c<definedExpStackPos; c++)
7384                      if(definedExpStack[c] == definedExp)
7385                         break;
7386                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7387                   {
7388                      Location backupYylloc = yylloc;
7389                      definedExpStack[definedExpStackPos++] = definedExp;
7390                      fileInput = TempFile { };
7391                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7392                      fileInput.Seek(0, start);
7393
7394                      echoOn = false;
7395                      parsedExpression = null;
7396                      resetScanner();
7397                      expression_yyparse();
7398                      delete fileInput;
7399
7400                      yylloc = backupYylloc;
7401
7402                      if(parsedExpression)
7403                      {
7404                         FreeIdentifier(id);
7405                         exp.type = bracketsExp;
7406                         exp.list = MkListOne(parsedExpression);
7407                         parsedExpression.loc = yylloc;
7408                         ProcessExpressionType(exp);
7409                         definedExpStackPos--;
7410                         return;
7411                      }
7412                      definedExpStackPos--;
7413                   }
7414                   else
7415                   {
7416                      if(inCompiler)
7417                      {
7418                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7419                      }
7420                   }
7421                }
7422                else
7423                {
7424                   GlobalData data = null;
7425                   if(thisNameSpace && !(id._class && !id._class.name))
7426                   {
7427                      char name[1024];
7428                      strcpy(name, thisNameSpace);
7429                      strcat(name, "::");
7430                      strcat(name, id.string);
7431                      data = FindGlobalData(name);
7432                   }
7433                   if(!data)
7434                      data = FindGlobalData(id.string);
7435                   if(data)
7436                   {
7437                      DeclareGlobalData(data);
7438                      exp.expType = data.dataType;
7439                      if(data.dataType) data.dataType.refCount++;
7440
7441                      delete id.string;
7442                      id.string = CopyString(data.fullName);
7443                      FreeSpecifier(id._class);
7444                      id._class = null;
7445
7446                      break;
7447                   }
7448                   else
7449                   {
7450                      GlobalFunction function = null;
7451                      if(thisNameSpace && !(id._class && !id._class.name))
7452                      {
7453                         char name[1024];
7454                         strcpy(name, thisNameSpace);
7455                         strcat(name, "::");
7456                         strcat(name, id.string);
7457                         function = eSystem_FindFunction(privateModule, name);
7458                      }
7459                      if(!function)
7460                         function = eSystem_FindFunction(privateModule, id.string);
7461                      if(function)
7462                      {
7463                         char name[1024];
7464                         delete id.string;
7465                         id.string = CopyString(function.name);
7466                         name[0] = 0;
7467
7468                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7469                            strcpy(name, "__ecereFunction_");
7470                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7471                         if(DeclareFunction(function, name))
7472                         {
7473                            delete id.string;
7474                            id.string = CopyString(name);
7475                         }
7476                         exp.expType = function.dataType;
7477                         if(function.dataType) function.dataType.refCount++;
7478
7479                         FreeSpecifier(id._class);
7480                         id._class = null;
7481
7482                         break;
7483                      }
7484                   }
7485                }
7486             }
7487          }
7488          unresolved = true;
7489          break;
7490       }
7491       case instanceExp:
7492       {
7493          Class _class;
7494          // Symbol classSym;
7495
7496          if(!exp.instance._class)
7497          {
7498             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7499             {
7500                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7501             }
7502          }
7503
7504          //classSym = FindClass(exp.instance._class.fullName);
7505          //_class = classSym ? classSym.registered : null;
7506
7507          ProcessInstantiationType(exp.instance);
7508          exp.isConstant = exp.instance.isConstant;
7509
7510          /*
7511          if(_class.type == unitClass && _class.base.type != systemClass)
7512          {
7513             {
7514                Type destType = exp.destType;
7515
7516                exp.destType = MkClassType(_class.base.fullName);
7517                exp.expType = MkClassType(_class.fullName);
7518                CheckExpressionType(exp, exp.destType, true);
7519
7520                exp.destType = destType;
7521             }
7522             exp.expType = MkClassType(_class.fullName);
7523          }
7524          else*/
7525          if(exp.instance._class)
7526          {
7527             exp.expType = MkClassType(exp.instance._class.name);
7528             /*if(exp.expType._class && exp.expType._class.registered && 
7529                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7530                exp.expType.byReference = true;*/
7531          }         
7532          break;
7533       }
7534       case constantExp:
7535       {
7536          if(!exp.expType)
7537          {
7538             Type type
7539             {
7540                refCount = 1;
7541                constant = true;
7542             };
7543             exp.expType = type;
7544
7545             if(exp.constant[0] == '\'')
7546             {
7547                if((int)((byte *)exp.constant)[1] > 127)
7548                {
7549                   int nb;
7550                   unichar ch = UTF8GetChar(exp.constant + 1, &nb);
7551                   if(nb < 2) ch = exp.constant[1];
7552                   delete exp.constant;
7553                   exp.constant = PrintUInt(ch);
7554                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7555                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7556                   type._class = FindClass("unichar");
7557
7558                   type.isSigned = false;
7559                }
7560                else
7561                {
7562                   type.kind = charType;
7563                   type.isSigned = true;
7564                }
7565             }
7566             else if(strchr(exp.constant, '.'))
7567             {
7568                char ch = exp.constant[strlen(exp.constant)-1];
7569                if(ch == 'f')
7570                   type.kind = floatType;
7571                else
7572                   type.kind = doubleType;
7573                type.isSigned = true;
7574             }
7575             else
7576             {
7577                if(exp.constant[0] == '0' && exp.constant[1])
7578                   type.isSigned = false;
7579                else if(strchr(exp.constant, 'L') || strchr(exp.constant, 'l'))
7580                   type.isSigned = false;
7581                else if(strtoll(exp.constant, null, 0) > MAXINT)
7582                   type.isSigned = false;
7583                else
7584                   type.isSigned = true;
7585                type.kind = intType;
7586             }
7587             exp.isConstant = true;
7588          }
7589          break;
7590       }
7591       case stringExp:
7592       {
7593          exp.isConstant = true;      // Why wasn't this constant?
7594          exp.expType = Type
7595          {
7596             refCount = 1;
7597             kind = pointerType;
7598             type = Type
7599             {
7600                refCount = 1;
7601                kind = charType;
7602                constant = true;
7603             }
7604          };
7605          break;
7606       }
7607       case newExp:
7608       case new0Exp:
7609          ProcessExpressionType(exp._new.size);
7610          exp.expType = Type
7611          {
7612             refCount = 1;
7613             kind = pointerType;
7614             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7615          };
7616          DeclareType(exp.expType.type, false, false);
7617          break;
7618       case renewExp:
7619       case renew0Exp:
7620          ProcessExpressionType(exp._renew.size);
7621          ProcessExpressionType(exp._renew.exp);
7622          exp.expType = Type
7623          {
7624             refCount = 1;
7625             kind = pointerType;
7626             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7627          };
7628          DeclareType(exp.expType.type, false, false);
7629          break;
7630       case opExp:
7631       {
7632          bool assign = false, boolResult = false, boolOps = false;
7633          Type type1 = null, type2 = null;
7634          bool useDestType = false, useSideType = false;
7635          Location oldyylloc = yylloc;
7636          bool useSideUnit = false;
7637
7638          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7639          Type dummy
7640          {
7641             count = 1;
7642             refCount = 1;
7643          };
7644
7645          switch(exp.op.op)
7646          {
7647             // Assignment Operators
7648             case '=': 
7649             case MUL_ASSIGN:
7650             case DIV_ASSIGN:
7651             case MOD_ASSIGN:
7652             case ADD_ASSIGN:
7653             case SUB_ASSIGN:
7654             case LEFT_ASSIGN:
7655             case RIGHT_ASSIGN:
7656             case AND_ASSIGN:
7657             case XOR_ASSIGN:
7658             case OR_ASSIGN:
7659                assign = true;
7660                break;
7661             // boolean Operators
7662             case '!':
7663                // Expect boolean operators
7664                //boolOps = true;
7665                //boolResult = true;
7666                break;
7667             case AND_OP:
7668             case OR_OP:
7669                // Expect boolean operands
7670                boolOps = true;
7671                boolResult = true;
7672                break;
7673             // Comparisons
7674             case EQ_OP:
7675             case '<':
7676             case '>':
7677             case LE_OP:
7678             case GE_OP:
7679             case NE_OP:
7680                // Gives boolean result
7681                boolResult = true;
7682                useSideType = true;
7683                break;
7684             case '+':
7685             case '-':
7686                useSideUnit = true;
7687
7688                // Just added these... testing
7689             case '|':
7690             case '&':
7691             case '^':
7692
7693             // DANGER: Verify units
7694             case '/':
7695             case '%':
7696             case '*':
7697                
7698                if(exp.op.op != '*' || exp.op.exp1)
7699                {
7700                   useSideType = true;
7701                   useDestType = true;
7702                }
7703                break;
7704
7705             /*// Implement speed etc.
7706             case '*':
7707             case '/':
7708                break;
7709             */
7710          }
7711          if(exp.op.op == '&')
7712          {
7713             // Added this here earlier for Iterator address as key
7714             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
7715             {
7716                Identifier id = exp.op.exp2.identifier;
7717                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7718                if(symbol && symbol.isIterator == 2)
7719                {
7720                   exp.type = memberExp;
7721                   exp.member.exp = exp.op.exp2;
7722                   exp.member.member = MkIdentifier("key");
7723                   exp.expType = null;
7724                   exp.op.exp2.expType = symbol.type;
7725                   symbol.type.refCount++;
7726                   ProcessExpressionType(exp);
7727                   FreeType(dummy);
7728                   break;
7729                }
7730                // exp.op.exp2.usage.usageRef = true;
7731             }
7732          }
7733
7734          //dummy.kind = TypeDummy;
7735
7736          if(exp.op.exp1)
7737          {
7738             if(exp.destType && exp.destType.kind == classType &&
7739                exp.destType._class && exp.destType._class.registered && useDestType &&
7740                
7741               ((exp.destType._class.registered.type == unitClass && useSideUnit) || 
7742                exp.destType._class.registered.type == enumClass ||
7743                exp.destType._class.registered.type == bitClass
7744                )) 
7745
7746               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
7747             {
7748                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7749                exp.op.exp1.destType = exp.destType;
7750                if(exp.destType)
7751                   exp.destType.refCount++;
7752             }
7753             else if(!assign)
7754             {
7755                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7756                exp.op.exp1.destType = dummy;
7757                dummy.refCount++;               
7758             }
7759
7760             // TESTING THIS HERE...
7761             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
7762             ProcessExpressionType(exp.op.exp1);
7763             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
7764
7765             if(exp.op.exp1.destType == dummy)
7766             {
7767                FreeType(dummy);
7768                exp.op.exp1.destType = null;
7769             }
7770             type1 = exp.op.exp1.expType;
7771          }
7772
7773          if(exp.op.exp2)
7774          {
7775             char expString[10240];
7776             expString[0] = '\0';
7777             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
7778             {
7779                if(exp.op.exp1)
7780                {
7781                   exp.op.exp2.destType = exp.op.exp1.expType;
7782                   if(exp.op.exp1.expType)
7783                      exp.op.exp1.expType.refCount++;
7784                }
7785                else
7786                {
7787                   exp.op.exp2.destType = exp.destType;
7788                   if(exp.destType)
7789                      exp.destType.refCount++;
7790                }
7791
7792                if(type1) type1.refCount++;
7793                exp.expType = type1;
7794             }
7795             else if(assign)
7796             {
7797                if(inCompiler)
7798                   PrintExpression(exp.op.exp2, expString);
7799
7800                if(type1 && type1.kind == pointerType)
7801                {
7802                   if(exp.op.op == MUL_ASSIGN || exp.op.op == DIV_ASSIGN ||exp.op.op == MOD_ASSIGN ||exp.op.op == LEFT_ASSIGN ||exp.op.op == RIGHT_ASSIGN ||
7803                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
7804                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
7805                   else if(exp.op.op == '=')
7806                   {
7807                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7808                      exp.op.exp2.destType = type1;
7809                      if(type1)
7810                         type1.refCount++;
7811                   }
7812                }
7813                else
7814                {
7815                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;) 
7816                   if(exp.op.op == MUL_ASSIGN || exp.op.op == DIV_ASSIGN ||exp.op.op == MOD_ASSIGN ||exp.op.op == LEFT_ASSIGN ||exp.op.op == RIGHT_ASSIGN/* ||
7817                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
7818                   else
7819                   {
7820                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7821                      exp.op.exp2.destType = type1;
7822                      if(type1)
7823                         type1.refCount++;
7824                   }
7825                }
7826                if(type1) type1.refCount++;
7827                exp.expType = type1;
7828             }
7829             else if(exp.destType && exp.destType.kind == classType &&
7830                exp.destType._class && exp.destType._class.registered && 
7831                
7832                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) || 
7833                   (exp.destType._class.registered.type == enumClass && useDestType)) 
7834                   )
7835             {
7836                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7837                exp.op.exp2.destType = exp.destType;
7838                if(exp.destType)
7839                   exp.destType.refCount++;
7840             }
7841             else
7842             {
7843                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7844                exp.op.exp2.destType = dummy;
7845                dummy.refCount++;
7846             }
7847
7848             // TESTING THIS HERE... (DANGEROUS)
7849             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered && 
7850                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
7851             {
7852                FreeType(exp.op.exp2.destType);
7853                exp.op.exp2.destType = type1;
7854                type1.refCount++;
7855             }
7856             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
7857             ProcessExpressionType(exp.op.exp2);
7858             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
7859
7860             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
7861             {
7862                if(exp.op.exp2.expType.kind == intSizeType || exp.op.exp2.expType.kind == intPtrType || exp.op.exp2.expType.kind == int64Type || exp.op.exp2.expType.kind == intType || exp.op.exp2.expType.kind == shortType || exp.op.exp2.expType.kind == charType)
7863                {
7864                   if(exp.op.op != '=' && type1.type.kind == voidType) 
7865                      Compiler_Error($"void *: unknown size\n");
7866                }
7867                else if(exp.op.exp2.expType.kind == pointerType || exp.op.exp2.expType.kind == arrayType || exp.op.exp2.expType.kind == functionType || exp.op.exp2.expType.kind == methodType|| 
7868                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
7869                               (exp.op.exp2.expType._class.registered.type == normalClass || 
7870                               exp.op.exp2.expType._class.registered.type == structClass ||
7871                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
7872                {
7873                   if(exp.op.op == ADD_ASSIGN)
7874                      Compiler_Error($"cannot add two pointers\n");                   
7875                }
7876                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType && 
7877                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
7878                {
7879                   if(exp.op.op == ADD_ASSIGN)
7880                      Compiler_Error($"cannot add two pointers\n");                   
7881                }
7882                else if(inCompiler)
7883                {
7884                   char type1String[1024];
7885                   char type2String[1024];
7886                   type1String[0] = '\0';
7887                   type2String[0] = '\0';
7888                   
7889                   PrintType(exp.op.exp2.expType, type1String, false, true);
7890                   PrintType(type1, type2String, false, true);
7891                   ChangeCh(expString, '\n', ' ');
7892                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
7893                }
7894             }
7895
7896             if(exp.op.exp2.destType == dummy)
7897             {
7898                FreeType(dummy);
7899                exp.op.exp2.destType = null;
7900             }
7901
7902             type2 = exp.op.exp2.expType;
7903          }
7904
7905          dummy.kind = voidType;
7906
7907          if(exp.op.op == SIZEOF)
7908          {
7909             exp.expType = Type
7910             {
7911                refCount = 1;
7912                kind = intType;
7913             };
7914             exp.isConstant = true;
7915          }
7916          // Get type of dereferenced pointer
7917          else if(exp.op.op == '*' && !exp.op.exp1)
7918          {
7919             exp.expType = Dereference(type2);
7920             if(type2 && type2.kind == classType)
7921                notByReference = true;
7922          }
7923          else if(exp.op.op == '&' && !exp.op.exp1)
7924             exp.expType = Reference(type2);
7925          else if(!assign)
7926          {
7927             if(boolOps)
7928             {
7929                if(exp.op.exp1) 
7930                {
7931                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7932                   exp.op.exp1.destType = MkClassType("bool");
7933                   exp.op.exp1.destType.truth = true;
7934                   if(!exp.op.exp1.expType)
7935                      ProcessExpressionType(exp.op.exp1);
7936                   else
7937                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
7938                   FreeType(exp.op.exp1.expType);
7939                   exp.op.exp1.expType = MkClassType("bool");
7940                   exp.op.exp1.expType.truth = true;
7941                }
7942                if(exp.op.exp2) 
7943                {
7944                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7945                   exp.op.exp2.destType = MkClassType("bool");
7946                   exp.op.exp2.destType.truth = true;
7947                   if(!exp.op.exp2.expType)
7948                      ProcessExpressionType(exp.op.exp2);
7949                   else
7950                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
7951                   FreeType(exp.op.exp2.expType);
7952                   exp.op.exp2.expType = MkClassType("bool");
7953                   exp.op.exp2.expType.truth = true;
7954                }
7955             }
7956             else if(exp.op.exp1 && exp.op.exp2 && 
7957                ((useSideType /*&& 
7958                      (useSideUnit || 
7959                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
7960                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
7961                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) && 
7962                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
7963             {
7964                if(type1 && type2 &&
7965                   // If either both are class or both are not class
7966                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
7967                {
7968                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7969                   exp.op.exp2.destType = type1;
7970                   type1.refCount++;
7971                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7972                   exp.op.exp1.destType = type2;
7973                   type2.refCount++;
7974                   // Warning here for adding Radians + Degrees with no destination type
7975                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) && 
7976                      type1._class.registered && type1._class.registered.type == unitClass && 
7977                      type2._class.registered && type2._class.registered.type == unitClass && 
7978                      type1._class.registered != type2._class.registered)
7979                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
7980                         type1._class.string, type2._class.string, type1._class.string);
7981
7982                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
7983                   {
7984                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
7985                      if(argExp)
7986                      {
7987                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
7988
7989                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
7990                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), 
7991                            exp.op.exp1)));
7992
7993                         ProcessExpressionType(exp.op.exp1);
7994
7995                         if(type2.kind != pointerType)
7996                         {
7997                            ProcessExpressionType(classExp);
7998
7999                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', 
8000                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8001                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8002                                  // noHeadClass
8003                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8004                                     OR_OP, 
8005                                  // normalClass
8006                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8007                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8008                                        MkPointer(null, null), null)))),                                  
8009                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8010
8011                            if(!exp.op.exp2.expType)
8012                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8013
8014                            ProcessExpressionType(exp.op.exp2);
8015                         }
8016                      }
8017                   }
8018                   
8019                   if(!boolResult && ((type1.kind == pointerType || type1.kind == arrayType || (type1.kind == classType && !strcmp(type1._class.string, "String"))) && (type2.kind == intSizeType || type2.kind == intPtrType || type2.kind == int64Type || type2.kind == intType || type2.kind == shortType || type2.kind == charType)))
8020                   {
8021                      if(type1.kind != classType && type1.type.kind == voidType) 
8022                         Compiler_Error($"void *: unknown size\n");
8023                      exp.expType = type1;
8024                      if(type1) type1.refCount++;
8025                   }
8026                   else if(!boolResult && ((type2.kind == pointerType || type2.kind == arrayType || (type2.kind == classType && !strcmp(type2._class.string, "String"))) && (type1.kind == intSizeType || type1.kind == intPtrType || type1.kind == int64Type || type1.kind == intType || type1.kind == shortType || type1.kind == charType)))
8027                   {
8028                      if(type2.kind != classType && type2.type.kind == voidType) 
8029                         Compiler_Error($"void *: unknown size\n");
8030                      exp.expType = type2;
8031                      if(type2) type2.refCount++;
8032                   }
8033                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8034                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8035                   {
8036                      Compiler_Warning($"different levels of indirection\n");
8037                   }
8038                   else 
8039                   {
8040                      bool success = false;
8041                      if(type1.kind == pointerType && type2.kind == pointerType)
8042                      {
8043                         if(exp.op.op == '+')
8044                            Compiler_Error($"cannot add two pointers\n");
8045                         else if(exp.op.op == '-')
8046                         {
8047                            // Pointer Subtraction gives integer
8048                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8049                            {
8050                               exp.expType = Type
8051                               {
8052                                  kind = intType;
8053                                  refCount = 1;
8054                               };
8055                               success = true;
8056
8057                               if(type1.type.kind == templateType)
8058                               {
8059                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8060                                  if(argExp)
8061                                  {
8062                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8063
8064                                     ProcessExpressionType(classExp);
8065
8066                                     exp.type = bracketsExp;
8067                                     exp.list = MkListOne(MkExpOp(
8068                                        MkExpBrackets(MkListOne(MkExpOp(
8069                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8070                                              , exp.op.op, 
8071                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/', 
8072                                           
8073                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8074
8075                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8076                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8077                                                 // noHeadClass
8078                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8079                                                    OR_OP, 
8080                                                 // normalClass
8081                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8082                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8083                                                       MkPointer(null, null), null)))),                                  
8084                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8085
8086                                              
8087                                              ));
8088                                     
8089                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8090                                     FreeType(dummy);
8091                                     return;                                       
8092                                  }
8093                               }
8094                            }
8095                         }
8096                      }
8097
8098                      if(!success && exp.op.exp1.type == constantExp)
8099                      {
8100                         // If first expression is constant, try to match that first
8101                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8102                         {
8103                            if(exp.expType) FreeType(exp.expType);
8104                            exp.expType = exp.op.exp1.destType;
8105                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8106                            success = true;
8107                         }
8108                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8109                         {
8110                            if(exp.expType) FreeType(exp.expType);
8111                            exp.expType = exp.op.exp2.destType;
8112                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8113                            success = true;
8114                         }
8115                      }
8116                      else if(!success)
8117                      {
8118                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8119                         {
8120                            if(exp.expType) FreeType(exp.expType);
8121                            exp.expType = exp.op.exp2.destType;
8122                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8123                            success = true;
8124                         }
8125                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8126                         {
8127                            if(exp.expType) FreeType(exp.expType);
8128                            exp.expType = exp.op.exp1.destType;
8129                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8130                            success = true;
8131                         }
8132                      }
8133                      if(!success)
8134                      {
8135                         char expString1[10240];
8136                         char expString2[10240];
8137                         char type1[1024];
8138                         char type2[1024];
8139                         expString1[0] = '\0';
8140                         expString2[0] = '\0';
8141                         type1[0] = '\0';
8142                         type2[0] = '\0';
8143                         if(inCompiler)
8144                         {
8145                            PrintExpression(exp.op.exp1, expString1);
8146                            ChangeCh(expString1, '\n', ' ');
8147                            PrintExpression(exp.op.exp2, expString2);
8148                            ChangeCh(expString2, '\n', ' ');
8149                            PrintType(exp.op.exp1.expType, type1, false, true);
8150                            PrintType(exp.op.exp2.expType, type2, false, true);
8151                         }
8152
8153                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8154                      }
8155                   }
8156                }
8157                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8158                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8159                {
8160                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8161                   // Convert e.g. / 4 into / 4.0
8162                   exp.op.exp1.destType = type2._class.registered.dataType;
8163                   if(type2._class.registered.dataType)
8164                      type2._class.registered.dataType.refCount++;
8165                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8166                   exp.expType = type2;
8167                   if(type2) type2.refCount++;
8168                }
8169                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8170                {
8171                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8172                   // Convert e.g. / 4 into / 4.0
8173                   exp.op.exp2.destType = type1._class.registered.dataType;
8174                   if(type1._class.registered.dataType)
8175                      type1._class.registered.dataType.refCount++;
8176                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8177                   exp.expType = type1;
8178                   if(type1) type1.refCount++;
8179                }
8180                else if(type1)
8181                {
8182                   bool valid = false;
8183
8184                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8185                   {
8186                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8187
8188                      if(!type1._class.registered.dataType)
8189                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8190                      exp.op.exp2.destType = type1._class.registered.dataType;
8191                      exp.op.exp2.destType.refCount++;
8192
8193                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8194                      type2 = exp.op.exp2.destType;
8195
8196                      exp.expType = type2;
8197                      type2.refCount++;
8198                   }
8199                   
8200                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8201                   {
8202                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8203
8204                      if(!type2._class.registered.dataType)
8205                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8206                      exp.op.exp1.destType = type2._class.registered.dataType;
8207                      exp.op.exp1.destType.refCount++;
8208
8209                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8210                      type1 = exp.op.exp1.destType;
8211                      exp.expType = type1;
8212                      type1.refCount++;
8213                   }
8214
8215                   // TESTING THIS NEW CODE
8216                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8217                   {
8218                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8219                      {
8220                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8221                         {
8222                            if(exp.expType) FreeType(exp.expType);
8223                            exp.expType = exp.op.exp1.expType;
8224                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8225                            valid = true;
8226                         }
8227                      }
8228
8229                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8230                      {
8231                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8232                         {
8233                            if(exp.expType) FreeType(exp.expType);
8234                            exp.expType = exp.op.exp2.expType;
8235                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8236                            valid = true;
8237                         }
8238                      }
8239                   }
8240
8241                   if(!valid)
8242                   {
8243                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8244                      exp.op.exp2.destType = type1;
8245                      type1.refCount++;
8246
8247                      /*
8248                      // Maybe this was meant to be an enum...
8249                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8250                      {
8251                         Type oldType = exp.op.exp2.expType;
8252                         exp.op.exp2.expType = null;
8253                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8254                            FreeType(oldType);
8255                         else
8256                            exp.op.exp2.expType = oldType;
8257                      }
8258                      */
8259
8260                      /*
8261                      // TESTING THIS HERE... LATEST ADDITION
8262                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8263                      {
8264                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8265                         exp.op.exp2.destType = type2._class.registered.dataType;
8266                         if(type2._class.registered.dataType)
8267                            type2._class.registered.dataType.refCount++;
8268                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8269                         
8270                         //exp.expType = type2._class.registered.dataType; //type2;
8271                         //if(type2) type2.refCount++;
8272                      }
8273
8274                      // TESTING THIS HERE... LATEST ADDITION
8275                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8276                      {
8277                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8278                         exp.op.exp1.destType = type1._class.registered.dataType;
8279                         if(type1._class.registered.dataType)
8280                            type1._class.registered.dataType.refCount++;
8281                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8282                         exp.expType = type1._class.registered.dataType; //type1;
8283                         if(type1) type1.refCount++;
8284                      }
8285                      */
8286
8287                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8288                      {
8289                         if(exp.expType) FreeType(exp.expType);
8290                         exp.expType = exp.op.exp2.destType;
8291                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8292                      }
8293                      else if(type1 && type2)
8294                      {
8295                         char expString1[10240];
8296                         char expString2[10240];
8297                         char type1String[1024];
8298                         char type2String[1024];
8299                         expString1[0] = '\0';
8300                         expString2[0] = '\0';
8301                         type1String[0] = '\0';
8302                         type2String[0] = '\0';
8303                         if(inCompiler)
8304                         {
8305                            PrintExpression(exp.op.exp1, expString1);
8306                            ChangeCh(expString1, '\n', ' ');
8307                            PrintExpression(exp.op.exp2, expString2);
8308                            ChangeCh(expString2, '\n', ' ');
8309                            PrintType(exp.op.exp1.expType, type1String, false, true);
8310                            PrintType(exp.op.exp2.expType, type2String, false, true);
8311                         }
8312
8313                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8314
8315                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8316                         {
8317                            exp.expType = exp.op.exp1.expType;
8318                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8319                         }
8320                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8321                         {
8322                            exp.expType = exp.op.exp2.expType;
8323                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8324                         }
8325                      }
8326                   }
8327                }
8328                else if(type2)
8329                {
8330                   // Maybe this was meant to be an enum...
8331                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8332                   {
8333                      Type oldType = exp.op.exp1.expType;
8334                      exp.op.exp1.expType = null;
8335                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8336                         FreeType(oldType);
8337                      else
8338                         exp.op.exp1.expType = oldType;
8339                   }
8340
8341                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8342                   exp.op.exp1.destType = type2;
8343                   type2.refCount++;
8344                   /*
8345                   // TESTING THIS HERE... LATEST ADDITION
8346                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8347                   {
8348                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8349                      exp.op.exp1.destType = type1._class.registered.dataType;
8350                      if(type1._class.registered.dataType)
8351                         type1._class.registered.dataType.refCount++;
8352                   }
8353
8354                   // TESTING THIS HERE... LATEST ADDITION
8355                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8356                   {
8357                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8358                      exp.op.exp2.destType = type2._class.registered.dataType;
8359                      if(type2._class.registered.dataType)
8360                         type2._class.registered.dataType.refCount++;
8361                   }
8362                   */
8363
8364                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8365                   {
8366                      if(exp.expType) FreeType(exp.expType);
8367                      exp.expType = exp.op.exp1.destType;
8368                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8369                   }
8370                }
8371             }
8372             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8373             {
8374                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8375                {
8376                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8377                   // Convert e.g. / 4 into / 4.0
8378                   exp.op.exp1.destType = type2._class.registered.dataType;
8379                   if(type2._class.registered.dataType)
8380                      type2._class.registered.dataType.refCount++;
8381                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8382                }
8383                if(exp.op.op == '!')
8384                {
8385                   exp.expType = MkClassType("bool");
8386                   exp.expType.truth = true;
8387                }
8388                else
8389                {
8390                   exp.expType = type2;
8391                   if(type2) type2.refCount++;
8392                }
8393             }
8394             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8395             {
8396                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8397                {
8398                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8399                   // Convert e.g. / 4 into / 4.0
8400                   exp.op.exp2.destType = type1._class.registered.dataType;
8401                   if(type1._class.registered.dataType)
8402                      type1._class.registered.dataType.refCount++;
8403                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8404                }
8405                exp.expType = type1;
8406                if(type1) type1.refCount++;
8407             }
8408          }
8409          
8410          yylloc = exp.loc;
8411          if(exp.op.exp1 && !exp.op.exp1.expType)
8412          {
8413             char expString[10000];
8414             expString[0] = '\0';
8415             if(inCompiler)
8416             {
8417                PrintExpression(exp.op.exp1, expString);
8418                ChangeCh(expString, '\n', ' ');
8419             }
8420             if(expString[0])
8421                Compiler_Error($"couldn't determine type of %s\n", expString);
8422          }
8423          if(exp.op.exp2 && !exp.op.exp2.expType)
8424          {
8425             char expString[10240];
8426             expString[0] = '\0';
8427             if(inCompiler)
8428             {
8429                PrintExpression(exp.op.exp2, expString);
8430                ChangeCh(expString, '\n', ' ');
8431             }
8432             if(expString[0])
8433                Compiler_Error($"couldn't determine type of %s\n", expString);
8434          }
8435
8436          if(boolResult)
8437          {
8438             FreeType(exp.expType);
8439             exp.expType = MkClassType("bool");
8440             exp.expType.truth = true;
8441          }
8442
8443          if(exp.op.op != SIZEOF)
8444             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8445                (!exp.op.exp2 || exp.op.exp2.isConstant);
8446
8447          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8448          {
8449             DeclareType(exp.op.exp2.expType, false, false);
8450          }
8451
8452          yylloc = oldyylloc;
8453
8454          FreeType(dummy);
8455          break;
8456       }
8457       case bracketsExp:
8458       case extensionExpressionExp:
8459       {
8460          Expression e;
8461          exp.isConstant = true;
8462          for(e = exp.list->first; e; e = e.next)
8463          {
8464             bool inced = false;
8465             if(!e.next)
8466             {
8467                FreeType(e.destType);
8468                e.destType = exp.destType;
8469                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8470             }
8471             ProcessExpressionType(e);
8472             if(inced)
8473                exp.destType.count--;
8474             if(!exp.expType && !e.next)
8475             {
8476                exp.expType = e.expType;
8477                if(e.expType) e.expType.refCount++;
8478             }
8479             if(!e.isConstant)
8480                exp.isConstant = false;
8481          }
8482
8483          // In case a cast became a member...
8484          e = exp.list->first;
8485          if(!e.next && e.type == memberExp)
8486          {
8487             // Preserve prev, next
8488             Expression next = exp.next, prev = exp.prev;
8489
8490
8491             FreeType(exp.expType);
8492             FreeType(exp.destType);
8493             delete exp.list;
8494             
8495             *exp = *e;
8496
8497             exp.prev = prev;
8498             exp.next = next;
8499
8500             delete e;
8501
8502             ProcessExpressionType(exp);
8503          }
8504          break;
8505       }
8506       case indexExp:
8507       {
8508          Expression e;
8509          exp.isConstant = true;
8510
8511          ProcessExpressionType(exp.index.exp);
8512          if(!exp.index.exp.isConstant)
8513             exp.isConstant = false;
8514
8515          if(exp.index.exp.expType)
8516          {
8517             Type source = exp.index.exp.expType;
8518             if(source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
8519                eClass_IsDerived(source._class.registered, containerClass) && 
8520                source._class.registered.templateArgs)
8521             {
8522                Class _class = source._class.registered;
8523                exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8524
8525                if(exp.index.index && exp.index.index->last)
8526                {
8527                   ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8528                }
8529             }
8530          }
8531
8532          for(e = exp.index.index->first; e; e = e.next)
8533          {
8534             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8535             {
8536                if(e.destType) FreeType(e.destType);
8537                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8538             }
8539             ProcessExpressionType(e);
8540             if(!e.next)
8541             {
8542                // Check if this type is int
8543             }
8544             if(!e.isConstant)
8545                exp.isConstant = false;
8546          }
8547
8548          if(!exp.expType)
8549             exp.expType = Dereference(exp.index.exp.expType);
8550          if(exp.expType)
8551             DeclareType(exp.expType, false, false);
8552          break;
8553       }
8554       case callExp:
8555       {
8556          Expression e;
8557          Type functionType;
8558          Type methodType = null;
8559          char name[1024];
8560          name[0] = '\0';
8561
8562          if(inCompiler)
8563          {
8564             PrintExpression(exp.call.exp,  name);
8565             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8566             {
8567                //exp.call.exp.expType = null;
8568                PrintExpression(exp.call.exp,  name);
8569             }
8570          }
8571          if(exp.call.exp.type == identifierExp)
8572          {
8573             Expression idExp = exp.call.exp;
8574             Identifier id = idExp.identifier;
8575             if(!strcmp(id.string, "__builtin_frame_address"))
8576             {
8577                exp.expType = ProcessTypeString("void *", true);
8578                if(exp.call.arguments && exp.call.arguments->first)
8579                   ProcessExpressionType(exp.call.arguments->first);
8580                break;
8581             }
8582             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8583             {
8584                exp.expType = ProcessTypeString("int", true);
8585                if(exp.call.arguments && exp.call.arguments->first)
8586                   ProcessExpressionType(exp.call.arguments->first);
8587                break;
8588             }
8589             else if(!strcmp(id.string, "Max") ||
8590                !strcmp(id.string, "Min") ||
8591                !strcmp(id.string, "Sgn") ||
8592                !strcmp(id.string, "Abs"))
8593             {
8594                Expression a = null;
8595                Expression b = null;
8596                Expression tempExp1 = null, tempExp2 = null;
8597                if((!strcmp(id.string, "Max") ||
8598                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8599                {
8600                   a = exp.call.arguments->first;
8601                   b = exp.call.arguments->last;
8602                   tempExp1 = a;
8603                   tempExp2 = b;
8604                }
8605                else if(exp.call.arguments->count == 1)
8606                {
8607                   a = exp.call.arguments->first;
8608                   tempExp1 = a;
8609                }
8610                
8611                if(a)
8612                {
8613                   exp.call.arguments->Clear();
8614                   idExp.identifier = null;
8615
8616                   FreeExpContents(exp);
8617
8618                   ProcessExpressionType(a);
8619                   if(b)
8620                      ProcessExpressionType(b);
8621
8622                   exp.type = bracketsExp;
8623                   exp.list = MkList();
8624
8625                   if(a.expType && (!b || b.expType))
8626                   {
8627                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
8628                      {
8629                         // Use the simpleStruct name/ids for now...
8630                         if(inCompiler)
8631                         {
8632                            OldList * specs = MkList();
8633                            OldList * decls = MkList();
8634                            Declaration decl;
8635                            char temp1[1024], temp2[1024];
8636
8637                            GetTypeSpecs(a.expType, specs);
8638
8639                            if(a && !a.isConstant && a.type != identifierExp)
8640                            {
8641                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
8642                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
8643                               tempExp1 = QMkExpId(temp1);
8644                               tempExp1.expType = a.expType;
8645                               if(a.expType)
8646                                  a.expType.refCount++;
8647                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
8648                            }
8649                            if(b && !b.isConstant && b.type != identifierExp)
8650                            {
8651                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
8652                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
8653                               tempExp2 = QMkExpId(temp2);
8654                               tempExp2.expType = b.expType;
8655                               if(b.expType)
8656                                  b.expType.refCount++;
8657                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
8658                            }                        
8659
8660                            decl = MkDeclaration(specs, decls);
8661                            if(!curCompound.compound.declarations)
8662                               curCompound.compound.declarations = MkList();
8663                            curCompound.compound.declarations->Insert(null, decl);
8664                         }
8665                      }
8666                   }
8667
8668                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
8669                   {
8670                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
8671                      ListAdd(exp.list, 
8672                         MkExpCondition(MkExpBrackets(MkListOne(
8673                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
8674                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
8675                      exp.expType = a.expType;
8676                      if(a.expType)
8677                         a.expType.refCount++;
8678                   }
8679                   else if(!strcmp(id.string, "Abs"))
8680                   {
8681                      ListAdd(exp.list, 
8682                         MkExpCondition(MkExpBrackets(MkListOne(
8683                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8684                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
8685                      exp.expType = a.expType;
8686                      if(a.expType)
8687                         a.expType.refCount++;
8688                   }
8689                   else if(!strcmp(id.string, "Sgn"))
8690                   {
8691                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
8692                      ListAdd(exp.list, 
8693                         MkExpCondition(MkExpBrackets(MkListOne(
8694                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
8695                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
8696                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8697                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
8698                      exp.expType = ProcessTypeString("int", false);
8699                   }
8700
8701                   FreeExpression(tempExp1);
8702                   if(tempExp2) FreeExpression(tempExp2);
8703
8704                   FreeIdentifier(id);
8705                   break;
8706                }
8707             }
8708          }
8709
8710          {
8711             Type dummy
8712             {
8713                count = 1;
8714                refCount = 1;
8715             };
8716             if(!exp.call.exp.destType)
8717             {
8718                exp.call.exp.destType = dummy;
8719                dummy.refCount++;
8720             }
8721             ProcessExpressionType(exp.call.exp);
8722             if(exp.call.exp.destType == dummy)
8723             {
8724                FreeType(dummy);
8725                exp.call.exp.destType = null;
8726             }
8727             FreeType(dummy);
8728          }
8729
8730          // Check argument types against parameter types
8731          functionType = exp.call.exp.expType;
8732
8733          if(functionType && functionType.kind == TypeKind::methodType)
8734          {
8735             methodType = functionType;
8736             functionType = methodType.method.dataType;
8737             
8738             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
8739             // TOCHECK: Instead of doing this here could this be done per param?
8740             if(exp.call.exp.expType.usedClass)
8741             {
8742                char typeString[1024];
8743                typeString[0] = '\0';
8744                PrintType(functionType, typeString, true, true);
8745                if(strstr(typeString, "thisclass"))
8746                {
8747                   OldList * specs = MkList();
8748                   Declarator decl;
8749                   {
8750                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
8751
8752                      decl = SpecDeclFromString(typeString, specs, null);
8753                      
8754                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
8755                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
8756                         exp.call.exp.expType.usedClass))
8757                         thisClassParams = false;
8758                      
8759                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
8760                      {
8761                         Class backupThisClass = thisClass;
8762                         thisClass = exp.call.exp.expType.usedClass;
8763                         ProcessDeclarator(decl);
8764                         thisClass = backupThisClass;
8765                      }
8766
8767                      thisClassParams = true;
8768
8769                      functionType = ProcessType(specs, decl);
8770                      functionType.refCount = 0;
8771                      FinishTemplatesContext(context);
8772                   }
8773
8774                   FreeList(specs, FreeSpecifier);
8775                   FreeDeclarator(decl);
8776                 }
8777             }
8778          }
8779          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
8780          {
8781             Type type = functionType.type;
8782             if(!functionType.refCount)
8783             {
8784                functionType.type = null;
8785                FreeType(functionType);
8786             }
8787             //methodType = functionType;
8788             functionType = type;
8789          }
8790          if(functionType && functionType.kind != TypeKind::functionType)
8791          {
8792             Compiler_Error($"called object %s is not a function\n", name);
8793          }
8794          else if(functionType)
8795          {
8796             bool emptyParams = false, noParams = false;
8797             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
8798             Type type = functionType.params.first;
8799             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
8800             int extra = 0;
8801             Location oldyylloc = yylloc;
8802
8803             if(!type) emptyParams = true;
8804
8805             // WORKING ON THIS:
8806             if(functionType.extraParam && e && functionType.thisClass)
8807             {
8808                e.destType = MkClassType(functionType.thisClass.string);
8809                e = e.next;
8810             }
8811
8812             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
8813             if(!functionType.staticMethod)
8814             {
8815                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType && 
8816                   memberExp.member.exp.expType._class)
8817                {
8818                   type = MkClassType(memberExp.member.exp.expType._class.string);
8819                   if(e)
8820                   {
8821                      e.destType = type;
8822                      e = e.next;
8823                      type = functionType.params.first;
8824                   }
8825                   else
8826                      type.refCount = 0;
8827                }
8828                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
8829                {
8830                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
8831                   if(e)
8832                   {
8833                      e.destType = type;
8834                      e = e.next;
8835                      type = functionType.params.first;
8836                   }
8837                   else
8838                      type.refCount = 0;
8839                   //extra = 1;
8840                }
8841             }
8842
8843             if(type && type.kind == voidType)
8844             {
8845                noParams = true;
8846                if(!type.refCount) FreeType(type);
8847                type = null;
8848             }
8849
8850             for( ; e; e = e.next)
8851             {
8852                if(!type && !emptyParams)
8853                {
8854                   yylloc = e.loc;
8855                   if(methodType && methodType.methodClass)
8856                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
8857                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
8858                         noParams ? 0 : functionType.params.count);
8859                   else
8860                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
8861                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
8862                         noParams ? 0 : functionType.params.count);
8863                   break;
8864                }
8865
8866                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
8867                {
8868                   Type templatedType = null;
8869                   Class _class = methodType.usedClass;
8870                   ClassTemplateParameter curParam = null;
8871                   int id = 0;
8872                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
8873                   {
8874                      Class sClass;
8875                      for(sClass = _class; sClass; sClass = sClass.base)
8876                      {
8877                         if(sClass.templateClass) sClass = sClass.templateClass;
8878                         id = 0;
8879                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
8880                         {
8881                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
8882                            {
8883                               Class nextClass;
8884                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
8885                               {
8886                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
8887                                  id += nextClass.templateParams.count;
8888                               }
8889                               break;
8890                            }
8891                            id++;
8892                         }
8893                         if(curParam) break;
8894                      }
8895                   }
8896                   if(curParam && _class.templateArgs[id].dataTypeString)
8897                   {
8898                      ClassTemplateArgument arg = _class.templateArgs[id];
8899                      {
8900                         Context context = SetupTemplatesContext(_class);
8901                      
8902                         /*if(!arg.dataType)
8903                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
8904                         templatedType = ProcessTypeString(arg.dataTypeString, false);
8905                         FinishTemplatesContext(context);
8906                      }
8907                      e.destType = templatedType;
8908                      if(templatedType)
8909                      {
8910                         templatedType.passAsTemplate = true;
8911                         // templatedType.refCount++;
8912                      }
8913                   }
8914                   else
8915                   {
8916                      e.destType = type;
8917                      if(type) type.refCount++;
8918                   }
8919                }
8920                else
8921                {
8922                   e.destType = type;
8923                   if(type) type.refCount++;
8924                }
8925                // Don't reach the end for the ellipsis
8926                if(type && type.kind != ellipsisType)
8927                {
8928                   Type next = type.next;
8929                   if(!type.refCount) FreeType(type);
8930                   type = next;
8931                }
8932             }
8933
8934             if(type && type.kind != ellipsisType)
8935             {
8936                if(methodType && methodType.methodClass)
8937                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
8938                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
8939                      functionType.params.count + extra);
8940                else
8941                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
8942                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
8943                      functionType.params.count + extra);
8944             }
8945             yylloc = oldyylloc;
8946             if(type && !type.refCount) FreeType(type);
8947          }
8948          else
8949          {
8950             functionType = Type
8951             {
8952                refCount = 0;
8953                kind = TypeKind::functionType;
8954             };
8955
8956             if(exp.call.exp.type == identifierExp)
8957             {
8958                char * string = exp.call.exp.identifier.string;
8959                if(inCompiler)
8960                {
8961                   Symbol symbol;
8962                   Location oldyylloc = yylloc;
8963
8964                   yylloc = exp.call.exp.identifier.loc;
8965                   if(strstr(string, "__builtin_") == string);
8966                   else
8967                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
8968                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
8969                   globalContext.symbols.Add((BTNode)symbol);
8970                   if(strstr(symbol.string, "::"))
8971                      globalContext.hasNameSpace = true;
8972
8973                   yylloc = oldyylloc;
8974                }
8975             }
8976             else if(exp.call.exp.type == memberExp)
8977             {
8978                /*Compiler_Warning($"%s undefined; assuming returning int\n",
8979                   exp.call.exp.member.member.string);*/
8980             }
8981             else
8982                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
8983
8984             if(!functionType.returnType)
8985             {
8986                functionType.returnType = Type
8987                {
8988                   refCount = 1;
8989                   kind = intType;
8990                };
8991             }
8992          }
8993          if(functionType && functionType.kind == TypeKind::functionType)
8994          {
8995             exp.expType = functionType.returnType;
8996
8997             if(functionType.returnType)
8998                functionType.returnType.refCount++;
8999
9000             if(!functionType.refCount)
9001                FreeType(functionType);
9002          }
9003
9004          if(exp.call.arguments)
9005          {
9006             for(e = exp.call.arguments->first; e; e = e.next)
9007             {
9008                Type destType = e.destType;
9009                ProcessExpressionType(e);
9010             }
9011          }
9012          break;
9013       }
9014       case memberExp:
9015       {
9016          Type type;
9017          Location oldyylloc = yylloc;
9018          bool thisPtr = (exp.member.exp && exp.member.exp.type == identifierExp && !strcmp(exp.member.exp.identifier.string, "this"));
9019          exp.thisPtr = thisPtr;
9020
9021          // DOING THIS LATER NOW...
9022          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9023          {
9024             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9025             /* TODO: Name Space Fix ups
9026             if(!exp.member.member.classSym)
9027                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9028             */
9029          }
9030
9031          ProcessExpressionType(exp.member.exp);
9032          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class && 
9033             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9034          {
9035             exp.isConstant = false;
9036          }
9037          else
9038             exp.isConstant = exp.member.exp.isConstant;
9039          type = exp.member.exp.expType;
9040
9041          yylloc = exp.loc;
9042
9043          if(type && (type.kind == templateType))
9044          {
9045             Class _class = thisClass ? thisClass : currentClass;
9046             ClassTemplateParameter param = null;
9047             if(_class)
9048             {
9049                for(param = _class.templateParams.first; param; param = param.next)
9050                {
9051                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9052                      break;
9053                }
9054             }
9055             if(param && param.defaultArg.member)
9056             {
9057                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9058                if(argExp)
9059                {
9060                   Expression expMember = exp.member.exp;
9061                   Declarator decl;
9062                   OldList * specs = MkList();
9063                   char thisClassTypeString[1024];
9064
9065                   FreeIdentifier(exp.member.member);
9066
9067                   ProcessExpressionType(argExp);
9068
9069                   {
9070                      char * colon = strstr(param.defaultArg.memberString, "::");
9071                      if(colon)
9072                      {
9073                         char className[1024];
9074                         Class sClass;
9075
9076                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9077                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9078                      }
9079                      else
9080                         strcpy(thisClassTypeString, _class.fullName);
9081                   }
9082
9083                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9084
9085                   exp.expType = ProcessType(specs, decl);
9086                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9087                   {
9088                      Class expClass = exp.expType._class.registered;
9089                      Class cClass = null;
9090                      int c;
9091                      int paramCount = 0;
9092                      int lastParam = -1;
9093                      
9094                      char templateString[1024];
9095                      ClassTemplateParameter param;
9096                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9097                      for(cClass = expClass; cClass; cClass = cClass.base)
9098                      {
9099                         int p = 0;
9100                         for(param = cClass.templateParams.first; param; param = param.next)
9101                         {
9102                            int id = p;
9103                            Class sClass;
9104                            ClassTemplateArgument arg;
9105                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9106                            arg = expClass.templateArgs[id];
9107
9108                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9109                            {
9110                               ClassTemplateParameter cParam;
9111                               //int p = numParams - sClass.templateParams.count;
9112                               int p = 0;
9113                               Class nextClass;
9114                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9115                               
9116                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9117                               {
9118                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9119                                  {
9120                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9121                                     {
9122                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9123                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9124                                        break;
9125                                     }
9126                                  }
9127                               }
9128                            }
9129
9130                            {
9131                               char argument[256];
9132                               argument[0] = '\0';
9133                               /*if(arg.name)
9134                               {
9135                                  strcat(argument, arg.name.string);
9136                                  strcat(argument, " = ");
9137                               }*/
9138                               switch(param.type)
9139                               {
9140                                  case expression:
9141                                  {
9142                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9143                                     char expString[1024];
9144                                     OldList * specs = MkList();
9145                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9146                                     Expression exp;
9147                                     char * string = PrintHexUInt64(arg.expression.ui64);
9148                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9149
9150                                     ProcessExpressionType(exp);
9151                                     ComputeExpression(exp);
9152                                     expString[0] = '\0';
9153                                     PrintExpression(exp, expString);
9154                                     strcat(argument, expString);
9155                                     // delete exp;
9156                                     FreeExpression(exp);
9157                                     break;
9158                                  }
9159                                  case identifier:
9160                                  {
9161                                     strcat(argument, arg.member.name);
9162                                     break;
9163                                  }
9164                                  case TemplateParameterType::type:
9165                                  {
9166                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9167                                     {
9168                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9169                                           strcat(argument, thisClassTypeString);
9170                                        else
9171                                           strcat(argument, arg.dataTypeString);
9172                                     }
9173                                     break;
9174                                  }
9175                               }
9176                               if(argument[0])
9177                               {
9178                                  if(paramCount) strcat(templateString, ", ");
9179                                  if(lastParam != p - 1)
9180                                  {
9181                                     strcat(templateString, param.name);
9182                                     strcat(templateString, " = ");
9183                                  }
9184                                  strcat(templateString, argument);
9185                                  paramCount++;
9186                                  lastParam = p;
9187                               }
9188                               p++;
9189                            }               
9190                         }
9191                      }
9192                      {
9193                         int len = strlen(templateString);
9194                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9195                         templateString[len++] = '>';
9196                         templateString[len++] = '\0';
9197                      }
9198                      {
9199                         Context context = SetupTemplatesContext(_class);
9200                         FreeType(exp.expType);
9201                         exp.expType = ProcessTypeString(templateString, false);
9202                         FinishTemplatesContext(context);
9203                      }                     
9204                   }
9205
9206                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9207                   exp.type = bracketsExp;
9208                   exp.list = MkListOne(MkExpOp(null, '*',
9209                   /*opExp;
9210                   exp.op.op = '*';
9211                   exp.op.exp1 = null;
9212                   exp.op.exp2 = */
9213                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9214                      MkExpBrackets(MkListOne(
9215                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9216                            '+',  
9217                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")), 
9218                            '+',
9219                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9220                            
9221                            ));
9222                }
9223             }
9224             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type && 
9225                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9226             {
9227                type = ProcessTemplateParameterType(type.templateParameter);
9228             }
9229          }
9230
9231          if(type && (type.kind == templateType));
9232          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
9233          {
9234             Identifier id = exp.member.member;
9235             TypeKind typeKind = type.kind;
9236             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9237             if(typeKind == subClassType && exp.member.exp.type == classExp)
9238             {
9239                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9240                typeKind = classType;
9241             }
9242
9243             if(id && (typeKind == intType || typeKind == enumType))
9244                _class = eSystem_FindClass(privateModule, "int");
9245
9246             if(_class && id)
9247             {
9248                /*bool thisPtr = 
9249                   (exp.member.exp.type == identifierExp && 
9250                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9251                Property prop = null;
9252                Method method = null;
9253                DataMember member = null;
9254                Property revConvert = null;
9255                ClassProperty classProp = null;
9256
9257                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9258                   exp.member.memberType = propertyMember;
9259
9260                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9261                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9262
9263                if(typeKind != subClassType)
9264                {
9265                   // Prioritize data members over properties for "this"
9266                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9267                   {
9268                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9269                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9270                      {
9271                         prop = eClass_FindProperty(_class, id.string, privateModule);
9272                         if(prop)
9273                            member = null;
9274                      }
9275                      if(!member && !prop)
9276                         prop = eClass_FindProperty(_class, id.string, privateModule);
9277                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9278                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9279                         exp.member.thisPtr = true;
9280                   }
9281                   // Prioritize properties over data members otherwise
9282                   else
9283                   {
9284                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9285                      if(!id.classSym)
9286                      {
9287                         prop = eClass_FindProperty(_class, id.string, null);
9288                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9289                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9290                      }
9291
9292                      if(!prop && !member)
9293                      {
9294                         method = eClass_FindMethod(_class, id.string, null);
9295                         if(!method)
9296                         {
9297                            prop = eClass_FindProperty(_class, id.string, privateModule);
9298                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9299                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9300                         }
9301                      }
9302
9303                      if(member && prop)
9304                      {
9305                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9306                            prop = null;
9307                         else
9308                            member = null;
9309                      }
9310                   }
9311                }
9312                if(!prop && !member)
9313                   method = eClass_FindMethod(_class, id.string, privateModule);
9314                if(!prop && !member && !method)
9315                {
9316                   if(typeKind == subClassType)
9317                   {
9318                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9319                      if(classProp)
9320                      {
9321                         exp.member.memberType = classPropertyMember;
9322                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9323                      }
9324                      else
9325                      {
9326                         // Assume this is a class_data member
9327                         char structName[1024];
9328                         Identifier id = exp.member.member;
9329                         Expression classExp = exp.member.exp;
9330                         type.refCount++;
9331
9332                         FreeType(classExp.expType);
9333                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9334                      
9335                         strcpy(structName, "__ecereClassData_");
9336                         FullClassNameCat(structName, type._class.string, false);
9337                         exp.type = pointerExp;
9338                         exp.member.member = id;
9339
9340                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9341                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), 
9342                               MkExpBrackets(MkListOne(MkExpOp(
9343                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)), 
9344                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9345                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9346                                  )));
9347
9348                         FreeType(type);
9349
9350                         ProcessExpressionType(exp);
9351                         return;
9352                      }
9353                   }
9354                   else
9355                   {
9356                      // Check for reverse conversion
9357                      // (Convert in an instantiation later, so that we can use
9358                      //  deep properties system)
9359                      Symbol classSym = FindClass(id.string);
9360                      if(classSym)
9361                      {
9362                         Class convertClass = classSym.registered;
9363                         if(convertClass)
9364                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9365                      }
9366                   }
9367                }
9368       
9369                if(prop)
9370                {
9371                   exp.member.memberType = propertyMember;
9372                   if(!prop.dataType)
9373                      ProcessPropertyType(prop);
9374                   exp.expType = prop.dataType;                     
9375                   if(prop.dataType) prop.dataType.refCount++;
9376                }
9377                else if(member)
9378                {
9379                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9380                   {
9381                      FreeExpContents(exp);
9382                      exp.type = identifierExp;
9383                      exp.identifier = MkIdentifier("class");
9384                      ProcessExpressionType(exp);
9385                      return;
9386                   }
9387
9388                   exp.member.memberType = dataMember;
9389                   DeclareStruct(_class.fullName, false);
9390                   if(!member.dataType)
9391                   {
9392                      Context context = SetupTemplatesContext(_class);
9393                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9394                      FinishTemplatesContext(context);
9395                   }
9396                   exp.expType = member.dataType;
9397                   if(member.dataType) member.dataType.refCount++;
9398                }
9399                else if(revConvert)
9400                {
9401                   exp.member.memberType = reverseConversionMember;
9402                   exp.expType = MkClassType(revConvert._class.fullName);
9403                }
9404                else if(method)
9405                {
9406                   if(inCompiler)
9407                   {
9408                      /*if(id._class)
9409                      {
9410                         exp.type = identifierExp;
9411                         exp.identifier = exp.member.member;
9412                      }
9413                      else*/
9414                         exp.member.memberType = methodMember;
9415                   }
9416                   if(!method.dataType)
9417                      ProcessMethodType(method);
9418                   exp.expType = Type
9419                   {
9420                      refCount = 1;
9421                      kind = methodType;
9422                      method = method;
9423                   };
9424
9425                   // Tricky spot here... To use instance versus class virtual table
9426                   // Put it back to what it was... What did we break?
9427
9428                   // Had to put it back for overriding Main of Thread global instance
9429
9430                   //exp.expType.methodClass = _class;
9431                   exp.expType.methodClass = (id && id._class) ? _class : null;
9432
9433                   // Need the actual class used for templated classes
9434                   exp.expType.usedClass = _class;
9435                }
9436                else if(!classProp)
9437                {
9438                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9439                   {
9440                      FreeExpContents(exp);
9441                      exp.type = identifierExp;
9442                      exp.identifier = MkIdentifier("class");
9443                      ProcessExpressionType(exp);
9444                      return;
9445                   }
9446                   yylloc = exp.member.member.loc;
9447                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9448                   if(inCompiler)
9449                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9450                }
9451
9452                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9453                {
9454                   Class tClass;
9455
9456                   tClass = _class;
9457                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9458
9459                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9460                   {
9461                      int id = 0;
9462                      ClassTemplateParameter curParam = null;
9463                      Class sClass;
9464
9465                      for(sClass = tClass; sClass; sClass = sClass.base)
9466                      {
9467                         id = 0;
9468                         if(sClass.templateClass) sClass = sClass.templateClass;
9469                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9470                         {
9471                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9472                            {
9473                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9474                                  id += sClass.templateParams.count;
9475                               break;
9476                            }
9477                            id++;
9478                         }
9479                         if(curParam) break;
9480                      }
9481
9482                      if(curParam && tClass.templateArgs[id].dataTypeString)
9483                      {
9484                         ClassTemplateArgument arg = tClass.templateArgs[id];
9485                         Context context = SetupTemplatesContext(tClass);
9486                         /*if(!arg.dataType)
9487                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9488                         FreeType(exp.expType);
9489                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9490                         if(exp.expType)
9491                         {
9492                            if(exp.expType.kind == thisClassType)
9493                            {
9494                               FreeType(exp.expType);
9495                               exp.expType = ReplaceThisClassType(_class);
9496                            }
9497
9498                            if(tClass.templateClass)
9499                               exp.expType.passAsTemplate = true;
9500                            //exp.expType.refCount++;
9501                            if(!exp.destType)
9502                            {
9503                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9504                               //exp.destType.refCount++;
9505
9506                               if(exp.destType.kind == thisClassType)
9507                               {
9508                                  FreeType(exp.destType);
9509                                  exp.destType = ReplaceThisClassType(_class);
9510                               }
9511                            }
9512                         }
9513                         FinishTemplatesContext(context);
9514                      }
9515                   }
9516                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9517                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9518                   {
9519                      int id = 0;
9520                      ClassTemplateParameter curParam = null;
9521                      Class sClass;
9522
9523                      for(sClass = tClass; sClass; sClass = sClass.base)
9524                      {
9525                         id = 0;
9526                         if(sClass.templateClass) sClass = sClass.templateClass;
9527                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9528                         {
9529                            if(curParam.type == TemplateParameterType::type && 
9530                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9531                            {
9532                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9533                                  id += sClass.templateParams.count;
9534                               break;
9535                            }
9536                            id++;
9537                         }
9538                         if(curParam) break;
9539                      }
9540
9541                      if(curParam)
9542                      {
9543                         ClassTemplateArgument arg = tClass.templateArgs[id];
9544                         Context context = SetupTemplatesContext(tClass);
9545                         Type basicType;
9546                         /*if(!arg.dataType)
9547                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9548                         
9549                         basicType = ProcessTypeString(arg.dataTypeString, false);
9550                         if(basicType)
9551                         {
9552                            if(basicType.kind == thisClassType)
9553                            {
9554                               FreeType(basicType);
9555                               basicType = ReplaceThisClassType(_class);
9556                            }
9557
9558                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
9559                            if(tClass.templateClass)
9560                               basicType.passAsTemplate = true;
9561                            */
9562                            
9563                            FreeType(exp.expType);
9564
9565                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
9566                            //exp.expType.refCount++;
9567                            if(!exp.destType)
9568                            {
9569                               exp.destType = exp.expType;
9570                               exp.destType.refCount++;
9571                            }
9572
9573                            {
9574                               Expression newExp { };
9575                               OldList * specs = MkList();
9576                               Declarator decl;
9577                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
9578                               *newExp = *exp;
9579                               if(exp.destType) exp.destType.refCount++;
9580                               if(exp.expType)  exp.expType.refCount++;
9581                               exp.type = castExp;
9582                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
9583                               exp.cast.exp = newExp;
9584                               //FreeType(exp.expType);
9585                               //exp.expType = null;
9586                               //ProcessExpressionType(sourceExp);
9587                            }
9588                         }
9589                         FinishTemplatesContext(context);
9590                      }
9591                   }
9592                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
9593                   {
9594                      Class expClass = exp.expType._class.registered;
9595                      if(expClass)
9596                      {
9597                         Class cClass = null;
9598                         int c;
9599                         int p = 0;
9600                         int paramCount = 0;
9601                         int lastParam = -1;
9602                         char templateString[1024];
9603                         ClassTemplateParameter param;
9604                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
9605                         while(cClass != expClass)
9606                         {
9607                            Class sClass;
9608                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
9609                            cClass = sClass;
9610
9611                            for(param = cClass.templateParams.first; param; param = param.next)
9612                            {
9613                               Class cClassCur = null;
9614                               int c;
9615                               int cp = 0;
9616                               ClassTemplateParameter paramCur = null;
9617                               ClassTemplateArgument arg;
9618                               while(cClassCur != tClass && !paramCur)
9619                               {
9620                                  Class sClassCur;
9621                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
9622                                  cClassCur = sClassCur;
9623
9624                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
9625                                  {
9626                                     if(!strcmp(paramCur.name, param.name))
9627                                     {
9628                                        
9629                                        break;
9630                                     }
9631                                     cp++;
9632                                  }
9633                               }
9634                               if(paramCur && paramCur.type == TemplateParameterType::type)
9635                                  arg = tClass.templateArgs[cp];
9636                               else
9637                                  arg = expClass.templateArgs[p];
9638
9639                               {
9640                                  char argument[256];
9641                                  argument[0] = '\0';
9642                                  /*if(arg.name)
9643                                  {
9644                                     strcat(argument, arg.name.string);
9645                                     strcat(argument, " = ");
9646                                  }*/
9647                                  switch(param.type)
9648                                  {
9649                                     case expression:
9650                                     {
9651                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9652                                        char expString[1024];
9653                                        OldList * specs = MkList();
9654                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9655                                        Expression exp;
9656                                        char * string = PrintHexUInt64(arg.expression.ui64);
9657                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9658
9659                                        ProcessExpressionType(exp);
9660                                        ComputeExpression(exp);
9661                                        expString[0] = '\0';
9662                                        PrintExpression(exp, expString);
9663                                        strcat(argument, expString);
9664                                        // delete exp;
9665                                        FreeExpression(exp);
9666                                        break;
9667                                     }
9668                                     case identifier:
9669                                     {
9670                                        strcat(argument, arg.member.name);
9671                                        break;
9672                                     }
9673                                     case TemplateParameterType::type:
9674                                     {
9675                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9676                                           strcat(argument, arg.dataTypeString);
9677                                        break;
9678                                     }
9679                                  }
9680                                  if(argument[0])
9681                                  {
9682                                     if(paramCount) strcat(templateString, ", ");
9683                                     if(lastParam != p - 1)
9684                                     {
9685                                        strcat(templateString, param.name);
9686                                        strcat(templateString, " = ");
9687                                     }                                       
9688                                     strcat(templateString, argument);
9689                                     paramCount++;
9690                                     lastParam = p;
9691                                  }
9692                               }
9693                               p++;
9694                            }
9695                         }
9696                         {
9697                            int len = strlen(templateString);
9698                            if(templateString[len-1] == '>') templateString[len++] = ' ';
9699                            templateString[len++] = '>';
9700                            templateString[len++] = '\0';
9701                         }
9702
9703                         FreeType(exp.expType);
9704                         {
9705                            Context context = SetupTemplatesContext(tClass);
9706                            exp.expType = ProcessTypeString(templateString, false);
9707                            FinishTemplatesContext(context);
9708                         }
9709                      }
9710                   }
9711                }
9712             }
9713             else
9714                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
9715          }
9716          else if(type && (type.kind == structType || type.kind == unionType))
9717          {
9718             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
9719             if(memberType)
9720             {
9721                exp.expType = memberType;
9722                if(memberType)
9723                   memberType.refCount++;
9724             }
9725          }
9726          else 
9727          {
9728             char expString[10240];
9729             expString[0] = '\0';
9730             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
9731             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
9732          }
9733
9734          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
9735          {
9736             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
9737             {
9738                Identifier id = exp.member.member;
9739                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9740                if(_class)
9741                {
9742                   FreeType(exp.expType);
9743                   exp.expType = ReplaceThisClassType(_class);
9744                }
9745             }
9746          }         
9747          yylloc = oldyylloc;
9748          break;
9749       }
9750       // Convert x->y into (*x).y
9751       case pointerExp:
9752       {
9753          Type destType = exp.destType;
9754
9755          // DOING THIS LATER NOW...
9756          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9757          {
9758             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9759             /* TODO: Name Space Fix ups
9760             if(!exp.member.member.classSym)
9761                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
9762             */
9763          }
9764
9765          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
9766          exp.type = memberExp;
9767          if(destType)
9768             destType.count++;
9769          ProcessExpressionType(exp);
9770          if(destType)
9771             destType.count--;
9772          break;
9773       }
9774       case classSizeExp:
9775       {
9776          //ComputeExpression(exp);
9777
9778          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
9779          if(classSym && classSym.registered)
9780          {
9781             if(classSym.registered.type == noHeadClass)
9782             {
9783                char name[1024];
9784                name[0] = '\0';
9785                DeclareStruct(classSym.string, false);
9786                FreeSpecifier(exp._class);
9787                exp.type = typeSizeExp;
9788                FullClassNameCat(name, classSym.string, false);
9789                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
9790             }
9791             else
9792             {
9793                if(classSym.registered.fixed)
9794                {
9795                   FreeSpecifier(exp._class);
9796                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
9797                   exp.type = constantExp;
9798                }
9799                else
9800                {
9801                   char className[1024];
9802                   strcpy(className, "__ecereClass_");
9803                   FullClassNameCat(className, classSym.string, true);
9804                   MangleClassName(className);
9805
9806                   DeclareClass(classSym, className);
9807
9808                   FreeExpContents(exp);
9809                   exp.type = pointerExp;
9810                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
9811                   exp.member.member = MkIdentifier("structSize");
9812                }
9813             }
9814          }
9815
9816          exp.expType = Type
9817          {
9818             refCount = 1;
9819             kind = intType;
9820          };
9821          // exp.isConstant = true;
9822          break;
9823       }
9824       case typeSizeExp:
9825       {
9826          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
9827
9828          exp.expType = Type
9829          {
9830             refCount = 1;
9831             kind = intType;
9832          };
9833          exp.isConstant = true;
9834
9835          DeclareType(type, false, false);
9836          FreeType(type);
9837          break;
9838       }
9839       case castExp:
9840       {
9841          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
9842          type.count = 1;
9843          FreeType(exp.cast.exp.destType);
9844          exp.cast.exp.destType = type;
9845          type.refCount++;
9846          ProcessExpressionType(exp.cast.exp);
9847          type.count = 0;
9848          exp.expType = type;
9849          //type.refCount++;
9850          
9851          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
9852          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
9853          {
9854             void * prev = exp.prev, * next = exp.next;
9855             Type expType = exp.cast.exp.destType;
9856             Expression castExp = exp.cast.exp;
9857             Type destType = exp.destType;
9858
9859             if(expType) expType.refCount++;
9860
9861             //FreeType(exp.destType);
9862             FreeType(exp.expType);
9863             FreeTypeName(exp.cast.typeName);
9864             
9865             *exp = *castExp;
9866             FreeType(exp.expType);
9867             FreeType(exp.destType);
9868
9869             exp.expType = expType;
9870             exp.destType = destType;
9871
9872             delete castExp;
9873
9874             exp.prev = prev;
9875             exp.next = next;
9876
9877          }
9878          else
9879          {
9880             exp.isConstant = exp.cast.exp.isConstant;
9881          }
9882          //FreeType(type);
9883          break;
9884       }
9885       case extensionInitializerExp:
9886       {
9887          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
9888          type.refCount++;
9889
9890          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
9891          // ProcessInitializer(exp.initializer.initializer, type);
9892          exp.expType = type;
9893          break;
9894       }
9895       case vaArgExp:
9896       {
9897          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
9898          ProcessExpressionType(exp.vaArg.exp);
9899          type.refCount++;
9900          exp.expType = type;
9901          break;
9902       }
9903       case conditionExp:
9904       {
9905          Expression e;
9906          exp.isConstant = true;
9907
9908          FreeType(exp.cond.cond.destType);
9909          exp.cond.cond.destType = MkClassType("bool");
9910          exp.cond.cond.destType.truth = true;
9911          ProcessExpressionType(exp.cond.cond);
9912          if(!exp.cond.cond.isConstant)
9913             exp.isConstant = false;
9914          for(e = exp.cond.exp->first; e; e = e.next)
9915          {
9916             if(!e.next)
9917             {
9918                FreeType(e.destType);
9919                e.destType = exp.destType;
9920                if(e.destType) e.destType.refCount++;
9921             }
9922             ProcessExpressionType(e);
9923             if(!e.next)
9924             {
9925                exp.expType = e.expType;
9926                if(e.expType) e.expType.refCount++;
9927             }
9928             if(!e.isConstant)
9929                exp.isConstant = false;
9930          }
9931
9932          FreeType(exp.cond.elseExp.destType);
9933          // Added this check if we failed to find an expType
9934          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
9935
9936          // Reversed it...
9937          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
9938
9939          if(exp.cond.elseExp.destType)
9940             exp.cond.elseExp.destType.refCount++;
9941          ProcessExpressionType(exp.cond.elseExp);
9942
9943          // FIXED THIS: Was done before calling process on elseExp
9944          if(!exp.cond.elseExp.isConstant)
9945             exp.isConstant = false;
9946          break;
9947       }
9948       case extensionCompoundExp:
9949       {
9950          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
9951          {
9952             Statement last = exp.compound.compound.statements->last;
9953             if(last.type == expressionStmt && last.expressions && last.expressions->last)
9954             {
9955                ((Expression)last.expressions->last).destType = exp.destType;
9956                if(exp.destType)
9957                   exp.destType.refCount++;
9958             }
9959             ProcessStatement(exp.compound);
9960             exp.expType = ((Expression)last.expressions->last).expType;
9961             if(((Expression)last.expressions->last).expType)
9962                exp.expType.refCount++;
9963          }
9964          break;
9965       }
9966       case classExp:
9967       {
9968          Specifier spec = exp._classExp.specifiers->first;
9969          if(spec && spec.type == nameSpecifier)
9970          {
9971             exp.expType = MkClassType(spec.name);
9972             exp.expType.kind = subClassType;
9973             exp.byReference = true;
9974          }
9975          else
9976          {
9977             exp.expType = MkClassType("ecere::com::Class");
9978             exp.byReference = true;
9979          }
9980          break;
9981       }
9982       case classDataExp:
9983       {
9984          Class _class = thisClass ? thisClass : currentClass;
9985          if(_class)
9986          {
9987             Identifier id = exp.classData.id;
9988             char structName[1024];
9989             Expression classExp;
9990             strcpy(structName, "__ecereClassData_");
9991             FullClassNameCat(structName, _class.fullName, false);
9992             exp.type = pointerExp;
9993             exp.member.member = id;
9994             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
9995                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
9996             else
9997                classExp = MkExpIdentifier(MkIdentifier("class"));
9998
9999             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10000                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), 
10001                   MkExpBrackets(MkListOne(MkExpOp(
10002                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)), 
10003                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10004                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10005                      )));
10006
10007             ProcessExpressionType(exp);
10008             return;
10009          }
10010          break;
10011       }
10012       case arrayExp:
10013       {
10014          Type type = null;
10015          char * typeString = null;
10016          char typeStringBuf[1024];
10017          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10018             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10019          {
10020             Class templateClass = exp.destType._class.registered;
10021             typeString = templateClass.templateArgs[2].dataTypeString;
10022          }
10023          else if(exp.list)
10024          {
10025             // Guess type from expressions in the array
10026             Expression e;
10027             for(e = exp.list->first; e; e = e.next)
10028             {
10029                ProcessExpressionType(e);
10030                if(e.expType)
10031                {
10032                   if(!type) { type = e.expType; type.refCount++; }
10033                   else
10034                   {
10035                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10036                      if(!MatchTypeExpression(e, type, null, false))
10037                      {
10038                         FreeType(type);
10039                         type = e.expType;
10040                         e.expType = null;
10041                         
10042                         e = exp.list->first;
10043                         ProcessExpressionType(e);
10044                         if(e.expType)
10045                         {
10046                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10047                            if(!MatchTypeExpression(e, type, null, false))
10048                            {
10049                               FreeType(e.expType);
10050                               e.expType = null;
10051                               FreeType(type);
10052                               type = null;
10053                               break;
10054                            }                           
10055                         }
10056                      }
10057                   }
10058                   if(e.expType)
10059                   {
10060                      FreeType(e.expType);
10061                      e.expType = null;
10062                   }
10063                }
10064             }
10065             if(type)
10066             {
10067                typeStringBuf[0] = '\0';
10068                PrintType(type, typeStringBuf, false, true);
10069                typeString = typeStringBuf;
10070                FreeType(type);
10071                type = null;
10072             }
10073          }
10074          if(typeString)
10075          {
10076             /*
10077             (Container)& (struct BuiltInContainer)
10078             {
10079                ._vTbl = class(BuiltInContainer)._vTbl,
10080                ._class = class(BuiltInContainer),
10081                .refCount = 0,
10082                .data = (int[]){ 1, 7, 3, 4, 5 },
10083                .count = 5,
10084                .type = class(int),
10085             }
10086             */
10087             char templateString[1024];
10088             OldList * initializers = MkList();
10089             OldList * structInitializers = MkList();
10090             OldList * specs = MkList();
10091             Expression expExt;
10092             Declarator decl = SpecDeclFromString(typeString, specs, null);
10093             sprintf(templateString, "Container<%s>", typeString);
10094
10095             if(exp.list)
10096             {
10097                Expression e;
10098                type = ProcessTypeString(typeString, false);
10099                while(e = exp.list->first)
10100                {
10101                   exp.list->Remove(e);
10102                   e.destType = type;
10103                   type.refCount++;
10104                   ProcessExpressionType(e);
10105                   ListAdd(initializers, MkInitializerAssignment(e));
10106                }
10107                FreeType(type);
10108                delete exp.list;
10109             }
10110             
10111             DeclareStruct("ecere::com::BuiltInContainer", false);
10112
10113             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10114                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10115             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10116                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10117             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10118                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10119             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10120                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10121                MkInitializerList(initializers))));
10122                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10123             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10124                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10125             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10126                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10127             exp.expType = ProcessTypeString(templateString, false);
10128             exp.type = bracketsExp;
10129             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10130                MkExpOp(null, '&',
10131                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10132                   MkInitializerList(structInitializers)))));
10133             ProcessExpressionType(expExt);
10134          }
10135          else
10136          {
10137             exp.expType = ProcessTypeString("Container", false);
10138             Compiler_Error($"Couldn't determine type of array elements\n");
10139          }
10140          break;
10141       }
10142    }
10143
10144    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10145    {
10146       FreeType(exp.expType);
10147       exp.expType = ReplaceThisClassType(thisClass);
10148    }
10149
10150    // Resolve structures here
10151    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10152    {
10153       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10154       // TODO: Fix members reference...
10155       if(symbol)
10156       {
10157          if(exp.expType.kind != enumType)
10158          {
10159             Type member;
10160             String enumName = CopyString(exp.expType.enumName);
10161
10162             // Fixed a memory leak on self-referencing C structs typedefs
10163             // by instantiating a new type rather than simply copying members
10164             // into exp.expType
10165             FreeType(exp.expType);
10166             exp.expType = Type { };
10167             exp.expType.kind = symbol.type.kind;
10168             exp.expType.refCount++;
10169             exp.expType.enumName = enumName;
10170
10171             exp.expType.members = symbol.type.members;
10172             for(member = symbol.type.members.first; member; member = member.next)
10173                member.refCount++;
10174          }
10175          else
10176          {
10177             NamedLink member;
10178             for(member = symbol.type.members.first; member; member = member.next)
10179             {
10180                NamedLink value { name = CopyString(member.name) };
10181                exp.expType.members.Add(value);
10182             }
10183          }
10184       }
10185    }
10186
10187    yylloc = exp.loc;
10188    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10189    else if(exp.destType && !exp.destType.keepCast)
10190    {
10191       if(!CheckExpressionType(exp, exp.destType, false))
10192       {
10193          if(!exp.destType.count || unresolved)
10194          {
10195             if(!exp.expType)
10196             {
10197                yylloc = exp.loc;
10198                if(exp.destType.kind != ellipsisType)
10199                {
10200                   char type2[1024];
10201                   type2[0] = '\0';
10202                   if(inCompiler)
10203                   {
10204                      char expString[10240];
10205                      expString[0] = '\0';
10206
10207                      PrintType(exp.destType, type2, false, true);
10208
10209                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10210                      if(unresolved)
10211                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10212                      else if(exp.type != dummyExp)
10213                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10214                   }
10215                }
10216                else
10217                {
10218                   char expString[10240] ;
10219                   expString[0] = '\0';
10220                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10221
10222                   if(unresolved)
10223                      Compiler_Error($"unresolved identifier %s\n", expString);
10224                   else if(exp.type != dummyExp)
10225                      Compiler_Error($"couldn't determine type of %s\n", expString);
10226                }
10227             }
10228             else
10229             {
10230                char type1[1024];
10231                char type2[1024];
10232                type1[0] = '\0';
10233                type2[0] = '\0';
10234                if(inCompiler)
10235                {
10236                   PrintType(exp.expType, type1, false, true);
10237                   PrintType(exp.destType, type2, false, true);
10238                }
10239
10240                //CheckExpressionType(exp, exp.destType, false);
10241
10242                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10243                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType && 
10244                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10245                else
10246                {
10247                   char expString[10240];
10248                   expString[0] = '\0';
10249                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10250
10251 #ifdef _DEBUG
10252                   CheckExpressionType(exp, exp.destType, false);
10253 #endif
10254                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10255                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10256                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10257
10258                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10259                   FreeType(exp.expType);
10260                   exp.destType.refCount++;
10261                   exp.expType = exp.destType;
10262                }
10263             }
10264          }
10265       }
10266       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10267       {
10268          Expression newExp { };
10269          char typeString[1024];
10270          OldList * specs = MkList();
10271          Declarator decl;
10272
10273          typeString[0] = '\0';
10274
10275          *newExp = *exp;
10276
10277          if(exp.expType)  exp.expType.refCount++;
10278          if(exp.expType)  exp.expType.refCount++;
10279          exp.type = castExp;
10280          newExp.destType = exp.expType;
10281
10282          PrintType(exp.expType, typeString, false, false);
10283          decl = SpecDeclFromString(typeString, specs, null);
10284          
10285          exp.cast.typeName = MkTypeName(specs, decl);
10286          exp.cast.exp = newExp;
10287       }
10288    }
10289    else if(unresolved)
10290    {
10291       if(exp.identifier._class && exp.identifier._class.name)
10292          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10293       else if(exp.identifier.string && exp.identifier.string[0])
10294          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10295    }
10296    else if(!exp.expType && exp.type != dummyExp)
10297    {
10298       char expString[10240];
10299       expString[0] = '\0';
10300       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10301       Compiler_Error($"couldn't determine type of %s\n", expString);
10302    }
10303
10304    // Let's try to support any_object & typed_object here:
10305    ApplyAnyObjectLogic(exp);
10306
10307    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10308       exp.expType._class.registered.type == noHeadClass)
10309    {
10310       exp.byReference = true;
10311    }
10312    /*else if(!notByReference && exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10313       exp.destType._class.registered.type == noHeadClass)
10314    {
10315       exp.byReference = true;
10316    }*/
10317    yylloc = oldyylloc;
10318 }
10319
10320 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10321 {
10322    // THIS CODE WILL FIND NEXT MEMBER...
10323    if(*curMember) 
10324    {
10325       *curMember = (*curMember).next;
10326
10327       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10328       {
10329          *curMember = subMemberStack[--(*subMemberStackPos)];
10330          *curMember = (*curMember).next;
10331       }
10332
10333       // SKIP ALL PROPERTIES HERE...
10334       while((*curMember) && (*curMember).isProperty)
10335          *curMember = (*curMember).next;
10336
10337       if(subMemberStackPos)
10338       {
10339          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10340          {
10341             subMemberStack[(*subMemberStackPos)++] = *curMember;
10342
10343             *curMember = (*curMember).members.first;
10344             while(*curMember && (*curMember).isProperty)
10345                *curMember = (*curMember).next;                     
10346          }
10347       }
10348    }
10349    while(!*curMember)
10350    {
10351       if(!*curMember)
10352       {
10353          if(subMemberStackPos && *subMemberStackPos)
10354          {
10355             *curMember = subMemberStack[--(*subMemberStackPos)];
10356             *curMember = (*curMember).next;
10357          }
10358          else
10359          {
10360             Class lastCurClass = *curClass;
10361
10362             if(*curClass == _class) break;     // REACHED THE END
10363
10364             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10365             *curMember = (*curClass).membersAndProperties.first;
10366          }
10367
10368          while((*curMember) && (*curMember).isProperty)
10369             *curMember = (*curMember).next;
10370          if(subMemberStackPos)
10371          {
10372             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10373             {
10374                subMemberStack[(*subMemberStackPos)++] = *curMember;
10375
10376                *curMember = (*curMember).members.first;
10377                while(*curMember && (*curMember).isProperty)
10378                   *curMember = (*curMember).next;                     
10379             }
10380          }
10381       }
10382    }
10383 }
10384
10385
10386 static void ProcessInitializer(Initializer init, Type type)
10387 {
10388    switch(init.type)
10389    {
10390       case expInitializer:
10391          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10392          {
10393             // TESTING THIS FOR SHUTTING = 0 WARNING
10394             if(init.exp && !init.exp.destType)
10395             {
10396                FreeType(init.exp.destType);
10397                init.exp.destType = type;
10398                if(type) type.refCount++;
10399             }
10400             if(init.exp)
10401             {
10402                ProcessExpressionType(init.exp);
10403                init.isConstant = init.exp.isConstant;
10404             }
10405             break;
10406          }
10407          else
10408          {
10409             Expression exp = init.exp;
10410             Instantiation inst = exp.instance;
10411             MembersInit members;
10412
10413             init.type = listInitializer;
10414             init.list = MkList();
10415
10416             if(inst.members)
10417             {
10418                for(members = inst.members->first; members; members = members.next)
10419                {
10420                   if(members.type == dataMembersInit)
10421                   {
10422                      MemberInit member;
10423                      for(member = members.dataMembers->first; member; member = member.next)
10424                      {
10425                         ListAdd(init.list, member.initializer);
10426                         member.initializer = null;
10427                      }
10428                   }
10429                   // Discard all MembersInitMethod
10430                }
10431             }
10432             FreeExpression(exp);
10433          }
10434       case listInitializer:
10435       {
10436          Initializer i;
10437          Type initializerType = null;
10438          Class curClass = null;
10439          DataMember curMember = null;
10440          DataMember subMemberStack[256];
10441          int subMemberStackPos = 0;
10442
10443          if(type && type.kind == arrayType)
10444             initializerType = Dereference(type);
10445          else if(type && (type.kind == structType || type.kind == unionType))
10446             initializerType = type.members.first;
10447
10448          for(i = init.list->first; i; i = i.next)
10449          {
10450             if(type && type.kind == classType && type._class && type._class.registered)
10451             {
10452                // 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)
10453                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10454                // TODO: Generate error on initializing a private data member this way from another module...
10455                if(curMember)
10456                {
10457                   if(!curMember.dataType)
10458                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10459                   initializerType = curMember.dataType;
10460                }
10461             }
10462             ProcessInitializer(i, initializerType);
10463             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10464                initializerType = initializerType.next;
10465             if(!i.isConstant)
10466                init.isConstant = false;
10467          }
10468
10469          if(type && type.kind == arrayType)
10470             FreeType(initializerType);
10471
10472          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10473          {
10474             Compiler_Error($"Assigning list initializer to non list\n");
10475          }
10476          break;
10477       }
10478    }
10479 }
10480
10481 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10482 {
10483    switch(spec.type)
10484    {
10485       case baseSpecifier:
10486       {
10487          if(spec.specifier == THISCLASS)
10488          {
10489             if(thisClass)
10490             {
10491                spec.type = nameSpecifier;
10492                spec.name = ReplaceThisClass(thisClass);
10493                spec.symbol = FindClass(spec.name);
10494                ProcessSpecifier(spec, declareStruct);
10495             }
10496          }
10497          break;
10498       }
10499       case nameSpecifier:
10500       {
10501          Symbol symbol = FindType(curContext, spec.name);
10502          if(symbol)
10503             DeclareType(symbol.type, true, true);
10504          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10505             DeclareStruct(spec.name, false);
10506          break;
10507       }
10508       case enumSpecifier:
10509       {
10510          Enumerator e;
10511          if(spec.list)
10512          {
10513             for(e = spec.list->first; e; e = e.next)
10514             {
10515                if(e.exp)
10516                   ProcessExpressionType(e.exp);
10517             }
10518          }
10519          break;
10520       }
10521       case structSpecifier:
10522       case unionSpecifier:
10523       {
10524          if(spec.definitions)
10525          {
10526             ClassDef def;
10527             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10528             //if(symbol)
10529                ProcessClass(spec.definitions, symbol);
10530             /*else
10531             {
10532                for(def = spec.definitions->first; def; def = def.next)
10533                {
10534                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10535                      ProcessDeclaration(def.decl);
10536                }
10537             }*/
10538          }
10539          break;
10540       }
10541       /*
10542       case classSpecifier:
10543       {
10544          Symbol classSym = FindClass(spec.name);
10545          if(classSym && classSym.registered && classSym.registered.type == structClass)
10546             DeclareStruct(spec.name, false);
10547          break;
10548       }
10549       */
10550    }
10551 }
10552
10553
10554 static void ProcessDeclarator(Declarator decl)
10555 {
10556    switch(decl.type)
10557    {
10558       case identifierDeclarator:
10559          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
10560          {
10561             FreeSpecifier(decl.identifier._class);
10562             decl.identifier._class = null;
10563          }
10564          break;
10565       case arrayDeclarator:
10566          if(decl.array.exp)
10567             ProcessExpressionType(decl.array.exp);
10568       case structDeclarator:
10569       case bracketsDeclarator:
10570       case functionDeclarator:
10571       case pointerDeclarator:
10572       case extendedDeclarator:
10573       case extendedDeclaratorEnd:
10574          if(decl.declarator)
10575             ProcessDeclarator(decl.declarator);
10576          if(decl.type == functionDeclarator)
10577          {
10578             Identifier id = GetDeclId(decl);
10579             if(id && id._class)
10580             {
10581                TypeName param
10582                {
10583                   qualifiers = MkListOne(id._class);
10584                   declarator = null;
10585                };
10586                if(!decl.function.parameters)
10587                   decl.function.parameters = MkList();               
10588                decl.function.parameters->Insert(null, param);
10589                id._class = null;
10590             }
10591             if(decl.function.parameters)
10592             {
10593                TypeName param;
10594                
10595                for(param = decl.function.parameters->first; param; param = param.next)
10596                {
10597                   if(param.qualifiers && param.qualifiers->first)
10598                   {
10599                      Specifier spec = param.qualifiers->first;
10600                      if(spec && spec.specifier == TYPED_OBJECT)
10601                      {
10602                         Declarator d = param.declarator;
10603                         TypeName newParam
10604                         {
10605                            qualifiers = MkListOne(MkSpecifier(VOID));
10606                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10607                         };
10608                         
10609                         FreeList(param.qualifiers, FreeSpecifier);
10610
10611                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
10612                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
10613
10614                         decl.function.parameters->Insert(param, newParam);
10615                         param = newParam;
10616                      }
10617                      else if(spec && spec.specifier == ANY_OBJECT)
10618                      {
10619                         Declarator d = param.declarator;
10620                         
10621                         FreeList(param.qualifiers, FreeSpecifier);
10622
10623                         param.qualifiers = MkListOne(MkSpecifier(VOID));
10624                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);                        
10625                      }
10626                      else if(spec.specifier == THISCLASS)
10627                      {
10628                         if(thisClass)
10629                         {
10630                            spec.type = nameSpecifier;
10631                            spec.name = ReplaceThisClass(thisClass);
10632                            spec.symbol = FindClass(spec.name);
10633                            ProcessSpecifier(spec, false);
10634                         }
10635                      }
10636                   }
10637
10638                   if(param.declarator)
10639                      ProcessDeclarator(param.declarator);
10640                }
10641             }
10642          }
10643          break;
10644    }
10645 }
10646
10647 static void ProcessDeclaration(Declaration decl)
10648 {
10649    yylloc = decl.loc;
10650    switch(decl.type)
10651    {
10652       case initDeclaration:
10653       {
10654          bool declareStruct = false;
10655          /*
10656          lineNum = decl.pos.line;
10657          column = decl.pos.col;
10658          */
10659
10660          if(decl.declarators)
10661          {
10662             InitDeclarator d;
10663          
10664             for(d = decl.declarators->first; d; d = d.next)
10665             {
10666                Type type, subType;
10667                ProcessDeclarator(d.declarator);
10668
10669                type = ProcessType(decl.specifiers, d.declarator);
10670
10671                if(d.initializer)
10672                {
10673                   ProcessInitializer(d.initializer, type);
10674
10675                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }                  
10676                   
10677                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
10678                      d.initializer.exp.type == instanceExp)
10679                   {
10680                      if(type.kind == classType && type._class == 
10681                         d.initializer.exp.expType._class)
10682                      {
10683                         Instantiation inst = d.initializer.exp.instance;
10684                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
10685                         
10686                         d.initializer.exp.instance = null;
10687                         if(decl.specifiers)
10688                            FreeList(decl.specifiers, FreeSpecifier);
10689                         FreeList(decl.declarators, FreeInitDeclarator);
10690
10691                         d = null;
10692
10693                         decl.type = instDeclaration;
10694                         decl.inst = inst;
10695                      }
10696                   }
10697                }
10698                for(subType = type; subType;)
10699                {
10700                   if(subType.kind == classType)
10701                   {
10702                      declareStruct = true;
10703                      break;
10704                   }
10705                   else if(subType.kind == pointerType)
10706                      break;
10707                   else if(subType.kind == arrayType)
10708                      subType = subType.arrayType;
10709                   else
10710                      break;
10711                }
10712
10713                FreeType(type);
10714                if(!d) break;
10715             }
10716          }
10717
10718          if(decl.specifiers)
10719          {
10720             Specifier s;
10721             for(s = decl.specifiers->first; s; s = s.next)
10722             {
10723                ProcessSpecifier(s, declareStruct);
10724             }
10725          }
10726          break;
10727       }
10728       case instDeclaration:
10729       {
10730          ProcessInstantiationType(decl.inst);
10731          break;
10732       }
10733       case structDeclaration:
10734       {
10735          Specifier spec;
10736          Declarator d;
10737          bool declareStruct = false;
10738
10739          if(decl.declarators)
10740          {
10741             for(d = decl.declarators->first; d; d = d.next)
10742             {
10743                Type type = ProcessType(decl.specifiers, d.declarator);
10744                Type subType;
10745                ProcessDeclarator(d);
10746                for(subType = type; subType;)
10747                {
10748                   if(subType.kind == classType)
10749                   {
10750                      declareStruct = true;
10751                      break;
10752                   }
10753                   else if(subType.kind == pointerType)
10754                      break;
10755                   else if(subType.kind == arrayType)
10756                      subType = subType.arrayType;
10757                   else
10758                      break;
10759                }
10760                FreeType(type);
10761             }
10762          }
10763          if(decl.specifiers)
10764          {
10765             for(spec = decl.specifiers->first; spec; spec = spec.next)
10766                ProcessSpecifier(spec, declareStruct);
10767          }
10768          break;
10769       }
10770    }
10771 }
10772
10773 static FunctionDefinition curFunction;
10774
10775 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
10776 {
10777    char propName[1024], propNameM[1024];
10778    char getName[1024], setName[1024];
10779    OldList * args;
10780
10781    DeclareProperty(prop, setName, getName);
10782
10783    // eInstance_FireWatchers(object, prop);
10784    strcpy(propName, "__ecereProp_");
10785    FullClassNameCat(propName, prop._class.fullName, false);
10786    strcat(propName, "_");
10787    // strcat(propName, prop.name);
10788    FullClassNameCat(propName, prop.name, true);
10789    MangleClassName(propName);
10790
10791    strcpy(propNameM, "__ecerePropM_");
10792    FullClassNameCat(propNameM, prop._class.fullName, false);
10793    strcat(propNameM, "_");
10794    // strcat(propNameM, prop.name);
10795    FullClassNameCat(propNameM, prop.name, true);
10796    MangleClassName(propNameM);
10797
10798    if(prop.isWatchable)
10799    {
10800       args = MkList();
10801       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10802       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10803       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10804
10805       args = MkList();
10806       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10807       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10808       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10809    }
10810
10811    
10812    {
10813       args = MkList();
10814       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10815       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10816       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10817
10818       args = MkList();
10819       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10820       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10821       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10822    }
10823    
10824    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) && 
10825       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
10826       curFunction.propSet.fireWatchersDone = true;
10827 }
10828
10829 static void ProcessStatement(Statement stmt)
10830 {
10831    yylloc = stmt.loc;
10832    /*
10833    lineNum = stmt.pos.line;
10834    column = stmt.pos.col;
10835    */
10836    switch(stmt.type)
10837    {
10838       case labeledStmt:
10839          ProcessStatement(stmt.labeled.stmt);
10840          break;
10841       case caseStmt:
10842          // This expression should be constant...
10843          if(stmt.caseStmt.exp)
10844          {
10845             FreeType(stmt.caseStmt.exp.destType);
10846             stmt.caseStmt.exp.destType = curSwitchType;
10847             if(curSwitchType) curSwitchType.refCount++;
10848             ProcessExpressionType(stmt.caseStmt.exp);
10849             ComputeExpression(stmt.caseStmt.exp);
10850          }
10851          if(stmt.caseStmt.stmt)
10852             ProcessStatement(stmt.caseStmt.stmt);
10853          break;
10854       case compoundStmt:
10855       {
10856          if(stmt.compound.context)
10857          {
10858             Declaration decl;
10859             Statement s;
10860
10861             Statement prevCompound = curCompound;
10862             Context prevContext = curContext;
10863
10864             if(!stmt.compound.isSwitch)
10865             {
10866                curCompound = stmt;
10867                curContext = stmt.compound.context;
10868             }
10869
10870             if(stmt.compound.declarations)
10871             {
10872                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
10873                   ProcessDeclaration(decl);
10874             }
10875             if(stmt.compound.statements)
10876             {
10877                for(s = stmt.compound.statements->first; s; s = s.next)
10878                   ProcessStatement(s);
10879             }
10880
10881             curContext = prevContext;
10882             curCompound = prevCompound;
10883          }
10884          break;
10885       }
10886       case expressionStmt:
10887       {
10888          Expression exp;
10889          if(stmt.expressions)
10890          {
10891             for(exp = stmt.expressions->first; exp; exp = exp.next)
10892                ProcessExpressionType(exp);
10893          }
10894          break;
10895       }
10896       case ifStmt:
10897       {
10898          Expression exp;
10899
10900          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
10901          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
10902          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
10903          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
10904          {
10905             ProcessExpressionType(exp);
10906          }
10907          if(stmt.ifStmt.stmt)
10908             ProcessStatement(stmt.ifStmt.stmt);
10909          if(stmt.ifStmt.elseStmt)
10910             ProcessStatement(stmt.ifStmt.elseStmt);
10911          break;
10912       }
10913       case switchStmt:
10914       {
10915          Type oldSwitchType = curSwitchType;
10916          if(stmt.switchStmt.exp)
10917          {
10918             Expression exp;
10919             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
10920             {
10921                if(!exp.next)
10922                {
10923                   /*
10924                   Type destType
10925                   {
10926                      kind = intType;
10927                      refCount = 1;
10928                   };
10929                   e.exp.destType = destType;
10930                   */
10931
10932                   ProcessExpressionType(exp);
10933                }
10934                if(!exp.next)
10935                   curSwitchType = exp.expType;
10936             }
10937          }
10938          ProcessStatement(stmt.switchStmt.stmt);
10939          curSwitchType = oldSwitchType;
10940          break;
10941       }
10942       case whileStmt:
10943       {
10944          if(stmt.whileStmt.exp)
10945          {
10946             Expression exp;
10947
10948             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
10949             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
10950             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
10951             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
10952             {
10953                ProcessExpressionType(exp);
10954             }
10955          }
10956          if(stmt.whileStmt.stmt)
10957             ProcessStatement(stmt.whileStmt.stmt);
10958          break;
10959       }
10960       case doWhileStmt:
10961       {
10962          if(stmt.doWhile.exp)
10963          {
10964             Expression exp;
10965
10966             if(stmt.doWhile.exp->last)
10967             {
10968                FreeType(((Expression)stmt.doWhile.exp->last).destType);
10969                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
10970                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
10971             }
10972             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
10973             {
10974                ProcessExpressionType(exp);
10975             }
10976          }
10977          if(stmt.doWhile.stmt)
10978             ProcessStatement(stmt.doWhile.stmt);
10979          break;
10980       }
10981       case forStmt:
10982       {
10983          Expression exp;
10984          if(stmt.forStmt.init)
10985             ProcessStatement(stmt.forStmt.init);
10986
10987          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
10988          {
10989             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
10990             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
10991             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
10992          }
10993
10994          if(stmt.forStmt.check)
10995             ProcessStatement(stmt.forStmt.check);
10996          if(stmt.forStmt.increment)
10997          {
10998             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
10999                ProcessExpressionType(exp);
11000          }
11001
11002          if(stmt.forStmt.stmt)
11003             ProcessStatement(stmt.forStmt.stmt);
11004          break;
11005       }
11006       case forEachStmt:
11007       {
11008          Identifier id = stmt.forEachStmt.id;
11009          OldList * exp = stmt.forEachStmt.exp;
11010          OldList * filter = stmt.forEachStmt.filter;
11011          Statement block = stmt.forEachStmt.stmt;
11012          char iteratorType[1024];
11013          Type source;
11014          Expression e;
11015          bool isBuiltin = exp && exp->last && 
11016             (((Expression)exp->last).type == ExpressionType::arrayExp || 
11017               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11018          Expression arrayExp;
11019          char * typeString = null;
11020          int builtinCount = 0;
11021
11022          for(e = exp ? exp->first : null; e; e = e.next)
11023          {
11024             if(!e.next)
11025             {
11026                FreeType(e.destType);
11027                e.destType = ProcessTypeString("Container", false);
11028             }
11029             if(!isBuiltin || e.next)
11030                ProcessExpressionType(e);
11031          }
11032
11033          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11034          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11035             eClass_IsDerived(source._class.registered, containerClass)))
11036          {
11037             Class _class = source ? source._class.registered : null;
11038             Symbol symbol;
11039             Expression expIt = null;
11040             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11041             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11042             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11043             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11044             stmt.type = compoundStmt;
11045             
11046             stmt.compound.context = Context { };
11047             stmt.compound.context.parent = curContext;
11048             curContext = stmt.compound.context;
11049
11050             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11051             {
11052                Class mapClass = eSystem_FindClass(privateModule, "Map");
11053                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11054                isCustomAVLTree = true;
11055                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11056                   isAVLTree = true;
11057                else if(eClass_IsDerived(source._class.registered, mapClass))
11058                   isMap = true;
11059             }
11060             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11061             else if(source && eClass_IsDerived(source._class.registered, linkListClass)) 
11062             {
11063                Class listClass = eSystem_FindClass(privateModule, "List");
11064                isLinkList = true;
11065                isList = eClass_IsDerived(source._class.registered, listClass);
11066             }
11067
11068             if(isArray)
11069             {
11070                Declarator decl;
11071                OldList * specs = MkList();
11072                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, 
11073                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11074                stmt.compound.declarations = MkListOne(
11075                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11076                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11077                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), 
11078                      MkInitializerAssignment(MkExpBrackets(exp))))));
11079             }
11080             else if(isBuiltin)
11081             {
11082                Type type = null;
11083                char typeStringBuf[1024];
11084                
11085                // TODO: Merge this code?
11086                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11087                if(((Expression)exp->last).type == castExp)
11088                {
11089                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11090                   if(typeName)
11091                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11092                }
11093
11094                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11095                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11096                   arrayExp.destType._class.registered.templateArgs)
11097                {
11098                   Class templateClass = arrayExp.destType._class.registered;
11099                   typeString = templateClass.templateArgs[2].dataTypeString;
11100                }
11101                else if(arrayExp.list)
11102                {
11103                   // Guess type from expressions in the array
11104                   Expression e;
11105                   for(e = arrayExp.list->first; e; e = e.next)
11106                   {
11107                      ProcessExpressionType(e);
11108                      if(e.expType)
11109                      {
11110                         if(!type) { type = e.expType; type.refCount++; }
11111                         else
11112                         {
11113                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11114                            if(!MatchTypeExpression(e, type, null, false))
11115                            {
11116                               FreeType(type);
11117                               type = e.expType;
11118                               e.expType = null;
11119                               
11120                               e = arrayExp.list->first;
11121                               ProcessExpressionType(e);
11122                               if(e.expType)
11123                               {
11124                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11125                                  if(!MatchTypeExpression(e, type, null, false))
11126                                  {
11127                                     FreeType(e.expType);
11128                                     e.expType = null;
11129                                     FreeType(type);
11130                                     type = null;
11131                                     break;
11132                                  }                           
11133                               }
11134                            }
11135                         }
11136                         if(e.expType)
11137                         {
11138                            FreeType(e.expType);
11139                            e.expType = null;
11140                         }
11141                      }
11142                   }
11143                   if(type)
11144                   {
11145                      typeStringBuf[0] = '\0';
11146                      PrintType(type, typeStringBuf, false, true);
11147                      typeString = typeStringBuf;
11148                      FreeType(type);
11149                   }
11150                }
11151                if(typeString)
11152                {
11153                   OldList * initializers = MkList();
11154                   Declarator decl;
11155                   OldList * specs = MkList();
11156                   if(arrayExp.list)
11157                   {
11158                      Expression e;
11159
11160                      builtinCount = arrayExp.list->count;
11161                      type = ProcessTypeString(typeString, false);
11162                      while(e = arrayExp.list->first)
11163                      {
11164                         arrayExp.list->Remove(e);
11165                         e.destType = type;
11166                         type.refCount++;
11167                         ProcessExpressionType(e);
11168                         ListAdd(initializers, MkInitializerAssignment(e));
11169                      }
11170                      FreeType(type);
11171                      delete arrayExp.list;
11172                   }
11173                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11174                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier), 
11175                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11176
11177                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorArray(PlugDeclarator(
11178                      /*CopyDeclarator(*/decl/*)*/, MkDeclaratorIdentifier(MkIdentifier("__internalArray"))), null), MkInitializerList(initializers)))));
11179                   
11180                   FreeList(exp, FreeExpression);
11181                }
11182                else
11183                {
11184                   arrayExp.expType = ProcessTypeString("Container", false);
11185                   Compiler_Error($"Couldn't determine type of array elements\n");
11186                }
11187
11188                /*
11189                Declarator decl;
11190                OldList * specs = MkList();
11191
11192                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, 
11193                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11194                stmt.compound.declarations = MkListOne(
11195                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11196                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11197                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))), 
11198                      MkInitializerAssignment(MkExpBrackets(exp))))));
11199                */
11200             }
11201             else if(isLinkList && !isList)
11202             {
11203                Declarator decl;
11204                OldList * specs = MkList();
11205                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11206                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11207                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11208                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")), 
11209                      MkInitializerAssignment(MkExpBrackets(exp))))));
11210             }
11211             /*else if(isCustomAVLTree)
11212             {
11213                Declarator decl;
11214                OldList * specs = MkList();
11215                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11216                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11217                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11218                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")), 
11219                      MkInitializerAssignment(MkExpBrackets(exp))))));
11220             }*/
11221             else if(_class.templateArgs)
11222             {
11223                if(isMap)
11224                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11225                else
11226                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11227
11228                stmt.compound.declarations = MkListOne(
11229                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11230                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null, 
11231                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11232             }
11233             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11234
11235             if(block && block.type == compoundStmt && block.compound.context)
11236             {
11237                block.compound.context.parent = stmt.compound.context;
11238             }
11239             if(filter)
11240             {
11241                block = MkIfStmt(filter, block, null);
11242             }
11243             if(isArray)
11244             {
11245                stmt.compound.statements = MkListOne(MkForStmt(
11246                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11247                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<', 
11248                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11249                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11250                   block));
11251               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11252               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11253               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11254             }
11255             else if(isBuiltin)
11256             {
11257                char count[128];
11258                //OldList * specs = MkList();
11259                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11260
11261                sprintf(count, "%d", builtinCount);
11262
11263                stmt.compound.statements = MkListOne(MkForStmt(
11264                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11265                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<', 
11266                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11267                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11268                   block));
11269
11270                /*
11271                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11272                stmt.compound.statements = MkListOne(MkForStmt(
11273                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11274                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<', 
11275                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11276                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11277                   block));
11278               */
11279               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11280               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11281               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11282             }
11283             else if(isLinkList && !isList)
11284             {
11285                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11286                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11287                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString && 
11288                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11289                {
11290                   stmt.compound.statements = MkListOne(MkForStmt(
11291                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11292                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11293                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11294                      block));
11295                }
11296                else
11297                {
11298                   OldList * specs = MkList();
11299                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11300                   stmt.compound.statements = MkListOne(MkForStmt(
11301                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11302                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11303                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11304                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11305                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11306                      block));
11307                }
11308                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11309                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11310                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11311             }
11312             /*else if(isCustomAVLTree)
11313             {
11314                stmt.compound.statements = MkListOne(MkForStmt(
11315                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11316                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11317                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11318                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11319                   block));
11320
11321                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11322                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11323                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11324             }*/
11325             else
11326             {
11327                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11328                   MkIdentifier("Next")), null)), block));
11329             }
11330             ProcessExpressionType(expIt);
11331             if(stmt.compound.declarations->first)
11332                ProcessDeclaration(stmt.compound.declarations->first);
11333
11334             if(symbol) 
11335                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11336
11337             ProcessStatement(stmt);
11338             curContext = stmt.compound.context.parent;
11339             break;
11340          }
11341          else
11342          {
11343             Compiler_Error($"Expression is not a container\n");
11344          }
11345          break;
11346       }
11347       case gotoStmt:
11348          break;
11349       case continueStmt:
11350          break;
11351       case breakStmt:
11352          break;
11353       case returnStmt:
11354       {
11355          Expression exp;
11356          if(stmt.expressions)
11357          {
11358             for(exp = stmt.expressions->first; exp; exp = exp.next)
11359             {
11360                if(!exp.next)
11361                {
11362                   if(curFunction && !curFunction.type)
11363                      curFunction.type = ProcessType(
11364                         curFunction.specifiers, curFunction.declarator);
11365                   FreeType(exp.destType);
11366                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11367                   if(exp.destType) exp.destType.refCount++;
11368                }
11369                ProcessExpressionType(exp);
11370             }
11371          }
11372          break;
11373       }
11374       case badDeclarationStmt:
11375       {
11376          ProcessDeclaration(stmt.decl);
11377          break;
11378       }
11379       case asmStmt:
11380       {
11381          AsmField field;
11382          if(stmt.asmStmt.inputFields)
11383          {
11384             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11385                if(field.expression)
11386                   ProcessExpressionType(field.expression);
11387          }
11388          if(stmt.asmStmt.outputFields)
11389          {
11390             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11391                if(field.expression)
11392                   ProcessExpressionType(field.expression);
11393          }
11394          if(stmt.asmStmt.clobberedFields)
11395          {
11396             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11397             {
11398                if(field.expression)
11399                   ProcessExpressionType(field.expression);
11400             }
11401          }
11402          break;
11403       }
11404       case watchStmt:
11405       {
11406          PropertyWatch propWatch;
11407          OldList * watches = stmt._watch.watches;
11408          Expression object = stmt._watch.object;
11409          Expression watcher = stmt._watch.watcher;
11410          if(watcher)
11411             ProcessExpressionType(watcher);
11412          if(object)
11413             ProcessExpressionType(object);
11414
11415          if(inCompiler)
11416          {
11417             if(watcher || thisClass)
11418             {
11419                External external = curExternal;
11420                Context context = curContext;
11421
11422                stmt.type = expressionStmt;
11423                stmt.expressions = MkList();
11424
11425                curExternal = external.prev;
11426
11427                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11428                {
11429                   ClassFunction func;
11430                   char watcherName[1024];
11431                   Class watcherClass = watcher ? 
11432                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11433                   External createdExternal;
11434
11435                   // Create a declaration above
11436                   External externalDecl = MkExternalDeclaration(null);
11437                   ast->Insert(curExternal.prev, externalDecl);
11438
11439                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11440                   if(propWatch.deleteWatch)
11441                      strcat(watcherName, "_delete");
11442                   else
11443                   {
11444                      Identifier propID;
11445                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11446                      {
11447                         strcat(watcherName, "_");
11448                         strcat(watcherName, propID.string);
11449                      }
11450                   }
11451
11452                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11453                   {
11454                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11455                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11456                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11457                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11458                      ProcessClassFunctionBody(func, propWatch.compound);
11459                      propWatch.compound = null;
11460
11461                      //afterExternal = afterExternal ? afterExternal : curExternal;
11462
11463                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11464                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11465                      // TESTING THIS...
11466                      createdExternal.symbol.idCode = external.symbol.idCode;
11467
11468                      curExternal = createdExternal;
11469                      ProcessFunction(createdExternal.function);
11470
11471
11472                      // Create a declaration above
11473                      {
11474                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier), 
11475                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11476                         externalDecl.declaration = decl;
11477                         if(decl.symbol && !decl.symbol.pointerExternal)
11478                            decl.symbol.pointerExternal = externalDecl;
11479                      }
11480
11481                      if(propWatch.deleteWatch)
11482                      {
11483                         OldList * args = MkList();
11484                         ListAdd(args, CopyExpression(object));
11485                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11486                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11487                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11488                      }
11489                      else
11490                      {
11491                         Class _class = object.expType._class.registered;
11492                         Identifier propID;
11493
11494                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11495                         {
11496                            char propName[1024];
11497                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11498                            if(prop)
11499                            {
11500                               char getName[1024], setName[1024];
11501                               OldList * args = MkList();
11502
11503                               DeclareProperty(prop, setName, getName);                              
11504                               
11505                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
11506                               strcpy(propName, "__ecereProp_");
11507                               FullClassNameCat(propName, prop._class.fullName, false);
11508                               strcat(propName, "_");
11509                               // strcat(propName, prop.name);
11510                               FullClassNameCat(propName, prop.name, true);
11511
11512                               ListAdd(args, CopyExpression(object));
11513                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11514                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11515                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11516
11517                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
11518                            }
11519                            else
11520                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11521                         }
11522                      }
11523                   }
11524                   else
11525                      Compiler_Error($"Invalid watched object\n");
11526                }
11527
11528                curExternal = external;
11529                curContext = context;
11530
11531                if(watcher)
11532                   FreeExpression(watcher);
11533                if(object)
11534                   FreeExpression(object);
11535                FreeList(watches, FreePropertyWatch);
11536             }
11537             else
11538                Compiler_Error($"No observer specified and not inside a _class\n");
11539          }
11540          else
11541          {
11542             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11543             {
11544                ProcessStatement(propWatch.compound);
11545             }
11546
11547          }
11548          break;
11549       }
11550       case fireWatchersStmt:
11551       {
11552          OldList * watches = stmt._watch.watches;
11553          Expression object = stmt._watch.object;
11554          Class _class;
11555          // DEBUGGER BUG: Why doesn't watches evaluate to null??
11556          // printf("%X\n", watches);
11557          // printf("%X\n", stmt._watch.watches);
11558          if(object)
11559             ProcessExpressionType(object);
11560
11561          if(inCompiler)
11562          {
11563             _class = object ? 
11564                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
11565
11566             if(_class)
11567             {
11568                Identifier propID;
11569
11570                stmt.type = expressionStmt;
11571                stmt.expressions = MkList();
11572
11573                // Check if we're inside a property set
11574                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11575                {
11576                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
11577                }
11578                else if(!watches)
11579                {
11580                   //Compiler_Error($"No property specified and not inside a property set\n");
11581                }
11582                if(watches)
11583                {
11584                   for(propID = watches->first; propID; propID = propID.next)
11585                   {
11586                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11587                      if(prop)
11588                      {
11589                         CreateFireWatcher(prop, object, stmt);
11590                      }
11591                      else
11592                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
11593                   }
11594                }
11595                else
11596                {
11597                   // Fire all properties!
11598                   Property prop;
11599                   Class base;
11600                   for(base = _class; base; base = base.base)
11601                   {
11602                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
11603                      {
11604                         if(prop.isProperty && prop.isWatchable)
11605                         {
11606                            CreateFireWatcher(prop, object, stmt);
11607                         }
11608                      }
11609                   }
11610                }
11611
11612                if(object)
11613                   FreeExpression(object);
11614                FreeList(watches, FreeIdentifier);
11615             }
11616             else
11617                Compiler_Error($"Invalid object specified and not inside a class\n");
11618          }
11619          break;
11620       }
11621       case stopWatchingStmt:
11622       {
11623          OldList * watches = stmt._watch.watches;
11624          Expression object = stmt._watch.object;
11625          Expression watcher = stmt._watch.watcher;
11626          Class _class;
11627          if(object)
11628             ProcessExpressionType(object);
11629          if(watcher)
11630             ProcessExpressionType(watcher);
11631          if(inCompiler)
11632          {
11633             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
11634
11635             if(watcher || thisClass)
11636             {
11637                if(_class)
11638                {
11639                   Identifier propID;
11640
11641                   stmt.type = expressionStmt;
11642                   stmt.expressions = MkList();
11643
11644                   if(!watches)
11645                   {
11646                      OldList * args;
11647                      // eInstance_StopWatching(object, null, watcher); 
11648                      args = MkList();
11649                      ListAdd(args, CopyExpression(object));
11650                      ListAdd(args, MkExpConstant("0"));
11651                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11652                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11653                   }
11654                   else
11655                   {
11656                      for(propID = watches->first; propID; propID = propID.next)
11657                      {
11658                         char propName[1024];
11659                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11660                         if(prop)
11661                         {
11662                            char getName[1024], setName[1024];
11663                            OldList * args = MkList();
11664
11665                            DeclareProperty(prop, setName, getName);
11666          
11667                            // eInstance_StopWatching(object, prop, watcher); 
11668                            strcpy(propName, "__ecereProp_");
11669                            FullClassNameCat(propName, prop._class.fullName, false);
11670                            strcat(propName, "_");
11671                            // strcat(propName, prop.name);
11672                            FullClassNameCat(propName, prop.name, true);
11673                            MangleClassName(propName);
11674
11675                            ListAdd(args, CopyExpression(object));
11676                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11677                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11678                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11679                         }
11680                         else
11681                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11682                      }
11683                   }
11684
11685                   if(object)
11686                      FreeExpression(object);
11687                   if(watcher)
11688                      FreeExpression(watcher);
11689                   FreeList(watches, FreeIdentifier);
11690                }
11691                else
11692                   Compiler_Error($"Invalid object specified and not inside a class\n");
11693             }
11694             else
11695                Compiler_Error($"No observer specified and not inside a class\n");
11696          }
11697          break;
11698       }
11699    }
11700 }
11701
11702 static void ProcessFunction(FunctionDefinition function)
11703 {
11704    Identifier id = GetDeclId(function.declarator);
11705    Symbol symbol = function.declarator ? function.declarator.symbol : null;
11706    Type type = symbol ? symbol.type : null;
11707    Class oldThisClass = thisClass;
11708    Context oldTopContext = topContext;
11709
11710    yylloc = function.loc;
11711    // Process thisClass
11712    
11713    if(type && type.thisClass)
11714    {
11715       Symbol classSym = type.thisClass;
11716       Class _class = type.thisClass.registered;
11717       char className[1024];
11718       char structName[1024];
11719       Declarator funcDecl;
11720       Symbol thisSymbol;
11721
11722       bool typedObject = false;
11723
11724       if(_class && !_class.base)
11725       {
11726          _class = currentClass;
11727          if(_class && !_class.symbol)
11728             _class.symbol = FindClass(_class.fullName);
11729          classSym = _class ? _class.symbol : null;
11730          typedObject = true;
11731       }
11732
11733       thisClass = _class;
11734
11735       if(inCompiler && _class)
11736       {
11737          if(type.kind == functionType)
11738          {
11739             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
11740             {
11741                //TypeName param = symbol.type.params.first;
11742                Type param = symbol.type.params.first;
11743                symbol.type.params.Remove(param);
11744                //FreeTypeName(param);
11745                FreeType(param);
11746             }
11747             if(type.classObjectType != classPointer)
11748             {
11749                symbol.type.params.Insert(null, MkClassType(_class.fullName));
11750                symbol.type.staticMethod = true;
11751                symbol.type.thisClass = null;
11752
11753                // HIGH DANGER: VERIFYING THIS...
11754                symbol.type.extraParam = false;
11755             }
11756          }
11757
11758          strcpy(className, "__ecereClass_");
11759          FullClassNameCat(className, _class.fullName, true);
11760
11761          MangleClassName(className);
11762
11763          structName[0] = 0;
11764          FullClassNameCat(structName, _class.fullName, false);
11765
11766          // [class] this
11767          
11768
11769          funcDecl = GetFuncDecl(function.declarator);
11770          if(funcDecl)
11771          {
11772             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
11773             {
11774                TypeName param = funcDecl.function.parameters->first;
11775                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
11776                {
11777                   funcDecl.function.parameters->Remove(param);
11778                   FreeTypeName(param);
11779                }
11780             }
11781
11782             // DANGER: Watch for this... Check if it's a Conversion?
11783             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
11784             
11785             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
11786             if(!function.propertyNoThis)
11787             {
11788                TypeName thisParam;
11789                
11790                if(type.classObjectType != classPointer)
11791                {
11792                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
11793                   if(!funcDecl.function.parameters)
11794                      funcDecl.function.parameters = MkList();
11795                   funcDecl.function.parameters->Insert(null, thisParam);
11796                }
11797
11798                if(typedObject)
11799                {
11800                   if(type.classObjectType != classPointer)
11801                   {
11802                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
11803                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
11804                   }
11805
11806                   thisParam = TypeName
11807                   {
11808                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11809                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11810                   };
11811                   funcDecl.function.parameters->Insert(null, thisParam);
11812                }
11813             }
11814          }
11815
11816          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
11817          {
11818             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
11819             funcDecl = GetFuncDecl(initDecl.declarator);
11820             if(funcDecl)
11821             {
11822                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
11823                {
11824                   TypeName param = funcDecl.function.parameters->first;
11825                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
11826                   {
11827                      funcDecl.function.parameters->Remove(param);
11828                      FreeTypeName(param);
11829                   }
11830                }
11831
11832                if(type.classObjectType != classPointer)
11833                {
11834                   // DANGER: Watch for this... Check if it's a Conversion?
11835                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
11836                   {
11837                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
11838
11839                      if(!funcDecl.function.parameters)
11840                         funcDecl.function.parameters = MkList();
11841                      funcDecl.function.parameters->Insert(null, thisParam);
11842                   }
11843                }
11844             }         
11845          }
11846       }
11847       
11848       // Add this to the context
11849       if(function.body)
11850       {
11851          if(type.classObjectType != classPointer)
11852          {
11853             thisSymbol = Symbol
11854             {
11855                string = CopyString("this");
11856                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
11857             };
11858             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
11859
11860             if(typedObject && thisSymbol.type)
11861             {
11862                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
11863                thisSymbol.type.byReference = type.byReference;
11864                /*
11865                thisSymbol = Symbol { string = CopyString("class") };
11866                function.body.compound.context.symbols.Add(thisSymbol);
11867                */
11868             }
11869          }
11870       }
11871
11872       // Pointer to class data
11873       
11874       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
11875       {
11876          DataMember member = null;
11877          {
11878             Class base;
11879             for(base = _class; base && base.type != systemClass; base = base.next)
11880             {
11881                for(member = base.membersAndProperties.first; member; member = member.next)
11882                   if(!member.isProperty)
11883                      break;
11884                if(member)
11885                   break;
11886             }
11887          }
11888          for(member = _class.membersAndProperties.first; member; member = member.next)
11889             if(!member.isProperty)
11890                break;
11891          if(member)
11892          {
11893             char pointerName[1024];
11894    
11895             Declaration decl;
11896             Initializer initializer;
11897             Expression exp, bytePtr;
11898    
11899             strcpy(pointerName, "__ecerePointer_");
11900             FullClassNameCat(pointerName, _class.fullName, false);
11901             {
11902                char className[1024];
11903                strcpy(className, "__ecereClass_");
11904                FullClassNameCat(className, classSym.string, true);
11905                MangleClassName(className);
11906
11907                // Testing This
11908                DeclareClass(classSym, className);
11909             }
11910
11911             // ((byte *) this)
11912             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
11913
11914             if(_class.fixed)
11915             {
11916                char string[256];
11917                sprintf(string, "%d", _class.offset);
11918                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
11919             }
11920             else
11921             {
11922                // ([bytePtr] + [className]->offset)
11923                exp = QBrackets(MkExpOp(bytePtr, '+',
11924                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
11925             }
11926
11927             // (this ? [exp] : 0)
11928             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
11929             exp.expType = Type
11930             {
11931                refCount = 1;
11932                kind = pointerType;
11933                type = Type { refCount = 1, kind = voidType };
11934             };
11935    
11936             if(function.body)
11937             {
11938                yylloc = function.body.loc;
11939                // ([structName] *) [exp]
11940                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
11941                initializer = MkInitializerAssignment(
11942                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
11943
11944                // [structName] * [pointerName] = [initializer];
11945                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
11946
11947                {
11948                   Context prevContext = curContext;
11949                   curContext = function.body.compound.context;
11950
11951                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
11952                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
11953
11954                   curContext = prevContext;
11955                }
11956
11957                // WHY?
11958                decl.symbol = null;
11959
11960                if(!function.body.compound.declarations)
11961                   function.body.compound.declarations = MkList();
11962                function.body.compound.declarations->Insert(null, decl);
11963             }
11964          }
11965       }
11966       
11967
11968       // Loop through the function and replace undeclared identifiers
11969       // which are a member of the class (methods, properties or data)
11970       // by "this.[member]"
11971    }
11972    else
11973       thisClass = null;
11974
11975    if(id)
11976    {
11977       FreeSpecifier(id._class);
11978       id._class = null;
11979
11980       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
11981       {
11982          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
11983          id = GetDeclId(initDecl.declarator);
11984
11985          FreeSpecifier(id._class);
11986          id._class = null;
11987       }
11988    }
11989    if(function.body)
11990       topContext = function.body.compound.context;
11991    {
11992       FunctionDefinition oldFunction = curFunction;
11993       curFunction = function;
11994       if(function.body)
11995          ProcessStatement(function.body);
11996
11997       // If this is a property set and no firewatchers has been done yet, add one here
11998       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
11999       {
12000          Statement prevCompound = curCompound;
12001          Context prevContext = curContext;
12002
12003          Statement fireWatchers = MkFireWatchersStmt(null, null);
12004          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12005          ListAdd(function.body.compound.statements, fireWatchers);
12006
12007          curCompound = function.body;
12008          curContext = function.body.compound.context;
12009
12010          ProcessStatement(fireWatchers);
12011
12012          curContext = prevContext;
12013          curCompound = prevCompound;
12014
12015       }
12016
12017       curFunction = oldFunction;
12018    }
12019
12020    if(function.declarator)
12021    {
12022       ProcessDeclarator(function.declarator);
12023    }
12024
12025    topContext = oldTopContext;
12026    thisClass = oldThisClass;
12027 }
12028
12029 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12030 static void ProcessClass(OldList definitions, Symbol symbol)
12031 {
12032    ClassDef def;
12033    External external = curExternal;
12034    Class regClass = symbol ? symbol.registered : null;
12035
12036    // Process all functions
12037    for(def = definitions.first; def; def = def.next)
12038    {
12039       if(def.type == functionClassDef)
12040       {
12041          if(def.function.declarator)
12042             curExternal = def.function.declarator.symbol.pointerExternal;
12043          else
12044             curExternal = external;
12045
12046          ProcessFunction((FunctionDefinition)def.function);
12047       }
12048       else if(def.type == declarationClassDef)
12049       {
12050          if(def.decl.type == instDeclaration)
12051          {
12052             thisClass = regClass;
12053             ProcessInstantiationType(def.decl.inst);
12054             thisClass = null;
12055          }
12056          // Testing this
12057          else
12058          {
12059             Class backThisClass = thisClass;
12060             if(regClass) thisClass = regClass;
12061             ProcessDeclaration(def.decl);
12062             thisClass = backThisClass;
12063          }
12064       }
12065       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12066       {
12067          MemberInit defProperty;
12068
12069          // Add this to the context
12070          Symbol thisSymbol = Symbol
12071          {
12072             string = CopyString("this");
12073             type = regClass ? MkClassType(regClass.fullName) : null;
12074          };
12075          globalContext.symbols.Add((BTNode)thisSymbol);
12076          
12077          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12078          {
12079             thisClass = regClass;
12080             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12081             thisClass = null;
12082          }
12083
12084          globalContext.symbols.Remove((BTNode)thisSymbol);
12085          FreeSymbol(thisSymbol);
12086       }
12087       else if(def.type == propertyClassDef && def.propertyDef)
12088       {
12089          PropertyDef prop = def.propertyDef;
12090
12091          // Add this to the context
12092          /*
12093          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12094          globalContext.symbols.Add(thisSymbol);
12095          */
12096          
12097          thisClass = regClass;
12098          if(prop.setStmt)
12099          {
12100             if(regClass)
12101             {
12102                Symbol thisSymbol
12103                {
12104                   string = CopyString("this");
12105                   type = MkClassType(regClass.fullName);
12106                };
12107                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12108             }
12109
12110             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12111             ProcessStatement(prop.setStmt);
12112          }
12113          if(prop.getStmt)
12114          {
12115             if(regClass)
12116             {
12117                Symbol thisSymbol
12118                {
12119                   string = CopyString("this");
12120                   type = MkClassType(regClass.fullName);
12121                };
12122                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12123             }
12124
12125             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12126             ProcessStatement(prop.getStmt);
12127          }
12128          if(prop.issetStmt)
12129          {
12130             if(regClass)
12131             {
12132                Symbol thisSymbol
12133                {
12134                   string = CopyString("this");
12135                   type = MkClassType(regClass.fullName);
12136                };
12137                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12138             }
12139
12140             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12141             ProcessStatement(prop.issetStmt);
12142          }
12143
12144          thisClass = null;
12145
12146          /*
12147          globalContext.symbols.Remove(thisSymbol);
12148          FreeSymbol(thisSymbol);
12149          */
12150       }
12151       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12152       {
12153          PropertyWatch propertyWatch = def.propertyWatch;
12154         
12155          thisClass = regClass;
12156          if(propertyWatch.compound)
12157          {
12158             Symbol thisSymbol
12159             {
12160                string = CopyString("this");
12161                type = regClass ? MkClassType(regClass.fullName) : null;
12162             };
12163
12164             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12165
12166             curExternal = null;
12167             ProcessStatement(propertyWatch.compound);
12168          }
12169          thisClass = null;
12170       }
12171    }
12172 }
12173
12174 void DeclareFunctionUtil(String s)
12175 {
12176    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12177    if(function)
12178    {
12179       char name[1024];
12180       name[0] = 0;
12181       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12182          strcpy(name, "__ecereFunction_");
12183       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12184       DeclareFunction(function, name);
12185    }
12186 }
12187
12188 void ComputeDataTypes()
12189 {
12190    External external;
12191    External temp { };
12192    External after = null;
12193
12194    currentClass = null;
12195
12196    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12197
12198    for(external = ast->first; external; external = external.next)
12199    {
12200       if(external.type == declarationExternal)
12201       {
12202          Declaration decl = external.declaration;
12203          if(decl)
12204          {
12205             OldList * decls = decl.declarators;
12206             if(decls)
12207             {
12208                InitDeclarator initDecl = decls->first;
12209                if(initDecl)
12210                {
12211                   Declarator declarator = initDecl.declarator;
12212                   if(declarator && declarator.type == identifierDeclarator)
12213                   {
12214                      Identifier id = declarator.identifier;
12215                      if(id && id.string)
12216                      {
12217                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12218                         {
12219                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12220                            after = external;
12221                         }
12222                      }
12223                   }
12224                }
12225             }
12226          }
12227        }
12228    }
12229
12230    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12231    ast->Insert(after, temp);
12232    curExternal = temp;
12233
12234    DeclareFunctionUtil("eSystem_New");
12235    DeclareFunctionUtil("eSystem_New0");
12236    DeclareFunctionUtil("eSystem_Renew");
12237    DeclareFunctionUtil("eSystem_Renew0");
12238    DeclareFunctionUtil("eClass_GetProperty");
12239
12240    DeclareStruct("ecere::com::Class", false);
12241    DeclareStruct("ecere::com::Instance", false);
12242    DeclareStruct("ecere::com::Property", false);
12243    DeclareStruct("ecere::com::DataMember", false);
12244    DeclareStruct("ecere::com::Method", false);
12245    DeclareStruct("ecere::com::SerialBuffer", false);
12246    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12247
12248    ast->Remove(temp);
12249
12250    for(external = ast->first; external; external = external.next)
12251    {
12252       afterExternal = curExternal = external;
12253       if(external.type == functionExternal)
12254       {
12255          currentClass = external.function._class;
12256          ProcessFunction(external.function);
12257       }
12258       // There shouldn't be any _class member access here anyways...
12259       else if(external.type == declarationExternal)
12260       {
12261          currentClass = null;
12262          ProcessDeclaration(external.declaration);
12263       }
12264       else if(external.type == classExternal)
12265       {
12266          ClassDefinition _class = external._class;
12267          currentClass = external.symbol.registered;
12268          if(_class.definitions)
12269          {
12270             ProcessClass(_class.definitions, _class.symbol);
12271          }
12272          if(inCompiler)
12273          {
12274             // Free class data...
12275             ast->Remove(external);
12276             delete external;
12277          }
12278       }
12279       else if(external.type == nameSpaceExternal)
12280       {
12281          thisNameSpace = external.id.string;
12282       }
12283    }
12284    currentClass = null;
12285    thisNameSpace = null;
12286
12287    delete temp.symbol;
12288    delete temp;
12289 }