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