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