cleaned all trailing white space from source files.
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50
51       if(exp)
52          OutputExpression(exp, f);
53       f.Seek(0, start);
54       count = strlen(string);
55       count += f.Read(string + count, 1, 1023);
56       string[count] = '\0';
57       delete f;
58    }
59 }
60
61 Type ProcessTemplateParameterType(TemplateParameter param)
62 {
63    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
64    {
65       // TOFIX: Will need to free this Type
66       if(!param.baseType)
67       {
68          if(param.dataTypeString)
69             param.baseType = ProcessTypeString(param.dataTypeString, false);
70          else
71             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
72       }
73       return param.baseType;
74    }
75    return null;
76 }
77
78 bool NeedCast(Type type1, Type type2)
79 {
80    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
81
82    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
83    {
84       return false;
85    }
86
87    if(type1.kind == type2.kind)
88    {
89       switch(type1.kind)
90       {
91          case charType:
92          case shortType:
93          case intType:
94          case int64Type:
95          case intPtrType:
96          case intSizeType:
97             if(type1.passAsTemplate && !type2.passAsTemplate)
98                return true;
99             return type1.isSigned != type2.isSigned;
100          case classType:
101             return type1._class != type2._class;
102          case pointerType:
103             return NeedCast(type1.type, type2.type);
104          default:
105             return true; //false; ????
106       }
107    }
108    return true;
109 }
110
111 static void ReplaceClassMembers(Expression exp, Class _class)
112 {
113    if(exp.type == identifierExp && exp.identifier)
114    {
115       Identifier id = exp.identifier;
116       Context ctx;
117       Symbol symbol = null;
118       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
119       {
120          // First, check if the identifier is declared inside the function
121          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
122          {
123             symbol = (Symbol)ctx.symbols.FindString(id.string);
124             if(symbol) break;
125          }
126       }
127
128       // If it is not, check if it is a member of the _class
129       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
130       {
131          Property prop = eClass_FindProperty(_class, id.string, privateModule);
132          Method method = null;
133          DataMember member = null;
134          ClassProperty classProp = null;
135          if(!prop)
136          {
137             method = eClass_FindMethod(_class, id.string, privateModule);
138          }
139          if(!prop && !method)
140             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
141          if(!prop && !method && !member)
142          {
143             classProp = eClass_FindClassProperty(_class, id.string);
144          }
145          if(prop || method || member || classProp)
146          {
147             // Replace by this.[member]
148             exp.type = memberExp;
149             exp.member.member = id;
150             exp.member.memberType = unresolvedMember;
151             exp.member.exp = QMkExpId("this");
152             //exp.member.exp.loc = exp.loc;
153             exp.addedThis = true;
154          }
155          else if(_class && _class.templateParams.first)
156          {
157             Class sClass;
158             for(sClass = _class; sClass; sClass = sClass.base)
159             {
160                if(sClass.templateParams.first)
161                {
162                   ClassTemplateParameter param;
163                   for(param = sClass.templateParams.first; param; param = param.next)
164                   {
165                      if(param.type == expression && !strcmp(param.name, id.string))
166                      {
167                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
168
169                         if(argExp)
170                         {
171                            Declarator decl;
172                            OldList * specs = MkList();
173
174                            FreeIdentifier(exp.member.member);
175
176                            ProcessExpressionType(argExp);
177
178                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
179
180                            exp.expType = ProcessType(specs, decl);
181
182                            // *[expType] *[argExp]
183                            exp.type = bracketsExp;
184                            exp.list = MkListOne(MkExpOp(null, '*',
185                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
186                         }
187                      }
188                   }
189                }
190             }
191          }
192       }
193    }
194 }
195
196 ////////////////////////////////////////////////////////////////////////
197 // PRINTING ////////////////////////////////////////////////////////////
198 ////////////////////////////////////////////////////////////////////////
199
200 public char * PrintInt(int64 result)
201 {
202    char temp[100];
203    if(result > MAXINT64)
204       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
205    else
206       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
207    return CopyString(temp);
208 }
209
210 public char * PrintUInt(uint64 result)
211 {
212    char temp[100];
213    if(result > MAXDWORD)
214       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
215    else if(result > MAXINT)
216       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
217    else
218       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
219    return CopyString(temp);
220 }
221
222 public char * PrintInt64(int64 result)
223 {
224    char temp[100];
225    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
226    return CopyString(temp);
227 }
228
229 public char * PrintUInt64(uint64 result)
230 {
231    char temp[100];
232    if(result > MAXINT64)
233       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
234    else
235       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
236    return CopyString(temp);
237 }
238
239 public char * PrintHexUInt(uint64 result)
240 {
241    char temp[100];
242    if(result > MAXDWORD)
243       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
244    else
245       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
246    return CopyString(temp);
247 }
248
249 public char * PrintHexUInt64(uint64 result)
250 {
251    char temp[100];
252    if(result > MAXDWORD)
253       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
254    else
255       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
256    return CopyString(temp);
257 }
258
259 public char * PrintShort(short result)
260 {
261    char temp[100];
262    sprintf(temp, "%d", (unsigned short)result);
263    return CopyString(temp);
264 }
265
266 public char * PrintUShort(unsigned short result)
267 {
268    char temp[100];
269    if(result > 32767)
270       sprintf(temp, "0x%X", (int)result);
271    else
272       sprintf(temp, "%d", (int)result);
273    return CopyString(temp);
274 }
275
276 public char * PrintChar(char result)
277 {
278    char temp[100];
279    if(result > 0 && isprint(result))
280       sprintf(temp, "'%c'", result);
281    else if(result < 0)
282       sprintf(temp, "%d", (int)result);
283    else
284       //sprintf(temp, "%#X", result);
285       sprintf(temp, "0x%X", (unsigned char)result);
286    return CopyString(temp);
287 }
288
289 public char * PrintUChar(unsigned char result)
290 {
291    char temp[100];
292    sprintf(temp, "0x%X", result);
293    return CopyString(temp);
294 }
295
296 public char * PrintFloat(float result)
297 {
298    char temp[350];
299    sprintf(temp, "%.16ff", result);
300    return CopyString(temp);
301 }
302
303 public char * PrintDouble(double result)
304 {
305    char temp[350];
306    sprintf(temp, "%.16f", result);
307    return CopyString(temp);
308 }
309
310 ////////////////////////////////////////////////////////////////////////
311 ////////////////////////////////////////////////////////////////////////
312
313 //public Operand GetOperand(Expression exp);
314
315 #define GETVALUE(name, t) \
316    public bool Get##name(Expression exp, t * value2) \
317    {                                                        \
318       Operand op2 = GetOperand(exp);                        \
319       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
320       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
321       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
322       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
323       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
324       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
325       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
326       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
327       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
328       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
329       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
330       else if(op2.kind == charType) *value2 = (t) op2.uc;                         \
331       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
332       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
333       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
334       else                                                                          \
335          return false;                                                              \
336       return true;                                                                  \
337    }
338
339 // To help the deubugger currently not preprocessing...
340 #define HELP(x) x
341
342 GETVALUE(Int, HELP(int));
343 GETVALUE(UInt, HELP(unsigned int));
344 GETVALUE(Int64, HELP(int64));
345 GETVALUE(UInt64, HELP(uint64));
346 GETVALUE(IntPtr, HELP(intptr));
347 GETVALUE(UIntPtr, HELP(uintptr));
348 GETVALUE(IntSize, HELP(intsize));
349 GETVALUE(UIntSize, HELP(uintsize));
350 GETVALUE(Short, HELP(short));
351 GETVALUE(UShort, HELP(unsigned short));
352 GETVALUE(Char, HELP(char));
353 GETVALUE(UChar, HELP(unsigned char));
354 GETVALUE(Float, HELP(float));
355 GETVALUE(Double, HELP(double));
356
357 void ComputeExpression(Expression exp);
358
359 void ComputeClassMembers(Class _class, bool isMember)
360 {
361    DataMember member = isMember ? (DataMember) _class : null;
362    Context context = isMember ? null : SetupTemplatesContext(_class);
363    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
364                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
365    {
366       int c;
367       int unionMemberOffset = 0;
368       int bitFields = 0;
369
370       /*
371       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
372          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
373       */
374
375       if(member)
376       {
377          member.memberOffset = 0;
378          if(targetBits < sizeof(void *) * 8)
379             member.structAlignment = 0;
380       }
381       else if(targetBits < sizeof(void *) * 8)
382          _class.structAlignment = 0;
383
384       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
385       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
386          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
387
388       if(!member && _class.destructionWatchOffset)
389          _class.memberOffset += sizeof(OldList);
390
391       // To avoid reentrancy...
392       //_class.structSize = -1;
393
394       {
395          DataMember dataMember;
396          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
397          {
398             if(!dataMember.isProperty)
399             {
400                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
401                {
402                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
403                   /*if(!dataMember.dataType)
404                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
405                      */
406                }
407             }
408          }
409       }
410
411       {
412          DataMember dataMember;
413          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
414          {
415             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
416             {
417                if(!isMember && _class.type == bitClass && dataMember.dataType)
418                {
419                   BitMember bitMember = (BitMember) dataMember;
420                   uint64 mask = 0;
421                   int d;
422
423                   ComputeTypeSize(dataMember.dataType);
424
425                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
426                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
427
428                   _class.memberOffset = bitMember.pos + bitMember.size;
429                   for(d = 0; d<bitMember.size; d++)
430                   {
431                      if(d)
432                         mask <<= 1;
433                      mask |= 1;
434                   }
435                   bitMember.mask = mask << bitMember.pos;
436                }
437                else if(dataMember.type == normalMember && dataMember.dataType)
438                {
439                   int size;
440                   int alignment = 0;
441
442                   // Prevent infinite recursion
443                   if(dataMember.dataType.kind != classType ||
444                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
445                      _class.type != structClass)))
446                      ComputeTypeSize(dataMember.dataType);
447
448                   if(dataMember.dataType.bitFieldCount)
449                   {
450                      bitFields += dataMember.dataType.bitFieldCount;
451                      size = 0;
452                   }
453                   else
454                   {
455                      if(bitFields)
456                      {
457                         int size = (bitFields + 7) / 8;
458
459                         if(isMember)
460                         {
461                            // TESTING THIS PADDING CODE
462                            if(alignment)
463                            {
464                               member.structAlignment = Max(member.structAlignment, alignment);
465
466                               if(member.memberOffset % alignment)
467                                  member.memberOffset += alignment - (member.memberOffset % alignment);
468                            }
469
470                            dataMember.offset = member.memberOffset;
471                            if(member.type == unionMember)
472                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
473                            else
474                            {
475                               member.memberOffset += size;
476                            }
477                         }
478                         else
479                         {
480                            // TESTING THIS PADDING CODE
481                            if(alignment)
482                            {
483                               _class.structAlignment = Max(_class.structAlignment, alignment);
484
485                               if(_class.memberOffset % alignment)
486                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
487                            }
488
489                            dataMember.offset = _class.memberOffset;
490                            _class.memberOffset += size;
491                         }
492                         bitFields = 0;
493                      }
494                      size = dataMember.dataType.size;
495                      alignment = dataMember.dataType.alignment;
496                   }
497
498                   if(isMember)
499                   {
500                      // TESTING THIS PADDING CODE
501                      if(alignment)
502                      {
503                         member.structAlignment = Max(member.structAlignment, alignment);
504
505                         if(member.memberOffset % alignment)
506                            member.memberOffset += alignment - (member.memberOffset % alignment);
507                      }
508
509                      dataMember.offset = member.memberOffset;
510                      if(member.type == unionMember)
511                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
512                      else
513                      {
514                         member.memberOffset += size;
515                      }
516                   }
517                   else
518                   {
519                      // TESTING THIS PADDING CODE
520                      if(alignment)
521                      {
522                         _class.structAlignment = Max(_class.structAlignment, alignment);
523
524                         if(_class.memberOffset % alignment)
525                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
526                      }
527
528                      dataMember.offset = _class.memberOffset;
529                      _class.memberOffset += size;
530                   }
531                }
532                else
533                {
534                   int alignment;
535
536                   ComputeClassMembers((Class)dataMember, true);
537                   alignment = dataMember.structAlignment;
538
539                   if(isMember)
540                   {
541                      if(alignment)
542                      {
543                         if(member.memberOffset % alignment)
544                            member.memberOffset += alignment - (member.memberOffset % alignment);
545
546                         member.structAlignment = Max(member.structAlignment, alignment);
547                      }
548                      dataMember.offset = member.memberOffset;
549                      if(member.type == unionMember)
550                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
551                      else
552                         member.memberOffset += dataMember.memberOffset;
553                   }
554                   else
555                   {
556                      if(alignment)
557                      {
558                         if(_class.memberOffset % alignment)
559                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
560                         _class.structAlignment = Max(_class.structAlignment, alignment);
561                      }
562                      dataMember.offset = _class.memberOffset;
563                      _class.memberOffset += dataMember.memberOffset;
564                   }
565                }
566             }
567          }
568          if(bitFields)
569          {
570             int alignment = 0;
571             int size = (bitFields + 7) / 8;
572
573             if(isMember)
574             {
575                // TESTING THIS PADDING CODE
576                if(alignment)
577                {
578                   member.structAlignment = Max(member.structAlignment, alignment);
579
580                   if(member.memberOffset % alignment)
581                      member.memberOffset += alignment - (member.memberOffset % alignment);
582                }
583
584                if(member.type == unionMember)
585                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
586                else
587                {
588                   member.memberOffset += size;
589                }
590             }
591             else
592             {
593                // TESTING THIS PADDING CODE
594                if(alignment)
595                {
596                   _class.structAlignment = Max(_class.structAlignment, alignment);
597
598                   if(_class.memberOffset % alignment)
599                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
600                }
601                _class.memberOffset += size;
602             }
603             bitFields = 0;
604          }
605       }
606       if(member && member.type == unionMember)
607       {
608          member.memberOffset = unionMemberOffset;
609       }
610
611       if(!isMember)
612       {
613          /*if(_class.type == structClass)
614             _class.size = _class.memberOffset;
615          else
616          */
617
618          if(_class.type != bitClass)
619          {
620             int extra = 0;
621             if(_class.structAlignment)
622             {
623                if(_class.memberOffset % _class.structAlignment)
624                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
625             }
626             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
627             if(!member)
628             {
629                Property prop;
630                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
631                {
632                   if(prop.isProperty && prop.isWatchable)
633                   {
634                      prop.watcherOffset = _class.structSize;
635                      _class.structSize += sizeof(OldList);
636                   }
637                }
638             }
639
640             // Fix Derivatives
641             {
642                OldLink derivative;
643                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
644                {
645                   Class deriv = derivative.data;
646
647                   if(deriv.computeSize)
648                   {
649                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
650                      deriv.offset = /*_class.offset + */_class.structSize;
651                      deriv.memberOffset = 0;
652                      // ----------------------
653
654                      deriv.structSize = deriv.offset;
655
656                      ComputeClassMembers(deriv, false);
657                   }
658                }
659             }
660          }
661       }
662    }
663    if(context)
664       FinishTemplatesContext(context);
665 }
666
667 public void ComputeModuleClasses(Module module)
668 {
669    Class _class;
670    OldLink subModule;
671
672    for(subModule = module.modules.first; subModule; subModule = subModule.next)
673       ComputeModuleClasses(subModule.data);
674    for(_class = module.classes.first; _class; _class = _class.next)
675       ComputeClassMembers(_class, false);
676 }
677
678
679 public int ComputeTypeSize(Type type)
680 {
681    uint size = type ? type.size : 0;
682    if(!size && type && !type.computing)
683    {
684       type.computing = true;
685       switch(type.kind)
686       {
687          case charType: type.alignment = size = sizeof(char); break;
688          case intType: type.alignment = size = sizeof(int); break;
689          case int64Type: type.alignment = size = sizeof(int64); break;
690          case intPtrType: type.alignment = size = targetBits / 8; break;
691          case intSizeType: type.alignment = size = targetBits / 8; break;
692          case longType: type.alignment = size = sizeof(long); break;
693          case shortType: type.alignment = size = sizeof(short); break;
694          case floatType: type.alignment = size = sizeof(float); break;
695          case doubleType: type.alignment = size = sizeof(double); break;
696          case classType:
697          {
698             Class _class = type._class ? type._class.registered : null;
699
700             if(_class && _class.type == structClass)
701             {
702                // Ensure all members are properly registered
703                ComputeClassMembers(_class, false);
704                type.alignment = _class.structAlignment;
705                size = _class.structSize;
706                if(type.alignment && size % type.alignment)
707                   size += type.alignment - (size % type.alignment);
708
709             }
710             else if(_class && (_class.type == unitClass ||
711                    _class.type == enumClass ||
712                    _class.type == bitClass))
713             {
714                if(!_class.dataType)
715                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
716                size = type.alignment = ComputeTypeSize(_class.dataType);
717             }
718             else
719                size = type.alignment = targetBits / 8; // sizeof(Instance *);
720             break;
721          }
722          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
723          case arrayType:
724             if(type.arraySizeExp)
725             {
726                ProcessExpressionType(type.arraySizeExp);
727                ComputeExpression(type.arraySizeExp);
728                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType &&
729                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
730                {
731                   Location oldLoc = yylloc;
732                   // bool isConstant = type.arraySizeExp.isConstant;
733                   char expression[10240];
734                   expression[0] = '\0';
735                   type.arraySizeExp.expType = null;
736                   yylloc = type.arraySizeExp.loc;
737                   if(inCompiler)
738                      PrintExpression(type.arraySizeExp, expression);
739                   Compiler_Error($"Array size not constant int (%s)\n", expression);
740                   yylloc = oldLoc;
741                }
742                GetInt(type.arraySizeExp, &type.arraySize);
743             }
744             else if(type.enumClass)
745             {
746                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
747                {
748                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
749                }
750                else
751                   type.arraySize = 0;
752             }
753             else
754             {
755                // Unimplemented auto size
756                type.arraySize = 0;
757             }
758
759             size = ComputeTypeSize(type.type) * type.arraySize;
760             if(type.type)
761                type.alignment = type.type.alignment;
762
763             break;
764          case structType:
765          {
766             Type member;
767             for(member = type.members.first; member; member = member.next)
768             {
769                uint addSize = ComputeTypeSize(member);
770
771                member.offset = size;
772                if(member.alignment && size % member.alignment)
773                   member.offset += member.alignment - (size % member.alignment);
774                size = member.offset;
775
776                type.alignment = Max(type.alignment, member.alignment);
777                size += addSize;
778             }
779             if(type.alignment && size % type.alignment)
780                size += type.alignment - (size % type.alignment);
781             break;
782          }
783          case unionType:
784          {
785             Type member;
786             for(member = type.members.first; member; member = member.next)
787             {
788                uint addSize = ComputeTypeSize(member);
789
790                member.offset = size;
791                if(member.alignment && size % member.alignment)
792                   member.offset += member.alignment - (size % member.alignment);
793                size = member.offset;
794
795                type.alignment = Max(type.alignment, member.alignment);
796                size = Max(size, addSize);
797             }
798             if(type.alignment && size % type.alignment)
799                size += type.alignment - (size % type.alignment);
800             break;
801          }
802          case templateType:
803          {
804             TemplateParameter param = type.templateParameter;
805             Type baseType = ProcessTemplateParameterType(param);
806             if(baseType)
807             {
808                size = ComputeTypeSize(baseType);
809                type.alignment = baseType.alignment;
810             }
811             else
812                type.alignment = size = sizeof(uint64);
813             break;
814          }
815          case enumType:
816          {
817             type.alignment = size = sizeof(enum { test });
818             break;
819          }
820          case thisClassType:
821          {
822             type.alignment = size = targetBits / 8; //sizeof(void *);
823             break;
824          }
825       }
826       type.size = size;
827       type.computing = false;
828    }
829    return size;
830 }
831
832
833 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
834 {
835    // This function is in need of a major review when implementing private members etc.
836    DataMember topMember = isMember ? (DataMember) _class : null;
837    uint totalSize = 0;
838    uint maxSize = 0;
839    int alignment, size;
840    DataMember member;
841    Context context = isMember ? null : SetupTemplatesContext(_class);
842    if(addedPadding)
843       *addedPadding = false;
844
845    if(!isMember && _class.base)
846    {
847       maxSize = _class.structSize;
848       //if(_class.base.type != systemClass) // Commented out with new Instance _class
849       {
850          // DANGER: Testing this noHeadClass here...
851          if(_class.type == structClass || _class.type == noHeadClass)
852             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
853          else
854          {
855             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
856             if(maxSize > baseSize)
857                maxSize -= baseSize;
858             else
859                maxSize = 0;
860          }
861       }
862    }
863
864    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
865    {
866       if(!member.isProperty)
867       {
868          switch(member.type)
869          {
870             case normalMember:
871             {
872                if(member.dataTypeString)
873                {
874                   OldList * specs = MkList(), * decls = MkList();
875                   Declarator decl;
876
877                   decl = SpecDeclFromString(member.dataTypeString, specs,
878                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
879                   ListAdd(decls, MkStructDeclarator(decl, null));
880                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
881
882                   if(!member.dataType)
883                      member.dataType = ProcessType(specs, decl);
884
885                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
886
887                   {
888                      Type type = ProcessType(specs, decl);
889                      DeclareType(member.dataType, false, false);
890                      FreeType(type);
891                   }
892                   /*
893                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
894                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
895                      DeclareStruct(member.dataType._class.string, false);
896                   */
897
898                   ComputeTypeSize(member.dataType);
899                   size = member.dataType.size;
900                   alignment = member.dataType.alignment;
901
902                   if(alignment)
903                   {
904                      if(totalSize % alignment)
905                         totalSize += alignment - (totalSize % alignment);
906                   }
907                   totalSize += size;
908                }
909                break;
910             }
911             case unionMember:
912             case structMember:
913             {
914                OldList * specs = MkList(), * list = MkList();
915
916                size = 0;
917                AddMembers(list, (Class)member, true, &size, topClass, null);
918                ListAdd(specs,
919                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
920                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
921                alignment = member.structAlignment;
922
923                if(alignment)
924                {
925                   if(totalSize % alignment)
926                      totalSize += alignment - (totalSize % alignment);
927                }
928                totalSize += size;
929                break;
930             }
931          }
932       }
933    }
934    if(retSize)
935    {
936       if(topMember && topMember.type == unionMember)
937          *retSize = Max(*retSize, totalSize);
938       else
939          *retSize += totalSize;
940    }
941    else if(totalSize < maxSize && _class.type != systemClass)
942    {
943       int autoPadding = 0;
944       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
945          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
946       if(totalSize + autoPadding < maxSize)
947       {
948          char sizeString[50];
949          sprintf(sizeString, "%d", maxSize - totalSize);
950          ListAdd(declarations,
951             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
952             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
953          if(addedPadding)
954             *addedPadding = true;
955       }
956    }
957    if(context)
958       FinishTemplatesContext(context);
959    return topMember ? topMember.memberID : _class.memberID;
960 }
961
962 static int DeclareMembers(Class _class, bool isMember)
963 {
964    DataMember topMember = isMember ? (DataMember) _class : null;
965    uint totalSize = 0;
966    DataMember member;
967    Context context = isMember ? null : SetupTemplatesContext(_class);
968
969    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
970       DeclareMembers(_class.base, false);
971
972    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
973    {
974       if(!member.isProperty)
975       {
976          switch(member.type)
977          {
978             case normalMember:
979             {
980                /*
981                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
982                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
983                   DeclareStruct(member.dataType._class.string, false);
984                   */
985                if(!member.dataType && member.dataTypeString)
986                   member.dataType = ProcessTypeString(member.dataTypeString, false);
987                if(member.dataType)
988                   DeclareType(member.dataType, false, false);
989                break;
990             }
991             case unionMember:
992             case structMember:
993             {
994                DeclareMembers((Class)member, true);
995                break;
996             }
997          }
998       }
999    }
1000    if(context)
1001       FinishTemplatesContext(context);
1002
1003    return topMember ? topMember.memberID : _class.memberID;
1004 }
1005
1006 void DeclareStruct(char * name, bool skipNoHead)
1007 {
1008    External external = null;
1009    Symbol classSym = FindClass(name);
1010
1011    if(!inCompiler || !classSym) return;
1012
1013    // We don't need any declaration for bit classes...
1014    if(classSym.registered &&
1015       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1016       return;
1017
1018    /*if(classSym.registered.templateClass)
1019       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1020    */
1021
1022    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1023    {
1024       // Add typedef struct
1025       Declaration decl;
1026       OldList * specifiers, * declarators;
1027       OldList * declarations = null;
1028       char structName[1024];
1029       external = (classSym.registered && classSym.registered.type == structClass) ?
1030          classSym.pointerExternal : classSym.structExternal;
1031
1032       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1033       // Moved this one up because DeclareClass done later will need it
1034
1035       classSym.declaring++;
1036
1037       if(strchr(classSym.string, '<'))
1038       {
1039          if(classSym.registered.templateClass)
1040          {
1041             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1042             classSym.declaring--;
1043          }
1044          return;
1045       }
1046
1047       //if(!skipNoHead)
1048          DeclareMembers(classSym.registered, false);
1049
1050       structName[0] = 0;
1051       FullClassNameCat(structName, name, false);
1052
1053       /*if(!external)
1054          external = MkExternalDeclaration(null);*/
1055
1056       if(!skipNoHead)
1057       {
1058          bool addedPadding = false;
1059          classSym.declaredStructSym = true;
1060
1061          declarations = MkList();
1062
1063          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1064
1065          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1066          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1067
1068          if(!declarations->count || (declarations->count == 1 && addedPadding))
1069          {
1070             FreeList(declarations, FreeClassDef);
1071             declarations = null;
1072          }
1073       }
1074       if(skipNoHead || declarations)
1075       {
1076          if(external && external.declaration)
1077          {
1078             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1079
1080             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1081             {
1082                // TODO: Fix this
1083                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1084
1085                // DANGER
1086                if(classSym.structExternal)
1087                   ast->Move(classSym.structExternal, curExternal.prev);
1088                ast->Move(classSym.pointerExternal, curExternal.prev);
1089
1090                classSym.id = curExternal.symbol.idCode;
1091                classSym.idCode = curExternal.symbol.idCode;
1092                // external = classSym.pointerExternal;
1093                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1094             }
1095          }
1096          else
1097          {
1098             if(!external)
1099                external = MkExternalDeclaration(null);
1100
1101             specifiers = MkList();
1102             declarators = MkList();
1103             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1104
1105             /*
1106             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1107             ListAdd(declarators, MkInitDeclarator(d, null));
1108             */
1109             external.declaration = decl = MkDeclaration(specifiers, declarators);
1110             if(decl.symbol && !decl.symbol.pointerExternal)
1111                decl.symbol.pointerExternal = external;
1112
1113             // For simple classes, keep the declaration as the external to move around
1114             if(classSym.registered && classSym.registered.type == structClass)
1115             {
1116                char className[1024];
1117                strcpy(className, "__ecereClass_");
1118                FullClassNameCat(className, classSym.string, true);
1119                MangleClassName(className);
1120
1121                // Testing This
1122                DeclareClass(classSym, className);
1123
1124                external.symbol = classSym;
1125                classSym.pointerExternal = external;
1126                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1127                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1128             }
1129             else
1130             {
1131                char className[1024];
1132                strcpy(className, "__ecereClass_");
1133                FullClassNameCat(className, classSym.string, true);
1134                MangleClassName(className);
1135
1136                // TOFIX: TESTING THIS...
1137                classSym.structExternal = external;
1138                DeclareClass(classSym, className);
1139                external.symbol = classSym;
1140             }
1141
1142             //if(curExternal)
1143                ast->Insert(curExternal ? curExternal.prev : null, external);
1144          }
1145       }
1146
1147       classSym.declaring--;
1148    }
1149    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1150    {
1151       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1152       // Moved this one up because DeclareClass done later will need it
1153
1154       // TESTING THIS:
1155       classSym.declaring++;
1156
1157       //if(!skipNoHead)
1158       {
1159          if(classSym.registered)
1160             DeclareMembers(classSym.registered, false);
1161       }
1162
1163       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1164       {
1165          // TODO: Fix this
1166          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1167
1168          // DANGER
1169          if(classSym.structExternal)
1170             ast->Move(classSym.structExternal, curExternal.prev);
1171          ast->Move(classSym.pointerExternal, curExternal.prev);
1172
1173          classSym.id = curExternal.symbol.idCode;
1174          classSym.idCode = curExternal.symbol.idCode;
1175          // external = classSym.pointerExternal;
1176          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1177       }
1178
1179       classSym.declaring--;
1180    }
1181    //return external;
1182 }
1183
1184 void DeclareProperty(Property prop, char * setName, char * getName)
1185 {
1186    Symbol symbol = prop.symbol;
1187    char propName[1024];
1188
1189    strcpy(setName, "__ecereProp_");
1190    FullClassNameCat(setName, prop._class.fullName, false);
1191    strcat(setName, "_Set_");
1192    // strcat(setName, prop.name);
1193    FullClassNameCat(setName, prop.name, true);
1194
1195    strcpy(getName, "__ecereProp_");
1196    FullClassNameCat(getName, prop._class.fullName, false);
1197    strcat(getName, "_Get_");
1198    FullClassNameCat(getName, prop.name, true);
1199    // strcat(getName, prop.name);
1200
1201    strcpy(propName, "__ecereProp_");
1202    FullClassNameCat(propName, prop._class.fullName, false);
1203    strcat(propName, "_");
1204    FullClassNameCat(propName, prop.name, true);
1205    // strcat(propName, prop.name);
1206
1207    // To support "char *" property
1208    MangleClassName(getName);
1209    MangleClassName(setName);
1210    MangleClassName(propName);
1211
1212    if(prop._class.type == structClass)
1213       DeclareStruct(prop._class.fullName, false);
1214
1215    if(!symbol || curExternal.symbol.idCode < symbol.id)
1216    {
1217       bool imported = false;
1218       bool dllImport = false;
1219       if(!symbol || symbol._import)
1220       {
1221          if(!symbol)
1222          {
1223             Symbol classSym;
1224             if(!prop._class.symbol)
1225                prop._class.symbol = FindClass(prop._class.fullName);
1226             classSym = prop._class.symbol;
1227             if(classSym && !classSym._import)
1228             {
1229                ModuleImport module;
1230
1231                if(prop._class.module)
1232                   module = FindModule(prop._class.module);
1233                else
1234                   module = mainModule;
1235
1236                classSym._import = ClassImport
1237                {
1238                   name = CopyString(prop._class.fullName);
1239                   isRemote = prop._class.isRemote;
1240                };
1241                module.classes.Add(classSym._import);
1242             }
1243             symbol = prop.symbol = Symbol { };
1244             symbol._import = (ClassImport)PropertyImport
1245             {
1246                name = CopyString(prop.name);
1247                isVirtual = false; //prop.isVirtual;
1248                hasSet = prop.Set ? true : false;
1249                hasGet = prop.Get ? true : false;
1250             };
1251             if(classSym)
1252                classSym._import.properties.Add(symbol._import);
1253          }
1254          imported = true;
1255          if(prop._class.module != privateModule && prop._class.module.importType != staticImport)
1256             dllImport = true;
1257       }
1258
1259       if(!symbol.type)
1260       {
1261          Context context = SetupTemplatesContext(prop._class);
1262          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1263          FinishTemplatesContext(context);
1264       }
1265
1266       // Get
1267       if(prop.Get)
1268       {
1269          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1270          {
1271             Declaration decl;
1272             OldList * specifiers, * declarators;
1273             Declarator d;
1274             OldList * params;
1275             Specifier spec;
1276             External external;
1277             Declarator typeDecl;
1278             bool simple = false;
1279
1280             specifiers = MkList();
1281             declarators = MkList();
1282             params = MkList();
1283
1284             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1285                MkDeclaratorIdentifier(MkIdentifier("this"))));
1286
1287             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1288             //if(imported)
1289             if(dllImport)
1290                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1291
1292             {
1293                Context context = SetupTemplatesContext(prop._class);
1294                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1295                FinishTemplatesContext(context);
1296             }
1297
1298             // Make sure the simple _class's type is declared
1299             for(spec = specifiers->first; spec; spec = spec.next)
1300             {
1301                if(spec.type == nameSpecifier /*SpecifierClass*/)
1302                {
1303                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1304                   {
1305                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1306                      symbol._class = classSym.registered;
1307                      if(classSym.registered && classSym.registered.type == structClass)
1308                      {
1309                         DeclareStruct(spec.name, false);
1310                         simple = true;
1311                      }
1312                   }
1313                }
1314             }
1315
1316             if(!simple)
1317                d = PlugDeclarator(typeDecl, d);
1318             else
1319             {
1320                ListAdd(params, MkTypeName(specifiers,
1321                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1322                specifiers = MkList();
1323             }
1324
1325             d = MkDeclaratorFunction(d, params);
1326
1327             //if(imported)
1328             if(dllImport)
1329                specifiers->Insert(null, MkSpecifier(EXTERN));
1330             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1331                specifiers->Insert(null, MkSpecifier(STATIC));
1332             if(simple)
1333                ListAdd(specifiers, MkSpecifier(VOID));
1334
1335             ListAdd(declarators, MkInitDeclarator(d, null));
1336
1337             decl = MkDeclaration(specifiers, declarators);
1338
1339             external = MkExternalDeclaration(decl);
1340             ast->Insert(curExternal.prev, external);
1341             external.symbol = symbol;
1342             symbol.externalGet = external;
1343
1344             ReplaceThisClassSpecifiers(specifiers, prop._class);
1345
1346             if(typeDecl)
1347                FreeDeclarator(typeDecl);
1348          }
1349          else
1350          {
1351             // Move declaration higher...
1352             ast->Move(symbol.externalGet, curExternal.prev);
1353          }
1354       }
1355
1356       // Set
1357       if(prop.Set)
1358       {
1359          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1360          {
1361             Declaration decl;
1362             OldList * specifiers, * declarators;
1363             Declarator d;
1364             OldList * params;
1365             Specifier spec;
1366             External external;
1367             Declarator typeDecl;
1368
1369             declarators = MkList();
1370             params = MkList();
1371
1372             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1373             if(!prop.conversion || prop._class.type == structClass)
1374             {
1375                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1376                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1377             }
1378
1379             specifiers = MkList();
1380
1381             {
1382                Context context = SetupTemplatesContext(prop._class);
1383                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1384                   MkDeclaratorIdentifier(MkIdentifier("value")));
1385                FinishTemplatesContext(context);
1386             }
1387             ListAdd(params, MkTypeName(specifiers, d));
1388
1389             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1390             //if(imported)
1391             if(dllImport)
1392                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1393             d = MkDeclaratorFunction(d, params);
1394
1395             // Make sure the simple _class's type is declared
1396             for(spec = specifiers->first; spec; spec = spec.next)
1397             {
1398                if(spec.type == nameSpecifier /*SpecifierClass*/)
1399                {
1400                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1401                   {
1402                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1403                      symbol._class = classSym.registered;
1404                      if(classSym.registered && classSym.registered.type == structClass)
1405                         DeclareStruct(spec.name, false);
1406                   }
1407                }
1408             }
1409
1410             ListAdd(declarators, MkInitDeclarator(d, null));
1411
1412             specifiers = MkList();
1413             //if(imported)
1414             if(dllImport)
1415                specifiers->Insert(null, MkSpecifier(EXTERN));
1416             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1417                specifiers->Insert(null, MkSpecifier(STATIC));
1418
1419             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1420             if(!prop.conversion || prop._class.type == structClass)
1421                ListAdd(specifiers, MkSpecifier(VOID));
1422             else
1423                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1424
1425             decl = MkDeclaration(specifiers, declarators);
1426
1427             external = MkExternalDeclaration(decl);
1428             ast->Insert(curExternal.prev, external);
1429             external.symbol = symbol;
1430             symbol.externalSet = external;
1431
1432             ReplaceThisClassSpecifiers(specifiers, prop._class);
1433          }
1434          else
1435          {
1436             // Move declaration higher...
1437             ast->Move(symbol.externalSet, curExternal.prev);
1438          }
1439       }
1440
1441       // Property (for Watchers)
1442       if(!symbol.externalPtr)
1443       {
1444          Declaration decl;
1445          External external;
1446          OldList * specifiers = MkList();
1447
1448          if(imported)
1449             specifiers->Insert(null, MkSpecifier(EXTERN));
1450          else
1451             specifiers->Insert(null, MkSpecifier(STATIC));
1452
1453          ListAdd(specifiers, MkSpecifierName("Property"));
1454
1455          {
1456             OldList * list = MkList();
1457             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1458                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1459
1460             if(!imported)
1461             {
1462                strcpy(propName, "__ecerePropM_");
1463                FullClassNameCat(propName, prop._class.fullName, false);
1464                strcat(propName, "_");
1465                // strcat(propName, prop.name);
1466                FullClassNameCat(propName, prop.name, true);
1467
1468                MangleClassName(propName);
1469
1470                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1471                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1472             }
1473             decl = MkDeclaration(specifiers, list);
1474          }
1475
1476          external = MkExternalDeclaration(decl);
1477          ast->Insert(curExternal.prev, external);
1478          external.symbol = symbol;
1479          symbol.externalPtr = external;
1480       }
1481       else
1482       {
1483          // Move declaration higher...
1484          ast->Move(symbol.externalPtr, curExternal.prev);
1485       }
1486
1487       symbol.id = curExternal.symbol.idCode;
1488    }
1489 }
1490
1491 // ***************** EXPRESSION PROCESSING ***************************
1492 public Type Dereference(Type source)
1493 {
1494    Type type = null;
1495    if(source)
1496    {
1497       if(source.kind == pointerType || source.kind == arrayType)
1498       {
1499          type = source.type;
1500          source.type.refCount++;
1501       }
1502       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1503       {
1504          type = Type
1505          {
1506             kind = charType;
1507             refCount = 1;
1508          };
1509       }
1510       // Support dereferencing of no head classes for now...
1511       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1512       {
1513          type = source;
1514          source.refCount++;
1515       }
1516       else
1517          Compiler_Error($"cannot dereference type\n");
1518    }
1519    return type;
1520 }
1521
1522 static Type Reference(Type source)
1523 {
1524    Type type = null;
1525    if(source)
1526    {
1527       type = Type
1528       {
1529          kind = pointerType;
1530          type = source;
1531          refCount = 1;
1532       };
1533       source.refCount++;
1534    }
1535    return type;
1536 }
1537
1538 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1539 {
1540    Identifier ident = member.identifiers ? member.identifiers->first : null;
1541    bool found = false;
1542    DataMember dataMember = null;
1543    Method method = null;
1544    bool freeType = false;
1545
1546    yylloc = member.loc;
1547
1548    if(!ident)
1549    {
1550       if(curMember)
1551       {
1552          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1553          if(*curMember)
1554          {
1555             found = true;
1556             dataMember = *curMember;
1557          }
1558       }
1559    }
1560    else
1561    {
1562       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1563       DataMember _subMemberStack[256];
1564       int _subMemberStackPos = 0;
1565
1566       // FILL MEMBER STACK
1567       if(!thisMember)
1568          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1569       if(thisMember)
1570       {
1571          dataMember = thisMember;
1572          if(curMember && thisMember.memberAccess == publicAccess)
1573          {
1574             *curMember = thisMember;
1575             *curClass = thisMember._class;
1576             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1577             *subMemberStackPos = _subMemberStackPos;
1578          }
1579          found = true;
1580       }
1581       else
1582       {
1583          // Setting a method
1584          method = eClass_FindMethod(_class, ident.string, privateModule);
1585          if(method && method.type == virtualMethod)
1586             found = true;
1587          else
1588             method = null;
1589       }
1590    }
1591
1592    if(found)
1593    {
1594       Type type = null;
1595       if(dataMember)
1596       {
1597          if(!dataMember.dataType && dataMember.dataTypeString)
1598          {
1599             //Context context = SetupTemplatesContext(dataMember._class);
1600             Context context = SetupTemplatesContext(_class);
1601             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1602             FinishTemplatesContext(context);
1603          }
1604          type = dataMember.dataType;
1605       }
1606       else if(method)
1607       {
1608          // This is for destination type...
1609          if(!method.dataType)
1610             ProcessMethodType(method);
1611          //DeclareMethod(method);
1612          // method.dataType = ((Symbol)method.symbol)->type;
1613          type = method.dataType;
1614       }
1615
1616       if(ident && ident.next)
1617       {
1618          for(ident = ident.next; ident && type; ident = ident.next)
1619          {
1620             if(type.kind == classType)
1621             {
1622                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1623                if(!dataMember)
1624                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1625                if(dataMember)
1626                   type = dataMember.dataType;
1627             }
1628             else if(type.kind == structType || type.kind == unionType)
1629             {
1630                Type memberType;
1631                for(memberType = type.members.first; memberType; memberType = memberType.next)
1632                {
1633                   if(!strcmp(memberType.name, ident.string))
1634                   {
1635                      type = memberType;
1636                      break;
1637                   }
1638                }
1639             }
1640          }
1641       }
1642
1643       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1644       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1645       {
1646          int id = 0;
1647          ClassTemplateParameter curParam = null;
1648          Class sClass;
1649          for(sClass = _class; sClass; sClass = sClass.base)
1650          {
1651             id = 0;
1652             if(sClass.templateClass) sClass = sClass.templateClass;
1653             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1654             {
1655                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1656                {
1657                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1658                   {
1659                      if(sClass.templateClass) sClass = sClass.templateClass;
1660                      id += sClass.templateParams.count;
1661                   }
1662                   break;
1663                }
1664                id++;
1665             }
1666             if(curParam) break;
1667          }
1668
1669          if(curParam)
1670          {
1671             ClassTemplateArgument arg = _class.templateArgs[id];
1672             if(arg.dataTypeString)
1673             {
1674                // FreeType(type);
1675                type = ProcessTypeString(arg.dataTypeString, false);
1676                freeType = true;
1677                if(type && _class.templateClass)
1678                   type.passAsTemplate = true;
1679                if(type)
1680                {
1681                   // type.refCount++;
1682                   /*if(!exp.destType)
1683                   {
1684                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1685                      exp.destType.refCount++;
1686                   }*/
1687                }
1688             }
1689          }
1690       }
1691       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1692       {
1693          Class expClass = type._class.registered;
1694          Class cClass = null;
1695          int c;
1696          int paramCount = 0;
1697          int lastParam = -1;
1698
1699          char templateString[1024];
1700          ClassTemplateParameter param;
1701          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1702          for(cClass = expClass; cClass; cClass = cClass.base)
1703          {
1704             int p = 0;
1705             if(cClass.templateClass) cClass = cClass.templateClass;
1706             for(param = cClass.templateParams.first; param; param = param.next)
1707             {
1708                int id = p;
1709                Class sClass;
1710                ClassTemplateArgument arg;
1711                for(sClass = cClass.base; sClass; sClass = sClass.base)
1712                {
1713                   if(sClass.templateClass) sClass = sClass.templateClass;
1714                   id += sClass.templateParams.count;
1715                }
1716                arg = expClass.templateArgs[id];
1717
1718                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1719                {
1720                   ClassTemplateParameter cParam;
1721                   //int p = numParams - sClass.templateParams.count;
1722                   int p = 0;
1723                   Class nextClass;
1724                   if(sClass.templateClass) sClass = sClass.templateClass;
1725
1726                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1727                   {
1728                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1729                      p += nextClass.templateParams.count;
1730                   }
1731
1732                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1733                   {
1734                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1735                      {
1736                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1737                         {
1738                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1739                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1740                            break;
1741                         }
1742                      }
1743                   }
1744                }
1745
1746                {
1747                   char argument[256];
1748                   argument[0] = '\0';
1749                   /*if(arg.name)
1750                   {
1751                      strcat(argument, arg.name.string);
1752                      strcat(argument, " = ");
1753                   }*/
1754                   switch(param.type)
1755                   {
1756                      case expression:
1757                      {
1758                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1759                         char expString[1024];
1760                         OldList * specs = MkList();
1761                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1762                         Expression exp;
1763                         char * string = PrintHexUInt64(arg.expression.ui64);
1764                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1765
1766                         ProcessExpressionType(exp);
1767                         ComputeExpression(exp);
1768                         expString[0] = '\0';
1769                         PrintExpression(exp, expString);
1770                         strcat(argument, expString);
1771                         //delete exp;
1772                         FreeExpression(exp);
1773                         break;
1774                      }
1775                      case identifier:
1776                      {
1777                         strcat(argument, arg.member.name);
1778                         break;
1779                      }
1780                      case TemplateParameterType::type:
1781                      {
1782                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1783                            strcat(argument, arg.dataTypeString);
1784                         break;
1785                      }
1786                   }
1787                   if(argument[0])
1788                   {
1789                      if(paramCount) strcat(templateString, ", ");
1790                      if(lastParam != p - 1)
1791                      {
1792                         strcat(templateString, param.name);
1793                         strcat(templateString, " = ");
1794                      }
1795                      strcat(templateString, argument);
1796                      paramCount++;
1797                      lastParam = p;
1798                   }
1799                   p++;
1800                }
1801             }
1802          }
1803          {
1804             int len = strlen(templateString);
1805             if(templateString[len-1] == '<')
1806                len--;
1807             else
1808             {
1809                if(templateString[len-1] == '>')
1810                   templateString[len++] = ' ';
1811                templateString[len++] = '>';
1812             }
1813             templateString[len++] = '\0';
1814          }
1815          {
1816             Context context = SetupTemplatesContext(_class);
1817             if(freeType) FreeType(type);
1818             type = ProcessTypeString(templateString, false);
1819             freeType = true;
1820             FinishTemplatesContext(context);
1821          }
1822       }
1823
1824       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1825       {
1826          ProcessExpressionType(member.initializer.exp);
1827          if(!member.initializer.exp.expType)
1828          {
1829             if(inCompiler)
1830             {
1831                char expString[10240];
1832                expString[0] = '\0';
1833                PrintExpression(member.initializer.exp, expString);
1834                ChangeCh(expString, '\n', ' ');
1835                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1836             }
1837          }
1838          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1839          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1840          {
1841             Compiler_Error($"incompatible instance method %s\n", ident.string);
1842          }
1843       }
1844       else if(member.initializer)
1845       {
1846          /*
1847          FreeType(member.exp.destType);
1848          member.exp.destType = type;
1849          if(member.exp.destType)
1850             member.exp.destType.refCount++;
1851          ProcessExpressionType(member.exp);
1852          */
1853
1854          ProcessInitializer(member.initializer, type);
1855       }
1856       if(freeType) FreeType(type);
1857    }
1858    else
1859    {
1860       if(_class && _class.type == unitClass)
1861       {
1862          if(member.initializer)
1863          {
1864             /*
1865             FreeType(member.exp.destType);
1866             member.exp.destType = MkClassType(_class.fullName);
1867             ProcessExpressionType(member.initializer, type);
1868             */
1869             Type type = MkClassType(_class.fullName);
1870             ProcessInitializer(member.initializer, type);
1871             FreeType(type);
1872          }
1873       }
1874       else
1875       {
1876          if(member.initializer)
1877          {
1878             //ProcessExpressionType(member.exp);
1879             ProcessInitializer(member.initializer, null);
1880          }
1881          if(ident)
1882          {
1883             if(method)
1884             {
1885                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1886             }
1887             else if(_class)
1888             {
1889                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1890                if(inCompiler)
1891                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1892             }
1893          }
1894          else if(_class)
1895             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1896       }
1897    }
1898 }
1899
1900 void ProcessInstantiationType(Instantiation inst)
1901 {
1902    yylloc = inst.loc;
1903    if(inst._class)
1904    {
1905       MembersInit members;
1906       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1907       Class _class;
1908
1909       /*if(!inst._class.symbol)
1910          inst._class.symbol = FindClass(inst._class.name);*/
1911       classSym = inst._class.symbol;
1912       _class = classSym ? classSym.registered : null;
1913
1914       // DANGER: Patch for mutex not declaring its struct when not needed
1915       if(!_class || _class.type != noHeadClass)
1916          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1917
1918       afterExternal = afterExternal ? afterExternal : curExternal;
1919
1920       if(inst.exp)
1921          ProcessExpressionType(inst.exp);
1922
1923       inst.isConstant = true;
1924       if(inst.members)
1925       {
1926          DataMember curMember = null;
1927          Class curClass = null;
1928          DataMember subMemberStack[256];
1929          int subMemberStackPos = 0;
1930
1931          for(members = inst.members->first; members; members = members.next)
1932          {
1933             switch(members.type)
1934             {
1935                case methodMembersInit:
1936                {
1937                   char name[1024];
1938                   static uint instMethodID = 0;
1939                   External external = curExternal;
1940                   Context context = curContext;
1941                   Declarator declarator = members.function.declarator;
1942                   Identifier nameID = GetDeclId(declarator);
1943                   char * unmangled = nameID ? nameID.string : null;
1944                   Expression exp;
1945                   External createdExternal = null;
1946
1947                   if(inCompiler)
1948                   {
1949                      char number[16];
1950                      //members.function.dontMangle = true;
1951                      strcpy(name, "__ecereInstMeth_");
1952                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
1953                      strcat(name, "_");
1954                      strcat(name, nameID.string);
1955                      strcat(name, "_");
1956                      sprintf(number, "_%08d", instMethodID++);
1957                      strcat(name, number);
1958                      nameID.string = CopyString(name);
1959                   }
1960
1961                   // Do modifications here...
1962                   if(declarator)
1963                   {
1964                      Symbol symbol = declarator.symbol;
1965                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
1966
1967                      if(method && method.type == virtualMethod)
1968                      {
1969                         symbol.method = method;
1970                         ProcessMethodType(method);
1971
1972                         if(!symbol.type.thisClass)
1973                         {
1974                            if(method.dataType.thisClass && currentClass &&
1975                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
1976                            {
1977                               if(!currentClass.symbol)
1978                                  currentClass.symbol = FindClass(currentClass.fullName);
1979                               symbol.type.thisClass = currentClass.symbol;
1980                            }
1981                            else
1982                            {
1983                               if(!_class.symbol)
1984                                  _class.symbol = FindClass(_class.fullName);
1985                               symbol.type.thisClass = _class.symbol;
1986                            }
1987                         }
1988                         // TESTING THIS HERE:
1989                         DeclareType(symbol.type, true, true);
1990
1991                      }
1992                      else if(classSym)
1993                      {
1994                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
1995                            unmangled, classSym.string);
1996                      }
1997                   }
1998
1999                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2000                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2001
2002                   if(nameID)
2003                   {
2004                      FreeSpecifier(nameID._class);
2005                      nameID._class = null;
2006                   }
2007
2008                   if(inCompiler)
2009                   {
2010
2011                      Type type = declarator.symbol.type;
2012                      External oldExternal = curExternal;
2013
2014                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2015                      // *** It was commented out for problems such as
2016                      /*
2017                            class VirtualDesktop : Window
2018                            {
2019                               clientSize = Size { };
2020                               Timer timer
2021                               {
2022                                  bool DelayExpired()
2023                                  {
2024                                     clientSize.w;
2025                                     return true;
2026                                  }
2027                               };
2028                            }
2029                      */
2030                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2031
2032                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2033
2034                      /*
2035                      if(strcmp(declarator.symbol.string, name))
2036                      {
2037                         printf("TOCHECK: Look out for this\n");
2038                         delete declarator.symbol.string;
2039                         declarator.symbol.string = CopyString(name);
2040                      }
2041
2042                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2043                      {
2044                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2045                         excludedSymbols->Remove(declarator.symbol);
2046                         globalContext.symbols.Add((BTNode)declarator.symbol);
2047                         if(strstr(declarator.symbol.string), "::")
2048                            globalContext.hasNameSpace = true;
2049
2050                      }
2051                      */
2052
2053                      //curExternal = curExternal.prev;
2054                      //afterExternal = afterExternal->next;
2055
2056                      //ProcessFunction(afterExternal->function);
2057
2058                      //curExternal = afterExternal;
2059                      {
2060                         External externalDecl;
2061                         externalDecl = MkExternalDeclaration(null);
2062                         ast->Insert(oldExternal.prev, externalDecl);
2063
2064                         // Which function does this process?
2065                         if(createdExternal.function)
2066                         {
2067                            ProcessFunction(createdExternal.function);
2068
2069                            //curExternal = oldExternal;
2070
2071                            {
2072                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2073
2074                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2075                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2076
2077                               //externalDecl = MkExternalDeclaration(decl);
2078
2079                               //***** ast->Insert(external.prev, externalDecl);
2080                               //ast->Insert(curExternal.prev, externalDecl);
2081                               externalDecl.declaration = decl;
2082                               if(decl.symbol && !decl.symbol.pointerExternal)
2083                                  decl.symbol.pointerExternal = externalDecl;
2084
2085                               // Trying this out...
2086                               declarator.symbol.pointerExternal = externalDecl;
2087                            }
2088                         }
2089                      }
2090                   }
2091                   else if(declarator)
2092                   {
2093                      curExternal = declarator.symbol.pointerExternal;
2094                      ProcessFunction((FunctionDefinition)members.function);
2095                   }
2096                   curExternal = external;
2097                   curContext = context;
2098
2099                   if(inCompiler)
2100                   {
2101                      FreeClassFunction(members.function);
2102
2103                      // In this pass, turn this into a MemberInitData
2104                      exp = QMkExpId(name);
2105                      members.type = dataMembersInit;
2106                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2107
2108                      delete unmangled;
2109                   }
2110                   break;
2111                }
2112                case dataMembersInit:
2113                {
2114                   if(members.dataMembers && classSym)
2115                   {
2116                      MemberInit member;
2117                      Location oldyyloc = yylloc;
2118                      for(member = members.dataMembers->first; member; member = member.next)
2119                      {
2120                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2121                         if(member.initializer && !member.initializer.isConstant)
2122                            inst.isConstant = false;
2123                      }
2124                      yylloc = oldyyloc;
2125                   }
2126                   break;
2127                }
2128             }
2129          }
2130       }
2131    }
2132 }
2133
2134 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2135 {
2136    // OPTIMIZATIONS: TESTING THIS...
2137    if(inCompiler)
2138    {
2139       if(type.kind == functionType)
2140       {
2141          Type param;
2142          if(declareParams)
2143          {
2144             for(param = type.params.first; param; param = param.next)
2145                DeclareType(param, declarePointers, true);
2146          }
2147          DeclareType(type.returnType, declarePointers, true);
2148       }
2149       else if(type.kind == pointerType && declarePointers)
2150          DeclareType(type.type, declarePointers, false);
2151       else if(type.kind == classType)
2152       {
2153          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2154             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2155       }
2156       else if(type.kind == structType || type.kind == unionType)
2157       {
2158          Type member;
2159          for(member = type.members.first; member; member = member.next)
2160             DeclareType(member, false, false);
2161       }
2162       else if(type.kind == arrayType)
2163          DeclareType(type.arrayType, declarePointers, false);
2164    }
2165 }
2166
2167 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2168 {
2169    ClassTemplateArgument * arg = null;
2170    int id = 0;
2171    ClassTemplateParameter curParam = null;
2172    Class sClass;
2173    for(sClass = _class; sClass; sClass = sClass.base)
2174    {
2175       id = 0;
2176       if(sClass.templateClass) sClass = sClass.templateClass;
2177       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2178       {
2179          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2180          {
2181             for(sClass = sClass.base; sClass; sClass = sClass.base)
2182             {
2183                if(sClass.templateClass) sClass = sClass.templateClass;
2184                id += sClass.templateParams.count;
2185             }
2186             break;
2187          }
2188          id++;
2189       }
2190       if(curParam) break;
2191    }
2192    if(curParam)
2193    {
2194       arg = &_class.templateArgs[id];
2195       if(arg && param.type == type)
2196          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2197    }
2198    return arg;
2199 }
2200
2201 public Context SetupTemplatesContext(Class _class)
2202 {
2203    Context context = PushContext();
2204    context.templateTypesOnly = true;
2205    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2206    {
2207       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2208       for(; param; param = param.next)
2209       {
2210          if(param.type == type && param.identifier)
2211          {
2212             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2213             curContext.templateTypes.Add((BTNode)type);
2214          }
2215       }
2216    }
2217    else if(_class)
2218    {
2219       Class sClass;
2220       for(sClass = _class; sClass; sClass = sClass.base)
2221       {
2222          ClassTemplateParameter p;
2223          for(p = sClass.templateParams.first; p; p = p.next)
2224          {
2225             //OldList * specs = MkList();
2226             //Declarator decl = null;
2227             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2228             if(p.type == type)
2229             {
2230                TemplateParameter param = p.param;
2231                TemplatedType type;
2232                if(!param)
2233                {
2234                   // ADD DATA TYPE HERE...
2235                   p.param = param = TemplateParameter
2236                   {
2237                      identifier = MkIdentifier(p.name), type = p.type,
2238                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2239                   };
2240                }
2241                type = TemplatedType { key = (uintptr)p.name, param = param };
2242                curContext.templateTypes.Add((BTNode)type);
2243             }
2244          }
2245       }
2246    }
2247    return context;
2248 }
2249
2250 public void FinishTemplatesContext(Context context)
2251 {
2252    PopContext(context);
2253    FreeContext(context);
2254    delete context;
2255 }
2256
2257 public void ProcessMethodType(Method method)
2258 {
2259    if(!method.dataType)
2260    {
2261       Context context = SetupTemplatesContext(method._class);
2262
2263       method.dataType = ProcessTypeString(method.dataTypeString, false);
2264
2265       FinishTemplatesContext(context);
2266
2267       if(method.type != virtualMethod && method.dataType)
2268       {
2269          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2270          {
2271             if(!method._class.symbol)
2272                method._class.symbol = FindClass(method._class.fullName);
2273             method.dataType.thisClass = method._class.symbol;
2274          }
2275       }
2276
2277       // Why was this commented out? Working fine without now...
2278
2279       /*
2280       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2281          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2282          */
2283    }
2284
2285    /*
2286    if(type)
2287    {
2288       char * par = strstr(type, "(");
2289       char * classOp = null;
2290       int classOpLen = 0;
2291       if(par)
2292       {
2293          int c;
2294          for(c = par-type-1; c >= 0; c++)
2295          {
2296             if(type[c] == ':' && type[c+1] == ':')
2297             {
2298                classOp = type + c - 1;
2299                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2300                {
2301                   classOp--;
2302                   classOpLen++;
2303                }
2304                break;
2305             }
2306             else if(!isspace(type[c]))
2307                break;
2308          }
2309       }
2310       if(classOp)
2311       {
2312          char temp[1024];
2313          int typeLen = strlen(type);
2314          memcpy(temp, classOp, classOpLen);
2315          temp[classOpLen] = '\0';
2316          if(temp[0])
2317             _class = eSystem_FindClass(module, temp);
2318          else
2319             _class = null;
2320          method.dataTypeString = new char[typeLen - classOpLen + 1];
2321          memcpy(method.dataTypeString, type, classOp - type);
2322          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2323       }
2324       else
2325          method.dataTypeString = type;
2326    }
2327    */
2328 }
2329
2330
2331 public void ProcessPropertyType(Property prop)
2332 {
2333    if(!prop.dataType)
2334    {
2335       Context context = SetupTemplatesContext(prop._class);
2336       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2337       FinishTemplatesContext(context);
2338    }
2339 }
2340
2341 public void DeclareMethod(Method method, char * name)
2342 {
2343    Symbol symbol = method.symbol;
2344    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2345    {
2346       bool imported = false;
2347       bool dllImport = false;
2348
2349       if(!method.dataType)
2350          method.dataType = ProcessTypeString(method.dataTypeString, false);
2351
2352       if(!symbol || symbol._import || method.type == virtualMethod)
2353       {
2354          if(!symbol || method.type == virtualMethod)
2355          {
2356             Symbol classSym;
2357             if(!method._class.symbol)
2358                method._class.symbol = FindClass(method._class.fullName);
2359             classSym = method._class.symbol;
2360             if(!classSym._import)
2361             {
2362                ModuleImport module;
2363
2364                if(method._class.module && method._class.module.name)
2365                   module = FindModule(method._class.module);
2366                else
2367                   module = mainModule;
2368                classSym._import = ClassImport
2369                {
2370                   name = CopyString(method._class.fullName);
2371                   isRemote = method._class.isRemote;
2372                };
2373                module.classes.Add(classSym._import);
2374             }
2375             if(!symbol)
2376             {
2377                symbol = method.symbol = Symbol { };
2378             }
2379             if(!symbol._import)
2380             {
2381                symbol._import = (ClassImport)MethodImport
2382                {
2383                   name = CopyString(method.name);
2384                   isVirtual = method.type == virtualMethod;
2385                };
2386                classSym._import.methods.Add(symbol._import);
2387             }
2388             if(!symbol)
2389             {
2390                // Set the symbol type
2391                /*
2392                if(!type.thisClass)
2393                {
2394                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2395                }
2396                else if(type.thisClass == (void *)-1)
2397                {
2398                   type.thisClass = null;
2399                }
2400                */
2401                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2402                symbol.type = method.dataType;
2403                if(symbol.type) symbol.type.refCount++;
2404             }
2405             /*
2406             if(!method.thisClass || strcmp(method.thisClass, "void"))
2407                symbol.type.params.Insert(null,
2408                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2409             */
2410          }
2411          if(!method.dataType.dllExport)
2412          {
2413             imported = true;
2414             if(method._class.module != privateModule && method._class.module.importType != staticImport)
2415                dllImport = true;
2416          }
2417       }
2418
2419       /* MOVING THIS UP
2420       if(!method.dataType)
2421          method.dataType = ((Symbol)method.symbol).type;
2422          //ProcessMethodType(method);
2423       */
2424
2425       if(method.type != virtualMethod && method.dataType)
2426          DeclareType(method.dataType, true, true);
2427
2428       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2429       {
2430          // We need a declaration here :)
2431          Declaration decl;
2432          OldList * specifiers, * declarators;
2433          Declarator d;
2434          Declarator funcDecl;
2435          External external;
2436
2437          specifiers = MkList();
2438          declarators = MkList();
2439
2440          //if(imported)
2441          if(dllImport)
2442             ListAdd(specifiers, MkSpecifier(EXTERN));
2443          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2444             ListAdd(specifiers, MkSpecifier(STATIC));
2445
2446          if(method.type == virtualMethod)
2447          {
2448             ListAdd(specifiers, MkSpecifier(INT));
2449             d = MkDeclaratorIdentifier(MkIdentifier(name));
2450          }
2451          else
2452          {
2453             d = MkDeclaratorIdentifier(MkIdentifier(name));
2454             //if(imported)
2455             if(dllImport)
2456                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2457             {
2458                Context context = SetupTemplatesContext(method._class);
2459                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2460                FinishTemplatesContext(context);
2461             }
2462             funcDecl = GetFuncDecl(d);
2463
2464             if(dllImport)
2465             {
2466                Specifier spec, next;
2467                for(spec = specifiers->first; spec; spec = next)
2468                {
2469                   next = spec.next;
2470                   if(spec.type == extendedSpecifier)
2471                   {
2472                      specifiers->Remove(spec);
2473                      FreeSpecifier(spec);
2474                   }
2475                }
2476             }
2477
2478             // Add this parameter if not a static method
2479             if(method.dataType && !method.dataType.staticMethod)
2480             {
2481                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2482                {
2483                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2484                   TypeName thisParam = MkTypeName(MkListOne(
2485                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2486                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2487                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2488                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2489
2490                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2491                   {
2492                      TypeName param = funcDecl.function.parameters->first;
2493                      funcDecl.function.parameters->Remove(param);
2494                      FreeTypeName(param);
2495                   }
2496
2497                   if(!funcDecl.function.parameters)
2498                      funcDecl.function.parameters = MkList();
2499                   funcDecl.function.parameters->Insert(null, thisParam);
2500                }
2501             }
2502             // Make sure we don't have empty parameter declarations for static methods...
2503             /*
2504             else if(!funcDecl.function.parameters)
2505             {
2506                funcDecl.function.parameters = MkList();
2507                funcDecl.function.parameters->Insert(null,
2508                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2509             }*/
2510          }
2511          // TESTING THIS:
2512          ProcessDeclarator(d);
2513
2514          ListAdd(declarators, MkInitDeclarator(d, null));
2515
2516          decl = MkDeclaration(specifiers, declarators);
2517
2518          ReplaceThisClassSpecifiers(specifiers, method._class);
2519
2520          // Keep a different symbol for the function definition than the declaration...
2521          if(symbol.pointerExternal)
2522          {
2523             Symbol functionSymbol { };
2524
2525             // Copy symbol
2526             {
2527                *functionSymbol = *symbol;
2528                functionSymbol.string = CopyString(symbol.string);
2529                if(functionSymbol.type)
2530                   functionSymbol.type.refCount++;
2531             }
2532
2533             excludedSymbols->Add(functionSymbol);
2534             symbol.pointerExternal.symbol = functionSymbol;
2535          }
2536          external = MkExternalDeclaration(decl);
2537          if(curExternal)
2538             ast->Insert(curExternal ? curExternal.prev : null, external);
2539          external.symbol = symbol;
2540          symbol.pointerExternal = external;
2541       }
2542       else if(ast)
2543       {
2544          // Move declaration higher...
2545          ast->Move(symbol.pointerExternal, curExternal.prev);
2546       }
2547
2548       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2549    }
2550 }
2551
2552 char * ReplaceThisClass(Class _class)
2553 {
2554    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2555    {
2556       bool first = true;
2557       int p = 0;
2558       ClassTemplateParameter param;
2559       int lastParam = -1;
2560
2561       char className[1024];
2562       strcpy(className, _class.fullName);
2563       for(param = _class.templateParams.first; param; param = param.next)
2564       {
2565          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2566          {
2567             if(first) strcat(className, "<");
2568             if(!first) strcat(className, ", ");
2569             if(lastParam + 1 != p)
2570             {
2571                strcat(className, param.name);
2572                strcat(className, " = ");
2573             }
2574             strcat(className, param.name);
2575             first = false;
2576             lastParam = p;
2577          }
2578          p++;
2579       }
2580       if(!first)
2581       {
2582          int len = strlen(className);
2583          if(className[len-1] == '>') className[len++] = ' ';
2584          className[len++] = '>';
2585          className[len++] = '\0';
2586       }
2587       return CopyString(className);
2588    }
2589    else
2590       return CopyString(_class.fullName);
2591 }
2592
2593 Type ReplaceThisClassType(Class _class)
2594 {
2595    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2596    {
2597       bool first = true;
2598       int p = 0;
2599       ClassTemplateParameter param;
2600       int lastParam = -1;
2601       char className[1024];
2602       strcpy(className, _class.fullName);
2603
2604       for(param = _class.templateParams.first; param; param = param.next)
2605       {
2606          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2607          {
2608             if(first) strcat(className, "<");
2609             if(!first) strcat(className, ", ");
2610             if(lastParam + 1 != p)
2611             {
2612                strcat(className, param.name);
2613                strcat(className, " = ");
2614             }
2615             strcat(className, param.name);
2616             first = false;
2617             lastParam = p;
2618          }
2619          p++;
2620       }
2621       if(!first)
2622       {
2623          int len = strlen(className);
2624          if(className[len-1] == '>') className[len++] = ' ';
2625          className[len++] = '>';
2626          className[len++] = '\0';
2627       }
2628       return MkClassType(className);
2629       //return ProcessTypeString(className, false);
2630    }
2631    else
2632    {
2633       return MkClassType(_class.fullName);
2634       //return ProcessTypeString(_class.fullName, false);
2635    }
2636 }
2637
2638 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2639 {
2640    if(specs != null && _class)
2641    {
2642       Specifier spec;
2643       for(spec = specs.first; spec; spec = spec.next)
2644       {
2645          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2646          {
2647             spec.type = nameSpecifier;
2648             spec.name = ReplaceThisClass(_class);
2649             spec.symbol = FindClass(spec.name); //_class.symbol;
2650          }
2651       }
2652    }
2653 }
2654
2655 // Returns imported or not
2656 bool DeclareFunction(GlobalFunction function, char * name)
2657 {
2658    Symbol symbol = function.symbol;
2659    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2660    {
2661       bool imported = false;
2662       bool dllImport = false;
2663
2664       if(!function.dataType)
2665       {
2666          function.dataType = ProcessTypeString(function.dataTypeString, false);
2667          if(!function.dataType.thisClass)
2668             function.dataType.staticMethod = true;
2669       }
2670
2671       if(inCompiler)
2672       {
2673          if(!symbol)
2674          {
2675             ModuleImport module = FindModule(function.module);
2676             // WARNING: This is not added anywhere...
2677             symbol = function.symbol = Symbol {  };
2678
2679             if(module.name)
2680             {
2681                if(!function.dataType.dllExport)
2682                {
2683                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2684                   module.functions.Add(symbol._import);
2685                }
2686             }
2687             // Set the symbol type
2688             {
2689                symbol.type = ProcessTypeString(function.dataTypeString, false);
2690                if(!symbol.type.thisClass)
2691                   symbol.type.staticMethod = true;
2692             }
2693          }
2694          imported = symbol._import ? true : false;
2695          if(imported && function.module != privateModule && function.module.importType != staticImport)
2696             dllImport = true;
2697       }
2698
2699       DeclareType(function.dataType, true, true);
2700
2701       if(inCompiler)
2702       {
2703          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2704          {
2705             // We need a declaration here :)
2706             Declaration decl;
2707             OldList * specifiers, * declarators;
2708             Declarator d;
2709             Declarator funcDecl;
2710             External external;
2711
2712             specifiers = MkList();
2713             declarators = MkList();
2714
2715             //if(imported)
2716                ListAdd(specifiers, MkSpecifier(EXTERN));
2717             /*
2718             else
2719                ListAdd(specifiers, MkSpecifier(STATIC));
2720             */
2721
2722             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2723             //if(imported)
2724             if(dllImport)
2725                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2726
2727             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2728             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2729             if(function.module.importType == staticImport)
2730             {
2731                Specifier spec;
2732                for(spec = specifiers->first; spec; spec = spec.next)
2733                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2734                   {
2735                      specifiers->Remove(spec);
2736                      FreeSpecifier(spec);
2737                      break;
2738                   }
2739             }
2740
2741             funcDecl = GetFuncDecl(d);
2742
2743             // Make sure we don't have empty parameter declarations for static methods...
2744             if(funcDecl && !funcDecl.function.parameters)
2745             {
2746                funcDecl.function.parameters = MkList();
2747                funcDecl.function.parameters->Insert(null,
2748                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2749             }
2750
2751             ListAdd(declarators, MkInitDeclarator(d, null));
2752
2753             {
2754                Context oldCtx = curContext;
2755                curContext = globalContext;
2756                decl = MkDeclaration(specifiers, declarators);
2757                curContext = oldCtx;
2758             }
2759
2760             // Keep a different symbol for the function definition than the declaration...
2761             if(symbol.pointerExternal)
2762             {
2763                Symbol functionSymbol { };
2764                // Copy symbol
2765                {
2766                   *functionSymbol = *symbol;
2767                   functionSymbol.string = CopyString(symbol.string);
2768                   if(functionSymbol.type)
2769                      functionSymbol.type.refCount++;
2770                }
2771
2772                excludedSymbols->Add(functionSymbol);
2773
2774                symbol.pointerExternal.symbol = functionSymbol;
2775             }
2776             external = MkExternalDeclaration(decl);
2777             if(curExternal)
2778                ast->Insert(curExternal.prev, external);
2779             external.symbol = symbol;
2780             symbol.pointerExternal = external;
2781          }
2782          else
2783          {
2784             // Move declaration higher...
2785             ast->Move(symbol.pointerExternal, curExternal.prev);
2786          }
2787
2788          if(curExternal)
2789             symbol.id = curExternal.symbol.idCode;
2790       }
2791    }
2792    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2793 }
2794
2795 void DeclareGlobalData(GlobalData data)
2796 {
2797    Symbol symbol = data.symbol;
2798    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2799    {
2800       if(inCompiler)
2801       {
2802          if(!symbol)
2803             symbol = data.symbol = Symbol { };
2804       }
2805       if(!data.dataType)
2806          data.dataType = ProcessTypeString(data.dataTypeString, false);
2807       DeclareType(data.dataType, true, true);
2808       if(inCompiler)
2809       {
2810          if(!symbol.pointerExternal)
2811          {
2812             // We need a declaration here :)
2813             Declaration decl;
2814             OldList * specifiers, * declarators;
2815             Declarator d;
2816             External external;
2817
2818             specifiers = MkList();
2819             declarators = MkList();
2820
2821             ListAdd(specifiers, MkSpecifier(EXTERN));
2822             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2823             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2824
2825             ListAdd(declarators, MkInitDeclarator(d, null));
2826
2827             decl = MkDeclaration(specifiers, declarators);
2828             external = MkExternalDeclaration(decl);
2829             if(curExternal)
2830                ast->Insert(curExternal.prev, external);
2831             external.symbol = symbol;
2832             symbol.pointerExternal = external;
2833          }
2834          else
2835          {
2836             // Move declaration higher...
2837             ast->Move(symbol.pointerExternal, curExternal.prev);
2838          }
2839
2840          if(curExternal)
2841             symbol.id = curExternal.symbol.idCode;
2842       }
2843    }
2844 }
2845
2846 class Conversion : struct
2847 {
2848    Conversion prev, next;
2849    Property convert;
2850    bool isGet;
2851    Type resultType;
2852 };
2853
2854 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2855 {
2856    if(source && dest)
2857    {
2858       // Property convert;
2859
2860       if(source.kind == templateType && dest.kind != templateType)
2861       {
2862          Type type = ProcessTemplateParameterType(source.templateParameter);
2863          if(type) source = type;
2864       }
2865
2866       if(dest.kind == templateType && source.kind != templateType)
2867       {
2868          Type type = ProcessTemplateParameterType(dest.templateParameter);
2869          if(type) dest = type;
2870       }
2871
2872       if(dest.classObjectType == typedObject)
2873       {
2874          if(source.classObjectType != anyObject)
2875             return true;
2876          else
2877          {
2878             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2879             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2880             {
2881                return true;
2882             }
2883          }
2884       }
2885       else
2886       {
2887          if(source.classObjectType == anyObject)
2888             return true;
2889          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2890             return true;
2891       }
2892
2893       if((dest.kind == structType && source.kind == structType) ||
2894          (dest.kind == unionType && source.kind == unionType))
2895       {
2896          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2897              (source.members.first && source.members.first == dest.members.first))
2898             return true;
2899       }
2900
2901       if(dest.kind == ellipsisType && source.kind != voidType)
2902          return true;
2903
2904       if(dest.kind == pointerType && dest.type.kind == voidType &&
2905          ((source.kind == classType && (!source._class || !source._class.registered || source._class.registered.type == structClass || source._class.registered.type == normalClass || source._class.registered.type == noHeadClass || source._class.registered.type == systemClass))
2906          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2907
2908          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2909
2910          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2911          return true;
2912       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2913          ((dest.kind == classType && (!dest._class || !dest._class.registered || dest._class.registered.type == structClass || dest._class.registered.type == normalClass || dest._class.registered.type == noHeadClass || dest._class.registered.type == systemClass))
2914          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2915
2916          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2917
2918          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2919          return true;
2920
2921       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2922       {
2923          if(source._class.registered && source._class.registered.type == unitClass)
2924          {
2925             if(conversions != null)
2926             {
2927                if(source._class.registered == dest._class.registered)
2928                   return true;
2929             }
2930             else
2931             {
2932                Class sourceBase, destBase;
2933                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2934                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2935                if(sourceBase == destBase)
2936                   return true;
2937             }
2938          }
2939          // Don't match enum inheriting from other enum if resolving enumeration values
2940          // TESTING: !dest.classObjectType
2941          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2942             (enumBaseType ||
2943                (!source._class.registered || source._class.registered.type != enumClass) ||
2944                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2945             return true;
2946          else
2947          {
2948             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2949             if(enumBaseType &&
2950                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2951                ((source._class && source._class.registered && source._class.registered.type != enumClass) || source.kind == classType)) // Added this here for a base enum to be acceptable for a derived enum (#139)
2952             {
2953                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2954                {
2955                   return true;
2956                }
2957             }
2958          }
2959       }
2960
2961       // JUST ADDED THIS...
2962       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2963          return true;
2964
2965       if(doConversion)
2966       {
2967          // Just added this for Straight conversion of ColorAlpha => Color
2968          if(source.kind == classType)
2969          {
2970             Class _class;
2971             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
2972             {
2973                Property convert;
2974                for(convert = _class.conversions.first; convert; convert = convert.next)
2975                {
2976                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
2977                   {
2978                      Conversion after = (conversions != null) ? conversions.last : null;
2979
2980                      if(!convert.dataType)
2981                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
2982                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
2983                      {
2984                         if(!conversions && !convert.Get)
2985                            return true;
2986                         else if(conversions != null)
2987                         {
2988                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
2989                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
2990                               (dest.kind != classType || dest._class.registered != _class.base))
2991                               return true;
2992                            else
2993                            {
2994                               Conversion conv { convert = convert, isGet = true };
2995                               // conversions.Add(conv);
2996                               conversions.Insert(after, conv);
2997                               return true;
2998                            }
2999                         }
3000                      }
3001                   }
3002                }
3003             }
3004          }
3005
3006          // MOVING THIS??
3007
3008          if(dest.kind == classType)
3009          {
3010             Class _class;
3011             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3012             {
3013                Property convert;
3014                for(convert = _class.conversions.first; convert; convert = convert.next)
3015                {
3016                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3017                   {
3018                      // Conversion after = (conversions != null) ? conversions.last : null;
3019
3020                      if(!convert.dataType)
3021                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3022                      // Just added this equality check to prevent recursion.... Make it safer?
3023                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3024                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3025                      {
3026                         if(!conversions && !convert.Set)
3027                            return true;
3028                         else if(conversions != null)
3029                         {
3030                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3031                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3032                               (source.kind != classType || source._class.registered != _class.base))
3033                               return true;
3034                            else
3035                            {
3036                               // *** Testing this! ***
3037                               Conversion conv { convert = convert };
3038                               conversions.Add(conv);
3039                               //conversions.Insert(after, conv);
3040                               return true;
3041                            }
3042                         }
3043                      }
3044                   }
3045                }
3046             }
3047             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3048             {
3049                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3050                   (source.kind != classType || source._class.registered.type != structClass))
3051                   return true;
3052             }*/
3053
3054             // TESTING THIS... IS THIS OK??
3055             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3056             {
3057                if(!dest._class.registered.dataType)
3058                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3059                // Only support this for classes...
3060                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3061                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3062                {
3063                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3064                   {
3065                      return true;
3066                   }
3067                }
3068             }
3069          }
3070
3071          // Moved this lower
3072          if(source.kind == classType)
3073          {
3074             Class _class;
3075             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3076             {
3077                Property convert;
3078                for(convert = _class.conversions.first; convert; convert = convert.next)
3079                {
3080                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3081                   {
3082                      Conversion after = (conversions != null) ? conversions.last : null;
3083
3084                      if(!convert.dataType)
3085                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3086                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3087                      {
3088                         if(!conversions && !convert.Get)
3089                            return true;
3090                         else if(conversions != null)
3091                         {
3092                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3093                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3094                               (dest.kind != classType || dest._class.registered != _class.base))
3095                               return true;
3096                            else
3097                            {
3098                               Conversion conv { convert = convert, isGet = true };
3099
3100                               // conversions.Add(conv);
3101                               conversions.Insert(after, conv);
3102                               return true;
3103                            }
3104                         }
3105                      }
3106                   }
3107                }
3108             }
3109
3110             // TESTING THIS... IS THIS OK??
3111             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3112             {
3113                if(!source._class.registered.dataType)
3114                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3115                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3116                {
3117                   return true;
3118                }
3119             }
3120          }
3121       }
3122
3123       if(source.kind == classType || source.kind == subClassType)
3124          ;
3125       else if(dest.kind == source.kind &&
3126          (dest.kind != structType && dest.kind != unionType &&
3127           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3128           return true;
3129       // RECENTLY ADDED THESE
3130       else if(dest.kind == doubleType && source.kind == floatType)
3131          return true;
3132       else if(dest.kind == shortType && source.kind == charType)
3133          return true;
3134       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == intSizeType /* Exception here for size_t */))
3135          return true;
3136       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3137          return true;
3138       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3139          return true;
3140       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3141          return true;
3142       else if(source.kind == enumType &&
3143          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3144           return true;
3145       else if(dest.kind == enumType &&
3146          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3147           return true;
3148       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3149               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3150       {
3151          Type paramSource, paramDest;
3152
3153          if(dest.kind == methodType)
3154             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3155          if(source.kind == methodType)
3156             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3157
3158          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3159          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3160          if(dest.kind == methodType)
3161             dest = dest.method.dataType;
3162          if(source.kind == methodType)
3163             source = source.method.dataType;
3164
3165          paramSource = source.params.first;
3166          if(paramSource && paramSource.kind == voidType) paramSource = null;
3167          paramDest = dest.params.first;
3168          if(paramDest && paramDest.kind == voidType) paramDest = null;
3169
3170
3171          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3172             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3173          {
3174             // Source thisClass must be derived from destination thisClass
3175             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3176                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3177             {
3178                if(paramDest && paramDest.kind == classType)
3179                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3180                else
3181                   Compiler_Error($"method class should not take an object\n");
3182                return false;
3183             }
3184             paramDest = paramDest.next;
3185          }
3186          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3187          {
3188             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3189             {
3190                if(dest.thisClass)
3191                {
3192                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3193                   {
3194                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3195                      return false;
3196                   }
3197                }
3198                else
3199                {
3200                   // THIS WAS BACKWARDS:
3201                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3202                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3203                   {
3204                      if(owningClassDest)
3205                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3206                      else
3207                         Compiler_Error($"overriding class expected to be derived from method class\n");
3208                      return false;
3209                   }
3210                }
3211                paramSource = paramSource.next;
3212             }
3213             else
3214             {
3215                if(dest.thisClass)
3216                {
3217                   // Source thisClass must be derived from destination thisClass
3218                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3219                   {
3220                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3221                      return false;
3222                   }
3223                }
3224                else
3225                {
3226                   // THIS WAS BACKWARDS TOO??
3227                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3228                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3229                   {
3230                      //if(owningClass)
3231                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3232                      //else
3233                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3234                      return false;
3235                   }
3236                }
3237             }
3238          }
3239
3240
3241          // Source return type must be derived from destination return type
3242          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3243          {
3244             Compiler_Warning($"incompatible return type for function\n");
3245             return false;
3246          }
3247
3248          // Check parameters
3249
3250          for(; paramDest; paramDest = paramDest.next)
3251          {
3252             if(!paramSource)
3253             {
3254                //Compiler_Warning($"not enough parameters\n");
3255                Compiler_Error($"not enough parameters\n");
3256                return false;
3257             }
3258             {
3259                Type paramDestType = paramDest;
3260                Type paramSourceType = paramSource;
3261                Type type = paramDestType;
3262
3263                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3264                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3265                   paramSource.kind != templateType)
3266                {
3267                   int id = 0;
3268                   ClassTemplateParameter curParam = null;
3269                   Class sClass;
3270                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3271                   {
3272                      id = 0;
3273                      if(sClass.templateClass) sClass = sClass.templateClass;
3274                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3275                      {
3276                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3277                         {
3278                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3279                            {
3280                               if(sClass.templateClass) sClass = sClass.templateClass;
3281                               id += sClass.templateParams.count;
3282                            }
3283                            break;
3284                         }
3285                         id++;
3286                      }
3287                      if(curParam) break;
3288                   }
3289
3290                   if(curParam)
3291                   {
3292                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3293                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3294                   }
3295                }
3296
3297                // paramDest must be derived from paramSource
3298                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3299                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3300                {
3301                   char type[1024];
3302                   type[0] = 0;
3303                   PrintType(paramDest, type, false, true);
3304                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3305
3306                   if(paramDestType != paramDest)
3307                      FreeType(paramDestType);
3308                   return false;
3309                }
3310                if(paramDestType != paramDest)
3311                   FreeType(paramDestType);
3312             }
3313
3314             paramSource = paramSource.next;
3315          }
3316          if(paramSource)
3317          {
3318             Compiler_Error($"too many parameters\n");
3319             return false;
3320          }
3321          return true;
3322       }
3323       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3324       {
3325          return true;
3326       }
3327       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3328          (source.kind == arrayType || source.kind == pointerType))
3329       {
3330          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3331             return true;
3332       }
3333    }
3334    return false;
3335 }
3336
3337 static void FreeConvert(Conversion convert)
3338 {
3339    if(convert.resultType)
3340       FreeType(convert.resultType);
3341 }
3342
3343 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3344                               char * string, OldList conversions)
3345 {
3346    BTNamedLink link;
3347
3348    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3349    {
3350       Class _class = link.data;
3351       if(_class.type == enumClass)
3352       {
3353          OldList converts { };
3354          Type type { };
3355          type.kind = classType;
3356
3357          if(!_class.symbol)
3358             _class.symbol = FindClass(_class.fullName);
3359          type._class = _class.symbol;
3360
3361          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3362          {
3363             NamedLink value;
3364             Class enumClass = eSystem_FindClass(privateModule, "enum");
3365             if(enumClass)
3366             {
3367                Class baseClass;
3368                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3369                {
3370                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3371                   for(value = e.values.first; value; value = value.next)
3372                   {
3373                      if(!strcmp(value.name, string))
3374                         break;
3375                   }
3376                   if(value)
3377                   {
3378                      FreeExpContents(sourceExp);
3379                      FreeType(sourceExp.expType);
3380
3381                      sourceExp.isConstant = true;
3382                      sourceExp.expType = MkClassType(baseClass.fullName);
3383                      //if(inCompiler)
3384                      {
3385                         char constant[256];
3386                         sourceExp.type = constantExp;
3387                         if(!strcmp(baseClass.dataTypeString, "int"))
3388                            sprintf(constant, "%d",(int)value.data);
3389                         else
3390                            sprintf(constant, "0x%X",(int)value.data);
3391                         sourceExp.constant = CopyString(constant);
3392                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3393                      }
3394
3395                      while(converts.first)
3396                      {
3397                         Conversion convert = converts.first;
3398                         converts.Remove(convert);
3399                         conversions.Add(convert);
3400                      }
3401                      delete type;
3402                      return true;
3403                   }
3404                }
3405             }
3406          }
3407          if(converts.first)
3408             converts.Free(FreeConvert);
3409          delete type;
3410       }
3411    }
3412    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3413       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3414          return true;
3415    return false;
3416 }
3417
3418 public bool ModuleVisibility(Module searchIn, Module searchFor)
3419 {
3420    SubModule subModule;
3421
3422    if(searchFor == searchIn)
3423       return true;
3424
3425    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3426    {
3427       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3428       {
3429          if(ModuleVisibility(subModule.module, searchFor))
3430             return true;
3431       }
3432    }
3433    return false;
3434 }
3435
3436 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3437 {
3438    Module module;
3439
3440    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3441       return true;
3442    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3443       return true;
3444    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3445       return true;
3446
3447    for(module = mainModule.application.allModules.first; module; module = module.next)
3448    {
3449       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3450          return true;
3451    }
3452    return false;
3453 }
3454
3455 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3456 {
3457    Type source = sourceExp.expType;
3458    Type realDest = dest;
3459    Type backupSourceExpType = null;
3460
3461    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3462       return true;
3463
3464    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3465    {
3466        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3467        {
3468           Class sourceBase, destBase;
3469           for(sourceBase = source._class.registered;
3470               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3471               sourceBase = sourceBase.base);
3472           for(destBase = dest._class.registered;
3473               destBase && destBase.base && destBase.base.type != systemClass;
3474               destBase = destBase.base);
3475           //if(source._class.registered == dest._class.registered)
3476           if(sourceBase == destBase)
3477              return true;
3478        }
3479    }
3480
3481    if(source)
3482    {
3483       OldList * specs;
3484       bool flag = false;
3485       int64 value = MAXINT;
3486
3487       source.refCount++;
3488       dest.refCount++;
3489
3490       if(sourceExp.type == constantExp)
3491       {
3492          if(source.isSigned)
3493             value = strtoll(sourceExp.constant, null, 0);
3494          else
3495             value = strtoull(sourceExp.constant, null, 0);
3496       }
3497       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3498       {
3499          if(source.isSigned)
3500             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3501          else
3502             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3503       }
3504
3505       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3506          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3507       {
3508          FreeType(source);
3509          source = Type { kind = intType, isSigned = false, refCount = 1 };
3510       }
3511
3512       if(dest.kind == classType)
3513       {
3514          Class _class = dest._class ? dest._class.registered : null;
3515
3516          if(_class && _class.type == unitClass)
3517          {
3518             if(source.kind != classType)
3519             {
3520                Type tempType { };
3521                Type tempDest, tempSource;
3522
3523                for(; _class.base.type != systemClass; _class = _class.base);
3524                tempSource = dest;
3525                tempDest = tempType;
3526
3527                tempType.kind = classType;
3528                if(!_class.symbol)
3529                   _class.symbol = FindClass(_class.fullName);
3530
3531                tempType._class = _class.symbol;
3532                tempType.truth = dest.truth;
3533                if(tempType._class)
3534                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3535
3536                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3537                backupSourceExpType = sourceExp.expType;
3538                sourceExp.expType = dest; dest.refCount++;
3539                //sourceExp.expType = MkClassType(_class.fullName);
3540                flag = true;
3541
3542                delete tempType;
3543             }
3544          }
3545
3546
3547          // Why wasn't there something like this?
3548          if(_class && _class.type == bitClass && source.kind != classType)
3549          {
3550             if(!dest._class.registered.dataType)
3551                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3552             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3553             {
3554                FreeType(source);
3555                FreeType(sourceExp.expType);
3556                source = sourceExp.expType = MkClassType(dest._class.string);
3557                source.refCount++;
3558
3559                //source.kind = classType;
3560                //source._class = dest._class;
3561             }
3562          }
3563
3564          // Adding two enumerations
3565          /*
3566          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3567          {
3568             if(!source._class.registered.dataType)
3569                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3570             if(!dest._class.registered.dataType)
3571                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3572
3573             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3574             {
3575                FreeType(source);
3576                source = sourceExp.expType = MkClassType(dest._class.string);
3577                source.refCount++;
3578
3579                //source.kind = classType;
3580                //source._class = dest._class;
3581             }
3582          }*/
3583
3584          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3585          {
3586             OldList * specs = MkList();
3587             Declarator decl;
3588             char string[1024];
3589
3590             ReadString(string, sourceExp.string);
3591             decl = SpecDeclFromString(string, specs, null);
3592
3593             FreeExpContents(sourceExp);
3594             FreeType(sourceExp.expType);
3595
3596             sourceExp.type = classExp;
3597             sourceExp._classExp.specifiers = specs;
3598             sourceExp._classExp.decl = decl;
3599             sourceExp.expType = dest;
3600             dest.refCount++;
3601
3602             FreeType(source);
3603             FreeType(dest);
3604             if(backupSourceExpType) FreeType(backupSourceExpType);
3605             return true;
3606          }
3607       }
3608       else if(source.kind == classType)
3609       {
3610          Class _class = source._class ? source._class.registered : null;
3611
3612          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3613          {
3614             /*
3615             if(dest.kind != classType)
3616             {
3617                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3618                if(!source._class.registered.dataType)
3619                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3620
3621                FreeType(dest);
3622                dest = MkClassType(source._class.string);
3623                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3624                //   dest = MkClassType(source._class.string);
3625             }
3626             */
3627
3628             if(dest.kind != classType)
3629             {
3630                Type tempType { };
3631                Type tempDest, tempSource;
3632
3633                if(!source._class.registered.dataType)
3634                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3635
3636                for(; _class.base.type != systemClass; _class = _class.base);
3637                tempDest = source;
3638                tempSource = tempType;
3639                tempType.kind = classType;
3640                tempType._class = FindClass(_class.fullName);
3641                tempType.truth = source.truth;
3642                tempType.classObjectType = source.classObjectType;
3643
3644                if(tempType._class)
3645                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3646
3647                // PUT THIS BACK TESTING UNITS?
3648                if(conversions.last)
3649                {
3650                   ((Conversion)(conversions.last)).resultType = dest;
3651                   dest.refCount++;
3652                }
3653
3654                FreeType(sourceExp.expType);
3655                sourceExp.expType = MkClassType(_class.fullName);
3656                sourceExp.expType.truth = source.truth;
3657                sourceExp.expType.classObjectType = source.classObjectType;
3658
3659                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3660
3661                if(!sourceExp.destType)
3662                {
3663                   FreeType(sourceExp.destType);
3664                   sourceExp.destType = sourceExp.expType;
3665                   if(sourceExp.expType)
3666                      sourceExp.expType.refCount++;
3667                }
3668                //flag = true;
3669                //source = _class.dataType;
3670
3671
3672                // TOCHECK: TESTING THIS NEW CODE
3673                if(!_class.dataType)
3674                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3675                FreeType(dest);
3676                dest = MkClassType(source._class.string);
3677                dest.truth = source.truth;
3678                dest.classObjectType = source.classObjectType;
3679
3680                FreeType(source);
3681                source = _class.dataType;
3682                source.refCount++;
3683
3684                delete tempType;
3685             }
3686          }
3687       }
3688
3689       if(!flag)
3690       {
3691          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3692          {
3693             FreeType(source);
3694             FreeType(dest);
3695             return true;
3696          }
3697       }
3698
3699       // Implicit Casts
3700       /*
3701       if(source.kind == classType)
3702       {
3703          Class _class = source._class.registered;
3704          if(_class.type == unitClass)
3705          {
3706             if(!_class.dataType)
3707                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3708             source = _class.dataType;
3709          }
3710       }*/
3711
3712       if(dest.kind == classType)
3713       {
3714          Class _class = dest._class ? dest._class.registered : null;
3715          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") ||
3716             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3717          {
3718             if(_class.type == normalClass || _class.type == noHeadClass)
3719             {
3720                Expression newExp { };
3721                *newExp = *sourceExp;
3722                if(sourceExp.destType) sourceExp.destType.refCount++;
3723                if(sourceExp.expType)  sourceExp.expType.refCount++;
3724                sourceExp.type = castExp;
3725                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3726                sourceExp.cast.exp = newExp;
3727                FreeType(sourceExp.expType);
3728                sourceExp.expType = null;
3729                ProcessExpressionType(sourceExp);
3730
3731                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3732                if(!inCompiler)
3733                {
3734                   FreeType(sourceExp.expType);
3735                   sourceExp.expType = dest;
3736                }
3737
3738                FreeType(source);
3739                if(inCompiler) FreeType(dest);
3740
3741                if(backupSourceExpType) FreeType(backupSourceExpType);
3742                return true;
3743             }
3744
3745             if(!_class.dataType)
3746                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3747             FreeType(dest);
3748             dest = _class.dataType;
3749             dest.refCount++;
3750          }
3751
3752          // Accept lower precision types for units, since we want to keep the unit type
3753          if(dest.kind == doubleType &&
3754             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3755              source.kind == charType))
3756          {
3757             specs = MkListOne(MkSpecifier(DOUBLE));
3758          }
3759          else if(dest.kind == floatType &&
3760             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3761             source.kind == doubleType))
3762          {
3763             specs = MkListOne(MkSpecifier(FLOAT));
3764          }
3765          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3766             source.kind == floatType || source.kind == doubleType))
3767          {
3768             specs = MkList();
3769             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3770             ListAdd(specs, MkSpecifier(INT64));
3771          }
3772          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3773             source.kind == floatType || source.kind == doubleType))
3774          {
3775             specs = MkList();
3776             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3777             ListAdd(specs, MkSpecifier(INT));
3778          }
3779          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == intType ||
3780             source.kind == floatType || source.kind == doubleType))
3781          {
3782             specs = MkList();
3783             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3784             ListAdd(specs, MkSpecifier(SHORT));
3785          }
3786          else if(dest.kind == charType && (source.kind == charType || source.kind == shortType || source.kind == intType ||
3787             source.kind == floatType || source.kind == doubleType))
3788          {
3789             specs = MkList();
3790             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3791             ListAdd(specs, MkSpecifier(CHAR));
3792          }
3793          else
3794          {
3795             FreeType(source);
3796             FreeType(dest);
3797             if(backupSourceExpType)
3798             {
3799                // Failed to convert: revert previous exp type
3800                if(sourceExp.expType) FreeType(sourceExp.expType);
3801                sourceExp.expType = backupSourceExpType;
3802             }
3803             return false;
3804          }
3805       }
3806       else if(dest.kind == doubleType &&
3807          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3808           source.kind == charType))
3809       {
3810          specs = MkListOne(MkSpecifier(DOUBLE));
3811       }
3812       else if(dest.kind == floatType &&
3813          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType))
3814       {
3815          specs = MkListOne(MkSpecifier(FLOAT));
3816       }
3817       else if(dest.kind == charType && (source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3818          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3819       {
3820          specs = MkList();
3821          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3822          ListAdd(specs, MkSpecifier(CHAR));
3823       }
3824       else if(dest.kind == shortType && (source.kind == enumType || source.kind == charType || source.kind == shortType ||
3825          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3826       {
3827          specs = MkList();
3828          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3829          ListAdd(specs, MkSpecifier(SHORT));
3830       }
3831       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == charType || source.kind == intType))
3832       {
3833          specs = MkList();
3834          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3835          ListAdd(specs, MkSpecifier(INT));
3836       }
3837       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3838       {
3839          specs = MkList();
3840          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3841          ListAdd(specs, MkSpecifier(INT64));
3842       }
3843       else if(dest.kind == enumType &&
3844          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType))
3845       {
3846          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3847       }
3848       else
3849       {
3850          FreeType(source);
3851          FreeType(dest);
3852          if(backupSourceExpType)
3853          {
3854             // Failed to convert: revert previous exp type
3855             if(sourceExp.expType) FreeType(sourceExp.expType);
3856             sourceExp.expType = backupSourceExpType;
3857          }
3858          return false;
3859       }
3860
3861       if(!flag)
3862       {
3863          Expression newExp { };
3864          *newExp = *sourceExp;
3865          newExp.prev = null;
3866          newExp.next = null;
3867          if(sourceExp.destType) sourceExp.destType.refCount++;
3868          if(sourceExp.expType)  sourceExp.expType.refCount++;
3869
3870          sourceExp.type = castExp;
3871          if(realDest.kind == classType)
3872          {
3873             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3874             FreeList(specs, FreeSpecifier);
3875          }
3876          else
3877             sourceExp.cast.typeName = MkTypeName(specs, null);
3878          if(newExp.type == opExp)
3879          {
3880             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3881          }
3882          else
3883             sourceExp.cast.exp = newExp;
3884
3885          FreeType(sourceExp.expType);
3886          sourceExp.expType = null;
3887          ProcessExpressionType(sourceExp);
3888       }
3889       else
3890          FreeList(specs, FreeSpecifier);
3891
3892       FreeType(dest);
3893       FreeType(source);
3894       if(backupSourceExpType) FreeType(backupSourceExpType);
3895
3896       return true;
3897    }
3898    else
3899    {
3900       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3901       if(sourceExp.type == identifierExp)
3902       {
3903          Identifier id = sourceExp.identifier;
3904          if(dest.kind == classType)
3905          {
3906             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3907             {
3908                Class _class = dest._class.registered;
3909                Class enumClass = eSystem_FindClass(privateModule, "enum");
3910                if(enumClass)
3911                {
3912                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3913                   {
3914                      NamedLink value;
3915                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3916                      for(value = e.values.first; value; value = value.next)
3917                      {
3918                         if(!strcmp(value.name, id.string))
3919                            break;
3920                      }
3921                      if(value)
3922                      {
3923                         FreeExpContents(sourceExp);
3924                         FreeType(sourceExp.expType);
3925
3926                         sourceExp.isConstant = true;
3927                         sourceExp.expType = MkClassType(_class.fullName);
3928                         //if(inCompiler)
3929                         {
3930                            char constant[256];
3931                            sourceExp.type = constantExp;
3932                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3933                               sprintf(constant, "%d", (int) value.data);
3934                            else
3935                               sprintf(constant, "0x%X", (int) value.data);
3936                            sourceExp.constant = CopyString(constant);
3937                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3938                         }
3939                         return true;
3940                      }
3941                   }
3942                }
3943             }
3944          }
3945
3946          // Loop through all enum classes
3947          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3948             return true;
3949       }
3950    }
3951    return false;
3952 }
3953
3954 #define TERTIARY(o, name, m, t, p) \
3955    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
3956    {                                                              \
3957       exp.type = constantExp;                                    \
3958       exp.string = p(op1.m ? op2.m : op3.m);                     \
3959       if(!exp.expType) \
3960          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3961       return true;                                                \
3962    }
3963
3964 #define BINARY(o, name, m, t, p) \
3965    static bool name(Expression exp, Operand op1, Operand op2)   \
3966    {                                                              \
3967       t value2 = op2.m;                                           \
3968       exp.type = constantExp;                                    \
3969       exp.string = p(op1.m o value2);                     \
3970       if(!exp.expType) \
3971          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3972       return true;                                                \
3973    }
3974
3975 #define BINARY_DIVIDE(o, name, m, t, p) \
3976    static bool name(Expression exp, Operand op1, Operand op2)   \
3977    {                                                              \
3978       t value2 = op2.m;                                           \
3979       exp.type = constantExp;                                    \
3980       exp.string = p(value2 ? (op1.m o value2) : 0);             \
3981       if(!exp.expType) \
3982          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3983       return true;                                                \
3984    }
3985
3986 #define UNARY(o, name, m, t, p) \
3987    static bool name(Expression exp, Operand op1)                \
3988    {                                                              \
3989       exp.type = constantExp;                                    \
3990       exp.string = p((t)(o op1.m));                                   \
3991       if(!exp.expType) \
3992          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3993       return true;                                                \
3994    }
3995
3996 #define OPERATOR_ALL(macro, o, name) \
3997    macro(o, Int##name, i, int, PrintInt) \
3998    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
3999    macro(o, Short##name, s, short, PrintShort) \
4000    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4001    macro(o, Char##name, c, char, PrintChar) \
4002    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4003    macro(o, Float##name, f, float, PrintFloat) \
4004    macro(o, Double##name, d, double, PrintDouble)
4005
4006 #define OPERATOR_INTTYPES(macro, o, name) \
4007    macro(o, Int##name, i, int, PrintInt) \
4008    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4009    macro(o, Short##name, s, short, PrintShort) \
4010    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4011    macro(o, Char##name, c, char, PrintChar) \
4012    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4013
4014
4015 // binary arithmetic
4016 OPERATOR_ALL(BINARY, +, Add)
4017 OPERATOR_ALL(BINARY, -, Sub)
4018 OPERATOR_ALL(BINARY, *, Mul)
4019 OPERATOR_ALL(BINARY_DIVIDE, /, Div)
4020 OPERATOR_INTTYPES(BINARY_DIVIDE, %, Mod)
4021
4022 // unary arithmetic
4023 OPERATOR_ALL(UNARY, -, Neg)
4024
4025 // unary arithmetic increment and decrement
4026 OPERATOR_ALL(UNARY, ++, Inc)
4027 OPERATOR_ALL(UNARY, --, Dec)
4028
4029 // binary arithmetic assignment
4030 OPERATOR_ALL(BINARY, =, Asign)
4031 OPERATOR_ALL(BINARY, +=, AddAsign)
4032 OPERATOR_ALL(BINARY, -=, SubAsign)
4033 OPERATOR_ALL(BINARY, *=, MulAsign)
4034 OPERATOR_ALL(BINARY_DIVIDE, /=, DivAsign)
4035 OPERATOR_INTTYPES(BINARY_DIVIDE, %=, ModAsign)
4036
4037 // binary bitwise
4038 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4039 OPERATOR_INTTYPES(BINARY, |, BitOr)
4040 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4041 OPERATOR_INTTYPES(BINARY, <<, LShift)
4042 OPERATOR_INTTYPES(BINARY, >>, RShift)
4043
4044 // unary bitwise
4045 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4046
4047 // binary bitwise assignment
4048 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4049 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4050 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4051 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4052 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4053
4054 // unary logical negation
4055 OPERATOR_INTTYPES(UNARY, !, Not)
4056
4057 // binary logical equality
4058 OPERATOR_ALL(BINARY, ==, Equ)
4059 OPERATOR_ALL(BINARY, !=, Nqu)
4060
4061 // binary logical
4062 OPERATOR_ALL(BINARY, &&, And)
4063 OPERATOR_ALL(BINARY, ||, Or)
4064
4065 // binary logical relational
4066 OPERATOR_ALL(BINARY, >, Grt)
4067 OPERATOR_ALL(BINARY, <, Sma)
4068 OPERATOR_ALL(BINARY, >=, GrtEqu)
4069 OPERATOR_ALL(BINARY, <=, SmaEqu)
4070
4071 // tertiary condition operator
4072 OPERATOR_ALL(TERTIARY, ?, Cond)
4073
4074 //Add, Sub, Mul, Div, Mod,     , Neg,     Inc, Dec,    Asign, AddAsign, SubAsign, MulAsign, DivAsign, ModAsign,     BitAnd, BitOr, BitXor, LShift, RShift, BitNot,     AndAsign, OrAsign, XorAsign, LShiftAsign, RShiftAsign,     Not,     Equ, Nqu,     And, Or,     Grt, Sma, GrtEqu, SmaEqu
4075 #define OPERATOR_TABLE_ALL(name, type) \
4076     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4077                           type##Neg, \
4078                           type##Inc, type##Dec, \
4079                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4080                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4081                           type##BitNot, \
4082                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4083                           type##Not, \
4084                           type##Equ, type##Nqu, \
4085                           type##And, type##Or, \
4086                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4087                         }; \
4088
4089 #define OPERATOR_TABLE_INTTYPES(name, type) \
4090     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4091                           type##Neg, \
4092                           type##Inc, type##Dec, \
4093                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4094                           null, null, null, null, null, \
4095                           null, \
4096                           null, null, null, null, null, \
4097                           null, \
4098                           type##Equ, type##Nqu, \
4099                           type##And, type##Or, \
4100                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4101                         }; \
4102
4103 OPERATOR_TABLE_ALL(int, Int)
4104 OPERATOR_TABLE_ALL(uint, UInt)
4105 OPERATOR_TABLE_ALL(short, Short)
4106 OPERATOR_TABLE_ALL(ushort, UShort)
4107 OPERATOR_TABLE_INTTYPES(float, Float)
4108 OPERATOR_TABLE_INTTYPES(double, Double)
4109 OPERATOR_TABLE_ALL(char, Char)
4110 OPERATOR_TABLE_ALL(uchar, UChar)
4111
4112 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4113 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4114 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4115 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4116 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4117 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4118 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4119 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4120
4121 public void ReadString(char * output,  char * string)
4122 {
4123    int len = strlen(string);
4124    int c,d = 0;
4125    bool quoted = false, escaped = false;
4126    for(c = 0; c<len; c++)
4127    {
4128       char ch = string[c];
4129       if(escaped)
4130       {
4131          switch(ch)
4132          {
4133             case 'n': output[d] = '\n'; break;
4134             case 't': output[d] = '\t'; break;
4135             case 'a': output[d] = '\a'; break;
4136             case 'b': output[d] = '\b'; break;
4137             case 'f': output[d] = '\f'; break;
4138             case 'r': output[d] = '\r'; break;
4139             case 'v': output[d] = '\v'; break;
4140             case '\\': output[d] = '\\'; break;
4141             case '\"': output[d] = '\"'; break;
4142             default: output[d++] = '\\'; output[d] = ch;
4143             //default: output[d] = ch;
4144          }
4145          d++;
4146          escaped = false;
4147       }
4148       else
4149       {
4150          if(ch == '\"')
4151             quoted ^= true;
4152          else if(quoted)
4153          {
4154             if(ch == '\\')
4155                escaped = true;
4156             else
4157                output[d++] = ch;
4158          }
4159       }
4160    }
4161    output[d] = '\0';
4162 }
4163
4164 public Operand GetOperand(Expression exp)
4165 {
4166    Operand op { };
4167    Type type = exp.expType;
4168    if(type)
4169    {
4170       while(type.kind == classType &&
4171          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4172       {
4173          if(!type._class.registered.dataType)
4174             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4175          type = type._class.registered.dataType;
4176
4177       }
4178       op.kind = type.kind;
4179       op.type = exp.expType;
4180       if(exp.isConstant && exp.type == constantExp)
4181       {
4182          switch(op.kind)
4183          {
4184             case charType:
4185             {
4186                if(exp.constant[0] == '\'')
4187                   op.c = exp.constant[1];
4188                else if(type.isSigned)
4189                {
4190                   op.c = (char)strtol(exp.constant, null, 0);
4191                   op.ops = charOps;
4192                }
4193                else
4194                {
4195                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4196                   op.ops = ucharOps;
4197                }
4198                break;
4199             }
4200             case shortType:
4201                if(type.isSigned)
4202                {
4203                   op.s = (short)strtol(exp.constant, null, 0);
4204                   op.ops = shortOps;
4205                }
4206                else
4207                {
4208                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4209                   op.ops = ushortOps;
4210                }
4211                break;
4212             case intType:
4213             case longType:
4214                if(type.isSigned)
4215                {
4216                   op.i = (int)strtol(exp.constant, null, 0);
4217                   op.ops = intOps;
4218                }
4219                else
4220                {
4221                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4222                   op.ops = uintOps;
4223                }
4224                op.kind = intType;
4225                break;
4226             case int64Type:
4227                if(type.isSigned)
4228                {
4229                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4230                   op.ops = intOps;
4231                }
4232                else
4233                {
4234                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4235                   op.ops = uintOps;
4236                }
4237                op.kind = intType;
4238                break;
4239             case intPtrType:
4240                if(type.isSigned)
4241                {
4242                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4243                   op.ops = intOps;
4244                }
4245                else
4246                {
4247                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4248                   op.ops = uintOps;
4249                }
4250                op.kind = intType;
4251                break;
4252             case intSizeType:
4253                if(type.isSigned)
4254                {
4255                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4256                   op.ops = intOps;
4257                }
4258                else
4259                {
4260                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4261                   op.ops = uintOps;
4262                }
4263                op.kind = intType;
4264                break;
4265             case floatType:
4266                op.f = (float)strtod(exp.constant, null);
4267                op.ops = floatOps;
4268                break;
4269             case doubleType:
4270                op.d = (double)strtod(exp.constant, null);
4271                op.ops = doubleOps;
4272                break;
4273             //case classType:    For when we have operator overloading...
4274             // Pointer additions
4275             //case functionType:
4276             case arrayType:
4277             case pointerType:
4278             case classType:
4279                op.ui64 = _strtoui64(exp.constant, null, 0);
4280                op.kind = pointerType;
4281                op.ops = uintOps;
4282                // op.ptrSize =
4283                break;
4284          }
4285       }
4286    }
4287    return op;
4288 }
4289
4290 static void UnusedFunction()
4291 {
4292    int a;
4293    a.OnGetString(0,0,0);
4294 }
4295 default:
4296 extern int __ecereVMethodID_class_OnGetString;
4297 public:
4298
4299 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4300 {
4301    DataMember dataMember;
4302    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4303    {
4304       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4305          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4306       else
4307       {
4308          Expression exp { };
4309          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4310          Type type;
4311          void * ptr = inst.data + dataMember.offset + offset;
4312          char * result = null;
4313          exp.loc = member.loc = inst.loc;
4314          ((Identifier)member.identifiers->first).loc = inst.loc;
4315
4316          if(!dataMember.dataType)
4317             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4318          type = dataMember.dataType;
4319          if(type.kind == classType)
4320          {
4321             Class _class = type._class.registered;
4322             if(_class.type == enumClass)
4323             {
4324                Class enumClass = eSystem_FindClass(privateModule, "enum");
4325                if(enumClass)
4326                {
4327                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4328                   NamedLink item;
4329                   for(item = e.values.first; item; item = item.next)
4330                   {
4331                      if((int)item.data == *(int *)ptr)
4332                      {
4333                         result = item.name;
4334                         break;
4335                      }
4336                   }
4337                   if(result)
4338                   {
4339                      exp.identifier = MkIdentifier(result);
4340                      exp.type = identifierExp;
4341                      exp.destType = MkClassType(_class.fullName);
4342                      ProcessExpressionType(exp);
4343                   }
4344                }
4345             }
4346             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4347             {
4348                if(!_class.dataType)
4349                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4350                type = _class.dataType;
4351             }
4352          }
4353          if(!result)
4354          {
4355             switch(type.kind)
4356             {
4357                case floatType:
4358                {
4359                   FreeExpContents(exp);
4360
4361                   exp.constant = PrintFloat(*(float*)ptr);
4362                   exp.type = constantExp;
4363                   break;
4364                }
4365                case doubleType:
4366                {
4367                   FreeExpContents(exp);
4368
4369                   exp.constant = PrintDouble(*(double*)ptr);
4370                   exp.type = constantExp;
4371                   break;
4372                }
4373                case intType:
4374                {
4375                   FreeExpContents(exp);
4376
4377                   exp.constant = PrintInt(*(int*)ptr);
4378                   exp.type = constantExp;
4379                   break;
4380                }
4381                case int64Type:
4382                {
4383                   FreeExpContents(exp);
4384
4385                   exp.constant = PrintInt64(*(int64*)ptr);
4386                   exp.type = constantExp;
4387                   break;
4388                }
4389                case intPtrType:
4390                {
4391                   FreeExpContents(exp);
4392                   // TODO: This should probably use proper type
4393                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4394                   exp.type = constantExp;
4395                   break;
4396                }
4397                case intSizeType:
4398                {
4399                   FreeExpContents(exp);
4400                   // TODO: This should probably use proper type
4401                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4402                   exp.type = constantExp;
4403                   break;
4404                }
4405                default:
4406                   Compiler_Error($"Unhandled type populating instance\n");
4407             }
4408          }
4409          ListAdd(memberList, member);
4410       }
4411
4412       if(parentDataMember.type == unionMember)
4413          break;
4414    }
4415 }
4416
4417 void PopulateInstance(Instantiation inst)
4418 {
4419    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4420    Class _class = classSym.registered;
4421    DataMember dataMember;
4422    OldList * memberList = MkList();
4423    // Added this check and ->Add to prevent memory leaks on bad code
4424    if(!inst.members)
4425       inst.members = MkListOne(MkMembersInitList(memberList));
4426    else
4427       inst.members->Add(MkMembersInitList(memberList));
4428    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4429    {
4430       if(!dataMember.isProperty)
4431       {
4432          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4433             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4434          else
4435          {
4436             Expression exp { };
4437             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4438             Type type;
4439             void * ptr = inst.data + dataMember.offset;
4440             char * result = null;
4441
4442             exp.loc = member.loc = inst.loc;
4443             ((Identifier)member.identifiers->first).loc = inst.loc;
4444
4445             if(!dataMember.dataType)
4446                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4447             type = dataMember.dataType;
4448             if(type.kind == classType)
4449             {
4450                Class _class = type._class.registered;
4451                if(_class.type == enumClass)
4452                {
4453                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4454                   if(enumClass)
4455                   {
4456                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4457                      NamedLink item;
4458                      for(item = e.values.first; item; item = item.next)
4459                      {
4460                         if((int)item.data == *(int *)ptr)
4461                         {
4462                            result = item.name;
4463                            break;
4464                         }
4465                      }
4466                   }
4467                   if(result)
4468                   {
4469                      exp.identifier = MkIdentifier(result);
4470                      exp.type = identifierExp;
4471                      exp.destType = MkClassType(_class.fullName);
4472                      ProcessExpressionType(exp);
4473                   }
4474                }
4475                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4476                {
4477                   if(!_class.dataType)
4478                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4479                   type = _class.dataType;
4480                }
4481             }
4482             if(!result)
4483             {
4484                switch(type.kind)
4485                {
4486                   case floatType:
4487                   {
4488                      exp.constant = PrintFloat(*(float*)ptr);
4489                      exp.type = constantExp;
4490                      break;
4491                   }
4492                   case doubleType:
4493                   {
4494                      exp.constant = PrintDouble(*(double*)ptr);
4495                      exp.type = constantExp;
4496                      break;
4497                   }
4498                   case intType:
4499                   {
4500                      exp.constant = PrintInt(*(int*)ptr);
4501                      exp.type = constantExp;
4502                      break;
4503                   }
4504                   case int64Type:
4505                   {
4506                      exp.constant = PrintInt64(*(int64*)ptr);
4507                      exp.type = constantExp;
4508                      break;
4509                   }
4510                   case intPtrType:
4511                   {
4512                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4513                      exp.type = constantExp;
4514                      break;
4515                   }
4516                   default:
4517                      Compiler_Error($"Unhandled type populating instance\n");
4518                }
4519             }
4520             ListAdd(memberList, member);
4521          }
4522       }
4523    }
4524 }
4525
4526 void ComputeInstantiation(Expression exp)
4527 {
4528    Instantiation inst = exp.instance;
4529    MembersInit members;
4530    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4531    Class _class = classSym ? classSym.registered : null;
4532    DataMember curMember = null;
4533    Class curClass = null;
4534    DataMember subMemberStack[256];
4535    int subMemberStackPos = 0;
4536    uint64 bits = 0;
4537
4538    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4539    {
4540       // Don't recompute the instantiation...
4541       // Non Simple classes will have become constants by now
4542       if(inst.data)
4543          return;
4544
4545       if(_class.type == normalClass || _class.type == noHeadClass)
4546       {
4547          inst.data = (byte *)eInstance_New(_class);
4548          if(_class.type == normalClass)
4549             ((Instance)inst.data)._refCount++;
4550       }
4551       else
4552          inst.data = new0 byte[_class.structSize];
4553    }
4554
4555    if(inst.members)
4556    {
4557       for(members = inst.members->first; members; members = members.next)
4558       {
4559          switch(members.type)
4560          {
4561             case dataMembersInit:
4562             {
4563                if(members.dataMembers)
4564                {
4565                   MemberInit member;
4566                   for(member = members.dataMembers->first; member; member = member.next)
4567                   {
4568                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4569                      bool found = false;
4570
4571                      Property prop = null;
4572                      DataMember dataMember = null;
4573                      Method method = null;
4574                      uint dataMemberOffset;
4575
4576                      if(!ident)
4577                      {
4578                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4579                         if(curMember)
4580                         {
4581                            if(curMember.isProperty)
4582                               prop = (Property)curMember;
4583                            else
4584                            {
4585                               dataMember = curMember;
4586
4587                               // CHANGED THIS HERE
4588                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4589
4590                               // 2013/17/29 -- It seems that this was missing here!
4591                               if(_class.type == normalClass)
4592                                  dataMemberOffset += _class.base.structSize;
4593                               // dataMemberOffset = dataMember.offset;
4594                            }
4595                            found = true;
4596                         }
4597                      }
4598                      else
4599                      {
4600                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4601                         if(prop)
4602                         {
4603                            found = true;
4604                            if(prop.memberAccess == publicAccess)
4605                            {
4606                               curMember = (DataMember)prop;
4607                               curClass = prop._class;
4608                            }
4609                         }
4610                         else
4611                         {
4612                            DataMember _subMemberStack[256];
4613                            int _subMemberStackPos = 0;
4614
4615                            // FILL MEMBER STACK
4616                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4617
4618                            if(dataMember)
4619                            {
4620                               found = true;
4621                               if(dataMember.memberAccess == publicAccess)
4622                               {
4623                                  curMember = dataMember;
4624                                  curClass = dataMember._class;
4625                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4626                                  subMemberStackPos = _subMemberStackPos;
4627                               }
4628                            }
4629                         }
4630                      }
4631
4632                      if(found && member.initializer && member.initializer.type == expInitializer)
4633                      {
4634                         Expression value = member.initializer.exp;
4635                         Type type = null;
4636                         bool deepMember = false;
4637                         if(prop)
4638                         {
4639                            type = prop.dataType;
4640                         }
4641                         else if(dataMember)
4642                         {
4643                            if(!dataMember.dataType)
4644                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4645
4646                            type = dataMember.dataType;
4647                         }
4648
4649                         if(ident && ident.next)
4650                         {
4651                            deepMember = true;
4652
4653                            // for(; ident && type; ident = ident.next)
4654                            for(ident = ident.next; ident && type; ident = ident.next)
4655                            {
4656                               if(type.kind == classType)
4657                               {
4658                                  prop = eClass_FindProperty(type._class.registered,
4659                                     ident.string, privateModule);
4660                                  if(prop)
4661                                     type = prop.dataType;
4662                                  else
4663                                  {
4664                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4665                                        ident.string, &dataMemberOffset, privateModule, null, null);
4666                                     if(dataMember)
4667                                        type = dataMember.dataType;
4668                                  }
4669                               }
4670                               else if(type.kind == structType || type.kind == unionType)
4671                               {
4672                                  Type memberType;
4673                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4674                                  {
4675                                     if(!strcmp(memberType.name, ident.string))
4676                                     {
4677                                        type = memberType;
4678                                        break;
4679                                     }
4680                                  }
4681                               }
4682                            }
4683                         }
4684                         if(value)
4685                         {
4686                            FreeType(value.destType);
4687                            value.destType = type;
4688                            if(type) type.refCount++;
4689                            ComputeExpression(value);
4690                         }
4691                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4692                         {
4693                            if(type.kind == classType)
4694                            {
4695                               Class _class = type._class.registered;
4696                               if(_class.type == bitClass || _class.type == unitClass ||
4697                                  _class.type == enumClass)
4698                               {
4699                                  if(!_class.dataType)
4700                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4701                                  type = _class.dataType;
4702                               }
4703                            }
4704
4705                            if(dataMember)
4706                            {
4707                               void * ptr = inst.data + dataMemberOffset;
4708
4709                               if(value.type == constantExp)
4710                               {
4711                                  switch(type.kind)
4712                                  {
4713                                     case intType:
4714                                     {
4715                                        GetInt(value, (int*)ptr);
4716                                        break;
4717                                     }
4718                                     case int64Type:
4719                                     {
4720                                        GetInt64(value, (int64*)ptr);
4721                                        break;
4722                                     }
4723                                     case intPtrType:
4724                                     {
4725                                        GetIntPtr(value, (intptr*)ptr);
4726                                        break;
4727                                     }
4728                                     case intSizeType:
4729                                     {
4730                                        GetIntSize(value, (intsize*)ptr);
4731                                        break;
4732                                     }
4733                                     case floatType:
4734                                     {
4735                                        GetFloat(value, (float*)ptr);
4736                                        break;
4737                                     }
4738                                     case doubleType:
4739                                     {
4740                                        GetDouble(value, (double *)ptr);
4741                                        break;
4742                                     }
4743                                  }
4744                               }
4745                               else if(value.type == instanceExp)
4746                               {
4747                                  if(type.kind == classType)
4748                                  {
4749                                     Class _class = type._class.registered;
4750                                     if(_class.type == structClass)
4751                                     {
4752                                        ComputeTypeSize(type);
4753                                        if(value.instance.data)
4754                                           memcpy(ptr, value.instance.data, type.size);
4755                                     }
4756                                  }
4757                               }
4758                            }
4759                            else if(prop)
4760                            {
4761                               if(value.type == instanceExp && value.instance.data)
4762                               {
4763                                  if(type.kind == classType)
4764                                  {
4765                                     Class _class = type._class.registered;
4766                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4767                                     {
4768                                        void (*Set)(void *, void *) = (void *)prop.Set;
4769                                        Set(inst.data, value.instance.data);
4770                                        PopulateInstance(inst);
4771                                     }
4772                                  }
4773                               }
4774                               else if(value.type == constantExp)
4775                               {
4776                                  switch(type.kind)
4777                                  {
4778                                     case doubleType:
4779                                     {
4780                                        void (*Set)(void *, double) = (void *)prop.Set;
4781                                        Set(inst.data, strtod(value.constant, null) );
4782                                        break;
4783                                     }
4784                                     case floatType:
4785                                     {
4786                                        void (*Set)(void *, float) = (void *)prop.Set;
4787                                        Set(inst.data, (float)(strtod(value.constant, null)));
4788                                        break;
4789                                     }
4790                                     case intType:
4791                                     {
4792                                        void (*Set)(void *, int) = (void *)prop.Set;
4793                                        Set(inst.data, (int)strtol(value.constant, null, 0));
4794                                        break;
4795                                     }
4796                                     case int64Type:
4797                                     {
4798                                        void (*Set)(void *, int64) = (void *)prop.Set;
4799                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4800                                        break;
4801                                     }
4802                                     case intPtrType:
4803                                     {
4804                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4805                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4806                                        break;
4807                                     }
4808                                     case intSizeType:
4809                                     {
4810                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4811                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4812                                        break;
4813                                     }
4814                                  }
4815                               }
4816                               else if(value.type == stringExp)
4817                               {
4818                                  char temp[1024];
4819                                  ReadString(temp, value.string);
4820                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4821                               }
4822                            }
4823                         }
4824                         else if(!deepMember && type && _class.type == unitClass)
4825                         {
4826                            if(prop)
4827                            {
4828                               // Only support converting units to units for now...
4829                               if(value.type == constantExp)
4830                               {
4831                                  if(type.kind == classType)
4832                                  {
4833                                     Class _class = type._class.registered;
4834                                     if(_class.type == unitClass)
4835                                     {
4836                                        if(!_class.dataType)
4837                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4838                                        type = _class.dataType;
4839                                     }
4840                                  }
4841                                  // TODO: Assuming same base type for units...
4842                                  switch(type.kind)
4843                                  {
4844                                     case floatType:
4845                                     {
4846                                        float fValue;
4847                                        float (*Set)(float) = (void *)prop.Set;
4848                                        GetFloat(member.initializer.exp, &fValue);
4849                                        exp.constant = PrintFloat(Set(fValue));
4850                                        exp.type = constantExp;
4851                                        break;
4852                                     }
4853                                     case doubleType:
4854                                     {
4855                                        double dValue;
4856                                        double (*Set)(double) = (void *)prop.Set;
4857                                        GetDouble(member.initializer.exp, &dValue);
4858                                        exp.constant = PrintDouble(Set(dValue));
4859                                        exp.type = constantExp;
4860                                        break;
4861                                     }
4862                                  }
4863                               }
4864                            }
4865                         }
4866                         else if(!deepMember && type && _class.type == bitClass)
4867                         {
4868                            if(prop)
4869                            {
4870                               if(value.type == instanceExp && value.instance.data)
4871                               {
4872                                  unsigned int (*Set)(void *) = (void *)prop.Set;
4873                                  bits = Set(value.instance.data);
4874                               }
4875                               else if(value.type == constantExp)
4876                               {
4877                               }
4878                            }
4879                            else if(dataMember)
4880                            {
4881                               BitMember bitMember = (BitMember) dataMember;
4882                               Type type;
4883                               int part = 0;
4884                               GetInt(value, &part);
4885                               bits = (bits & ~bitMember.mask);
4886                               if(!bitMember.dataType)
4887                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
4888
4889                               type = bitMember.dataType;
4890
4891                               if(type.kind == classType && type._class && type._class.registered)
4892                               {
4893                                  if(!type._class.registered.dataType)
4894                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4895                                  type = type._class.registered.dataType;
4896                               }
4897
4898                               switch(type.kind)
4899                               {
4900                                  case charType:
4901                                     if(type.isSigned)
4902                                        bits |= ((char)part << bitMember.pos);
4903                                     else
4904                                        bits |= ((unsigned char)part << bitMember.pos);
4905                                     break;
4906                                  case shortType:
4907                                     if(type.isSigned)
4908                                        bits |= ((short)part << bitMember.pos);
4909                                     else
4910                                        bits |= ((unsigned short)part << bitMember.pos);
4911                                     break;
4912                                  case intType:
4913                                  case longType:
4914                                     if(type.isSigned)
4915                                        bits |= ((int)part << bitMember.pos);
4916                                     else
4917                                        bits |= ((unsigned int)part << bitMember.pos);
4918                                     break;
4919                                  case int64Type:
4920                                     if(type.isSigned)
4921                                        bits |= ((int64)part << bitMember.pos);
4922                                     else
4923                                        bits |= ((uint64)part << bitMember.pos);
4924                                     break;
4925                                  case intPtrType:
4926                                     if(type.isSigned)
4927                                     {
4928                                        bits |= ((intptr)part << bitMember.pos);
4929                                     }
4930                                     else
4931                                     {
4932                                        bits |= ((uintptr)part << bitMember.pos);
4933                                     }
4934                                     break;
4935                                  case intSizeType:
4936                                     if(type.isSigned)
4937                                     {
4938                                        bits |= ((ssize_t)(intsize)part << bitMember.pos);
4939                                     }
4940                                     else
4941                                     {
4942                                        bits |= ((size_t) (uintsize)part << bitMember.pos);
4943                                     }
4944                                     break;
4945                               }
4946                            }
4947                         }
4948                      }
4949                      else
4950                      {
4951                         if(_class && _class.type == unitClass)
4952                         {
4953                            ComputeExpression(member.initializer.exp);
4954                            exp.constant = member.initializer.exp.constant;
4955                            exp.type = constantExp;
4956
4957                            member.initializer.exp.constant = null;
4958                         }
4959                      }
4960                   }
4961                }
4962                break;
4963             }
4964          }
4965       }
4966    }
4967    if(_class && _class.type == bitClass)
4968    {
4969       exp.constant = PrintHexUInt(bits);
4970       exp.type = constantExp;
4971    }
4972    if(exp.type != instanceExp)
4973    {
4974       FreeInstance(inst);
4975    }
4976 }
4977
4978 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
4979 {
4980    if(exp.op.op == SIZEOF)
4981    {
4982       FreeExpContents(exp);
4983       exp.type = constantExp;
4984       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
4985    }
4986    else
4987    {
4988       if(!exp.op.exp1)
4989       {
4990          switch(exp.op.op)
4991          {
4992             // unary arithmetic
4993             case '+':
4994             {
4995                // Provide default unary +
4996                Expression exp2 = exp.op.exp2;
4997                exp.op.exp2 = null;
4998                FreeExpContents(exp);
4999                FreeType(exp.expType);
5000                FreeType(exp.destType);
5001                *exp = *exp2;
5002                delete exp2;
5003                break;
5004             }
5005             case '-':
5006                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5007                break;
5008             // unary arithmetic increment and decrement
5009                   //OPERATOR_ALL(UNARY, ++, Inc)
5010                   //OPERATOR_ALL(UNARY, --, Dec)
5011             // unary bitwise
5012             case '~':
5013                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5014                break;
5015             // unary logical negation
5016             case '!':
5017                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5018                break;
5019          }
5020       }
5021       else
5022       {
5023          switch(exp.op.op)
5024          {
5025             // binary arithmetic
5026             case '+':
5027                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5028                break;
5029             case '-':
5030                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5031                break;
5032             case '*':
5033                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5034                break;
5035             case '/':
5036                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5037                break;
5038             case '%':
5039                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5040                break;
5041             // binary arithmetic assignment
5042                   //OPERATOR_ALL(BINARY, =, Asign)
5043                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5044                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5045                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5046                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5047                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5048             // binary bitwise
5049             case '&':
5050                if(exp.op.exp2)
5051                {
5052                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5053                }
5054                break;
5055             case '|':
5056                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5057                break;
5058             case '^':
5059                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5060                break;
5061             case LEFT_OP:
5062                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5063                break;
5064             case RIGHT_OP:
5065                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5066                break;
5067             // binary bitwise assignment
5068                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5069                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5070                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5071                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5072                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5073             // binary logical equality
5074             case EQ_OP:
5075                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5076                break;
5077             case NE_OP:
5078                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5079                break;
5080             // binary logical
5081             case AND_OP:
5082                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5083                break;
5084             case OR_OP:
5085                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5086                break;
5087             // binary logical relational
5088             case '>':
5089                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5090                break;
5091             case '<':
5092                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5093                break;
5094             case GE_OP:
5095                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5096                break;
5097             case LE_OP:
5098                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5099                break;
5100          }
5101       }
5102    }
5103 }
5104
5105 void ComputeExpression(Expression exp)
5106 {
5107    char expString[10240];
5108    expString[0] = '\0';
5109 #ifdef _DEBUG
5110    PrintExpression(exp, expString);
5111 #endif
5112
5113    switch(exp.type)
5114    {
5115       case instanceExp:
5116       {
5117          ComputeInstantiation(exp);
5118          break;
5119       }
5120       /*
5121       case constantExp:
5122          break;
5123       */
5124       case opExp:
5125       {
5126          Expression exp1, exp2 = null;
5127          Operand op1 { };
5128          Operand op2 { };
5129
5130          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5131          if(exp.op.exp2)
5132             ComputeExpression(exp.op.exp2);
5133          if(exp.op.exp1)
5134          {
5135             ComputeExpression(exp.op.exp1);
5136             exp1 = exp.op.exp1;
5137             exp2 = exp.op.exp2;
5138             op1 = GetOperand(exp1);
5139             if(op1.type) op1.type.refCount++;
5140             if(exp2)
5141             {
5142                op2 = GetOperand(exp2);
5143                if(op2.type) op2.type.refCount++;
5144             }
5145          }
5146          else
5147          {
5148             exp1 = exp.op.exp2;
5149             op1 = GetOperand(exp1);
5150             if(op1.type) op1.type.refCount++;
5151          }
5152
5153          CallOperator(exp, exp1, exp2, op1, op2);
5154          /*
5155          switch(exp.op.op)
5156          {
5157             // Unary operators
5158             case '&':
5159                // Also binary
5160                if(exp.op.exp1 && exp.op.exp2)
5161                {
5162                   // Binary And
5163                   if(op1.ops.BitAnd)
5164                   {
5165                      FreeExpContents(exp);
5166                      op1.ops.BitAnd(exp, op1, op2);
5167                   }
5168                }
5169                break;
5170             case '*':
5171                if(exp.op.exp1)
5172                {
5173                   if(op1.ops.Mul)
5174                   {
5175                      FreeExpContents(exp);
5176                      op1.ops.Mul(exp, op1, op2);
5177                   }
5178                }
5179                break;
5180             case '+':
5181                if(exp.op.exp1)
5182                {
5183                   if(op1.ops.Add)
5184                   {
5185                      FreeExpContents(exp);
5186                      op1.ops.Add(exp, op1, op2);
5187                   }
5188                }
5189                else
5190                {
5191                   // Provide default unary +
5192                   Expression exp2 = exp.op.exp2;
5193                   exp.op.exp2 = null;
5194                   FreeExpContents(exp);
5195                   FreeType(exp.expType);
5196                   FreeType(exp.destType);
5197
5198                   *exp = *exp2;
5199                   delete exp2;
5200                }
5201                break;
5202             case '-':
5203                if(exp.op.exp1)
5204                {
5205                   if(op1.ops.Sub)
5206                   {
5207                      FreeExpContents(exp);
5208                      op1.ops.Sub(exp, op1, op2);
5209                   }
5210                }
5211                else
5212                {
5213                   if(op1.ops.Neg)
5214                   {
5215                      FreeExpContents(exp);
5216                      op1.ops.Neg(exp, op1);
5217                   }
5218                }
5219                break;
5220             case '~':
5221                if(op1.ops.BitNot)
5222                {
5223                   FreeExpContents(exp);
5224                   op1.ops.BitNot(exp, op1);
5225                }
5226                break;
5227             case '!':
5228                if(op1.ops.Not)
5229                {
5230                   FreeExpContents(exp);
5231                   op1.ops.Not(exp, op1);
5232                }
5233                break;
5234             // Binary only operators
5235             case '/':
5236                if(op1.ops.Div)
5237                {
5238                   FreeExpContents(exp);
5239                   op1.ops.Div(exp, op1, op2);
5240                }
5241                break;
5242             case '%':
5243                if(op1.ops.Mod)
5244                {
5245                   FreeExpContents(exp);
5246                   op1.ops.Mod(exp, op1, op2);
5247                }
5248                break;
5249             case LEFT_OP:
5250                break;
5251             case RIGHT_OP:
5252                break;
5253             case '<':
5254                if(exp.op.exp1)
5255                {
5256                   if(op1.ops.Sma)
5257                   {
5258                      FreeExpContents(exp);
5259                      op1.ops.Sma(exp, op1, op2);
5260                   }
5261                }
5262                break;
5263             case '>':
5264                if(exp.op.exp1)
5265                {
5266                   if(op1.ops.Grt)
5267                   {
5268                      FreeExpContents(exp);
5269                      op1.ops.Grt(exp, op1, op2);
5270                   }
5271                }
5272                break;
5273             case LE_OP:
5274                if(exp.op.exp1)
5275                {
5276                   if(op1.ops.SmaEqu)
5277                   {
5278                      FreeExpContents(exp);
5279                      op1.ops.SmaEqu(exp, op1, op2);
5280                   }
5281                }
5282                break;
5283             case GE_OP:
5284                if(exp.op.exp1)
5285                {
5286                   if(op1.ops.GrtEqu)
5287                   {
5288                      FreeExpContents(exp);
5289                      op1.ops.GrtEqu(exp, op1, op2);
5290                   }
5291                }
5292                break;
5293             case EQ_OP:
5294                if(exp.op.exp1)
5295                {
5296                   if(op1.ops.Equ)
5297                   {
5298                      FreeExpContents(exp);
5299                      op1.ops.Equ(exp, op1, op2);
5300                   }
5301                }
5302                break;
5303             case NE_OP:
5304                if(exp.op.exp1)
5305                {
5306                   if(op1.ops.Nqu)
5307                   {
5308                      FreeExpContents(exp);
5309                      op1.ops.Nqu(exp, op1, op2);
5310                   }
5311                }
5312                break;
5313             case '|':
5314                if(op1.ops.BitOr)
5315                {
5316                   FreeExpContents(exp);
5317                   op1.ops.BitOr(exp, op1, op2);
5318                }
5319                break;
5320             case '^':
5321                if(op1.ops.BitXor)
5322                {
5323                   FreeExpContents(exp);
5324                   op1.ops.BitXor(exp, op1, op2);
5325                }
5326                break;
5327             case AND_OP:
5328                break;
5329             case OR_OP:
5330                break;
5331             case SIZEOF:
5332                FreeExpContents(exp);
5333                exp.type = constantExp;
5334                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5335                break;
5336          }
5337          */
5338          if(op1.type) FreeType(op1.type);
5339          if(op2.type) FreeType(op2.type);
5340          break;
5341       }
5342       case bracketsExp:
5343       case extensionExpressionExp:
5344       {
5345          Expression e, n;
5346          for(e = exp.list->first; e; e = n)
5347          {
5348             n = e.next;
5349             if(!n)
5350             {
5351                OldList * list = exp.list;
5352                ComputeExpression(e);
5353                //FreeExpContents(exp);
5354                FreeType(exp.expType);
5355                FreeType(exp.destType);
5356                *exp = *e;
5357                delete e;
5358                delete list;
5359             }
5360             else
5361             {
5362                FreeExpression(e);
5363             }
5364          }
5365          break;
5366       }
5367       /*
5368
5369       case ExpIndex:
5370       {
5371          Expression e;
5372          exp.isConstant = true;
5373
5374          ComputeExpression(exp.index.exp);
5375          if(!exp.index.exp.isConstant)
5376             exp.isConstant = false;
5377
5378          for(e = exp.index.index->first; e; e = e.next)
5379          {
5380             ComputeExpression(e);
5381             if(!e.next)
5382             {
5383                // Check if this type is int
5384             }
5385             if(!e.isConstant)
5386                exp.isConstant = false;
5387          }
5388          exp.expType = Dereference(exp.index.exp.expType);
5389          break;
5390       }
5391       */
5392       case memberExp:
5393       {
5394          Expression memberExp = exp.member.exp;
5395          Identifier memberID = exp.member.member;
5396
5397          Type type;
5398          ComputeExpression(exp.member.exp);
5399          type = exp.member.exp.expType;
5400          if(type)
5401          {
5402             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);
5403             Property prop = null;
5404             DataMember member = null;
5405             Class convertTo = null;
5406             if(type.kind == subClassType && exp.member.exp.type == classExp)
5407                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5408
5409             if(!_class)
5410             {
5411                char string[256];
5412                Symbol classSym;
5413                string[0] = '\0';
5414                PrintTypeNoConst(type, string, false, true);
5415                classSym = FindClass(string);
5416                _class = classSym ? classSym.registered : null;
5417             }
5418
5419             if(exp.member.member)
5420             {
5421                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5422                if(!prop)
5423                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5424             }
5425             if(!prop && !member && _class && exp.member.member)
5426             {
5427                Symbol classSym = FindClass(exp.member.member.string);
5428                convertTo = _class;
5429                _class = classSym ? classSym.registered : null;
5430                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5431             }
5432
5433             if(prop)
5434             {
5435                if(prop.compiled)
5436                {
5437                   Type type = prop.dataType;
5438                   // TODO: Assuming same base type for units...
5439                   if(_class.type == unitClass)
5440                   {
5441                      if(type.kind == classType)
5442                      {
5443                         Class _class = type._class.registered;
5444                         if(_class.type == unitClass)
5445                         {
5446                            if(!_class.dataType)
5447                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5448                            type = _class.dataType;
5449                         }
5450                      }
5451                      switch(type.kind)
5452                      {
5453                         case floatType:
5454                         {
5455                            float value;
5456                            float (*Get)(float) = (void *)prop.Get;
5457                            GetFloat(exp.member.exp, &value);
5458                            exp.constant = PrintFloat(Get ? Get(value) : value);
5459                            exp.type = constantExp;
5460                            break;
5461                         }
5462                         case doubleType:
5463                         {
5464                            double value;
5465                            double (*Get)(double);
5466                            GetDouble(exp.member.exp, &value);
5467
5468                            if(convertTo)
5469                               Get = (void *)prop.Set;
5470                            else
5471                               Get = (void *)prop.Get;
5472                            exp.constant = PrintDouble(Get ? Get(value) : value);
5473                            exp.type = constantExp;
5474                            break;
5475                         }
5476                      }
5477                   }
5478                   else
5479                   {
5480                      if(convertTo)
5481                      {
5482                         Expression value = exp.member.exp;
5483                         Type type;
5484                         if(!prop.dataType)
5485                            ProcessPropertyType(prop);
5486
5487                         type = prop.dataType;
5488                         if(!type)
5489                         {
5490                             // printf("Investigate this\n");
5491                         }
5492                         else if(_class.type == structClass)
5493                         {
5494                            switch(type.kind)
5495                            {
5496                               case classType:
5497                               {
5498                                  Class propertyClass = type._class.registered;
5499                                  if(propertyClass.type == structClass && value.type == instanceExp)
5500                                  {
5501                                     void (*Set)(void *, void *) = (void *)prop.Set;
5502                                     exp.instance = Instantiation { };
5503                                     exp.instance.data = new0 byte[_class.structSize];
5504                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5505                                     exp.instance.loc = exp.loc;
5506                                     exp.type = instanceExp;
5507                                     Set(exp.instance.data, value.instance.data);
5508                                     PopulateInstance(exp.instance);
5509                                  }
5510                                  break;
5511                               }
5512                               case intType:
5513                               {
5514                                  int intValue;
5515                                  void (*Set)(void *, int) = (void *)prop.Set;
5516
5517                                  exp.instance = Instantiation { };
5518                                  exp.instance.data = new0 byte[_class.structSize];
5519                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5520                                  exp.instance.loc = exp.loc;
5521                                  exp.type = instanceExp;
5522
5523                                  GetInt(value, &intValue);
5524
5525                                  Set(exp.instance.data, intValue);
5526                                  PopulateInstance(exp.instance);
5527                                  break;
5528                               }
5529                               case int64Type:
5530                               {
5531                                  int64 intValue;
5532                                  void (*Set)(void *, int64) = (void *)prop.Set;
5533
5534                                  exp.instance = Instantiation { };
5535                                  exp.instance.data = new0 byte[_class.structSize];
5536                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5537                                  exp.instance.loc = exp.loc;
5538                                  exp.type = instanceExp;
5539
5540                                  GetInt64(value, &intValue);
5541
5542                                  Set(exp.instance.data, intValue);
5543                                  PopulateInstance(exp.instance);
5544                                  break;
5545                               }
5546                               case intPtrType:
5547                               {
5548                                  // TOFIX:
5549                                  intptr intValue;
5550                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5551
5552                                  exp.instance = Instantiation { };
5553                                  exp.instance.data = new0 byte[_class.structSize];
5554                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5555                                  exp.instance.loc = exp.loc;
5556                                  exp.type = instanceExp;
5557
5558                                  GetIntPtr(value, &intValue);
5559
5560                                  Set(exp.instance.data, intValue);
5561                                  PopulateInstance(exp.instance);
5562                                  break;
5563                               }
5564                               case intSizeType:
5565                               {
5566                                  // TOFIX:
5567                                  intsize intValue;
5568                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5569
5570                                  exp.instance = Instantiation { };
5571                                  exp.instance.data = new0 byte[_class.structSize];
5572                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5573                                  exp.instance.loc = exp.loc;
5574                                  exp.type = instanceExp;
5575
5576                                  GetIntSize(value, &intValue);
5577
5578                                  Set(exp.instance.data, intValue);
5579                                  PopulateInstance(exp.instance);
5580                                  break;
5581                               }
5582                               case doubleType:
5583                               {
5584                                  double doubleValue;
5585                                  void (*Set)(void *, double) = (void *)prop.Set;
5586
5587                                  exp.instance = Instantiation { };
5588                                  exp.instance.data = new0 byte[_class.structSize];
5589                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5590                                  exp.instance.loc = exp.loc;
5591                                  exp.type = instanceExp;
5592
5593                                  GetDouble(value, &doubleValue);
5594
5595                                  Set(exp.instance.data, doubleValue);
5596                                  PopulateInstance(exp.instance);
5597                                  break;
5598                               }
5599                            }
5600                         }
5601                         else if(_class.type == bitClass)
5602                         {
5603                            switch(type.kind)
5604                            {
5605                               case classType:
5606                               {
5607                                  Class propertyClass = type._class.registered;
5608                                  if(propertyClass.type == structClass && value.instance.data)
5609                                  {
5610                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5611                                     unsigned int bits = Set(value.instance.data);
5612                                     exp.constant = PrintHexUInt(bits);
5613                                     exp.type = constantExp;
5614                                     break;
5615                                  }
5616                                  else if(_class.type == bitClass)
5617                                  {
5618                                     unsigned int value;
5619                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5620                                     unsigned int bits;
5621
5622                                     GetUInt(exp.member.exp, &value);
5623                                     bits = Set(value);
5624                                     exp.constant = PrintHexUInt(bits);
5625                                     exp.type = constantExp;
5626                                  }
5627                               }
5628                            }
5629                         }
5630                      }
5631                      else
5632                      {
5633                         if(_class.type == bitClass)
5634                         {
5635                            unsigned int value;
5636                            GetUInt(exp.member.exp, &value);
5637
5638                            switch(type.kind)
5639                            {
5640                               case classType:
5641                               {
5642                                  Class _class = type._class.registered;
5643                                  if(_class.type == structClass)
5644                                  {
5645                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5646
5647                                     exp.instance = Instantiation { };
5648                                     exp.instance.data = new0 byte[_class.structSize];
5649                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5650                                     exp.instance.loc = exp.loc;
5651                                     //exp.instance.fullSet = true;
5652                                     exp.type = instanceExp;
5653                                     Get(value, exp.instance.data);
5654                                     PopulateInstance(exp.instance);
5655                                  }
5656                                  else if(_class.type == bitClass)
5657                                  {
5658                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5659                                     uint64 bits = Get(value);
5660                                     exp.constant = PrintHexUInt64(bits);
5661                                     exp.type = constantExp;
5662                                  }
5663                                  break;
5664                               }
5665                            }
5666                         }
5667                         else if(_class.type == structClass)
5668                         {
5669                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5670                            switch(type.kind)
5671                            {
5672                               case classType:
5673                               {
5674                                  Class _class = type._class.registered;
5675                                  if(_class.type == structClass && value)
5676                                  {
5677                                     void (*Get)(void *, void *) = (void *)prop.Get;
5678
5679                                     exp.instance = Instantiation { };
5680                                     exp.instance.data = new0 byte[_class.structSize];
5681                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5682                                     exp.instance.loc = exp.loc;
5683                                     //exp.instance.fullSet = true;
5684                                     exp.type = instanceExp;
5685                                     Get(value, exp.instance.data);
5686                                     PopulateInstance(exp.instance);
5687                                  }
5688                                  break;
5689                               }
5690                            }
5691                         }
5692                         /*else
5693                         {
5694                            char * value = exp.member.exp.instance.data;
5695                            switch(type.kind)
5696                            {
5697                               case classType:
5698                               {
5699                                  Class _class = type._class.registered;
5700                                  if(_class.type == normalClass)
5701                                  {
5702                                     void *(*Get)(void *) = (void *)prop.Get;
5703
5704                                     exp.instance = Instantiation { };
5705                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5706                                     exp.type = instanceExp;
5707                                     exp.instance.data = Get(value, exp.instance.data);
5708                                  }
5709                                  break;
5710                               }
5711                            }
5712                         }
5713                         */
5714                      }
5715                   }
5716                }
5717                else
5718                {
5719                   exp.isConstant = false;
5720                }
5721             }
5722             else if(member)
5723             {
5724             }
5725          }
5726
5727          if(exp.type != ExpressionType::memberExp)
5728          {
5729             FreeExpression(memberExp);
5730             FreeIdentifier(memberID);
5731          }
5732          break;
5733       }
5734       case typeSizeExp:
5735       {
5736          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5737          FreeExpContents(exp);
5738          exp.constant = PrintUInt(ComputeTypeSize(type));
5739          exp.type = constantExp;
5740          FreeType(type);
5741          break;
5742       }
5743       case classSizeExp:
5744       {
5745          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5746          if(classSym && classSym.registered)
5747          {
5748             if(classSym.registered.fixed)
5749             {
5750                FreeSpecifier(exp._class);
5751                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5752                exp.type = constantExp;
5753             }
5754             else
5755             {
5756                char className[1024];
5757                strcpy(className, "__ecereClass_");
5758                FullClassNameCat(className, classSym.string, true);
5759                MangleClassName(className);
5760
5761                DeclareClass(classSym, className);
5762
5763                FreeExpContents(exp);
5764                exp.type = pointerExp;
5765                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
5766                exp.member.member = MkIdentifier("structSize");
5767             }
5768          }
5769          break;
5770       }
5771       case castExp:
5772       //case constantExp:
5773       {
5774          Type type;
5775          Expression e = exp;
5776          if(exp.type == castExp)
5777          {
5778             if(exp.cast.exp)
5779                ComputeExpression(exp.cast.exp);
5780             e = exp.cast.exp;
5781          }
5782          if(e && exp.expType)
5783          {
5784             /*if(exp.destType)
5785                type = exp.destType;
5786             else*/
5787                type = exp.expType;
5788             if(type.kind == classType)
5789             {
5790                Class _class = type._class.registered;
5791                if(_class && (_class.type == unitClass || _class.type == bitClass))
5792                {
5793                   if(!_class.dataType)
5794                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5795                   type = _class.dataType;
5796                }
5797             }
5798
5799             switch(type.kind)
5800             {
5801                case charType:
5802                   if(type.isSigned)
5803                   {
5804                      char value;
5805                      GetChar(e, &value);
5806                      FreeExpContents(exp);
5807                      exp.constant = PrintChar(value);
5808                      exp.type = constantExp;
5809                   }
5810                   else
5811                   {
5812                      unsigned char value;
5813                      GetUChar(e, &value);
5814                      FreeExpContents(exp);
5815                      exp.constant = PrintUChar(value);
5816                      exp.type = constantExp;
5817                   }
5818                   break;
5819                case shortType:
5820                   if(type.isSigned)
5821                   {
5822                      short value;
5823                      GetShort(e, &value);
5824                      FreeExpContents(exp);
5825                      exp.constant = PrintShort(value);
5826                      exp.type = constantExp;
5827                   }
5828                   else
5829                   {
5830                      unsigned short value;
5831                      GetUShort(e, &value);
5832                      FreeExpContents(exp);
5833                      exp.constant = PrintUShort(value);
5834                      exp.type = constantExp;
5835                   }
5836                   break;
5837                case intType:
5838                   if(type.isSigned)
5839                   {
5840                      int value;
5841                      GetInt(e, &value);
5842                      FreeExpContents(exp);
5843                      exp.constant = PrintInt(value);
5844                      exp.type = constantExp;
5845                   }
5846                   else
5847                   {
5848                      unsigned int value;
5849                      GetUInt(e, &value);
5850                      FreeExpContents(exp);
5851                      exp.constant = PrintUInt(value);
5852                      exp.type = constantExp;
5853                   }
5854                   break;
5855                case int64Type:
5856                   if(type.isSigned)
5857                   {
5858                      int64 value;
5859                      GetInt64(e, &value);
5860                      FreeExpContents(exp);
5861                      exp.constant = PrintInt64(value);
5862                      exp.type = constantExp;
5863                   }
5864                   else
5865                   {
5866                      uint64 value;
5867                      GetUInt64(e, &value);
5868                      FreeExpContents(exp);
5869                      exp.constant = PrintUInt64(value);
5870                      exp.type = constantExp;
5871                   }
5872                   break;
5873                case intPtrType:
5874                   if(type.isSigned)
5875                   {
5876                      intptr value;
5877                      GetIntPtr(e, &value);
5878                      FreeExpContents(exp);
5879                      exp.constant = PrintInt64((int64)value);
5880                      exp.type = constantExp;
5881                   }
5882                   else
5883                   {
5884                      uintptr value;
5885                      GetUIntPtr(e, &value);
5886                      FreeExpContents(exp);
5887                      exp.constant = PrintUInt64((uint64)value);
5888                      exp.type = constantExp;
5889                   }
5890                   break;
5891                case intSizeType:
5892                   if(type.isSigned)
5893                   {
5894                      intsize value;
5895                      GetIntSize(e, &value);
5896                      FreeExpContents(exp);
5897                      exp.constant = PrintInt64((int64)value);
5898                      exp.type = constantExp;
5899                   }
5900                   else
5901                   {
5902                      uintsize value;
5903                      GetUIntSize(e, &value);
5904                      FreeExpContents(exp);
5905                      exp.constant = PrintUInt64((uint64)value);
5906                      exp.type = constantExp;
5907                   }
5908                   break;
5909                case floatType:
5910                {
5911                   float value;
5912                   GetFloat(e, &value);
5913                   FreeExpContents(exp);
5914                   exp.constant = PrintFloat(value);
5915                   exp.type = constantExp;
5916                   break;
5917                }
5918                case doubleType:
5919                {
5920                   double value;
5921                   GetDouble(e, &value);
5922                   FreeExpContents(exp);
5923                   exp.constant = PrintDouble(value);
5924                   exp.type = constantExp;
5925                   break;
5926                }
5927             }
5928          }
5929          break;
5930       }
5931       case conditionExp:
5932       {
5933          Operand op1 { };
5934          Operand op2 { };
5935          Operand op3 { };
5936
5937          if(exp.cond.exp)
5938             // Caring only about last expression for now...
5939             ComputeExpression(exp.cond.exp->last);
5940          if(exp.cond.elseExp)
5941             ComputeExpression(exp.cond.elseExp);
5942          if(exp.cond.cond)
5943             ComputeExpression(exp.cond.cond);
5944
5945          op1 = GetOperand(exp.cond.cond);
5946          if(op1.type) op1.type.refCount++;
5947          op2 = GetOperand(exp.cond.exp->last);
5948          if(op2.type) op2.type.refCount++;
5949          op3 = GetOperand(exp.cond.elseExp);
5950          if(op3.type) op3.type.refCount++;
5951
5952          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
5953          if(op1.type) FreeType(op1.type);
5954          if(op2.type) FreeType(op2.type);
5955          if(op3.type) FreeType(op3.type);
5956          break;
5957       }
5958    }
5959 }
5960
5961 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
5962 {
5963    bool result = true;
5964    if(destType)
5965    {
5966       OldList converts { };
5967       Conversion convert;
5968
5969       if(destType.kind == voidType)
5970          return false;
5971
5972       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
5973          result = false;
5974       if(converts.count)
5975       {
5976          // for(convert = converts.last; convert; convert = convert.prev)
5977          for(convert = converts.first; convert; convert = convert.next)
5978          {
5979             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
5980             if(!empty)
5981             {
5982                Expression newExp { };
5983                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
5984
5985                // TODO: Check this...
5986                *newExp = *exp;
5987                newExp.destType = null;
5988
5989                if(convert.isGet)
5990                {
5991                   // [exp].ColorRGB
5992                   exp.type = memberExp;
5993                   exp.addedThis = true;
5994                   exp.member.exp = newExp;
5995                   FreeType(exp.member.exp.expType);
5996
5997                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
5998                   exp.member.exp.expType.classObjectType = objectType;
5999                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6000                   exp.member.memberType = propertyMember;
6001                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6002                   // TESTING THIS... for (int)degrees
6003                   exp.needCast = true;
6004                   if(exp.expType) exp.expType.refCount++;
6005                   ApplyAnyObjectLogic(exp.member.exp);
6006                }
6007                else
6008                {
6009
6010                   /*if(exp.isConstant)
6011                   {
6012                      // Color { ColorRGB = [exp] };
6013                      exp.type = instanceExp;
6014                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6015                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6016                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6017                   }
6018                   else*/
6019                   {
6020                      // If not constant, don't turn it yet into an instantiation
6021                      // (Go through the deep members system first)
6022                      exp.type = memberExp;
6023                      exp.addedThis = true;
6024                      exp.member.exp = newExp;
6025
6026                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6027                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6028                         newExp.expType._class.registered.type == noHeadClass)
6029                      {
6030                         newExp.byReference = true;
6031                      }
6032
6033                      FreeType(exp.member.exp.expType);
6034                      /*exp.member.exp.expType = convert.convert.dataType;
6035                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6036                      exp.member.exp.expType = null;
6037                      if(convert.convert.dataType)
6038                      {
6039                         exp.member.exp.expType = { };
6040                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6041                         exp.member.exp.expType.refCount = 1;
6042                         exp.member.exp.expType.classObjectType = objectType;
6043                         ApplyAnyObjectLogic(exp.member.exp);
6044                      }
6045
6046                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6047                      exp.member.memberType = reverseConversionMember;
6048                      exp.expType = convert.resultType ? convert.resultType :
6049                         MkClassType(convert.convert._class.fullName);
6050                      exp.needCast = true;
6051                      if(convert.resultType) convert.resultType.refCount++;
6052                   }
6053                }
6054             }
6055             else
6056             {
6057                FreeType(exp.expType);
6058                if(convert.isGet)
6059                {
6060                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6061                   exp.needCast = true;
6062                   if(exp.expType) exp.expType.refCount++;
6063                }
6064                else
6065                {
6066                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6067                   exp.needCast = true;
6068                   if(convert.resultType)
6069                      convert.resultType.refCount++;
6070                }
6071             }
6072          }
6073          if(exp.isConstant && inCompiler)
6074             ComputeExpression(exp);
6075
6076          converts.Free(FreeConvert);
6077       }
6078
6079       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6080       {
6081          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6082       }
6083       if(!result && exp.expType && exp.destType)
6084       {
6085          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6086              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6087             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6088             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6089             result = true;
6090       }
6091    }
6092    // if(result) CheckTemplateTypes(exp);
6093    return result;
6094 }
6095
6096 void CheckTemplateTypes(Expression exp)
6097 {
6098    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6099    {
6100       Expression newExp { };
6101       Statement compound;
6102       Context context;
6103       *newExp = *exp;
6104       if(exp.destType) exp.destType.refCount++;
6105       if(exp.expType)  exp.expType.refCount++;
6106       newExp.prev = null;
6107       newExp.next = null;
6108
6109       switch(exp.expType.kind)
6110       {
6111          case doubleType:
6112             if(exp.destType.classObjectType)
6113             {
6114                // We need to pass the address, just pass it along (Undo what was done above)
6115                if(exp.destType) exp.destType.refCount--;
6116                if(exp.expType)  exp.expType.refCount--;
6117                delete newExp;
6118             }
6119             else
6120             {
6121                // If we're looking for value:
6122                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6123                OldList * specs;
6124                OldList * unionDefs = MkList();
6125                OldList * statements = MkList();
6126                context = PushContext();
6127                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6128                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6129                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6130                exp.type = extensionCompoundExp;
6131                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6132                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6133                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6134                exp.compound.compound.context = context;
6135                PopContext(context);
6136             }
6137             break;
6138          default:
6139             exp.type = castExp;
6140             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6141             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6142             break;
6143       }
6144    }
6145    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6146    {
6147       Expression newExp { };
6148       Statement compound;
6149       Context context;
6150       *newExp = *exp;
6151       if(exp.destType) exp.destType.refCount++;
6152       if(exp.expType)  exp.expType.refCount++;
6153       newExp.prev = null;
6154       newExp.next = null;
6155
6156       switch(exp.expType.kind)
6157       {
6158          case doubleType:
6159             if(exp.destType.classObjectType)
6160             {
6161                // We need to pass the address, just pass it along (Undo what was done above)
6162                if(exp.destType) exp.destType.refCount--;
6163                if(exp.expType)  exp.expType.refCount--;
6164                delete newExp;
6165             }
6166             else
6167             {
6168                // If we're looking for value:
6169                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6170                OldList * specs;
6171                OldList * unionDefs = MkList();
6172                OldList * statements = MkList();
6173                context = PushContext();
6174                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6175                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6176                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6177                exp.type = extensionCompoundExp;
6178                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6179                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6180                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6181                exp.compound.compound.context = context;
6182                PopContext(context);
6183             }
6184             break;
6185          case classType:
6186          {
6187             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6188             {
6189                exp.type = bracketsExp;
6190                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6191                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6192                ProcessExpressionType(exp.list->first);
6193                break;
6194             }
6195             else
6196             {
6197                exp.type = bracketsExp;
6198                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6199                newExp.needCast = true;
6200                ProcessExpressionType(exp.list->first);
6201                break;
6202             }
6203          }
6204          default:
6205          {
6206             if(exp.expType.kind == templateType)
6207             {
6208                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6209                if(type)
6210                {
6211                   FreeType(exp.destType);
6212                   FreeType(exp.expType);
6213                   delete newExp;
6214                   break;
6215                }
6216             }
6217             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6218             {
6219                exp.type = opExp;
6220                exp.op.op = '*';
6221                exp.op.exp1 = null;
6222                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6223                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6224             }
6225             else
6226             {
6227                char typeString[1024];
6228                Declarator decl;
6229                OldList * specs = MkList();
6230                typeString[0] = '\0';
6231                PrintType(exp.expType, typeString, false, false);
6232                decl = SpecDeclFromString(typeString, specs, null);
6233
6234                exp.type = castExp;
6235                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6236                exp.cast.typeName = MkTypeName(specs, decl);
6237                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6238                exp.cast.exp.needCast = true;
6239             }
6240             break;
6241          }
6242       }
6243    }
6244 }
6245 // TODO: The Symbol tree should be reorganized by namespaces
6246 // Name Space:
6247 //    - Tree of all symbols within (stored without namespace)
6248 //    - Tree of sub-namespaces
6249
6250 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6251 {
6252    int nsLen = strlen(nameSpace);
6253    Symbol symbol;
6254    // Start at the name space prefix
6255    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6256    {
6257       char * s = symbol.string;
6258       if(!strncmp(s, nameSpace, nsLen))
6259       {
6260          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6261          int c;
6262          char * namePart;
6263          for(c = strlen(s)-1; c >= 0; c--)
6264             if(s[c] == ':')
6265                break;
6266
6267          namePart = s+c+1;
6268          if(!strcmp(namePart, name))
6269          {
6270             // TODO: Error on ambiguity
6271             return symbol;
6272          }
6273       }
6274       else
6275          break;
6276    }
6277    return null;
6278 }
6279
6280 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6281 {
6282    int c;
6283    char nameSpace[1024];
6284    char * namePart;
6285    bool gotColon = false;
6286
6287    nameSpace[0] = '\0';
6288    for(c = strlen(name)-1; c >= 0; c--)
6289       if(name[c] == ':')
6290       {
6291          gotColon = true;
6292          break;
6293       }
6294
6295    namePart = name+c+1;
6296    while(c >= 0 && name[c] == ':') c--;
6297    if(c >= 0)
6298    {
6299       // Try an exact match first
6300       Symbol symbol = (Symbol)tree.FindString(name);
6301       if(symbol)
6302          return symbol;
6303
6304       // Namespace specified
6305       memcpy(nameSpace, name, c + 1);
6306       nameSpace[c+1] = 0;
6307
6308       return ScanWithNameSpace(tree, nameSpace, namePart);
6309    }
6310    else if(gotColon)
6311    {
6312       // Looking for a global symbol, e.g. ::Sleep()
6313       Symbol symbol = (Symbol)tree.FindString(namePart);
6314       return symbol;
6315    }
6316    else
6317    {
6318       // Name only (no namespace specified)
6319       Symbol symbol = (Symbol)tree.FindString(namePart);
6320       if(symbol)
6321          return symbol;
6322       return ScanWithNameSpace(tree, "", namePart);
6323    }
6324    return null;
6325 }
6326
6327 static void ProcessDeclaration(Declaration decl);
6328
6329 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6330 {
6331 #ifdef _DEBUG
6332    //Time startTime = GetTime();
6333 #endif
6334    // Optimize this later? Do this before/less?
6335    Context ctx;
6336    Symbol symbol = null;
6337    // First, check if the identifier is declared inside the function
6338    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6339
6340    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6341    {
6342       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6343       {
6344          symbol = null;
6345          if(thisNameSpace)
6346          {
6347             char curName[1024];
6348             strcpy(curName, thisNameSpace);
6349             strcat(curName, "::");
6350             strcat(curName, name);
6351             // Try to resolve in current namespace first
6352             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6353          }
6354          if(!symbol)
6355             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6356       }
6357       else
6358          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6359
6360       if(symbol || ctx == endContext) break;
6361    }
6362    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6363    {
6364       if(symbol.pointerExternal.type == functionExternal)
6365       {
6366          FunctionDefinition function = symbol.pointerExternal.function;
6367
6368          // Modified this recently...
6369          Context tmpContext = curContext;
6370          curContext = null;
6371          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6372          curContext = tmpContext;
6373
6374          symbol.pointerExternal.symbol = symbol;
6375
6376          // TESTING THIS:
6377          DeclareType(symbol.type, true, true);
6378
6379          ast->Insert(curExternal.prev, symbol.pointerExternal);
6380
6381          symbol.id = curExternal.symbol.idCode;
6382
6383       }
6384       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6385       {
6386          ast->Move(symbol.pointerExternal, curExternal.prev);
6387          symbol.id = curExternal.symbol.idCode;
6388       }
6389    }
6390 #ifdef _DEBUG
6391    //findSymbolTotalTime += GetTime() - startTime;
6392 #endif
6393    return symbol;
6394 }
6395
6396 static void GetTypeSpecs(Type type, OldList * specs)
6397 {
6398    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6399    switch(type.kind)
6400    {
6401       case classType:
6402       {
6403          if(type._class.registered)
6404          {
6405             if(!type._class.registered.dataType)
6406                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6407             GetTypeSpecs(type._class.registered.dataType, specs);
6408          }
6409          break;
6410       }
6411       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6412       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6413       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6414       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6415       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6416       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6417       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6418       case intType:
6419       default:
6420          ListAdd(specs, MkSpecifier(INT)); break;
6421    }
6422 }
6423
6424 static void PrintArraySize(Type arrayType, char * string)
6425 {
6426    char size[256];
6427    size[0] = '\0';
6428    strcat(size, "[");
6429    if(arrayType.enumClass)
6430       strcat(size, arrayType.enumClass.string);
6431    else if(arrayType.arraySizeExp)
6432       PrintExpression(arrayType.arraySizeExp, size);
6433    strcat(size, "]");
6434    strcat(string, size);
6435 }
6436
6437 // WARNING : This function expects a null terminated string since it recursively concatenate...
6438 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6439 {
6440    if(type)
6441    {
6442       if(printConst && type.constant)
6443          strcat(string, "const ");
6444       switch(type.kind)
6445       {
6446          case classType:
6447          {
6448             Symbol c = type._class;
6449             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6450             //       look into merging with thisclass ?
6451             if(type.classObjectType == typedObject)
6452                strcat(string, "typed_object");
6453             else if(type.classObjectType == anyObject)
6454                strcat(string, "any_object");
6455             else
6456             {
6457                if(c && c.string)
6458                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6459             }
6460             if(type.byReference)
6461                strcat(string, " &");
6462             break;
6463          }
6464          case voidType: strcat(string, "void"); break;
6465          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6466          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6467          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6468          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6469          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6470          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6471          case floatType: strcat(string, "float"); break;
6472          case doubleType: strcat(string, "double"); break;
6473          case structType:
6474             if(type.enumName)
6475             {
6476                strcat(string, "struct ");
6477                strcat(string, type.enumName);
6478             }
6479             else if(type.typeName)
6480                strcat(string, type.typeName);
6481             else
6482             {
6483                Type member;
6484                strcat(string, "struct { ");
6485                for(member = type.members.first; member; member = member.next)
6486                {
6487                   PrintType(member, string, true, fullName);
6488                   strcat(string,"; ");
6489                }
6490                strcat(string,"}");
6491             }
6492             break;
6493          case unionType:
6494             if(type.enumName)
6495             {
6496                strcat(string, "union ");
6497                strcat(string, type.enumName);
6498             }
6499             else if(type.typeName)
6500                strcat(string, type.typeName);
6501             else
6502             {
6503                strcat(string, "union ");
6504                strcat(string,"(unnamed)");
6505             }
6506             break;
6507          case enumType:
6508             if(type.enumName)
6509             {
6510                strcat(string, "enum ");
6511                strcat(string, type.enumName);
6512             }
6513             else if(type.typeName)
6514                strcat(string, type.typeName);
6515             else
6516                strcat(string, "int"); // "enum");
6517             break;
6518          case ellipsisType:
6519             strcat(string, "...");
6520             break;
6521          case subClassType:
6522             strcat(string, "subclass(");
6523             strcat(string, type._class ? type._class.string : "int");
6524             strcat(string, ")");
6525             break;
6526          case templateType:
6527             strcat(string, type.templateParameter.identifier.string);
6528             break;
6529          case thisClassType:
6530             strcat(string, "thisclass");
6531             break;
6532          case vaListType:
6533             strcat(string, "__builtin_va_list");
6534             break;
6535       }
6536    }
6537 }
6538
6539 static void PrintName(Type type, char * string, bool fullName)
6540 {
6541    if(type.name && type.name[0])
6542    {
6543       if(fullName)
6544          strcat(string, type.name);
6545       else
6546       {
6547          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6548          if(name) name += 2; else name = type.name;
6549          strcat(string, name);
6550       }
6551    }
6552 }
6553
6554 static void PrintAttribs(Type type, char * string)
6555 {
6556    if(type)
6557    {
6558       if(type.dllExport)   strcat(string, "dllexport ");
6559       if(type.attrStdcall) strcat(string, "stdcall ");
6560    }
6561 }
6562
6563 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6564 {
6565    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6566    {
6567       Type attrType = null;
6568       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6569          PrintAttribs(type, string);
6570       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6571          strcat(string, " const");
6572       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6573       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6574          strcat(string, " (");
6575       if(type.kind == pointerType)
6576       {
6577          if(type.type.kind == functionType || type.type.kind == methodType)
6578             PrintAttribs(type.type, string);
6579       }
6580       if(type.kind == pointerType)
6581       {
6582          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6583             strcat(string, "*");
6584          else
6585             strcat(string, " *");
6586       }
6587       if(printConst && type.constant && type.kind == pointerType)
6588          strcat(string, " const");
6589    }
6590    else
6591       PrintTypeSpecs(type, string, fullName, printConst);
6592 }
6593
6594 static void PostPrintType(Type type, char * string, bool fullName)
6595 {
6596    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6597       strcat(string, ")");
6598    if(type.kind == arrayType)
6599       PrintArraySize(type, string);
6600    else if(type.kind == functionType)
6601    {
6602       Type param;
6603       strcat(string, "(");
6604       for(param = type.params.first; param; param = param.next)
6605       {
6606          PrintType(param, string, true, fullName);
6607          if(param.next) strcat(string, ", ");
6608       }
6609       strcat(string, ")");
6610    }
6611    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6612       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6613 }
6614
6615 // *****
6616 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6617 // *****
6618 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6619 {
6620    PrePrintType(type, string, fullName, null, printConst);
6621
6622    if(type.thisClass || (printName && type.name && type.name[0]))
6623       strcat(string, " ");
6624    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6625    {
6626       Symbol _class = type.thisClass;
6627       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6628       {
6629          if(type.classObjectType == classPointer)
6630             strcat(string, "class");
6631          else
6632             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6633       }
6634       else if(_class && _class.string)
6635       {
6636          String s = _class.string;
6637          if(fullName)
6638             strcat(string, s);
6639          else
6640          {
6641             char * name = RSearchString(s, "::", strlen(s), true, false);
6642             if(name) name += 2; else name = s;
6643             strcat(string, name);
6644          }
6645       }
6646       strcat(string, "::");
6647    }
6648
6649    if(printName && type.name)
6650       PrintName(type, string, fullName);
6651    PostPrintType(type, string, fullName);
6652    if(type.bitFieldCount)
6653    {
6654       char count[100];
6655       sprintf(count, ":%d", type.bitFieldCount);
6656       strcat(string, count);
6657    }
6658 }
6659
6660 void PrintType(Type type, char * string, bool printName, bool fullName)
6661 {
6662    _PrintType(type, string, printName, fullName, true);
6663 }
6664
6665 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6666 {
6667    _PrintType(type, string, printName, fullName, false);
6668 }
6669
6670 static Type FindMember(Type type, char * string)
6671 {
6672    Type memberType;
6673    for(memberType = type.members.first; memberType; memberType = memberType.next)
6674    {
6675       if(!memberType.name)
6676       {
6677          Type subType = FindMember(memberType, string);
6678          if(subType)
6679             return subType;
6680       }
6681       else if(!strcmp(memberType.name, string))
6682          return memberType;
6683    }
6684    return null;
6685 }
6686
6687 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6688 {
6689    Type memberType;
6690    for(memberType = type.members.first; memberType; memberType = memberType.next)
6691    {
6692       if(!memberType.name)
6693       {
6694          Type subType = FindMember(memberType, string);
6695          if(subType)
6696          {
6697             *offset += memberType.offset;
6698             return subType;
6699          }
6700       }
6701       else if(!strcmp(memberType.name, string))
6702       {
6703          *offset += memberType.offset;
6704          return memberType;
6705       }
6706    }
6707    return null;
6708 }
6709
6710 Expression ParseExpressionString(char * expression)
6711 {
6712    fileInput = TempFile { };
6713    fileInput.Write(expression, 1, strlen(expression));
6714    fileInput.Seek(0, start);
6715
6716    echoOn = false;
6717    parsedExpression = null;
6718    resetScanner();
6719    expression_yyparse();
6720    delete fileInput;
6721
6722    return parsedExpression;
6723 }
6724
6725 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6726 {
6727    Identifier id = exp.identifier;
6728    Method method = null;
6729    Property prop = null;
6730    DataMember member = null;
6731    ClassProperty classProp = null;
6732
6733    if(_class && _class.type == enumClass)
6734    {
6735       NamedLink value = null;
6736       Class enumClass = eSystem_FindClass(privateModule, "enum");
6737       if(enumClass)
6738       {
6739          Class baseClass;
6740          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
6741          {
6742             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
6743             for(value = e.values.first; value; value = value.next)
6744             {
6745                if(!strcmp(value.name, id.string))
6746                   break;
6747             }
6748             if(value)
6749             {
6750                char constant[256];
6751
6752                FreeExpContents(exp);
6753
6754                exp.type = constantExp;
6755                exp.isConstant = true;
6756                if(!strcmp(baseClass.dataTypeString, "int"))
6757                   sprintf(constant, "%d",(int)value.data);
6758                else
6759                   sprintf(constant, "0x%X",(int)value.data);
6760                exp.constant = CopyString(constant);
6761                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
6762                exp.expType = MkClassType(baseClass.fullName);
6763                break;
6764             }
6765          }
6766       }
6767       if(value)
6768          return true;
6769    }
6770    if((method = eClass_FindMethod(_class, id.string, privateModule)))
6771    {
6772       ProcessMethodType(method);
6773       exp.expType = Type
6774       {
6775          refCount = 1;
6776          kind = methodType;
6777          method = method;
6778          // Crash here?
6779          // TOCHECK: Put it back to what it was...
6780          // methodClass = _class;
6781          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
6782       };
6783       //id._class = null;
6784       return true;
6785    }
6786    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
6787    {
6788       if(!prop.dataType)
6789          ProcessPropertyType(prop);
6790       exp.expType = prop.dataType;
6791       if(prop.dataType) prop.dataType.refCount++;
6792       return true;
6793    }
6794    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
6795    {
6796       if(!member.dataType)
6797          member.dataType = ProcessTypeString(member.dataTypeString, false);
6798       exp.expType = member.dataType;
6799       if(member.dataType) member.dataType.refCount++;
6800       return true;
6801    }
6802    else if((classProp = eClass_FindClassProperty(_class, id.string)))
6803    {
6804       if(!classProp.dataType)
6805          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
6806
6807       if(classProp.constant)
6808       {
6809          FreeExpContents(exp);
6810
6811          exp.isConstant = true;
6812          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
6813          {
6814             //char constant[256];
6815             exp.type = stringExp;
6816             exp.constant = QMkString((char *)classProp.Get(_class));
6817          }
6818          else
6819          {
6820             char constant[256];
6821             exp.type = constantExp;
6822             sprintf(constant, "%d", (int)classProp.Get(_class));
6823             exp.constant = CopyString(constant);
6824          }
6825       }
6826       else
6827       {
6828          // TO IMPLEMENT...
6829       }
6830
6831       exp.expType = classProp.dataType;
6832       if(classProp.dataType) classProp.dataType.refCount++;
6833       return true;
6834    }
6835    return false;
6836 }
6837
6838 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
6839 {
6840    BinaryTree * tree = &nameSpace.functions;
6841    GlobalData data = (GlobalData)tree->FindString(name);
6842    NameSpace * child;
6843    if(!data)
6844    {
6845       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
6846       {
6847          data = ScanGlobalData(child, name);
6848          if(data)
6849             break;
6850       }
6851    }
6852    return data;
6853 }
6854
6855 static GlobalData FindGlobalData(char * name)
6856 {
6857    int start = 0, c;
6858    NameSpace * nameSpace;
6859    nameSpace = globalData;
6860    for(c = 0; name[c]; c++)
6861    {
6862       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
6863       {
6864          NameSpace * newSpace;
6865          char * spaceName = new char[c - start + 1];
6866          strncpy(spaceName, name + start, c - start);
6867          spaceName[c-start] = '\0';
6868          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
6869          delete spaceName;
6870          if(!newSpace)
6871             return null;
6872          nameSpace = newSpace;
6873          if(name[c] == ':') c++;
6874          start = c+1;
6875       }
6876    }
6877    if(c - start)
6878    {
6879       return ScanGlobalData(nameSpace, name + start);
6880    }
6881    return null;
6882 }
6883
6884 static int definedExpStackPos;
6885 static void * definedExpStack[512];
6886
6887 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
6888 void ReplaceExpContents(Expression checkedExp, Expression newExp)
6889 {
6890    Expression prev = checkedExp.prev, next = checkedExp.next;
6891
6892    FreeExpContents(checkedExp);
6893    FreeType(checkedExp.expType);
6894    FreeType(checkedExp.destType);
6895
6896    *checkedExp = *newExp;
6897
6898    delete newExp;
6899
6900    checkedExp.prev = prev;
6901    checkedExp.next = next;
6902 }
6903
6904 void ApplyAnyObjectLogic(Expression e)
6905 {
6906    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
6907 #ifdef _DEBUG
6908    char debugExpString[4096];
6909    debugExpString[0] = '\0';
6910    PrintExpression(e, debugExpString);
6911 #endif
6912
6913    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
6914    {
6915       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
6916       //ellipsisDestType = destType;
6917       if(e && e.expType)
6918       {
6919          Type type = e.expType;
6920          Class _class = null;
6921          //Type destType = e.destType;
6922
6923          if(type.kind == classType && type._class && type._class.registered)
6924          {
6925             _class = type._class.registered;
6926          }
6927          else if(type.kind == subClassType)
6928          {
6929             _class = FindClass("ecere::com::Class").registered;
6930          }
6931          else
6932          {
6933             char string[1024] = "";
6934             Symbol classSym;
6935
6936             PrintTypeNoConst(type, string, false, true);
6937             classSym = FindClass(string);
6938             if(classSym) _class = classSym.registered;
6939          }
6940
6941          if((_class && (_class.type == enumClass || _class.type == unitClass || _class.type == bitClass || _class.type == systemClass) && strcmp(_class.fullName, "class") && strcmp(_class.fullName, "uintptr") && strcmp(_class.fullName, "intptr")) || // Patched so that class isn't considered SYSTEM...
6942             (!e.expType.classObjectType && (((type.kind != pointerType && type.kind != intPtrType && type.kind != subClassType && (type.kind != classType || !type._class || !type._class.registered || type._class.registered.type == structClass))) ||
6943             destType.byReference)))
6944          {
6945             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
6946             {
6947                Expression checkedExp = e, newExp;
6948
6949                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
6950                {
6951                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
6952                   {
6953                      if(checkedExp.type == extensionCompoundExp)
6954                      {
6955                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
6956                      }
6957                      else
6958                         checkedExp = checkedExp.list->last;
6959                   }
6960                   else if(checkedExp.type == castExp)
6961                      checkedExp = checkedExp.cast.exp;
6962                }
6963
6964                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
6965                {
6966                   newExp = checkedExp.op.exp2;
6967                   checkedExp.op.exp2 = null;
6968                   FreeExpContents(checkedExp);
6969
6970                   if(e.expType && e.expType.passAsTemplate)
6971                   {
6972                      char size[100];
6973                      ComputeTypeSize(e.expType);
6974                      sprintf(size, "%d", e.expType.size);
6975                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
6976                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
6977                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
6978                   }
6979
6980                   ReplaceExpContents(checkedExp, newExp);
6981                   e.byReference = true;
6982                }
6983                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
6984                {
6985                   Expression checkedExp, newExp;
6986
6987                   {
6988                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
6989                      bool hasAddress =
6990                         e.type == identifierExp ||
6991                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
6992                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
6993                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
6994                         e.type == indexExp;
6995
6996                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
6997                      {
6998                         Context context = PushContext();
6999                         Declarator decl;
7000                         OldList * specs = MkList();
7001                         char typeString[1024];
7002                         Expression newExp { };
7003
7004                         typeString[0] = '\0';
7005                         *newExp = *e;
7006
7007                         //if(e.destType) e.destType.refCount++;
7008                         // if(exp.expType) exp.expType.refCount++;
7009                         newExp.prev = null;
7010                         newExp.next = null;
7011                         newExp.expType = null;
7012
7013                         PrintTypeNoConst(e.expType, typeString, false, true);
7014                         decl = SpecDeclFromString(typeString, specs, null);
7015                         newExp.destType = ProcessType(specs, decl);
7016
7017                         curContext = context;
7018
7019                         // We need a current compound for this
7020                         if(curCompound)
7021                         {
7022                            char name[100];
7023                            OldList * stmts = MkList();
7024                            e.type = extensionCompoundExp;
7025                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7026                            if(!curCompound.compound.declarations)
7027                               curCompound.compound.declarations = MkList();
7028                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7029                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7030                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7031                            e.compound = MkCompoundStmt(null, stmts);
7032                         }
7033                         else
7034                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7035
7036                         /*
7037                         e.compound = MkCompoundStmt(
7038                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7039                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7040
7041                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7042                         */
7043
7044                         {
7045                            Type type = e.destType;
7046                            e.destType = { };
7047                            CopyTypeInto(e.destType, type);
7048                            e.destType.refCount = 1;
7049                            e.destType.classObjectType = none;
7050                            FreeType(type);
7051                         }
7052
7053                         e.compound.compound.context = context;
7054                         PopContext(context);
7055                         curContext = context.parent;
7056                      }
7057                   }
7058
7059                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7060                   checkedExp = e;
7061                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7062                   {
7063                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7064                      {
7065                         if(checkedExp.type == extensionCompoundExp)
7066                         {
7067                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7068                         }
7069                         else
7070                            checkedExp = checkedExp.list->last;
7071                      }
7072                      else if(checkedExp.type == castExp)
7073                         checkedExp = checkedExp.cast.exp;
7074                   }
7075                   {
7076                      Expression operand { };
7077                      operand = *checkedExp;
7078                      checkedExp.destType = null;
7079                      checkedExp.expType = null;
7080                      checkedExp.Clear();
7081                      checkedExp.type = opExp;
7082                      checkedExp.op.op = '&';
7083                      checkedExp.op.exp1 = null;
7084                      checkedExp.op.exp2 = operand;
7085
7086                      //newExp = MkExpOp(null, '&', checkedExp);
7087                   }
7088                   //ReplaceExpContents(checkedExp, newExp);
7089                }
7090             }
7091          }
7092       }
7093    }
7094    {
7095       // If expression type is a simple class, make it an address
7096       // FixReference(e, true);
7097    }
7098 //#if 0
7099    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7100       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7101          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7102    {
7103       if(e.expType.classObjectType && destType && destType.classObjectType) //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class"))
7104       {
7105          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7106       }
7107       else
7108       {
7109          Expression thisExp { };
7110
7111          *thisExp = *e;
7112          thisExp.prev = null;
7113          thisExp.next = null;
7114          e.Clear();
7115
7116          e.type = bracketsExp;
7117          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7118          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7119             ((Expression)e.list->first).byReference = true;
7120
7121          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7122          {
7123             e.expType = thisExp.expType;
7124             e.expType.refCount++;
7125          }
7126          else*/
7127          {
7128             e.expType = { };
7129             CopyTypeInto(e.expType, thisExp.expType);
7130             e.expType.byReference = false;
7131             e.expType.refCount = 1;
7132
7133             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7134                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7135             {
7136                e.expType.classObjectType = none;
7137             }
7138          }
7139       }
7140    }
7141 // TOFIX: Try this for a nice IDE crash!
7142 //#endif
7143    // The other way around
7144    else
7145 //#endif
7146    if(destType && e.expType &&
7147          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7148          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7149          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7150    {
7151       if(destType.kind == ellipsisType)
7152       {
7153          Compiler_Error($"Unspecified type\n");
7154       }
7155       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7156       {
7157          bool byReference = e.expType.byReference;
7158          Expression thisExp { };
7159          Declarator decl;
7160          OldList * specs = MkList();
7161          char typeString[1024]; // Watch buffer overruns
7162          Type type;
7163          ClassObjectType backupClassObjectType;
7164          bool backupByReference;
7165
7166          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7167             type = e.expType;
7168          else
7169             type = destType;
7170
7171          backupClassObjectType = type.classObjectType;
7172          backupByReference = type.byReference;
7173
7174          type.classObjectType = none;
7175          type.byReference = false;
7176
7177          typeString[0] = '\0';
7178          PrintType(type, typeString, false, true);
7179          decl = SpecDeclFromString(typeString, specs, null);
7180
7181          type.classObjectType = backupClassObjectType;
7182          type.byReference = backupByReference;
7183
7184          *thisExp = *e;
7185          thisExp.prev = null;
7186          thisExp.next = null;
7187          e.Clear();
7188
7189          if( ( type.kind == classType && type._class && type._class.registered &&
7190                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7191                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7192              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7193              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7194          {
7195             e.type = opExp;
7196             e.op.op = '*';
7197             e.op.exp1 = null;
7198             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7199
7200             e.expType = { };
7201             CopyTypeInto(e.expType, type);
7202             e.expType.byReference = false;
7203             e.expType.refCount = 1;
7204          }
7205          else
7206          {
7207             e.type = castExp;
7208             e.cast.typeName = MkTypeName(specs, decl);
7209             e.cast.exp = thisExp;
7210             e.byReference = true;
7211             e.expType = type;
7212             type.refCount++;
7213          }
7214          e.destType = destType;
7215          destType.refCount++;
7216       }
7217    }
7218 }
7219
7220 void ProcessExpressionType(Expression exp)
7221 {
7222    bool unresolved = false;
7223    Location oldyylloc = yylloc;
7224    bool notByReference = false;
7225 #ifdef _DEBUG
7226    char debugExpString[4096];
7227    debugExpString[0] = '\0';
7228    PrintExpression(exp, debugExpString);
7229 #endif
7230    if(!exp || exp.expType)
7231       return;
7232
7233    //eSystem_Logf("%s\n", expString);
7234
7235    // Testing this here
7236    yylloc = exp.loc;
7237    switch(exp.type)
7238    {
7239       case identifierExp:
7240       {
7241          Identifier id = exp.identifier;
7242          if(!id) return;
7243
7244          // DOING THIS LATER NOW...
7245          if(id._class && id._class.name)
7246          {
7247             id.classSym = id._class.symbol; // FindClass(id._class.name);
7248             /* TODO: Name Space Fix ups
7249             if(!id.classSym)
7250                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7251             */
7252          }
7253
7254          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7255          {
7256             exp.expType = ProcessTypeString("Module", true);
7257             break;
7258          }
7259          else */if(strstr(id.string, "__ecereClass") == id.string)
7260          {
7261             exp.expType = ProcessTypeString("ecere::com::Class", true);
7262             break;
7263          }
7264          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7265          {
7266             // Added this here as well
7267             ReplaceClassMembers(exp, thisClass);
7268             if(exp.type != identifierExp)
7269             {
7270                ProcessExpressionType(exp);
7271                break;
7272             }
7273
7274             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7275                break;
7276          }
7277          else
7278          {
7279             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7280             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7281             if(!symbol/* && exp.destType*/)
7282             {
7283                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7284                   break;
7285                else
7286                {
7287                   if(thisClass)
7288                   {
7289                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7290                      if(exp.type != identifierExp)
7291                      {
7292                         ProcessExpressionType(exp);
7293                         break;
7294                      }
7295                   }
7296                   // Static methods called from inside the _class
7297                   else if(currentClass && !id._class)
7298                   {
7299                      if(ResolveIdWithClass(exp, currentClass, true))
7300                         break;
7301                   }
7302                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7303                }
7304             }
7305
7306             // If we manage to resolve this symbol
7307             if(symbol)
7308             {
7309                Type type = symbol.type;
7310                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7311
7312                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7313                {
7314                   Context context = SetupTemplatesContext(_class);
7315                   type = ReplaceThisClassType(_class);
7316                   FinishTemplatesContext(context);
7317                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7318                }
7319
7320                FreeSpecifier(id._class);
7321                id._class = null;
7322                delete id.string;
7323                id.string = CopyString(symbol.string);
7324
7325                id.classSym = null;
7326                exp.expType = type;
7327                if(type)
7328                   type.refCount++;
7329                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7330                   // Add missing cases here... enum Classes...
7331                   exp.isConstant = true;
7332
7333                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7334                if(symbol.isParam || !strcmp(id.string, "this"))
7335                {
7336                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7337                      exp.byReference = true;
7338
7339                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7340                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7341                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7342                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7343                   {
7344                      Identifier id = exp.identifier;
7345                      exp.type = bracketsExp;
7346                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7347                   }*/
7348                }
7349
7350                if(symbol.isIterator)
7351                {
7352                   if(symbol.isIterator == 3)
7353                   {
7354                      exp.type = bracketsExp;
7355                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7356                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7357                      exp.expType = null;
7358                      ProcessExpressionType(exp);
7359                   }
7360                   else if(symbol.isIterator != 4)
7361                   {
7362                      exp.type = memberExp;
7363                      exp.member.exp = MkExpIdentifier(exp.identifier);
7364                      exp.member.exp.expType = exp.expType;
7365                      /*if(symbol.isIterator == 6)
7366                         exp.member.member = MkIdentifier("key");
7367                      else*/
7368                         exp.member.member = MkIdentifier("data");
7369                      exp.expType = null;
7370                      ProcessExpressionType(exp);
7371                   }
7372                }
7373                break;
7374             }
7375             else
7376             {
7377                DefinedExpression definedExp = null;
7378                if(thisNameSpace && !(id._class && !id._class.name))
7379                {
7380                   char name[1024];
7381                   strcpy(name, thisNameSpace);
7382                   strcat(name, "::");
7383                   strcat(name, id.string);
7384                   definedExp = eSystem_FindDefine(privateModule, name);
7385                }
7386                if(!definedExp)
7387                   definedExp = eSystem_FindDefine(privateModule, id.string);
7388                if(definedExp)
7389                {
7390                   int c;
7391                   for(c = 0; c<definedExpStackPos; c++)
7392                      if(definedExpStack[c] == definedExp)
7393                         break;
7394                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7395                   {
7396                      Location backupYylloc = yylloc;
7397                      definedExpStack[definedExpStackPos++] = definedExp;
7398                      fileInput = TempFile { };
7399                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7400                      fileInput.Seek(0, start);
7401
7402                      echoOn = false;
7403                      parsedExpression = null;
7404                      resetScanner();
7405                      expression_yyparse();
7406                      delete fileInput;
7407
7408                      yylloc = backupYylloc;
7409
7410                      if(parsedExpression)
7411                      {
7412                         FreeIdentifier(id);
7413                         exp.type = bracketsExp;
7414                         exp.list = MkListOne(parsedExpression);
7415                         parsedExpression.loc = yylloc;
7416                         ProcessExpressionType(exp);
7417                         definedExpStackPos--;
7418                         return;
7419                      }
7420                      definedExpStackPos--;
7421                   }
7422                   else
7423                   {
7424                      if(inCompiler)
7425                      {
7426                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7427                      }
7428                   }
7429                }
7430                else
7431                {
7432                   GlobalData data = null;
7433                   if(thisNameSpace && !(id._class && !id._class.name))
7434                   {
7435                      char name[1024];
7436                      strcpy(name, thisNameSpace);
7437                      strcat(name, "::");
7438                      strcat(name, id.string);
7439                      data = FindGlobalData(name);
7440                   }
7441                   if(!data)
7442                      data = FindGlobalData(id.string);
7443                   if(data)
7444                   {
7445                      DeclareGlobalData(data);
7446                      exp.expType = data.dataType;
7447                      if(data.dataType) data.dataType.refCount++;
7448
7449                      delete id.string;
7450                      id.string = CopyString(data.fullName);
7451                      FreeSpecifier(id._class);
7452                      id._class = null;
7453
7454                      break;
7455                   }
7456                   else
7457                   {
7458                      GlobalFunction function = null;
7459                      if(thisNameSpace && !(id._class && !id._class.name))
7460                      {
7461                         char name[1024];
7462                         strcpy(name, thisNameSpace);
7463                         strcat(name, "::");
7464                         strcat(name, id.string);
7465                         function = eSystem_FindFunction(privateModule, name);
7466                      }
7467                      if(!function)
7468                         function = eSystem_FindFunction(privateModule, id.string);
7469                      if(function)
7470                      {
7471                         char name[1024];
7472                         delete id.string;
7473                         id.string = CopyString(function.name);
7474                         name[0] = 0;
7475
7476                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7477                            strcpy(name, "__ecereFunction_");
7478                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7479                         if(DeclareFunction(function, name))
7480                         {
7481                            delete id.string;
7482                            id.string = CopyString(name);
7483                         }
7484                         exp.expType = function.dataType;
7485                         if(function.dataType) function.dataType.refCount++;
7486
7487                         FreeSpecifier(id._class);
7488                         id._class = null;
7489
7490                         break;
7491                      }
7492                   }
7493                }
7494             }
7495          }
7496          unresolved = true;
7497          break;
7498       }
7499       case instanceExp:
7500       {
7501          Class _class;
7502          // Symbol classSym;
7503
7504          if(!exp.instance._class)
7505          {
7506             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7507             {
7508                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7509             }
7510          }
7511
7512          //classSym = FindClass(exp.instance._class.fullName);
7513          //_class = classSym ? classSym.registered : null;
7514
7515          ProcessInstantiationType(exp.instance);
7516          exp.isConstant = exp.instance.isConstant;
7517
7518          /*
7519          if(_class.type == unitClass && _class.base.type != systemClass)
7520          {
7521             {
7522                Type destType = exp.destType;
7523
7524                exp.destType = MkClassType(_class.base.fullName);
7525                exp.expType = MkClassType(_class.fullName);
7526                CheckExpressionType(exp, exp.destType, true);
7527
7528                exp.destType = destType;
7529             }
7530             exp.expType = MkClassType(_class.fullName);
7531          }
7532          else*/
7533          if(exp.instance._class)
7534          {
7535             exp.expType = MkClassType(exp.instance._class.name);
7536             /*if(exp.expType._class && exp.expType._class.registered &&
7537                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7538                exp.expType.byReference = true;*/
7539          }
7540          break;
7541       }
7542       case constantExp:
7543       {
7544          if(!exp.expType)
7545          {
7546             Type type
7547             {
7548                refCount = 1;
7549                constant = true;
7550             };
7551             exp.expType = type;
7552
7553             if(exp.constant[0] == '\'')
7554             {
7555                if((int)((byte *)exp.constant)[1] > 127)
7556                {
7557                   int nb;
7558                   unichar ch = UTF8GetChar(exp.constant + 1, &nb);
7559                   if(nb < 2) ch = exp.constant[1];
7560                   delete exp.constant;
7561                   exp.constant = PrintUInt(ch);
7562                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7563                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7564                   type._class = FindClass("unichar");
7565
7566                   type.isSigned = false;
7567                }
7568                else
7569                {
7570                   type.kind = charType;
7571                   type.isSigned = true;
7572                }
7573             }
7574             else if(strchr(exp.constant, '.'))
7575             {
7576                char ch = exp.constant[strlen(exp.constant)-1];
7577                if(ch == 'f')
7578                   type.kind = floatType;
7579                else
7580                   type.kind = doubleType;
7581                type.isSigned = true;
7582             }
7583             else
7584             {
7585                if(exp.constant[0] == '0' && exp.constant[1])
7586                   type.isSigned = false;
7587                else if(strchr(exp.constant, 'L') || strchr(exp.constant, 'l'))
7588                   type.isSigned = false;
7589                else if(strtoll(exp.constant, null, 0) > MAXINT)
7590                   type.isSigned = false;
7591                else
7592                   type.isSigned = true;
7593                type.kind = intType;
7594             }
7595             exp.isConstant = true;
7596             if(exp.destType && exp.destType.kind == doubleType)
7597                type.kind = doubleType;
7598             else if(exp.destType && exp.destType.kind == floatType)
7599                type.kind = floatType;
7600             else if(exp.destType && exp.destType.kind == int64Type)
7601                type.kind = int64Type;
7602          }
7603          break;
7604       }
7605       case stringExp:
7606       {
7607          exp.isConstant = true;      // Why wasn't this constant?
7608          exp.expType = Type
7609          {
7610             refCount = 1;
7611             kind = pointerType;
7612             type = Type
7613             {
7614                refCount = 1;
7615                kind = charType;
7616                constant = true;
7617             }
7618          };
7619          break;
7620       }
7621       case newExp:
7622       case new0Exp:
7623          ProcessExpressionType(exp._new.size);
7624          exp.expType = Type
7625          {
7626             refCount = 1;
7627             kind = pointerType;
7628             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7629          };
7630          DeclareType(exp.expType.type, false, false);
7631          break;
7632       case renewExp:
7633       case renew0Exp:
7634          ProcessExpressionType(exp._renew.size);
7635          ProcessExpressionType(exp._renew.exp);
7636          exp.expType = Type
7637          {
7638             refCount = 1;
7639             kind = pointerType;
7640             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7641          };
7642          DeclareType(exp.expType.type, false, false);
7643          break;
7644       case opExp:
7645       {
7646          bool assign = false, boolResult = false, boolOps = false;
7647          Type type1 = null, type2 = null;
7648          bool useDestType = false, useSideType = false;
7649          Location oldyylloc = yylloc;
7650          bool useSideUnit = false;
7651
7652          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7653          Type dummy
7654          {
7655             count = 1;
7656             refCount = 1;
7657          };
7658
7659          switch(exp.op.op)
7660          {
7661             // Assignment Operators
7662             case '=':
7663             case MUL_ASSIGN:
7664             case DIV_ASSIGN:
7665             case MOD_ASSIGN:
7666             case ADD_ASSIGN:
7667             case SUB_ASSIGN:
7668             case LEFT_ASSIGN:
7669             case RIGHT_ASSIGN:
7670             case AND_ASSIGN:
7671             case XOR_ASSIGN:
7672             case OR_ASSIGN:
7673                assign = true;
7674                break;
7675             // boolean Operators
7676             case '!':
7677                // Expect boolean operators
7678                //boolOps = true;
7679                //boolResult = true;
7680                break;
7681             case AND_OP:
7682             case OR_OP:
7683                // Expect boolean operands
7684                boolOps = true;
7685                boolResult = true;
7686                break;
7687             // Comparisons
7688             case EQ_OP:
7689             case '<':
7690             case '>':
7691             case LE_OP:
7692             case GE_OP:
7693             case NE_OP:
7694                // Gives boolean result
7695                boolResult = true;
7696                useSideType = true;
7697                break;
7698             case '+':
7699             case '-':
7700                useSideUnit = true;
7701
7702                // Just added these... testing
7703             case '|':
7704             case '&':
7705             case '^':
7706
7707             // DANGER: Verify units
7708             case '/':
7709             case '%':
7710             case '*':
7711
7712                if(exp.op.op != '*' || exp.op.exp1)
7713                {
7714                   useSideType = true;
7715                   useDestType = true;
7716                }
7717                break;
7718
7719             /*// Implement speed etc.
7720             case '*':
7721             case '/':
7722                break;
7723             */
7724          }
7725          if(exp.op.op == '&')
7726          {
7727             // Added this here earlier for Iterator address as key
7728             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
7729             {
7730                Identifier id = exp.op.exp2.identifier;
7731                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7732                if(symbol && symbol.isIterator == 2)
7733                {
7734                   exp.type = memberExp;
7735                   exp.member.exp = exp.op.exp2;
7736                   exp.member.member = MkIdentifier("key");
7737                   exp.expType = null;
7738                   exp.op.exp2.expType = symbol.type;
7739                   symbol.type.refCount++;
7740                   ProcessExpressionType(exp);
7741                   FreeType(dummy);
7742                   break;
7743                }
7744                // exp.op.exp2.usage.usageRef = true;
7745             }
7746          }
7747
7748          //dummy.kind = TypeDummy;
7749
7750          if(exp.op.exp1)
7751          {
7752             if(exp.destType && exp.destType.kind == classType &&
7753                exp.destType._class && exp.destType._class.registered && useDestType &&
7754
7755               ((exp.destType._class.registered.type == unitClass && useSideUnit) ||
7756                exp.destType._class.registered.type == enumClass ||
7757                exp.destType._class.registered.type == bitClass
7758                ))
7759
7760               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
7761             {
7762                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7763                exp.op.exp1.destType = exp.destType;
7764                if(exp.destType)
7765                   exp.destType.refCount++;
7766             }
7767             else if(!assign)
7768             {
7769                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7770                exp.op.exp1.destType = dummy;
7771                dummy.refCount++;
7772             }
7773
7774             // TESTING THIS HERE...
7775             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
7776             ProcessExpressionType(exp.op.exp1);
7777             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
7778
7779             if(exp.op.exp1.destType == dummy)
7780             {
7781                FreeType(dummy);
7782                exp.op.exp1.destType = null;
7783             }
7784             type1 = exp.op.exp1.expType;
7785          }
7786
7787          if(exp.op.exp2)
7788          {
7789             char expString[10240];
7790             expString[0] = '\0';
7791             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
7792             {
7793                if(exp.op.exp1)
7794                {
7795                   exp.op.exp2.destType = exp.op.exp1.expType;
7796                   if(exp.op.exp1.expType)
7797                      exp.op.exp1.expType.refCount++;
7798                }
7799                else
7800                {
7801                   exp.op.exp2.destType = exp.destType;
7802                   if(exp.destType)
7803                      exp.destType.refCount++;
7804                }
7805
7806                if(type1) type1.refCount++;
7807                exp.expType = type1;
7808             }
7809             else if(assign)
7810             {
7811                if(inCompiler)
7812                   PrintExpression(exp.op.exp2, expString);
7813
7814                if(type1 && type1.kind == pointerType)
7815                {
7816                   if(exp.op.op == MUL_ASSIGN || exp.op.op == DIV_ASSIGN ||exp.op.op == MOD_ASSIGN ||exp.op.op == LEFT_ASSIGN ||exp.op.op == RIGHT_ASSIGN ||
7817                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
7818                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
7819                   else if(exp.op.op == '=')
7820                   {
7821                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7822                      exp.op.exp2.destType = type1;
7823                      if(type1)
7824                         type1.refCount++;
7825                   }
7826                }
7827                else
7828                {
7829                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
7830                   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/* ||
7831                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
7832                   else
7833                   {
7834                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7835                      exp.op.exp2.destType = type1;
7836                      if(type1)
7837                         type1.refCount++;
7838                   }
7839                }
7840                if(type1) type1.refCount++;
7841                exp.expType = type1;
7842             }
7843             else if(exp.destType && exp.destType.kind == classType &&
7844                exp.destType._class && exp.destType._class.registered &&
7845
7846                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) ||
7847                   (exp.destType._class.registered.type == enumClass && useDestType))
7848                   )
7849             {
7850                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7851                exp.op.exp2.destType = exp.destType;
7852                if(exp.destType)
7853                   exp.destType.refCount++;
7854             }
7855             else
7856             {
7857                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7858                exp.op.exp2.destType = dummy;
7859                dummy.refCount++;
7860             }
7861
7862             // TESTING THIS HERE... (DANGEROUS)
7863             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
7864                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
7865             {
7866                FreeType(exp.op.exp2.destType);
7867                exp.op.exp2.destType = type1;
7868                type1.refCount++;
7869             }
7870             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
7871             ProcessExpressionType(exp.op.exp2);
7872             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
7873
7874             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
7875             {
7876                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)
7877                {
7878                   if(exp.op.op != '=' && type1.type.kind == voidType)
7879                      Compiler_Error($"void *: unknown size\n");
7880                }
7881                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||
7882                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
7883                               (exp.op.exp2.expType._class.registered.type == normalClass ||
7884                               exp.op.exp2.expType._class.registered.type == structClass ||
7885                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
7886                {
7887                   if(exp.op.op == ADD_ASSIGN)
7888                      Compiler_Error($"cannot add two pointers\n");
7889                }
7890                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
7891                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
7892                {
7893                   if(exp.op.op == ADD_ASSIGN)
7894                      Compiler_Error($"cannot add two pointers\n");
7895                }
7896                else if(inCompiler)
7897                {
7898                   char type1String[1024];
7899                   char type2String[1024];
7900                   type1String[0] = '\0';
7901                   type2String[0] = '\0';
7902
7903                   PrintType(exp.op.exp2.expType, type1String, false, true);
7904                   PrintType(type1, type2String, false, true);
7905                   ChangeCh(expString, '\n', ' ');
7906                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
7907                }
7908             }
7909
7910             if(exp.op.exp2.destType == dummy)
7911             {
7912                FreeType(dummy);
7913                exp.op.exp2.destType = null;
7914             }
7915
7916             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
7917             {
7918                type2 = { };
7919                type2.refCount = 1;
7920                CopyTypeInto(type2, exp.op.exp2.expType);
7921                type2.isSigned = true;
7922             }
7923             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
7924             {
7925                type2 = { kind = intType };
7926                type2.refCount = 1;
7927                type2.isSigned = true;
7928             }
7929             else
7930                type2 = exp.op.exp2.expType;
7931          }
7932
7933          dummy.kind = voidType;
7934
7935          if(exp.op.op == SIZEOF)
7936          {
7937             exp.expType = Type
7938             {
7939                refCount = 1;
7940                kind = intType;
7941             };
7942             exp.isConstant = true;
7943          }
7944          // Get type of dereferenced pointer
7945          else if(exp.op.op == '*' && !exp.op.exp1)
7946          {
7947             exp.expType = Dereference(type2);
7948             if(type2 && type2.kind == classType)
7949                notByReference = true;
7950          }
7951          else if(exp.op.op == '&' && !exp.op.exp1)
7952             exp.expType = Reference(type2);
7953          else if(!assign)
7954          {
7955             if(boolOps)
7956             {
7957                if(exp.op.exp1)
7958                {
7959                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7960                   exp.op.exp1.destType = MkClassType("bool");
7961                   exp.op.exp1.destType.truth = true;
7962                   if(!exp.op.exp1.expType)
7963                      ProcessExpressionType(exp.op.exp1);
7964                   else
7965                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
7966                   FreeType(exp.op.exp1.expType);
7967                   exp.op.exp1.expType = MkClassType("bool");
7968                   exp.op.exp1.expType.truth = true;
7969                }
7970                if(exp.op.exp2)
7971                {
7972                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7973                   exp.op.exp2.destType = MkClassType("bool");
7974                   exp.op.exp2.destType.truth = true;
7975                   if(!exp.op.exp2.expType)
7976                      ProcessExpressionType(exp.op.exp2);
7977                   else
7978                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
7979                   FreeType(exp.op.exp2.expType);
7980                   exp.op.exp2.expType = MkClassType("bool");
7981                   exp.op.exp2.expType.truth = true;
7982                }
7983             }
7984             else if(exp.op.exp1 && exp.op.exp2 &&
7985                ((useSideType /*&&
7986                      (useSideUnit ||
7987                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
7988                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
7989                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
7990                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
7991             {
7992                if(type1 && type2 &&
7993                   // If either both are class or both are not class
7994                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
7995                {
7996                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7997                   exp.op.exp2.destType = type1;
7998                   type1.refCount++;
7999                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8000                   exp.op.exp1.destType = type2;
8001                   type2.refCount++;
8002                   // Warning here for adding Radians + Degrees with no destination type
8003                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8004                      type1._class.registered && type1._class.registered.type == unitClass &&
8005                      type2._class.registered && type2._class.registered.type == unitClass &&
8006                      type1._class.registered != type2._class.registered)
8007                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8008                         type1._class.string, type2._class.string, type1._class.string);
8009
8010                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8011                   {
8012                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8013                      if(argExp)
8014                      {
8015                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8016
8017                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8018                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8019                            exp.op.exp1)));
8020
8021                         ProcessExpressionType(exp.op.exp1);
8022
8023                         if(type2.kind != pointerType)
8024                         {
8025                            ProcessExpressionType(classExp);
8026
8027                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8028                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8029                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8030                                  // noHeadClass
8031                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8032                                     OR_OP,
8033                                  // normalClass
8034                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8035                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8036                                        MkPointer(null, null), null)))),
8037                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8038
8039                            if(!exp.op.exp2.expType)
8040                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8041
8042                            ProcessExpressionType(exp.op.exp2);
8043                         }
8044                      }
8045                   }
8046
8047                   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)))
8048                   {
8049                      if(type1.kind != classType && type1.type.kind == voidType)
8050                         Compiler_Error($"void *: unknown size\n");
8051                      exp.expType = type1;
8052                      if(type1) type1.refCount++;
8053                   }
8054                   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)))
8055                   {
8056                      if(type2.kind != classType && type2.type.kind == voidType)
8057                         Compiler_Error($"void *: unknown size\n");
8058                      exp.expType = type2;
8059                      if(type2) type2.refCount++;
8060                   }
8061                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8062                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8063                   {
8064                      Compiler_Warning($"different levels of indirection\n");
8065                   }
8066                   else
8067                   {
8068                      bool success = false;
8069                      if(type1.kind == pointerType && type2.kind == pointerType)
8070                      {
8071                         if(exp.op.op == '+')
8072                            Compiler_Error($"cannot add two pointers\n");
8073                         else if(exp.op.op == '-')
8074                         {
8075                            // Pointer Subtraction gives integer
8076                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8077                            {
8078                               exp.expType = Type
8079                               {
8080                                  kind = intType;
8081                                  refCount = 1;
8082                               };
8083                               success = true;
8084
8085                               if(type1.type.kind == templateType)
8086                               {
8087                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8088                                  if(argExp)
8089                                  {
8090                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8091
8092                                     ProcessExpressionType(classExp);
8093
8094                                     exp.type = bracketsExp;
8095                                     exp.list = MkListOne(MkExpOp(
8096                                        MkExpBrackets(MkListOne(MkExpOp(
8097                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8098                                              , exp.op.op,
8099                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8100
8101                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8102
8103                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8104                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8105                                                 // noHeadClass
8106                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8107                                                    OR_OP,
8108                                                 // normalClass
8109                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8110                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8111                                                       MkPointer(null, null), null)))),
8112                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8113
8114
8115                                              ));
8116
8117                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8118                                     FreeType(dummy);
8119                                     return;
8120                                  }
8121                               }
8122                            }
8123                         }
8124                      }
8125
8126                      if(!success && exp.op.exp1.type == constantExp)
8127                      {
8128                         // If first expression is constant, try to match that first
8129                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8130                         {
8131                            if(exp.expType) FreeType(exp.expType);
8132                            exp.expType = exp.op.exp1.destType;
8133                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8134                            success = true;
8135                         }
8136                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8137                         {
8138                            if(exp.expType) FreeType(exp.expType);
8139                            exp.expType = exp.op.exp2.destType;
8140                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8141                            success = true;
8142                         }
8143                      }
8144                      else if(!success)
8145                      {
8146                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8147                         {
8148                            if(exp.expType) FreeType(exp.expType);
8149                            exp.expType = exp.op.exp2.destType;
8150                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8151                            success = true;
8152                         }
8153                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8154                         {
8155                            if(exp.expType) FreeType(exp.expType);
8156                            exp.expType = exp.op.exp1.destType;
8157                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8158                            success = true;
8159                         }
8160                      }
8161                      if(!success)
8162                      {
8163                         char expString1[10240];
8164                         char expString2[10240];
8165                         char type1[1024];
8166                         char type2[1024];
8167                         expString1[0] = '\0';
8168                         expString2[0] = '\0';
8169                         type1[0] = '\0';
8170                         type2[0] = '\0';
8171                         if(inCompiler)
8172                         {
8173                            PrintExpression(exp.op.exp1, expString1);
8174                            ChangeCh(expString1, '\n', ' ');
8175                            PrintExpression(exp.op.exp2, expString2);
8176                            ChangeCh(expString2, '\n', ' ');
8177                            PrintType(exp.op.exp1.expType, type1, false, true);
8178                            PrintType(exp.op.exp2.expType, type2, false, true);
8179                         }
8180
8181                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8182                      }
8183                   }
8184                }
8185                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8186                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8187                {
8188                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8189                   // Convert e.g. / 4 into / 4.0
8190                   exp.op.exp1.destType = type2._class.registered.dataType;
8191                   if(type2._class.registered.dataType)
8192                      type2._class.registered.dataType.refCount++;
8193                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8194                   exp.expType = type2;
8195                   if(type2) type2.refCount++;
8196                }
8197                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8198                {
8199                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8200                   // Convert e.g. / 4 into / 4.0
8201                   exp.op.exp2.destType = type1._class.registered.dataType;
8202                   if(type1._class.registered.dataType)
8203                      type1._class.registered.dataType.refCount++;
8204                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8205                   exp.expType = type1;
8206                   if(type1) type1.refCount++;
8207                }
8208                else if(type1)
8209                {
8210                   bool valid = false;
8211
8212                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8213                   {
8214                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8215
8216                      if(!type1._class.registered.dataType)
8217                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8218                      exp.op.exp2.destType = type1._class.registered.dataType;
8219                      exp.op.exp2.destType.refCount++;
8220
8221                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8222                      type2 = exp.op.exp2.destType;
8223
8224                      exp.expType = type2;
8225                      type2.refCount++;
8226                   }
8227
8228                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8229                   {
8230                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8231
8232                      if(!type2._class.registered.dataType)
8233                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8234                      exp.op.exp1.destType = type2._class.registered.dataType;
8235                      exp.op.exp1.destType.refCount++;
8236
8237                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8238                      type1 = exp.op.exp1.destType;
8239                      exp.expType = type1;
8240                      type1.refCount++;
8241                   }
8242
8243                   // TESTING THIS NEW CODE
8244                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8245                   {
8246                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8247                      {
8248                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8249                         {
8250                            if(exp.expType) FreeType(exp.expType);
8251                            exp.expType = exp.op.exp1.expType;
8252                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8253                            valid = true;
8254                         }
8255                      }
8256
8257                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8258                      {
8259                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8260                         {
8261                            if(exp.expType) FreeType(exp.expType);
8262                            exp.expType = exp.op.exp2.expType;
8263                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8264                            valid = true;
8265                         }
8266                      }
8267                   }
8268
8269                   if(!valid)
8270                   {
8271                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8272                      exp.op.exp2.destType = type1;
8273                      type1.refCount++;
8274
8275                      /*
8276                      // Maybe this was meant to be an enum...
8277                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8278                      {
8279                         Type oldType = exp.op.exp2.expType;
8280                         exp.op.exp2.expType = null;
8281                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8282                            FreeType(oldType);
8283                         else
8284                            exp.op.exp2.expType = oldType;
8285                      }
8286                      */
8287
8288                      /*
8289                      // TESTING THIS HERE... LATEST ADDITION
8290                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8291                      {
8292                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8293                         exp.op.exp2.destType = type2._class.registered.dataType;
8294                         if(type2._class.registered.dataType)
8295                            type2._class.registered.dataType.refCount++;
8296                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8297
8298                         //exp.expType = type2._class.registered.dataType; //type2;
8299                         //if(type2) type2.refCount++;
8300                      }
8301
8302                      // TESTING THIS HERE... LATEST ADDITION
8303                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8304                      {
8305                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8306                         exp.op.exp1.destType = type1._class.registered.dataType;
8307                         if(type1._class.registered.dataType)
8308                            type1._class.registered.dataType.refCount++;
8309                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8310                         exp.expType = type1._class.registered.dataType; //type1;
8311                         if(type1) type1.refCount++;
8312                      }
8313                      */
8314
8315                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8316                      {
8317                         if(exp.expType) FreeType(exp.expType);
8318                         exp.expType = exp.op.exp2.destType;
8319                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8320                      }
8321                      else if(type1 && type2)
8322                      {
8323                         char expString1[10240];
8324                         char expString2[10240];
8325                         char type1String[1024];
8326                         char type2String[1024];
8327                         expString1[0] = '\0';
8328                         expString2[0] = '\0';
8329                         type1String[0] = '\0';
8330                         type2String[0] = '\0';
8331                         if(inCompiler)
8332                         {
8333                            PrintExpression(exp.op.exp1, expString1);
8334                            ChangeCh(expString1, '\n', ' ');
8335                            PrintExpression(exp.op.exp2, expString2);
8336                            ChangeCh(expString2, '\n', ' ');
8337                            PrintType(exp.op.exp1.expType, type1String, false, true);
8338                            PrintType(exp.op.exp2.expType, type2String, false, true);
8339                         }
8340
8341                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8342
8343                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8344                         {
8345                            exp.expType = exp.op.exp1.expType;
8346                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8347                         }
8348                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8349                         {
8350                            exp.expType = exp.op.exp2.expType;
8351                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8352                         }
8353                      }
8354                   }
8355                }
8356                else if(type2)
8357                {
8358                   // Maybe this was meant to be an enum...
8359                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8360                   {
8361                      Type oldType = exp.op.exp1.expType;
8362                      exp.op.exp1.expType = null;
8363                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8364                         FreeType(oldType);
8365                      else
8366                         exp.op.exp1.expType = oldType;
8367                   }
8368
8369                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8370                   exp.op.exp1.destType = type2;
8371                   type2.refCount++;
8372                   /*
8373                   // TESTING THIS HERE... LATEST ADDITION
8374                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8375                   {
8376                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8377                      exp.op.exp1.destType = type1._class.registered.dataType;
8378                      if(type1._class.registered.dataType)
8379                         type1._class.registered.dataType.refCount++;
8380                   }
8381
8382                   // TESTING THIS HERE... LATEST ADDITION
8383                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8384                   {
8385                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8386                      exp.op.exp2.destType = type2._class.registered.dataType;
8387                      if(type2._class.registered.dataType)
8388                         type2._class.registered.dataType.refCount++;
8389                   }
8390                   */
8391
8392                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8393                   {
8394                      if(exp.expType) FreeType(exp.expType);
8395                      exp.expType = exp.op.exp1.destType;
8396                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8397                   }
8398                }
8399             }
8400             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8401             {
8402                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8403                {
8404                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8405                   // Convert e.g. / 4 into / 4.0
8406                   exp.op.exp1.destType = type2._class.registered.dataType;
8407                   if(type2._class.registered.dataType)
8408                      type2._class.registered.dataType.refCount++;
8409                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8410                }
8411                if(exp.op.op == '!')
8412                {
8413                   exp.expType = MkClassType("bool");
8414                   exp.expType.truth = true;
8415                }
8416                else
8417                {
8418                   exp.expType = type2;
8419                   if(type2) type2.refCount++;
8420                }
8421             }
8422             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8423             {
8424                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8425                {
8426                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8427                   // Convert e.g. / 4 into / 4.0
8428                   exp.op.exp2.destType = type1._class.registered.dataType;
8429                   if(type1._class.registered.dataType)
8430                      type1._class.registered.dataType.refCount++;
8431                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8432                }
8433                exp.expType = type1;
8434                if(type1) type1.refCount++;
8435             }
8436          }
8437
8438          yylloc = exp.loc;
8439          if(exp.op.exp1 && !exp.op.exp1.expType)
8440          {
8441             char expString[10000];
8442             expString[0] = '\0';
8443             if(inCompiler)
8444             {
8445                PrintExpression(exp.op.exp1, expString);
8446                ChangeCh(expString, '\n', ' ');
8447             }
8448             if(expString[0])
8449                Compiler_Error($"couldn't determine type of %s\n", expString);
8450          }
8451          if(exp.op.exp2 && !exp.op.exp2.expType)
8452          {
8453             char expString[10240];
8454             expString[0] = '\0';
8455             if(inCompiler)
8456             {
8457                PrintExpression(exp.op.exp2, expString);
8458                ChangeCh(expString, '\n', ' ');
8459             }
8460             if(expString[0])
8461                Compiler_Error($"couldn't determine type of %s\n", expString);
8462          }
8463
8464          if(boolResult)
8465          {
8466             FreeType(exp.expType);
8467             exp.expType = MkClassType("bool");
8468             exp.expType.truth = true;
8469          }
8470
8471          if(exp.op.op != SIZEOF)
8472             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8473                (!exp.op.exp2 || exp.op.exp2.isConstant);
8474
8475          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8476          {
8477             DeclareType(exp.op.exp2.expType, false, false);
8478          }
8479
8480          yylloc = oldyylloc;
8481
8482          FreeType(dummy);
8483          break;
8484       }
8485       case bracketsExp:
8486       case extensionExpressionExp:
8487       {
8488          Expression e;
8489          exp.isConstant = true;
8490          for(e = exp.list->first; e; e = e.next)
8491          {
8492             bool inced = false;
8493             if(!e.next)
8494             {
8495                FreeType(e.destType);
8496                e.destType = exp.destType;
8497                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8498             }
8499             ProcessExpressionType(e);
8500             if(inced)
8501                exp.destType.count--;
8502             if(!exp.expType && !e.next)
8503             {
8504                exp.expType = e.expType;
8505                if(e.expType) e.expType.refCount++;
8506             }
8507             if(!e.isConstant)
8508                exp.isConstant = false;
8509          }
8510
8511          // In case a cast became a member...
8512          e = exp.list->first;
8513          if(!e.next && e.type == memberExp)
8514          {
8515             // Preserve prev, next
8516             Expression next = exp.next, prev = exp.prev;
8517
8518
8519             FreeType(exp.expType);
8520             FreeType(exp.destType);
8521             delete exp.list;
8522
8523             *exp = *e;
8524
8525             exp.prev = prev;
8526             exp.next = next;
8527
8528             delete e;
8529
8530             ProcessExpressionType(exp);
8531          }
8532          break;
8533       }
8534       case indexExp:
8535       {
8536          Expression e;
8537          exp.isConstant = true;
8538
8539          ProcessExpressionType(exp.index.exp);
8540          if(!exp.index.exp.isConstant)
8541             exp.isConstant = false;
8542
8543          if(exp.index.exp.expType)
8544          {
8545             Type source = exp.index.exp.expType;
8546             if(source.kind == classType && source._class && source._class.registered)
8547             {
8548                Class _class = source._class.registered;
8549                Class c = _class.templateClass ? _class.templateClass : _class;
8550                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
8551                {
8552                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8553
8554                   if(exp.index.index && exp.index.index->last)
8555                   {
8556                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8557                   }
8558                }
8559             }
8560          }
8561
8562          for(e = exp.index.index->first; e; e = e.next)
8563          {
8564             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8565             {
8566                if(e.destType) FreeType(e.destType);
8567                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8568             }
8569             ProcessExpressionType(e);
8570             if(!e.next)
8571             {
8572                // Check if this type is int
8573             }
8574             if(!e.isConstant)
8575                exp.isConstant = false;
8576          }
8577
8578          if(!exp.expType)
8579             exp.expType = Dereference(exp.index.exp.expType);
8580          if(exp.expType)
8581             DeclareType(exp.expType, false, false);
8582          break;
8583       }
8584       case callExp:
8585       {
8586          Expression e;
8587          Type functionType;
8588          Type methodType = null;
8589          char name[1024];
8590          name[0] = '\0';
8591
8592          if(inCompiler)
8593          {
8594             PrintExpression(exp.call.exp,  name);
8595             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8596             {
8597                //exp.call.exp.expType = null;
8598                PrintExpression(exp.call.exp,  name);
8599             }
8600          }
8601          if(exp.call.exp.type == identifierExp)
8602          {
8603             Expression idExp = exp.call.exp;
8604             Identifier id = idExp.identifier;
8605             if(!strcmp(id.string, "__builtin_frame_address"))
8606             {
8607                exp.expType = ProcessTypeString("void *", true);
8608                if(exp.call.arguments && exp.call.arguments->first)
8609                   ProcessExpressionType(exp.call.arguments->first);
8610                break;
8611             }
8612             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8613             {
8614                exp.expType = ProcessTypeString("int", true);
8615                if(exp.call.arguments && exp.call.arguments->first)
8616                   ProcessExpressionType(exp.call.arguments->first);
8617                break;
8618             }
8619             else if(!strcmp(id.string, "Max") ||
8620                !strcmp(id.string, "Min") ||
8621                !strcmp(id.string, "Sgn") ||
8622                !strcmp(id.string, "Abs"))
8623             {
8624                Expression a = null;
8625                Expression b = null;
8626                Expression tempExp1 = null, tempExp2 = null;
8627                if((!strcmp(id.string, "Max") ||
8628                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8629                {
8630                   a = exp.call.arguments->first;
8631                   b = exp.call.arguments->last;
8632                   tempExp1 = a;
8633                   tempExp2 = b;
8634                }
8635                else if(exp.call.arguments->count == 1)
8636                {
8637                   a = exp.call.arguments->first;
8638                   tempExp1 = a;
8639                }
8640
8641                if(a)
8642                {
8643                   exp.call.arguments->Clear();
8644                   idExp.identifier = null;
8645
8646                   FreeExpContents(exp);
8647
8648                   ProcessExpressionType(a);
8649                   if(b)
8650                      ProcessExpressionType(b);
8651
8652                   exp.type = bracketsExp;
8653                   exp.list = MkList();
8654
8655                   if(a.expType && (!b || b.expType))
8656                   {
8657                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
8658                      {
8659                         // Use the simpleStruct name/ids for now...
8660                         if(inCompiler)
8661                         {
8662                            OldList * specs = MkList();
8663                            OldList * decls = MkList();
8664                            Declaration decl;
8665                            char temp1[1024], temp2[1024];
8666
8667                            GetTypeSpecs(a.expType, specs);
8668
8669                            if(a && !a.isConstant && a.type != identifierExp)
8670                            {
8671                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
8672                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
8673                               tempExp1 = QMkExpId(temp1);
8674                               tempExp1.expType = a.expType;
8675                               if(a.expType)
8676                                  a.expType.refCount++;
8677                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
8678                            }
8679                            if(b && !b.isConstant && b.type != identifierExp)
8680                            {
8681                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
8682                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
8683                               tempExp2 = QMkExpId(temp2);
8684                               tempExp2.expType = b.expType;
8685                               if(b.expType)
8686                                  b.expType.refCount++;
8687                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
8688                            }
8689
8690                            decl = MkDeclaration(specs, decls);
8691                            if(!curCompound.compound.declarations)
8692                               curCompound.compound.declarations = MkList();
8693                            curCompound.compound.declarations->Insert(null, decl);
8694                         }
8695                      }
8696                   }
8697
8698                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
8699                   {
8700                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
8701                      ListAdd(exp.list,
8702                         MkExpCondition(MkExpBrackets(MkListOne(
8703                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
8704                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
8705                      exp.expType = a.expType;
8706                      if(a.expType)
8707                         a.expType.refCount++;
8708                   }
8709                   else if(!strcmp(id.string, "Abs"))
8710                   {
8711                      ListAdd(exp.list,
8712                         MkExpCondition(MkExpBrackets(MkListOne(
8713                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8714                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
8715                      exp.expType = a.expType;
8716                      if(a.expType)
8717                         a.expType.refCount++;
8718                   }
8719                   else if(!strcmp(id.string, "Sgn"))
8720                   {
8721                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
8722                      ListAdd(exp.list,
8723                         MkExpCondition(MkExpBrackets(MkListOne(
8724                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
8725                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
8726                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8727                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
8728                      exp.expType = ProcessTypeString("int", false);
8729                   }
8730
8731                   FreeExpression(tempExp1);
8732                   if(tempExp2) FreeExpression(tempExp2);
8733
8734                   FreeIdentifier(id);
8735                   break;
8736                }
8737             }
8738          }
8739
8740          {
8741             Type dummy
8742             {
8743                count = 1;
8744                refCount = 1;
8745             };
8746             if(!exp.call.exp.destType)
8747             {
8748                exp.call.exp.destType = dummy;
8749                dummy.refCount++;
8750             }
8751             ProcessExpressionType(exp.call.exp);
8752             if(exp.call.exp.destType == dummy)
8753             {
8754                FreeType(dummy);
8755                exp.call.exp.destType = null;
8756             }
8757             FreeType(dummy);
8758          }
8759
8760          // Check argument types against parameter types
8761          functionType = exp.call.exp.expType;
8762
8763          if(functionType && functionType.kind == TypeKind::methodType)
8764          {
8765             methodType = functionType;
8766             functionType = methodType.method.dataType;
8767
8768             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
8769             // TOCHECK: Instead of doing this here could this be done per param?
8770             if(exp.call.exp.expType.usedClass)
8771             {
8772                char typeString[1024];
8773                typeString[0] = '\0';
8774                {
8775                   Symbol back = functionType.thisClass;
8776                   // Do not output class specifier here (thisclass was added to this)
8777                   functionType.thisClass = null;
8778                   PrintType(functionType, typeString, true, true);
8779                   functionType.thisClass = back;
8780                }
8781                if(strstr(typeString, "thisclass"))
8782                {
8783                   OldList * specs = MkList();
8784                   Declarator decl;
8785                   {
8786                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
8787
8788                      decl = SpecDeclFromString(typeString, specs, null);
8789
8790                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
8791                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
8792                         exp.call.exp.expType.usedClass))
8793                         thisClassParams = false;
8794
8795                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
8796                      {
8797                         Class backupThisClass = thisClass;
8798                         thisClass = exp.call.exp.expType.usedClass;
8799                         ProcessDeclarator(decl);
8800                         thisClass = backupThisClass;
8801                      }
8802
8803                      thisClassParams = true;
8804
8805                      functionType = ProcessType(specs, decl);
8806                      functionType.refCount = 0;
8807                      FinishTemplatesContext(context);
8808                   }
8809
8810                   FreeList(specs, FreeSpecifier);
8811                   FreeDeclarator(decl);
8812                 }
8813             }
8814          }
8815          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
8816          {
8817             Type type = functionType.type;
8818             if(!functionType.refCount)
8819             {
8820                functionType.type = null;
8821                FreeType(functionType);
8822             }
8823             //methodType = functionType;
8824             functionType = type;
8825          }
8826          if(functionType && functionType.kind != TypeKind::functionType)
8827          {
8828             Compiler_Error($"called object %s is not a function\n", name);
8829          }
8830          else if(functionType)
8831          {
8832             bool emptyParams = false, noParams = false;
8833             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
8834             Type type = functionType.params.first;
8835             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
8836             int extra = 0;
8837             Location oldyylloc = yylloc;
8838
8839             if(!type) emptyParams = true;
8840
8841             // WORKING ON THIS:
8842             if(functionType.extraParam && e && functionType.thisClass)
8843             {
8844                e.destType = MkClassType(functionType.thisClass.string);
8845                e = e.next;
8846             }
8847
8848             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
8849             // Fixed #141 by adding '&& !functionType.extraParam'
8850             if(!functionType.staticMethod && !functionType.extraParam)
8851             {
8852                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
8853                   memberExp.member.exp.expType._class)
8854                {
8855                   type = MkClassType(memberExp.member.exp.expType._class.string);
8856                   if(e)
8857                   {
8858                      e.destType = type;
8859                      e = e.next;
8860                      type = functionType.params.first;
8861                   }
8862                   else
8863                      type.refCount = 0;
8864                }
8865                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
8866                {
8867                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
8868                   type.byReference = functionType.byReference;
8869                   type.typedByReference = functionType.typedByReference;
8870                   if(e)
8871                   {
8872                      // Allow manually passing a class for typed object
8873                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
8874                         e = e.next;
8875                      e.destType = type;
8876                      e = e.next;
8877                      type = functionType.params.first;
8878                   }
8879                   else
8880                      type.refCount = 0;
8881                   //extra = 1;
8882                }
8883             }
8884
8885             if(type && type.kind == voidType)
8886             {
8887                noParams = true;
8888                if(!type.refCount) FreeType(type);
8889                type = null;
8890             }
8891
8892             for( ; e; e = e.next)
8893             {
8894                if(!type && !emptyParams)
8895                {
8896                   yylloc = e.loc;
8897                   if(methodType && methodType.methodClass)
8898                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
8899                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
8900                         noParams ? 0 : functionType.params.count);
8901                   else
8902                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
8903                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
8904                         noParams ? 0 : functionType.params.count);
8905                   break;
8906                }
8907
8908                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
8909                {
8910                   Type templatedType = null;
8911                   Class _class = methodType.usedClass;
8912                   ClassTemplateParameter curParam = null;
8913                   int id = 0;
8914                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
8915                   {
8916                      Class sClass;
8917                      for(sClass = _class; sClass; sClass = sClass.base)
8918                      {
8919                         if(sClass.templateClass) sClass = sClass.templateClass;
8920                         id = 0;
8921                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
8922                         {
8923                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
8924                            {
8925                               Class nextClass;
8926                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
8927                               {
8928                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
8929                                  id += nextClass.templateParams.count;
8930                               }
8931                               break;
8932                            }
8933                            id++;
8934                         }
8935                         if(curParam) break;
8936                      }
8937                   }
8938                   if(curParam && _class.templateArgs[id].dataTypeString)
8939                   {
8940                      ClassTemplateArgument arg = _class.templateArgs[id];
8941                      {
8942                         Context context = SetupTemplatesContext(_class);
8943
8944                         /*if(!arg.dataType)
8945                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
8946                         templatedType = ProcessTypeString(arg.dataTypeString, false);
8947                         FinishTemplatesContext(context);
8948                      }
8949                      e.destType = templatedType;
8950                      if(templatedType)
8951                      {
8952                         templatedType.passAsTemplate = true;
8953                         // templatedType.refCount++;
8954                      }
8955                   }
8956                   else
8957                   {
8958                      e.destType = type;
8959                      if(type) type.refCount++;
8960                   }
8961                }
8962                else
8963                {
8964                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
8965                   {
8966                      e.destType = type.prev;
8967                      e.destType.refCount++;
8968                   }
8969                   else
8970                   {
8971                      e.destType = type;
8972                      if(type) type.refCount++;
8973                   }
8974                }
8975                // Don't reach the end for the ellipsis
8976                if(type && type.kind != ellipsisType)
8977                {
8978                   Type next = type.next;
8979                   if(!type.refCount) FreeType(type);
8980                   type = next;
8981                }
8982             }
8983
8984             if(type && type.kind != ellipsisType)
8985             {
8986                if(methodType && methodType.methodClass)
8987                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
8988                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
8989                      functionType.params.count + extra);
8990                else
8991                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
8992                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
8993                      functionType.params.count + extra);
8994             }
8995             yylloc = oldyylloc;
8996             if(type && !type.refCount) FreeType(type);
8997          }
8998          else
8999          {
9000             functionType = Type
9001             {
9002                refCount = 0;
9003                kind = TypeKind::functionType;
9004             };
9005
9006             if(exp.call.exp.type == identifierExp)
9007             {
9008                char * string = exp.call.exp.identifier.string;
9009                if(inCompiler)
9010                {
9011                   Symbol symbol;
9012                   Location oldyylloc = yylloc;
9013
9014                   yylloc = exp.call.exp.identifier.loc;
9015                   if(strstr(string, "__builtin_") == string)
9016                   {
9017                      if(exp.destType)
9018                      {
9019                         functionType.returnType = exp.destType;
9020                         exp.destType.refCount++;
9021                      }
9022                   }
9023                   else
9024                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9025                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9026                   globalContext.symbols.Add((BTNode)symbol);
9027                   if(strstr(symbol.string, "::"))
9028                      globalContext.hasNameSpace = true;
9029
9030                   yylloc = oldyylloc;
9031                }
9032             }
9033             else if(exp.call.exp.type == memberExp)
9034             {
9035                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9036                   exp.call.exp.member.member.string);*/
9037             }
9038             else
9039                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9040
9041             if(!functionType.returnType)
9042             {
9043                functionType.returnType = Type
9044                {
9045                   refCount = 1;
9046                   kind = intType;
9047                };
9048             }
9049          }
9050          if(functionType && functionType.kind == TypeKind::functionType)
9051          {
9052             exp.expType = functionType.returnType;
9053
9054             if(functionType.returnType)
9055                functionType.returnType.refCount++;
9056
9057             if(!functionType.refCount)
9058                FreeType(functionType);
9059          }
9060
9061          if(exp.call.arguments)
9062          {
9063             for(e = exp.call.arguments->first; e; e = e.next)
9064             {
9065                Type destType = e.destType;
9066                ProcessExpressionType(e);
9067             }
9068          }
9069          break;
9070       }
9071       case memberExp:
9072       {
9073          Type type;
9074          Location oldyylloc = yylloc;
9075          bool thisPtr;
9076          Expression checkExp = exp.member.exp;
9077          while(checkExp)
9078          {
9079             if(checkExp.type == castExp)
9080                checkExp = checkExp.cast.exp;
9081             else if(checkExp.type == bracketsExp)
9082                checkExp = checkExp.list ? checkExp.list->first : null;
9083             else
9084                break;
9085          }
9086
9087          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9088          exp.thisPtr = thisPtr;
9089
9090          // DOING THIS LATER NOW...
9091          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9092          {
9093             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9094             /* TODO: Name Space Fix ups
9095             if(!exp.member.member.classSym)
9096                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9097             */
9098          }
9099
9100          ProcessExpressionType(exp.member.exp);
9101          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9102             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9103          {
9104             exp.isConstant = false;
9105          }
9106          else
9107             exp.isConstant = exp.member.exp.isConstant;
9108          type = exp.member.exp.expType;
9109
9110          yylloc = exp.loc;
9111
9112          if(type && (type.kind == templateType))
9113          {
9114             Class _class = thisClass ? thisClass : currentClass;
9115             ClassTemplateParameter param = null;
9116             if(_class)
9117             {
9118                for(param = _class.templateParams.first; param; param = param.next)
9119                {
9120                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9121                      break;
9122                }
9123             }
9124             if(param && param.defaultArg.member)
9125             {
9126                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9127                if(argExp)
9128                {
9129                   Expression expMember = exp.member.exp;
9130                   Declarator decl;
9131                   OldList * specs = MkList();
9132                   char thisClassTypeString[1024];
9133
9134                   FreeIdentifier(exp.member.member);
9135
9136                   ProcessExpressionType(argExp);
9137
9138                   {
9139                      char * colon = strstr(param.defaultArg.memberString, "::");
9140                      if(colon)
9141                      {
9142                         char className[1024];
9143                         Class sClass;
9144
9145                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9146                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9147                      }
9148                      else
9149                         strcpy(thisClassTypeString, _class.fullName);
9150                   }
9151
9152                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9153
9154                   exp.expType = ProcessType(specs, decl);
9155                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9156                   {
9157                      Class expClass = exp.expType._class.registered;
9158                      Class cClass = null;
9159                      int c;
9160                      int paramCount = 0;
9161                      int lastParam = -1;
9162
9163                      char templateString[1024];
9164                      ClassTemplateParameter param;
9165                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9166                      for(cClass = expClass; cClass; cClass = cClass.base)
9167                      {
9168                         int p = 0;
9169                         for(param = cClass.templateParams.first; param; param = param.next)
9170                         {
9171                            int id = p;
9172                            Class sClass;
9173                            ClassTemplateArgument arg;
9174                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9175                            arg = expClass.templateArgs[id];
9176
9177                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9178                            {
9179                               ClassTemplateParameter cParam;
9180                               //int p = numParams - sClass.templateParams.count;
9181                               int p = 0;
9182                               Class nextClass;
9183                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9184
9185                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9186                               {
9187                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9188                                  {
9189                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9190                                     {
9191                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9192                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9193                                        break;
9194                                     }
9195                                  }
9196                               }
9197                            }
9198
9199                            {
9200                               char argument[256];
9201                               argument[0] = '\0';
9202                               /*if(arg.name)
9203                               {
9204                                  strcat(argument, arg.name.string);
9205                                  strcat(argument, " = ");
9206                               }*/
9207                               switch(param.type)
9208                               {
9209                                  case expression:
9210                                  {
9211                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9212                                     char expString[1024];
9213                                     OldList * specs = MkList();
9214                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9215                                     Expression exp;
9216                                     char * string = PrintHexUInt64(arg.expression.ui64);
9217                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9218
9219                                     ProcessExpressionType(exp);
9220                                     ComputeExpression(exp);
9221                                     expString[0] = '\0';
9222                                     PrintExpression(exp, expString);
9223                                     strcat(argument, expString);
9224                                     // delete exp;
9225                                     FreeExpression(exp);
9226                                     break;
9227                                  }
9228                                  case identifier:
9229                                  {
9230                                     strcat(argument, arg.member.name);
9231                                     break;
9232                                  }
9233                                  case TemplateParameterType::type:
9234                                  {
9235                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9236                                     {
9237                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9238                                           strcat(argument, thisClassTypeString);
9239                                        else
9240                                           strcat(argument, arg.dataTypeString);
9241                                     }
9242                                     break;
9243                                  }
9244                               }
9245                               if(argument[0])
9246                               {
9247                                  if(paramCount) strcat(templateString, ", ");
9248                                  if(lastParam != p - 1)
9249                                  {
9250                                     strcat(templateString, param.name);
9251                                     strcat(templateString, " = ");
9252                                  }
9253                                  strcat(templateString, argument);
9254                                  paramCount++;
9255                                  lastParam = p;
9256                               }
9257                               p++;
9258                            }
9259                         }
9260                      }
9261                      {
9262                         int len = strlen(templateString);
9263                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9264                         templateString[len++] = '>';
9265                         templateString[len++] = '\0';
9266                      }
9267                      {
9268                         Context context = SetupTemplatesContext(_class);
9269                         FreeType(exp.expType);
9270                         exp.expType = ProcessTypeString(templateString, false);
9271                         FinishTemplatesContext(context);
9272                      }
9273                   }
9274
9275                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9276                   exp.type = bracketsExp;
9277                   exp.list = MkListOne(MkExpOp(null, '*',
9278                   /*opExp;
9279                   exp.op.op = '*';
9280                   exp.op.exp1 = null;
9281                   exp.op.exp2 = */
9282                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9283                      MkExpBrackets(MkListOne(
9284                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9285                            '+',
9286                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9287                            '+',
9288                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9289
9290                            ));
9291                }
9292             }
9293             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9294                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9295             {
9296                type = ProcessTemplateParameterType(type.templateParameter);
9297             }
9298          }
9299          // TODO: *** This seems to be where we should add method support for all basic types ***
9300          if(type && (type.kind == templateType));
9301          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9302                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType ||
9303                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9304                           (type.kind == pointerType && type.type.kind == charType)))
9305          {
9306             Identifier id = exp.member.member;
9307             TypeKind typeKind = type.kind;
9308             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9309             if(typeKind == subClassType && exp.member.exp.type == classExp)
9310             {
9311                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9312                typeKind = classType;
9313             }
9314
9315             if(id)
9316             {
9317                if(typeKind == intType || typeKind == enumType)
9318                   _class = eSystem_FindClass(privateModule, "int");
9319                else if(!_class)
9320                {
9321                   if(type.kind == classType && type._class && type._class.registered)
9322                   {
9323                      _class = type._class.registered;
9324                   }
9325                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9326                   {
9327                      _class = FindClass("char *").registered;
9328                   }
9329                   else if(type.kind == pointerType)
9330                   {
9331                      _class = eSystem_FindClass(privateModule, "uintptr");
9332                      FreeType(exp.expType);
9333                      exp.expType = ProcessTypeString("uintptr", false);
9334                      exp.byReference = true;
9335                   }
9336                   else
9337                   {
9338                      char string[1024] = "";
9339                      Symbol classSym;
9340                      PrintTypeNoConst(type, string, false, true);
9341                      classSym = FindClass(string);
9342                      if(classSym) _class = classSym.registered;
9343                   }
9344                }
9345             }
9346
9347             if(_class && id)
9348             {
9349                /*bool thisPtr =
9350                   (exp.member.exp.type == identifierExp &&
9351                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9352                Property prop = null;
9353                Method method = null;
9354                DataMember member = null;
9355                Property revConvert = null;
9356                ClassProperty classProp = null;
9357
9358                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9359                   exp.member.memberType = propertyMember;
9360
9361                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9362                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9363
9364                if(typeKind != subClassType)
9365                {
9366                   // Prioritize data members over properties for "this"
9367                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9368                   {
9369                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9370                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9371                      {
9372                         prop = eClass_FindProperty(_class, id.string, privateModule);
9373                         if(prop)
9374                            member = null;
9375                      }
9376                      if(!member && !prop)
9377                         prop = eClass_FindProperty(_class, id.string, privateModule);
9378                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9379                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9380                         exp.member.thisPtr = true;
9381                   }
9382                   // Prioritize properties over data members otherwise
9383                   else
9384                   {
9385                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9386                      if(!id.classSym)
9387                      {
9388                         prop = eClass_FindProperty(_class, id.string, null);
9389                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9390                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9391                      }
9392
9393                      if(!prop && !member)
9394                      {
9395                         method = eClass_FindMethod(_class, id.string, null);
9396                         if(!method)
9397                         {
9398                            prop = eClass_FindProperty(_class, id.string, privateModule);
9399                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9400                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9401                         }
9402                      }
9403
9404                      if(member && prop)
9405                      {
9406                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9407                            prop = null;
9408                         else
9409                            member = null;
9410                      }
9411                   }
9412                }
9413                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9414                   method = eClass_FindMethod(_class, id.string, privateModule);
9415                if(!prop && !member && !method)
9416                {
9417                   if(typeKind == subClassType)
9418                   {
9419                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9420                      if(classProp)
9421                      {
9422                         exp.member.memberType = classPropertyMember;
9423                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9424                      }
9425                      else
9426                      {
9427                         // Assume this is a class_data member
9428                         char structName[1024];
9429                         Identifier id = exp.member.member;
9430                         Expression classExp = exp.member.exp;
9431                         type.refCount++;
9432
9433                         FreeType(classExp.expType);
9434                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9435
9436                         strcpy(structName, "__ecereClassData_");
9437                         FullClassNameCat(structName, type._class.string, false);
9438                         exp.type = pointerExp;
9439                         exp.member.member = id;
9440
9441                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9442                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9443                               MkExpBrackets(MkListOne(MkExpOp(
9444                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9445                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9446                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9447                                  )));
9448
9449                         FreeType(type);
9450
9451                         ProcessExpressionType(exp);
9452                         return;
9453                      }
9454                   }
9455                   else
9456                   {
9457                      // Check for reverse conversion
9458                      // (Convert in an instantiation later, so that we can use
9459                      //  deep properties system)
9460                      Symbol classSym = FindClass(id.string);
9461                      if(classSym)
9462                      {
9463                         Class convertClass = classSym.registered;
9464                         if(convertClass)
9465                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9466                      }
9467                   }
9468                }
9469
9470                if(prop)
9471                {
9472                   exp.member.memberType = propertyMember;
9473                   if(!prop.dataType)
9474                      ProcessPropertyType(prop);
9475                   exp.expType = prop.dataType;
9476                   if(prop.dataType) prop.dataType.refCount++;
9477                }
9478                else if(member)
9479                {
9480                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9481                   {
9482                      FreeExpContents(exp);
9483                      exp.type = identifierExp;
9484                      exp.identifier = MkIdentifier("class");
9485                      ProcessExpressionType(exp);
9486                      return;
9487                   }
9488
9489                   exp.member.memberType = dataMember;
9490                   DeclareStruct(_class.fullName, false);
9491                   if(!member.dataType)
9492                   {
9493                      Context context = SetupTemplatesContext(_class);
9494                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9495                      FinishTemplatesContext(context);
9496                   }
9497                   exp.expType = member.dataType;
9498                   if(member.dataType) member.dataType.refCount++;
9499                }
9500                else if(revConvert)
9501                {
9502                   exp.member.memberType = reverseConversionMember;
9503                   exp.expType = MkClassType(revConvert._class.fullName);
9504                }
9505                else if(method)
9506                {
9507                   //if(inCompiler)
9508                   {
9509                      /*if(id._class)
9510                      {
9511                         exp.type = identifierExp;
9512                         exp.identifier = exp.member.member;
9513                      }
9514                      else*/
9515                         exp.member.memberType = methodMember;
9516                   }
9517                   if(!method.dataType)
9518                      ProcessMethodType(method);
9519                   exp.expType = Type
9520                   {
9521                      refCount = 1;
9522                      kind = methodType;
9523                      method = method;
9524                   };
9525
9526                   // Tricky spot here... To use instance versus class virtual table
9527                   // Put it back to what it was... What did we break?
9528
9529                   // Had to put it back for overriding Main of Thread global instance
9530
9531                   //exp.expType.methodClass = _class;
9532                   exp.expType.methodClass = (id && id._class) ? _class : null;
9533
9534                   // Need the actual class used for templated classes
9535                   exp.expType.usedClass = _class;
9536                }
9537                else if(!classProp)
9538                {
9539                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9540                   {
9541                      FreeExpContents(exp);
9542                      exp.type = identifierExp;
9543                      exp.identifier = MkIdentifier("class");
9544                      ProcessExpressionType(exp);
9545                      return;
9546                   }
9547                   yylloc = exp.member.member.loc;
9548                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9549                   if(inCompiler)
9550                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9551                }
9552
9553                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9554                {
9555                   Class tClass;
9556
9557                   tClass = _class;
9558                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9559
9560                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9561                   {
9562                      int id = 0;
9563                      ClassTemplateParameter curParam = null;
9564                      Class sClass;
9565
9566                      for(sClass = tClass; sClass; sClass = sClass.base)
9567                      {
9568                         id = 0;
9569                         if(sClass.templateClass) sClass = sClass.templateClass;
9570                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9571                         {
9572                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9573                            {
9574                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9575                                  id += sClass.templateParams.count;
9576                               break;
9577                            }
9578                            id++;
9579                         }
9580                         if(curParam) break;
9581                      }
9582
9583                      if(curParam && tClass.templateArgs[id].dataTypeString)
9584                      {
9585                         ClassTemplateArgument arg = tClass.templateArgs[id];
9586                         Context context = SetupTemplatesContext(tClass);
9587                         /*if(!arg.dataType)
9588                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9589                         FreeType(exp.expType);
9590                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9591                         if(exp.expType)
9592                         {
9593                            if(exp.expType.kind == thisClassType)
9594                            {
9595                               FreeType(exp.expType);
9596                               exp.expType = ReplaceThisClassType(_class);
9597                            }
9598
9599                            if(tClass.templateClass)
9600                               exp.expType.passAsTemplate = true;
9601                            //exp.expType.refCount++;
9602                            if(!exp.destType)
9603                            {
9604                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9605                               //exp.destType.refCount++;
9606
9607                               if(exp.destType.kind == thisClassType)
9608                               {
9609                                  FreeType(exp.destType);
9610                                  exp.destType = ReplaceThisClassType(_class);
9611                               }
9612                            }
9613                         }
9614                         FinishTemplatesContext(context);
9615                      }
9616                   }
9617                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9618                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9619                   {
9620                      int id = 0;
9621                      ClassTemplateParameter curParam = null;
9622                      Class sClass;
9623
9624                      for(sClass = tClass; sClass; sClass = sClass.base)
9625                      {
9626                         id = 0;
9627                         if(sClass.templateClass) sClass = sClass.templateClass;
9628                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9629                         {
9630                            if(curParam.type == TemplateParameterType::type &&
9631                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9632                            {
9633                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9634                                  id += sClass.templateParams.count;
9635                               break;
9636                            }
9637                            id++;
9638                         }
9639                         if(curParam) break;
9640                      }
9641
9642                      if(curParam)
9643                      {
9644                         ClassTemplateArgument arg = tClass.templateArgs[id];
9645                         Context context = SetupTemplatesContext(tClass);
9646                         Type basicType;
9647                         /*if(!arg.dataType)
9648                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9649
9650                         basicType = ProcessTypeString(arg.dataTypeString, false);
9651                         if(basicType)
9652                         {
9653                            if(basicType.kind == thisClassType)
9654                            {
9655                               FreeType(basicType);
9656                               basicType = ReplaceThisClassType(_class);
9657                            }
9658
9659                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
9660                            if(tClass.templateClass)
9661                               basicType.passAsTemplate = true;
9662                            */
9663
9664                            FreeType(exp.expType);
9665
9666                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
9667                            //exp.expType.refCount++;
9668                            if(!exp.destType)
9669                            {
9670                               exp.destType = exp.expType;
9671                               exp.destType.refCount++;
9672                            }
9673
9674                            {
9675                               Expression newExp { };
9676                               OldList * specs = MkList();
9677                               Declarator decl;
9678                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
9679                               *newExp = *exp;
9680                               if(exp.destType) exp.destType.refCount++;
9681                               if(exp.expType)  exp.expType.refCount++;
9682                               exp.type = castExp;
9683                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
9684                               exp.cast.exp = newExp;
9685                               //FreeType(exp.expType);
9686                               //exp.expType = null;
9687                               //ProcessExpressionType(sourceExp);
9688                            }
9689                         }
9690                         FinishTemplatesContext(context);
9691                      }
9692                   }
9693                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
9694                   {
9695                      Class expClass = exp.expType._class.registered;
9696                      if(expClass)
9697                      {
9698                         Class cClass = null;
9699                         int c;
9700                         int p = 0;
9701                         int paramCount = 0;
9702                         int lastParam = -1;
9703                         char templateString[1024];
9704                         ClassTemplateParameter param;
9705                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
9706                         while(cClass != expClass)
9707                         {
9708                            Class sClass;
9709                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
9710                            cClass = sClass;
9711
9712                            for(param = cClass.templateParams.first; param; param = param.next)
9713                            {
9714                               Class cClassCur = null;
9715                               int c;
9716                               int cp = 0;
9717                               ClassTemplateParameter paramCur = null;
9718                               ClassTemplateArgument arg;
9719                               while(cClassCur != tClass && !paramCur)
9720                               {
9721                                  Class sClassCur;
9722                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
9723                                  cClassCur = sClassCur;
9724
9725                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
9726                                  {
9727                                     if(!strcmp(paramCur.name, param.name))
9728                                     {
9729
9730                                        break;
9731                                     }
9732                                     cp++;
9733                                  }
9734                               }
9735                               if(paramCur && paramCur.type == TemplateParameterType::type)
9736                                  arg = tClass.templateArgs[cp];
9737                               else
9738                                  arg = expClass.templateArgs[p];
9739
9740                               {
9741                                  char argument[256];
9742                                  argument[0] = '\0';
9743                                  /*if(arg.name)
9744                                  {
9745                                     strcat(argument, arg.name.string);
9746                                     strcat(argument, " = ");
9747                                  }*/
9748                                  switch(param.type)
9749                                  {
9750                                     case expression:
9751                                     {
9752                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9753                                        char expString[1024];
9754                                        OldList * specs = MkList();
9755                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9756                                        Expression exp;
9757                                        char * string = PrintHexUInt64(arg.expression.ui64);
9758                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9759
9760                                        ProcessExpressionType(exp);
9761                                        ComputeExpression(exp);
9762                                        expString[0] = '\0';
9763                                        PrintExpression(exp, expString);
9764                                        strcat(argument, expString);
9765                                        // delete exp;
9766                                        FreeExpression(exp);
9767                                        break;
9768                                     }
9769                                     case identifier:
9770                                     {
9771                                        strcat(argument, arg.member.name);
9772                                        break;
9773                                     }
9774                                     case TemplateParameterType::type:
9775                                     {
9776                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9777                                           strcat(argument, arg.dataTypeString);
9778                                        break;
9779                                     }
9780                                  }
9781                                  if(argument[0])
9782                                  {
9783                                     if(paramCount) strcat(templateString, ", ");
9784                                     if(lastParam != p - 1)
9785                                     {
9786                                        strcat(templateString, param.name);
9787                                        strcat(templateString, " = ");
9788                                     }
9789                                     strcat(templateString, argument);
9790                                     paramCount++;
9791                                     lastParam = p;
9792                                  }
9793                               }
9794                               p++;
9795                            }
9796                         }
9797                         {
9798                            int len = strlen(templateString);
9799                            if(templateString[len-1] == '>') templateString[len++] = ' ';
9800                            templateString[len++] = '>';
9801                            templateString[len++] = '\0';
9802                         }
9803
9804                         FreeType(exp.expType);
9805                         {
9806                            Context context = SetupTemplatesContext(tClass);
9807                            exp.expType = ProcessTypeString(templateString, false);
9808                            FinishTemplatesContext(context);
9809                         }
9810                      }
9811                   }
9812                }
9813             }
9814             else
9815                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
9816          }
9817          else if(type && (type.kind == structType || type.kind == unionType))
9818          {
9819             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
9820             if(memberType)
9821             {
9822                exp.expType = memberType;
9823                if(memberType)
9824                   memberType.refCount++;
9825             }
9826          }
9827          else
9828          {
9829             char expString[10240];
9830             expString[0] = '\0';
9831             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
9832             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
9833          }
9834
9835          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
9836          {
9837             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
9838             {
9839                Identifier id = exp.member.member;
9840                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9841                if(_class)
9842                {
9843                   FreeType(exp.expType);
9844                   exp.expType = ReplaceThisClassType(_class);
9845                }
9846             }
9847          }
9848          yylloc = oldyylloc;
9849          break;
9850       }
9851       // Convert x->y into (*x).y
9852       case pointerExp:
9853       {
9854          Type destType = exp.destType;
9855
9856          // DOING THIS LATER NOW...
9857          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9858          {
9859             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9860             /* TODO: Name Space Fix ups
9861             if(!exp.member.member.classSym)
9862                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
9863             */
9864          }
9865
9866          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
9867          exp.type = memberExp;
9868          if(destType)
9869             destType.count++;
9870          ProcessExpressionType(exp);
9871          if(destType)
9872             destType.count--;
9873          break;
9874       }
9875       case classSizeExp:
9876       {
9877          //ComputeExpression(exp);
9878
9879          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
9880          if(classSym && classSym.registered)
9881          {
9882             if(classSym.registered.type == noHeadClass)
9883             {
9884                char name[1024];
9885                name[0] = '\0';
9886                DeclareStruct(classSym.string, false);
9887                FreeSpecifier(exp._class);
9888                exp.type = typeSizeExp;
9889                FullClassNameCat(name, classSym.string, false);
9890                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
9891             }
9892             else
9893             {
9894                if(classSym.registered.fixed)
9895                {
9896                   FreeSpecifier(exp._class);
9897                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
9898                   exp.type = constantExp;
9899                }
9900                else
9901                {
9902                   char className[1024];
9903                   strcpy(className, "__ecereClass_");
9904                   FullClassNameCat(className, classSym.string, true);
9905                   MangleClassName(className);
9906
9907                   DeclareClass(classSym, className);
9908
9909                   FreeExpContents(exp);
9910                   exp.type = pointerExp;
9911                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
9912                   exp.member.member = MkIdentifier("structSize");
9913                }
9914             }
9915          }
9916
9917          exp.expType = Type
9918          {
9919             refCount = 1;
9920             kind = intType;
9921          };
9922          // exp.isConstant = true;
9923          break;
9924       }
9925       case typeSizeExp:
9926       {
9927          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
9928
9929          exp.expType = Type
9930          {
9931             refCount = 1;
9932             kind = intType;
9933          };
9934          exp.isConstant = true;
9935
9936          DeclareType(type, false, false);
9937          FreeType(type);
9938          break;
9939       }
9940       case castExp:
9941       {
9942          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
9943          type.count = 1;
9944          FreeType(exp.cast.exp.destType);
9945          exp.cast.exp.destType = type;
9946          type.refCount++;
9947          ProcessExpressionType(exp.cast.exp);
9948          type.count = 0;
9949          exp.expType = type;
9950          //type.refCount++;
9951
9952          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
9953          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
9954          {
9955             void * prev = exp.prev, * next = exp.next;
9956             Type expType = exp.cast.exp.destType;
9957             Expression castExp = exp.cast.exp;
9958             Type destType = exp.destType;
9959
9960             if(expType) expType.refCount++;
9961
9962             //FreeType(exp.destType);
9963             FreeType(exp.expType);
9964             FreeTypeName(exp.cast.typeName);
9965
9966             *exp = *castExp;
9967             FreeType(exp.expType);
9968             FreeType(exp.destType);
9969
9970             exp.expType = expType;
9971             exp.destType = destType;
9972
9973             delete castExp;
9974
9975             exp.prev = prev;
9976             exp.next = next;
9977
9978          }
9979          else
9980          {
9981             exp.isConstant = exp.cast.exp.isConstant;
9982          }
9983          //FreeType(type);
9984          break;
9985       }
9986       case extensionInitializerExp:
9987       {
9988          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
9989          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
9990          // ProcessInitializer(exp.initializer.initializer, type);
9991          exp.expType = type;
9992          break;
9993       }
9994       case vaArgExp:
9995       {
9996          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
9997          ProcessExpressionType(exp.vaArg.exp);
9998          exp.expType = type;
9999          break;
10000       }
10001       case conditionExp:
10002       {
10003          Expression e;
10004          exp.isConstant = true;
10005
10006          FreeType(exp.cond.cond.destType);
10007          exp.cond.cond.destType = MkClassType("bool");
10008          exp.cond.cond.destType.truth = true;
10009          ProcessExpressionType(exp.cond.cond);
10010          if(!exp.cond.cond.isConstant)
10011             exp.isConstant = false;
10012          for(e = exp.cond.exp->first; e; e = e.next)
10013          {
10014             if(!e.next)
10015             {
10016                FreeType(e.destType);
10017                e.destType = exp.destType;
10018                if(e.destType) e.destType.refCount++;
10019             }
10020             ProcessExpressionType(e);
10021             if(!e.next)
10022             {
10023                exp.expType = e.expType;
10024                if(e.expType) e.expType.refCount++;
10025             }
10026             if(!e.isConstant)
10027                exp.isConstant = false;
10028          }
10029
10030          FreeType(exp.cond.elseExp.destType);
10031          // Added this check if we failed to find an expType
10032          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10033
10034          // Reversed it...
10035          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10036
10037          if(exp.cond.elseExp.destType)
10038             exp.cond.elseExp.destType.refCount++;
10039          ProcessExpressionType(exp.cond.elseExp);
10040
10041          // FIXED THIS: Was done before calling process on elseExp
10042          if(!exp.cond.elseExp.isConstant)
10043             exp.isConstant = false;
10044          break;
10045       }
10046       case extensionCompoundExp:
10047       {
10048          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10049          {
10050             Statement last = exp.compound.compound.statements->last;
10051             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10052             {
10053                ((Expression)last.expressions->last).destType = exp.destType;
10054                if(exp.destType)
10055                   exp.destType.refCount++;
10056             }
10057             ProcessStatement(exp.compound);
10058             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10059             if(exp.expType)
10060                exp.expType.refCount++;
10061          }
10062          break;
10063       }
10064       case classExp:
10065       {
10066          Specifier spec = exp._classExp.specifiers->first;
10067          if(spec && spec.type == nameSpecifier)
10068          {
10069             exp.expType = MkClassType(spec.name);
10070             exp.expType.kind = subClassType;
10071             exp.byReference = true;
10072          }
10073          else
10074          {
10075             exp.expType = MkClassType("ecere::com::Class");
10076             exp.byReference = true;
10077          }
10078          break;
10079       }
10080       case classDataExp:
10081       {
10082          Class _class = thisClass ? thisClass : currentClass;
10083          if(_class)
10084          {
10085             Identifier id = exp.classData.id;
10086             char structName[1024];
10087             Expression classExp;
10088             strcpy(structName, "__ecereClassData_");
10089             FullClassNameCat(structName, _class.fullName, false);
10090             exp.type = pointerExp;
10091             exp.member.member = id;
10092             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10093                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10094             else
10095                classExp = MkExpIdentifier(MkIdentifier("class"));
10096
10097             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10098                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10099                   MkExpBrackets(MkListOne(MkExpOp(
10100                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10101                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10102                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10103                      )));
10104
10105             ProcessExpressionType(exp);
10106             return;
10107          }
10108          break;
10109       }
10110       case arrayExp:
10111       {
10112          Type type = null;
10113          char * typeString = null;
10114          char typeStringBuf[1024];
10115          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10116             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10117          {
10118             Class templateClass = exp.destType._class.registered;
10119             typeString = templateClass.templateArgs[2].dataTypeString;
10120          }
10121          else if(exp.list)
10122          {
10123             // Guess type from expressions in the array
10124             Expression e;
10125             for(e = exp.list->first; e; e = e.next)
10126             {
10127                ProcessExpressionType(e);
10128                if(e.expType)
10129                {
10130                   if(!type) { type = e.expType; type.refCount++; }
10131                   else
10132                   {
10133                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10134                      if(!MatchTypeExpression(e, type, null, false))
10135                      {
10136                         FreeType(type);
10137                         type = e.expType;
10138                         e.expType = null;
10139
10140                         e = exp.list->first;
10141                         ProcessExpressionType(e);
10142                         if(e.expType)
10143                         {
10144                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10145                            if(!MatchTypeExpression(e, type, null, false))
10146                            {
10147                               FreeType(e.expType);
10148                               e.expType = null;
10149                               FreeType(type);
10150                               type = null;
10151                               break;
10152                            }
10153                         }
10154                      }
10155                   }
10156                   if(e.expType)
10157                   {
10158                      FreeType(e.expType);
10159                      e.expType = null;
10160                   }
10161                }
10162             }
10163             if(type)
10164             {
10165                typeStringBuf[0] = '\0';
10166                PrintTypeNoConst(type, typeStringBuf, false, true);
10167                typeString = typeStringBuf;
10168                FreeType(type);
10169                type = null;
10170             }
10171          }
10172          if(typeString)
10173          {
10174             /*
10175             (Container)& (struct BuiltInContainer)
10176             {
10177                ._vTbl = class(BuiltInContainer)._vTbl,
10178                ._class = class(BuiltInContainer),
10179                .refCount = 0,
10180                .data = (int[]){ 1, 7, 3, 4, 5 },
10181                .count = 5,
10182                .type = class(int),
10183             }
10184             */
10185             char templateString[1024];
10186             OldList * initializers = MkList();
10187             OldList * structInitializers = MkList();
10188             OldList * specs = MkList();
10189             Expression expExt;
10190             Declarator decl = SpecDeclFromString(typeString, specs, null);
10191             sprintf(templateString, "Container<%s>", typeString);
10192
10193             if(exp.list)
10194             {
10195                Expression e;
10196                type = ProcessTypeString(typeString, false);
10197                while(e = exp.list->first)
10198                {
10199                   exp.list->Remove(e);
10200                   e.destType = type;
10201                   type.refCount++;
10202                   ProcessExpressionType(e);
10203                   ListAdd(initializers, MkInitializerAssignment(e));
10204                }
10205                FreeType(type);
10206                delete exp.list;
10207             }
10208
10209             DeclareStruct("ecere::com::BuiltInContainer", false);
10210
10211             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10212                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10213             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10214                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10215             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10216                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10217             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10218                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10219                MkInitializerList(initializers))));
10220                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10221             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10222                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10223             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10224                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10225             exp.expType = ProcessTypeString(templateString, false);
10226             exp.type = bracketsExp;
10227             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10228                MkExpOp(null, '&',
10229                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10230                   MkInitializerList(structInitializers)))));
10231             ProcessExpressionType(expExt);
10232          }
10233          else
10234          {
10235             exp.expType = ProcessTypeString("Container", false);
10236             Compiler_Error($"Couldn't determine type of array elements\n");
10237          }
10238          break;
10239       }
10240    }
10241
10242    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10243    {
10244       FreeType(exp.expType);
10245       exp.expType = ReplaceThisClassType(thisClass);
10246    }
10247
10248    // Resolve structures here
10249    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10250    {
10251       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10252       // TODO: Fix members reference...
10253       if(symbol)
10254       {
10255          if(exp.expType.kind != enumType)
10256          {
10257             Type member;
10258             String enumName = CopyString(exp.expType.enumName);
10259
10260             // Fixed a memory leak on self-referencing C structs typedefs
10261             // by instantiating a new type rather than simply copying members
10262             // into exp.expType
10263             FreeType(exp.expType);
10264             exp.expType = Type { };
10265             exp.expType.kind = symbol.type.kind;
10266             exp.expType.refCount++;
10267             exp.expType.enumName = enumName;
10268
10269             exp.expType.members = symbol.type.members;
10270             for(member = symbol.type.members.first; member; member = member.next)
10271                member.refCount++;
10272          }
10273          else
10274          {
10275             NamedLink member;
10276             for(member = symbol.type.members.first; member; member = member.next)
10277             {
10278                NamedLink value { name = CopyString(member.name) };
10279                exp.expType.members.Add(value);
10280             }
10281          }
10282       }
10283    }
10284
10285    yylloc = exp.loc;
10286    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10287    else if(exp.destType && !exp.destType.keepCast)
10288    {
10289       if(!CheckExpressionType(exp, exp.destType, false))
10290       {
10291          if(!exp.destType.count || unresolved)
10292          {
10293             if(!exp.expType)
10294             {
10295                yylloc = exp.loc;
10296                if(exp.destType.kind != ellipsisType)
10297                {
10298                   char type2[1024];
10299                   type2[0] = '\0';
10300                   if(inCompiler)
10301                   {
10302                      char expString[10240];
10303                      expString[0] = '\0';
10304
10305                      PrintType(exp.destType, type2, false, true);
10306
10307                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10308                      if(unresolved)
10309                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10310                      else if(exp.type != dummyExp)
10311                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10312                   }
10313                }
10314                else
10315                {
10316                   char expString[10240] ;
10317                   expString[0] = '\0';
10318                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10319
10320                   if(unresolved)
10321                      Compiler_Error($"unresolved identifier %s\n", expString);
10322                   else if(exp.type != dummyExp)
10323                      Compiler_Error($"couldn't determine type of %s\n", expString);
10324                }
10325             }
10326             else
10327             {
10328                char type1[1024];
10329                char type2[1024];
10330                type1[0] = '\0';
10331                type2[0] = '\0';
10332                if(inCompiler)
10333                {
10334                   PrintType(exp.expType, type1, false, true);
10335                   PrintType(exp.destType, type2, false, true);
10336                }
10337
10338                //CheckExpressionType(exp, exp.destType, false);
10339
10340                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10341                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10342                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10343                else
10344                {
10345                   char expString[10240];
10346                   expString[0] = '\0';
10347                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10348
10349 #ifdef _DEBUG
10350                   CheckExpressionType(exp, exp.destType, false);
10351 #endif
10352                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10353                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10354                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10355
10356                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10357                   FreeType(exp.expType);
10358                   exp.destType.refCount++;
10359                   exp.expType = exp.destType;
10360                }
10361             }
10362          }
10363       }
10364       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10365       {
10366          Expression newExp { };
10367          char typeString[1024];
10368          OldList * specs = MkList();
10369          Declarator decl;
10370
10371          typeString[0] = '\0';
10372
10373          *newExp = *exp;
10374
10375          if(exp.expType)  exp.expType.refCount++;
10376          if(exp.expType)  exp.expType.refCount++;
10377          exp.type = castExp;
10378          newExp.destType = exp.expType;
10379
10380          PrintType(exp.expType, typeString, false, false);
10381          decl = SpecDeclFromString(typeString, specs, null);
10382
10383          exp.cast.typeName = MkTypeName(specs, decl);
10384          exp.cast.exp = newExp;
10385       }
10386    }
10387    else if(unresolved)
10388    {
10389       if(exp.identifier._class && exp.identifier._class.name)
10390          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10391       else if(exp.identifier.string && exp.identifier.string[0])
10392          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10393    }
10394    else if(!exp.expType && exp.type != dummyExp)
10395    {
10396       char expString[10240];
10397       expString[0] = '\0';
10398       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10399       Compiler_Error($"couldn't determine type of %s\n", expString);
10400    }
10401
10402    // Let's try to support any_object & typed_object here:
10403    if(inCompiler)
10404       ApplyAnyObjectLogic(exp);
10405
10406    // Mark nohead classes as by reference, unless we're casting them to an integral type
10407    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10408       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10409          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10410           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType)))
10411    {
10412       exp.byReference = true;
10413    }
10414    yylloc = oldyylloc;
10415 }
10416
10417 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10418 {
10419    // THIS CODE WILL FIND NEXT MEMBER...
10420    if(*curMember)
10421    {
10422       *curMember = (*curMember).next;
10423
10424       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10425       {
10426          *curMember = subMemberStack[--(*subMemberStackPos)];
10427          *curMember = (*curMember).next;
10428       }
10429
10430       // SKIP ALL PROPERTIES HERE...
10431       while((*curMember) && (*curMember).isProperty)
10432          *curMember = (*curMember).next;
10433
10434       if(subMemberStackPos)
10435       {
10436          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10437          {
10438             subMemberStack[(*subMemberStackPos)++] = *curMember;
10439
10440             *curMember = (*curMember).members.first;
10441             while(*curMember && (*curMember).isProperty)
10442                *curMember = (*curMember).next;
10443          }
10444       }
10445    }
10446    while(!*curMember)
10447    {
10448       if(!*curMember)
10449       {
10450          if(subMemberStackPos && *subMemberStackPos)
10451          {
10452             *curMember = subMemberStack[--(*subMemberStackPos)];
10453             *curMember = (*curMember).next;
10454          }
10455          else
10456          {
10457             Class lastCurClass = *curClass;
10458
10459             if(*curClass == _class) break;     // REACHED THE END
10460
10461             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10462             *curMember = (*curClass).membersAndProperties.first;
10463          }
10464
10465          while((*curMember) && (*curMember).isProperty)
10466             *curMember = (*curMember).next;
10467          if(subMemberStackPos)
10468          {
10469             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10470             {
10471                subMemberStack[(*subMemberStackPos)++] = *curMember;
10472
10473                *curMember = (*curMember).members.first;
10474                while(*curMember && (*curMember).isProperty)
10475                   *curMember = (*curMember).next;
10476             }
10477          }
10478       }
10479    }
10480 }
10481
10482
10483 static void ProcessInitializer(Initializer init, Type type)
10484 {
10485    switch(init.type)
10486    {
10487       case expInitializer:
10488          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10489          {
10490             // TESTING THIS FOR SHUTTING = 0 WARNING
10491             if(init.exp && !init.exp.destType)
10492             {
10493                FreeType(init.exp.destType);
10494                init.exp.destType = type;
10495                if(type) type.refCount++;
10496             }
10497             if(init.exp)
10498             {
10499                ProcessExpressionType(init.exp);
10500                init.isConstant = init.exp.isConstant;
10501             }
10502             break;
10503          }
10504          else
10505          {
10506             Expression exp = init.exp;
10507             Instantiation inst = exp.instance;
10508             MembersInit members;
10509
10510             init.type = listInitializer;
10511             init.list = MkList();
10512
10513             if(inst.members)
10514             {
10515                for(members = inst.members->first; members; members = members.next)
10516                {
10517                   if(members.type == dataMembersInit)
10518                   {
10519                      MemberInit member;
10520                      for(member = members.dataMembers->first; member; member = member.next)
10521                      {
10522                         ListAdd(init.list, member.initializer);
10523                         member.initializer = null;
10524                      }
10525                   }
10526                   // Discard all MembersInitMethod
10527                }
10528             }
10529             FreeExpression(exp);
10530          }
10531       case listInitializer:
10532       {
10533          Initializer i;
10534          Type initializerType = null;
10535          Class curClass = null;
10536          DataMember curMember = null;
10537          DataMember subMemberStack[256];
10538          int subMemberStackPos = 0;
10539
10540          if(type && type.kind == arrayType)
10541             initializerType = Dereference(type);
10542          else if(type && (type.kind == structType || type.kind == unionType))
10543             initializerType = type.members.first;
10544
10545          for(i = init.list->first; i; i = i.next)
10546          {
10547             if(type && type.kind == classType && type._class && type._class.registered)
10548             {
10549                // 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)
10550                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10551                // TODO: Generate error on initializing a private data member this way from another module...
10552                if(curMember)
10553                {
10554                   if(!curMember.dataType)
10555                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10556                   initializerType = curMember.dataType;
10557                }
10558             }
10559             ProcessInitializer(i, initializerType);
10560             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10561                initializerType = initializerType.next;
10562             if(!i.isConstant)
10563                init.isConstant = false;
10564          }
10565
10566          if(type && type.kind == arrayType)
10567             FreeType(initializerType);
10568
10569          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10570          {
10571             Compiler_Error($"Assigning list initializer to non list\n");
10572          }
10573          break;
10574       }
10575    }
10576 }
10577
10578 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10579 {
10580    switch(spec.type)
10581    {
10582       case baseSpecifier:
10583       {
10584          if(spec.specifier == THISCLASS)
10585          {
10586             if(thisClass)
10587             {
10588                spec.type = nameSpecifier;
10589                spec.name = ReplaceThisClass(thisClass);
10590                spec.symbol = FindClass(spec.name);
10591                ProcessSpecifier(spec, declareStruct);
10592             }
10593          }
10594          break;
10595       }
10596       case nameSpecifier:
10597       {
10598          Symbol symbol = FindType(curContext, spec.name);
10599          if(symbol)
10600             DeclareType(symbol.type, true, true);
10601          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10602             DeclareStruct(spec.name, false);
10603          break;
10604       }
10605       case enumSpecifier:
10606       {
10607          Enumerator e;
10608          if(spec.list)
10609          {
10610             for(e = spec.list->first; e; e = e.next)
10611             {
10612                if(e.exp)
10613                   ProcessExpressionType(e.exp);
10614             }
10615          }
10616          break;
10617       }
10618       case structSpecifier:
10619       case unionSpecifier:
10620       {
10621          if(spec.definitions)
10622          {
10623             ClassDef def;
10624             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10625             //if(symbol)
10626                ProcessClass(spec.definitions, symbol);
10627             /*else
10628             {
10629                for(def = spec.definitions->first; def; def = def.next)
10630                {
10631                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10632                      ProcessDeclaration(def.decl);
10633                }
10634             }*/
10635          }
10636          break;
10637       }
10638       /*
10639       case classSpecifier:
10640       {
10641          Symbol classSym = FindClass(spec.name);
10642          if(classSym && classSym.registered && classSym.registered.type == structClass)
10643             DeclareStruct(spec.name, false);
10644          break;
10645       }
10646       */
10647    }
10648 }
10649
10650
10651 static void ProcessDeclarator(Declarator decl)
10652 {
10653    switch(decl.type)
10654    {
10655       case identifierDeclarator:
10656          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
10657          {
10658             FreeSpecifier(decl.identifier._class);
10659             decl.identifier._class = null;
10660          }
10661          break;
10662       case arrayDeclarator:
10663          if(decl.array.exp)
10664             ProcessExpressionType(decl.array.exp);
10665       case structDeclarator:
10666       case bracketsDeclarator:
10667       case functionDeclarator:
10668       case pointerDeclarator:
10669       case extendedDeclarator:
10670       case extendedDeclaratorEnd:
10671          if(decl.declarator)
10672             ProcessDeclarator(decl.declarator);
10673          if(decl.type == functionDeclarator)
10674          {
10675             Identifier id = GetDeclId(decl);
10676             if(id && id._class)
10677             {
10678                TypeName param
10679                {
10680                   qualifiers = MkListOne(id._class);
10681                   declarator = null;
10682                };
10683                if(!decl.function.parameters)
10684                   decl.function.parameters = MkList();
10685                decl.function.parameters->Insert(null, param);
10686                id._class = null;
10687             }
10688             if(decl.function.parameters)
10689             {
10690                TypeName param;
10691
10692                for(param = decl.function.parameters->first; param; param = param.next)
10693                {
10694                   if(param.qualifiers && param.qualifiers->first)
10695                   {
10696                      Specifier spec = param.qualifiers->first;
10697                      if(spec && spec.specifier == TYPED_OBJECT)
10698                      {
10699                         Declarator d = param.declarator;
10700                         TypeName newParam
10701                         {
10702                            qualifiers = MkListOne(MkSpecifier(VOID));
10703                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10704                         };
10705
10706                         FreeList(param.qualifiers, FreeSpecifier);
10707
10708                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
10709                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
10710
10711                         decl.function.parameters->Insert(param, newParam);
10712                         param = newParam;
10713                      }
10714                      else if(spec && spec.specifier == ANY_OBJECT)
10715                      {
10716                         Declarator d = param.declarator;
10717
10718                         FreeList(param.qualifiers, FreeSpecifier);
10719
10720                         param.qualifiers = MkListOne(MkSpecifier(VOID));
10721                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10722                      }
10723                      else if(spec.specifier == THISCLASS)
10724                      {
10725                         if(thisClass)
10726                         {
10727                            spec.type = nameSpecifier;
10728                            spec.name = ReplaceThisClass(thisClass);
10729                            spec.symbol = FindClass(spec.name);
10730                            ProcessSpecifier(spec, false);
10731                         }
10732                      }
10733                   }
10734
10735                   if(param.declarator)
10736                      ProcessDeclarator(param.declarator);
10737                }
10738             }
10739          }
10740          break;
10741    }
10742 }
10743
10744 static void ProcessDeclaration(Declaration decl)
10745 {
10746    yylloc = decl.loc;
10747    switch(decl.type)
10748    {
10749       case initDeclaration:
10750       {
10751          bool declareStruct = false;
10752          /*
10753          lineNum = decl.pos.line;
10754          column = decl.pos.col;
10755          */
10756
10757          if(decl.declarators)
10758          {
10759             InitDeclarator d;
10760
10761             for(d = decl.declarators->first; d; d = d.next)
10762             {
10763                Type type, subType;
10764                ProcessDeclarator(d.declarator);
10765
10766                type = ProcessType(decl.specifiers, d.declarator);
10767
10768                if(d.initializer)
10769                {
10770                   ProcessInitializer(d.initializer, type);
10771
10772                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
10773
10774                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
10775                      d.initializer.exp.type == instanceExp)
10776                   {
10777                      if(type.kind == classType && type._class ==
10778                         d.initializer.exp.expType._class)
10779                      {
10780                         Instantiation inst = d.initializer.exp.instance;
10781                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
10782
10783                         d.initializer.exp.instance = null;
10784                         if(decl.specifiers)
10785                            FreeList(decl.specifiers, FreeSpecifier);
10786                         FreeList(decl.declarators, FreeInitDeclarator);
10787
10788                         d = null;
10789
10790                         decl.type = instDeclaration;
10791                         decl.inst = inst;
10792                      }
10793                   }
10794                }
10795                for(subType = type; subType;)
10796                {
10797                   if(subType.kind == classType)
10798                   {
10799                      declareStruct = true;
10800                      break;
10801                   }
10802                   else if(subType.kind == pointerType)
10803                      break;
10804                   else if(subType.kind == arrayType)
10805                      subType = subType.arrayType;
10806                   else
10807                      break;
10808                }
10809
10810                FreeType(type);
10811                if(!d) break;
10812             }
10813          }
10814
10815          if(decl.specifiers)
10816          {
10817             Specifier s;
10818             for(s = decl.specifiers->first; s; s = s.next)
10819             {
10820                ProcessSpecifier(s, declareStruct);
10821             }
10822          }
10823          break;
10824       }
10825       case instDeclaration:
10826       {
10827          ProcessInstantiationType(decl.inst);
10828          break;
10829       }
10830       case structDeclaration:
10831       {
10832          Specifier spec;
10833          Declarator d;
10834          bool declareStruct = false;
10835
10836          if(decl.declarators)
10837          {
10838             for(d = decl.declarators->first; d; d = d.next)
10839             {
10840                Type type = ProcessType(decl.specifiers, d.declarator);
10841                Type subType;
10842                ProcessDeclarator(d);
10843                for(subType = type; subType;)
10844                {
10845                   if(subType.kind == classType)
10846                   {
10847                      declareStruct = true;
10848                      break;
10849                   }
10850                   else if(subType.kind == pointerType)
10851                      break;
10852                   else if(subType.kind == arrayType)
10853                      subType = subType.arrayType;
10854                   else
10855                      break;
10856                }
10857                FreeType(type);
10858             }
10859          }
10860          if(decl.specifiers)
10861          {
10862             for(spec = decl.specifiers->first; spec; spec = spec.next)
10863                ProcessSpecifier(spec, declareStruct);
10864          }
10865          break;
10866       }
10867    }
10868 }
10869
10870 static FunctionDefinition curFunction;
10871
10872 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
10873 {
10874    char propName[1024], propNameM[1024];
10875    char getName[1024], setName[1024];
10876    OldList * args;
10877
10878    DeclareProperty(prop, setName, getName);
10879
10880    // eInstance_FireWatchers(object, prop);
10881    strcpy(propName, "__ecereProp_");
10882    FullClassNameCat(propName, prop._class.fullName, false);
10883    strcat(propName, "_");
10884    // strcat(propName, prop.name);
10885    FullClassNameCat(propName, prop.name, true);
10886    MangleClassName(propName);
10887
10888    strcpy(propNameM, "__ecerePropM_");
10889    FullClassNameCat(propNameM, prop._class.fullName, false);
10890    strcat(propNameM, "_");
10891    // strcat(propNameM, prop.name);
10892    FullClassNameCat(propNameM, prop.name, true);
10893    MangleClassName(propNameM);
10894
10895    if(prop.isWatchable)
10896    {
10897       args = MkList();
10898       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10899       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10900       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10901
10902       args = MkList();
10903       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10904       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10905       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10906    }
10907
10908
10909    {
10910       args = MkList();
10911       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10912       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10913       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10914
10915       args = MkList();
10916       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10917       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10918       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10919    }
10920
10921    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
10922       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
10923       curFunction.propSet.fireWatchersDone = true;
10924 }
10925
10926 static void ProcessStatement(Statement stmt)
10927 {
10928    yylloc = stmt.loc;
10929    /*
10930    lineNum = stmt.pos.line;
10931    column = stmt.pos.col;
10932    */
10933    switch(stmt.type)
10934    {
10935       case labeledStmt:
10936          ProcessStatement(stmt.labeled.stmt);
10937          break;
10938       case caseStmt:
10939          // This expression should be constant...
10940          if(stmt.caseStmt.exp)
10941          {
10942             FreeType(stmt.caseStmt.exp.destType);
10943             stmt.caseStmt.exp.destType = curSwitchType;
10944             if(curSwitchType) curSwitchType.refCount++;
10945             ProcessExpressionType(stmt.caseStmt.exp);
10946             ComputeExpression(stmt.caseStmt.exp);
10947          }
10948          if(stmt.caseStmt.stmt)
10949             ProcessStatement(stmt.caseStmt.stmt);
10950          break;
10951       case compoundStmt:
10952       {
10953          if(stmt.compound.context)
10954          {
10955             Declaration decl;
10956             Statement s;
10957
10958             Statement prevCompound = curCompound;
10959             Context prevContext = curContext;
10960
10961             if(!stmt.compound.isSwitch)
10962             {
10963                curCompound = stmt;
10964                curContext = stmt.compound.context;
10965             }
10966
10967             if(stmt.compound.declarations)
10968             {
10969                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
10970                   ProcessDeclaration(decl);
10971             }
10972             if(stmt.compound.statements)
10973             {
10974                for(s = stmt.compound.statements->first; s; s = s.next)
10975                   ProcessStatement(s);
10976             }
10977
10978             curContext = prevContext;
10979             curCompound = prevCompound;
10980          }
10981          break;
10982       }
10983       case expressionStmt:
10984       {
10985          Expression exp;
10986          if(stmt.expressions)
10987          {
10988             for(exp = stmt.expressions->first; exp; exp = exp.next)
10989                ProcessExpressionType(exp);
10990          }
10991          break;
10992       }
10993       case ifStmt:
10994       {
10995          Expression exp;
10996
10997          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
10998          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
10999          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11000          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11001          {
11002             ProcessExpressionType(exp);
11003          }
11004          if(stmt.ifStmt.stmt)
11005             ProcessStatement(stmt.ifStmt.stmt);
11006          if(stmt.ifStmt.elseStmt)
11007             ProcessStatement(stmt.ifStmt.elseStmt);
11008          break;
11009       }
11010       case switchStmt:
11011       {
11012          Type oldSwitchType = curSwitchType;
11013          if(stmt.switchStmt.exp)
11014          {
11015             Expression exp;
11016             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11017             {
11018                if(!exp.next)
11019                {
11020                   /*
11021                   Type destType
11022                   {
11023                      kind = intType;
11024                      refCount = 1;
11025                   };
11026                   e.exp.destType = destType;
11027                   */
11028
11029                   ProcessExpressionType(exp);
11030                }
11031                if(!exp.next)
11032                   curSwitchType = exp.expType;
11033             }
11034          }
11035          ProcessStatement(stmt.switchStmt.stmt);
11036          curSwitchType = oldSwitchType;
11037          break;
11038       }
11039       case whileStmt:
11040       {
11041          if(stmt.whileStmt.exp)
11042          {
11043             Expression exp;
11044
11045             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11046             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11047             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11048             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11049             {
11050                ProcessExpressionType(exp);
11051             }
11052          }
11053          if(stmt.whileStmt.stmt)
11054             ProcessStatement(stmt.whileStmt.stmt);
11055          break;
11056       }
11057       case doWhileStmt:
11058       {
11059          if(stmt.doWhile.exp)
11060          {
11061             Expression exp;
11062
11063             if(stmt.doWhile.exp->last)
11064             {
11065                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11066                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11067                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11068             }
11069             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11070             {
11071                ProcessExpressionType(exp);
11072             }
11073          }
11074          if(stmt.doWhile.stmt)
11075             ProcessStatement(stmt.doWhile.stmt);
11076          break;
11077       }
11078       case forStmt:
11079       {
11080          Expression exp;
11081          if(stmt.forStmt.init)
11082             ProcessStatement(stmt.forStmt.init);
11083
11084          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11085          {
11086             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11087             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11088             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11089          }
11090
11091          if(stmt.forStmt.check)
11092             ProcessStatement(stmt.forStmt.check);
11093          if(stmt.forStmt.increment)
11094          {
11095             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11096                ProcessExpressionType(exp);
11097          }
11098
11099          if(stmt.forStmt.stmt)
11100             ProcessStatement(stmt.forStmt.stmt);
11101          break;
11102       }
11103       case forEachStmt:
11104       {
11105          Identifier id = stmt.forEachStmt.id;
11106          OldList * exp = stmt.forEachStmt.exp;
11107          OldList * filter = stmt.forEachStmt.filter;
11108          Statement block = stmt.forEachStmt.stmt;
11109          char iteratorType[1024];
11110          Type source;
11111          Expression e;
11112          bool isBuiltin = exp && exp->last &&
11113             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11114               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11115          Expression arrayExp;
11116          char * typeString = null;
11117          int builtinCount = 0;
11118
11119          for(e = exp ? exp->first : null; e; e = e.next)
11120          {
11121             if(!e.next)
11122             {
11123                FreeType(e.destType);
11124                e.destType = ProcessTypeString("Container", false);
11125             }
11126             if(!isBuiltin || e.next)
11127                ProcessExpressionType(e);
11128          }
11129
11130          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11131          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11132             eClass_IsDerived(source._class.registered, containerClass)))
11133          {
11134             Class _class = source ? source._class.registered : null;
11135             Symbol symbol;
11136             Expression expIt = null;
11137             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11138             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11139             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11140             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11141             stmt.type = compoundStmt;
11142
11143             stmt.compound.context = Context { };
11144             stmt.compound.context.parent = curContext;
11145             curContext = stmt.compound.context;
11146
11147             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11148             {
11149                Class mapClass = eSystem_FindClass(privateModule, "Map");
11150                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11151                isCustomAVLTree = true;
11152                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11153                   isAVLTree = true;
11154                else if(eClass_IsDerived(source._class.registered, mapClass))
11155                   isMap = true;
11156             }
11157             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11158             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11159             {
11160                Class listClass = eSystem_FindClass(privateModule, "List");
11161                isLinkList = true;
11162                isList = eClass_IsDerived(source._class.registered, listClass);
11163             }
11164
11165             if(isArray)
11166             {
11167                Declarator decl;
11168                OldList * specs = MkList();
11169                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11170                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11171                stmt.compound.declarations = MkListOne(
11172                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11173                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11174                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11175                      MkInitializerAssignment(MkExpBrackets(exp))))));
11176             }
11177             else if(isBuiltin)
11178             {
11179                Type type = null;
11180                char typeStringBuf[1024];
11181
11182                // TODO: Merge this code?
11183                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11184                if(((Expression)exp->last).type == castExp)
11185                {
11186                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11187                   if(typeName)
11188                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11189                }
11190
11191                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11192                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11193                   arrayExp.destType._class.registered.templateArgs)
11194                {
11195                   Class templateClass = arrayExp.destType._class.registered;
11196                   typeString = templateClass.templateArgs[2].dataTypeString;
11197                }
11198                else if(arrayExp.list)
11199                {
11200                   // Guess type from expressions in the array
11201                   Expression e;
11202                   for(e = arrayExp.list->first; e; e = e.next)
11203                   {
11204                      ProcessExpressionType(e);
11205                      if(e.expType)
11206                      {
11207                         if(!type) { type = e.expType; type.refCount++; }
11208                         else
11209                         {
11210                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11211                            if(!MatchTypeExpression(e, type, null, false))
11212                            {
11213                               FreeType(type);
11214                               type = e.expType;
11215                               e.expType = null;
11216
11217                               e = arrayExp.list->first;
11218                               ProcessExpressionType(e);
11219                               if(e.expType)
11220                               {
11221                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11222                                  if(!MatchTypeExpression(e, type, null, false))
11223                                  {
11224                                     FreeType(e.expType);
11225                                     e.expType = null;
11226                                     FreeType(type);
11227                                     type = null;
11228                                     break;
11229                                  }
11230                               }
11231                            }
11232                         }
11233                         if(e.expType)
11234                         {
11235                            FreeType(e.expType);
11236                            e.expType = null;
11237                         }
11238                      }
11239                   }
11240                   if(type)
11241                   {
11242                      typeStringBuf[0] = '\0';
11243                      PrintType(type, typeStringBuf, false, true);
11244                      typeString = typeStringBuf;
11245                      FreeType(type);
11246                   }
11247                }
11248                if(typeString)
11249                {
11250                   OldList * initializers = MkList();
11251                   Declarator decl;
11252                   OldList * specs = MkList();
11253                   if(arrayExp.list)
11254                   {
11255                      Expression e;
11256
11257                      builtinCount = arrayExp.list->count;
11258                      type = ProcessTypeString(typeString, false);
11259                      while(e = arrayExp.list->first)
11260                      {
11261                         arrayExp.list->Remove(e);
11262                         e.destType = type;
11263                         type.refCount++;
11264                         ProcessExpressionType(e);
11265                         ListAdd(initializers, MkInitializerAssignment(e));
11266                      }
11267                      FreeType(type);
11268                      delete arrayExp.list;
11269                   }
11270                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11271                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11272                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11273
11274                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11275                      PlugDeclarator(
11276                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11277                         ), MkInitializerList(initializers)))));
11278                   FreeList(exp, FreeExpression);
11279                }
11280                else
11281                {
11282                   arrayExp.expType = ProcessTypeString("Container", false);
11283                   Compiler_Error($"Couldn't determine type of array elements\n");
11284                }
11285
11286                /*
11287                Declarator decl;
11288                OldList * specs = MkList();
11289
11290                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11291                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11292                stmt.compound.declarations = MkListOne(
11293                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11294                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11295                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11296                      MkInitializerAssignment(MkExpBrackets(exp))))));
11297                */
11298             }
11299             else if(isLinkList && !isList)
11300             {
11301                Declarator decl;
11302                OldList * specs = MkList();
11303                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11304                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11305                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11306                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11307                      MkInitializerAssignment(MkExpBrackets(exp))))));
11308             }
11309             /*else if(isCustomAVLTree)
11310             {
11311                Declarator decl;
11312                OldList * specs = MkList();
11313                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11314                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11315                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11316                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11317                      MkInitializerAssignment(MkExpBrackets(exp))))));
11318             }*/
11319             else if(_class.templateArgs)
11320             {
11321                if(isMap)
11322                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11323                else
11324                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11325
11326                stmt.compound.declarations = MkListOne(
11327                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11328                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11329                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11330             }
11331             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11332
11333             if(block && block.type == compoundStmt && block.compound.context)
11334             {
11335                block.compound.context.parent = stmt.compound.context;
11336             }
11337             if(filter)
11338             {
11339                block = MkIfStmt(filter, block, null);
11340             }
11341             if(isArray)
11342             {
11343                stmt.compound.statements = MkListOne(MkForStmt(
11344                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11345                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11346                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11347                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11348                   block));
11349               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11350               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11351               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11352             }
11353             else if(isBuiltin)
11354             {
11355                char count[128];
11356                //OldList * specs = MkList();
11357                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11358
11359                sprintf(count, "%d", builtinCount);
11360
11361                stmt.compound.statements = MkListOne(MkForStmt(
11362                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11363                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11364                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11365                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11366                   block));
11367
11368                /*
11369                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11370                stmt.compound.statements = MkListOne(MkForStmt(
11371                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11372                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11373                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11374                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11375                   block));
11376               */
11377               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11378               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11379               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11380             }
11381             else if(isLinkList && !isList)
11382             {
11383                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11384                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11385                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11386                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11387                {
11388                   stmt.compound.statements = MkListOne(MkForStmt(
11389                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11390                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11391                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11392                      block));
11393                }
11394                else
11395                {
11396                   OldList * specs = MkList();
11397                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11398                   stmt.compound.statements = MkListOne(MkForStmt(
11399                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11400                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11401                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11402                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11403                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11404                      block));
11405                }
11406                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11407                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11408                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11409             }
11410             /*else if(isCustomAVLTree)
11411             {
11412                stmt.compound.statements = MkListOne(MkForStmt(
11413                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11414                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11415                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11416                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11417                   block));
11418
11419                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11420                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11421                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11422             }*/
11423             else
11424             {
11425                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11426                   MkIdentifier("Next")), null)), block));
11427             }
11428             ProcessExpressionType(expIt);
11429             if(stmt.compound.declarations->first)
11430                ProcessDeclaration(stmt.compound.declarations->first);
11431
11432             if(symbol)
11433                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11434
11435             ProcessStatement(stmt);
11436             curContext = stmt.compound.context.parent;
11437             break;
11438          }
11439          else
11440          {
11441             Compiler_Error($"Expression is not a container\n");
11442          }
11443          break;
11444       }
11445       case gotoStmt:
11446          break;
11447       case continueStmt:
11448          break;
11449       case breakStmt:
11450          break;
11451       case returnStmt:
11452       {
11453          Expression exp;
11454          if(stmt.expressions)
11455          {
11456             for(exp = stmt.expressions->first; exp; exp = exp.next)
11457             {
11458                if(!exp.next)
11459                {
11460                   if(curFunction && !curFunction.type)
11461                      curFunction.type = ProcessType(
11462                         curFunction.specifiers, curFunction.declarator);
11463                   FreeType(exp.destType);
11464                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11465                   if(exp.destType) exp.destType.refCount++;
11466                }
11467                ProcessExpressionType(exp);
11468             }
11469          }
11470          break;
11471       }
11472       case badDeclarationStmt:
11473       {
11474          ProcessDeclaration(stmt.decl);
11475          break;
11476       }
11477       case asmStmt:
11478       {
11479          AsmField field;
11480          if(stmt.asmStmt.inputFields)
11481          {
11482             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11483                if(field.expression)
11484                   ProcessExpressionType(field.expression);
11485          }
11486          if(stmt.asmStmt.outputFields)
11487          {
11488             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11489                if(field.expression)
11490                   ProcessExpressionType(field.expression);
11491          }
11492          if(stmt.asmStmt.clobberedFields)
11493          {
11494             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11495             {
11496                if(field.expression)
11497                   ProcessExpressionType(field.expression);
11498             }
11499          }
11500          break;
11501       }
11502       case watchStmt:
11503       {
11504          PropertyWatch propWatch;
11505          OldList * watches = stmt._watch.watches;
11506          Expression object = stmt._watch.object;
11507          Expression watcher = stmt._watch.watcher;
11508          if(watcher)
11509             ProcessExpressionType(watcher);
11510          if(object)
11511             ProcessExpressionType(object);
11512
11513          if(inCompiler)
11514          {
11515             if(watcher || thisClass)
11516             {
11517                External external = curExternal;
11518                Context context = curContext;
11519
11520                stmt.type = expressionStmt;
11521                stmt.expressions = MkList();
11522
11523                curExternal = external.prev;
11524
11525                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11526                {
11527                   ClassFunction func;
11528                   char watcherName[1024];
11529                   Class watcherClass = watcher ?
11530                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11531                   External createdExternal;
11532
11533                   // Create a declaration above
11534                   External externalDecl = MkExternalDeclaration(null);
11535                   ast->Insert(curExternal.prev, externalDecl);
11536
11537                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11538                   if(propWatch.deleteWatch)
11539                      strcat(watcherName, "_delete");
11540                   else
11541                   {
11542                      Identifier propID;
11543                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11544                      {
11545                         strcat(watcherName, "_");
11546                         strcat(watcherName, propID.string);
11547                      }
11548                   }
11549
11550                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11551                   {
11552                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11553                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11554                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11555                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11556                      ProcessClassFunctionBody(func, propWatch.compound);
11557                      propWatch.compound = null;
11558
11559                      //afterExternal = afterExternal ? afterExternal : curExternal;
11560
11561                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11562                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11563                      // TESTING THIS...
11564                      createdExternal.symbol.idCode = external.symbol.idCode;
11565
11566                      curExternal = createdExternal;
11567                      ProcessFunction(createdExternal.function);
11568
11569
11570                      // Create a declaration above
11571                      {
11572                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
11573                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11574                         externalDecl.declaration = decl;
11575                         if(decl.symbol && !decl.symbol.pointerExternal)
11576                            decl.symbol.pointerExternal = externalDecl;
11577                      }
11578
11579                      if(propWatch.deleteWatch)
11580                      {
11581                         OldList * args = MkList();
11582                         ListAdd(args, CopyExpression(object));
11583                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11584                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11585                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11586                      }
11587                      else
11588                      {
11589                         Class _class = object.expType._class.registered;
11590                         Identifier propID;
11591
11592                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11593                         {
11594                            char propName[1024];
11595                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11596                            if(prop)
11597                            {
11598                               char getName[1024], setName[1024];
11599                               OldList * args = MkList();
11600
11601                               DeclareProperty(prop, setName, getName);
11602
11603                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
11604                               strcpy(propName, "__ecereProp_");
11605                               FullClassNameCat(propName, prop._class.fullName, false);
11606                               strcat(propName, "_");
11607                               // strcat(propName, prop.name);
11608                               FullClassNameCat(propName, prop.name, true);
11609
11610                               ListAdd(args, CopyExpression(object));
11611                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11612                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11613                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11614
11615                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
11616                            }
11617                            else
11618                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11619                         }
11620                      }
11621                   }
11622                   else
11623                      Compiler_Error($"Invalid watched object\n");
11624                }
11625
11626                curExternal = external;
11627                curContext = context;
11628
11629                if(watcher)
11630                   FreeExpression(watcher);
11631                if(object)
11632                   FreeExpression(object);
11633                FreeList(watches, FreePropertyWatch);
11634             }
11635             else
11636                Compiler_Error($"No observer specified and not inside a _class\n");
11637          }
11638          else
11639          {
11640             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11641             {
11642                ProcessStatement(propWatch.compound);
11643             }
11644
11645          }
11646          break;
11647       }
11648       case fireWatchersStmt:
11649       {
11650          OldList * watches = stmt._watch.watches;
11651          Expression object = stmt._watch.object;
11652          Class _class;
11653          // DEBUGGER BUG: Why doesn't watches evaluate to null??
11654          // printf("%X\n", watches);
11655          // printf("%X\n", stmt._watch.watches);
11656          if(object)
11657             ProcessExpressionType(object);
11658
11659          if(inCompiler)
11660          {
11661             _class = object ?
11662                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
11663
11664             if(_class)
11665             {
11666                Identifier propID;
11667
11668                stmt.type = expressionStmt;
11669                stmt.expressions = MkList();
11670
11671                // Check if we're inside a property set
11672                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11673                {
11674                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
11675                }
11676                else if(!watches)
11677                {
11678                   //Compiler_Error($"No property specified and not inside a property set\n");
11679                }
11680                if(watches)
11681                {
11682                   for(propID = watches->first; propID; propID = propID.next)
11683                   {
11684                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11685                      if(prop)
11686                      {
11687                         CreateFireWatcher(prop, object, stmt);
11688                      }
11689                      else
11690                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
11691                   }
11692                }
11693                else
11694                {
11695                   // Fire all properties!
11696                   Property prop;
11697                   Class base;
11698                   for(base = _class; base; base = base.base)
11699                   {
11700                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
11701                      {
11702                         if(prop.isProperty && prop.isWatchable)
11703                         {
11704                            CreateFireWatcher(prop, object, stmt);
11705                         }
11706                      }
11707                   }
11708                }
11709
11710                if(object)
11711                   FreeExpression(object);
11712                FreeList(watches, FreeIdentifier);
11713             }
11714             else
11715                Compiler_Error($"Invalid object specified and not inside a class\n");
11716          }
11717          break;
11718       }
11719       case stopWatchingStmt:
11720       {
11721          OldList * watches = stmt._watch.watches;
11722          Expression object = stmt._watch.object;
11723          Expression watcher = stmt._watch.watcher;
11724          Class _class;
11725          if(object)
11726             ProcessExpressionType(object);
11727          if(watcher)
11728             ProcessExpressionType(watcher);
11729          if(inCompiler)
11730          {
11731             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
11732
11733             if(watcher || thisClass)
11734             {
11735                if(_class)
11736                {
11737                   Identifier propID;
11738
11739                   stmt.type = expressionStmt;
11740                   stmt.expressions = MkList();
11741
11742                   if(!watches)
11743                   {
11744                      OldList * args;
11745                      // eInstance_StopWatching(object, null, watcher);
11746                      args = MkList();
11747                      ListAdd(args, CopyExpression(object));
11748                      ListAdd(args, MkExpConstant("0"));
11749                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11750                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11751                   }
11752                   else
11753                   {
11754                      for(propID = watches->first; propID; propID = propID.next)
11755                      {
11756                         char propName[1024];
11757                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11758                         if(prop)
11759                         {
11760                            char getName[1024], setName[1024];
11761                            OldList * args = MkList();
11762
11763                            DeclareProperty(prop, setName, getName);
11764
11765                            // eInstance_StopWatching(object, prop, watcher);
11766                            strcpy(propName, "__ecereProp_");
11767                            FullClassNameCat(propName, prop._class.fullName, false);
11768                            strcat(propName, "_");
11769                            // strcat(propName, prop.name);
11770                            FullClassNameCat(propName, prop.name, true);
11771                            MangleClassName(propName);
11772
11773                            ListAdd(args, CopyExpression(object));
11774                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11775                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11776                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11777                         }
11778                         else
11779                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11780                      }
11781                   }
11782
11783                   if(object)
11784                      FreeExpression(object);
11785                   if(watcher)
11786                      FreeExpression(watcher);
11787                   FreeList(watches, FreeIdentifier);
11788                }
11789                else
11790                   Compiler_Error($"Invalid object specified and not inside a class\n");
11791             }
11792             else
11793                Compiler_Error($"No observer specified and not inside a class\n");
11794          }
11795          break;
11796       }
11797    }
11798 }
11799
11800 static void ProcessFunction(FunctionDefinition function)
11801 {
11802    Identifier id = GetDeclId(function.declarator);
11803    Symbol symbol = function.declarator ? function.declarator.symbol : null;
11804    Type type = symbol ? symbol.type : null;
11805    Class oldThisClass = thisClass;
11806    Context oldTopContext = topContext;
11807
11808    yylloc = function.loc;
11809    // Process thisClass
11810
11811    if(type && type.thisClass)
11812    {
11813       Symbol classSym = type.thisClass;
11814       Class _class = type.thisClass.registered;
11815       char className[1024];
11816       char structName[1024];
11817       Declarator funcDecl;
11818       Symbol thisSymbol;
11819
11820       bool typedObject = false;
11821
11822       if(_class && !_class.base)
11823       {
11824          _class = currentClass;
11825          if(_class && !_class.symbol)
11826             _class.symbol = FindClass(_class.fullName);
11827          classSym = _class ? _class.symbol : null;
11828          typedObject = true;
11829       }
11830
11831       thisClass = _class;
11832
11833       if(inCompiler && _class)
11834       {
11835          if(type.kind == functionType)
11836          {
11837             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
11838             {
11839                //TypeName param = symbol.type.params.first;
11840                Type param = symbol.type.params.first;
11841                symbol.type.params.Remove(param);
11842                //FreeTypeName(param);
11843                FreeType(param);
11844             }
11845             if(type.classObjectType != classPointer)
11846             {
11847                symbol.type.params.Insert(null, MkClassType(_class.fullName));
11848                symbol.type.staticMethod = true;
11849                symbol.type.thisClass = null;
11850
11851                // HIGH DANGER: VERIFYING THIS...
11852                symbol.type.extraParam = false;
11853             }
11854          }
11855
11856          strcpy(className, "__ecereClass_");
11857          FullClassNameCat(className, _class.fullName, true);
11858
11859          MangleClassName(className);
11860
11861          structName[0] = 0;
11862          FullClassNameCat(structName, _class.fullName, false);
11863
11864          // [class] this
11865
11866
11867          funcDecl = GetFuncDecl(function.declarator);
11868          if(funcDecl)
11869          {
11870             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
11871             {
11872                TypeName param = funcDecl.function.parameters->first;
11873                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
11874                {
11875                   funcDecl.function.parameters->Remove(param);
11876                   FreeTypeName(param);
11877                }
11878             }
11879
11880             // DANGER: Watch for this... Check if it's a Conversion?
11881             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
11882
11883             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
11884             if(!function.propertyNoThis)
11885             {
11886                TypeName thisParam;
11887
11888                if(type.classObjectType != classPointer)
11889                {
11890                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
11891                   if(!funcDecl.function.parameters)
11892                      funcDecl.function.parameters = MkList();
11893                   funcDecl.function.parameters->Insert(null, thisParam);
11894                }
11895
11896                if(typedObject)
11897                {
11898                   if(type.classObjectType != classPointer)
11899                   {
11900                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
11901                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
11902                   }
11903
11904                   thisParam = TypeName
11905                   {
11906                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11907                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11908                   };
11909                   funcDecl.function.parameters->Insert(null, thisParam);
11910                }
11911             }
11912          }
11913
11914          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
11915          {
11916             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
11917             funcDecl = GetFuncDecl(initDecl.declarator);
11918             if(funcDecl)
11919             {
11920                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
11921                {
11922                   TypeName param = funcDecl.function.parameters->first;
11923                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
11924                   {
11925                      funcDecl.function.parameters->Remove(param);
11926                      FreeTypeName(param);
11927                   }
11928                }
11929
11930                if(type.classObjectType != classPointer)
11931                {
11932                   // DANGER: Watch for this... Check if it's a Conversion?
11933                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
11934                   {
11935                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
11936
11937                      if(!funcDecl.function.parameters)
11938                         funcDecl.function.parameters = MkList();
11939                      funcDecl.function.parameters->Insert(null, thisParam);
11940                   }
11941                }
11942             }
11943          }
11944       }
11945
11946       // Add this to the context
11947       if(function.body)
11948       {
11949          if(type.classObjectType != classPointer)
11950          {
11951             thisSymbol = Symbol
11952             {
11953                string = CopyString("this");
11954                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
11955             };
11956             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
11957
11958             if(typedObject && thisSymbol.type)
11959             {
11960                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
11961                thisSymbol.type.byReference = type.byReference;
11962                thisSymbol.type.typedByReference = type.byReference;
11963                /*
11964                thisSymbol = Symbol { string = CopyString("class") };
11965                function.body.compound.context.symbols.Add(thisSymbol);
11966                */
11967             }
11968          }
11969       }
11970
11971       // Pointer to class data
11972
11973       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
11974       {
11975          DataMember member = null;
11976          {
11977             Class base;
11978             for(base = _class; base && base.type != systemClass; base = base.next)
11979             {
11980                for(member = base.membersAndProperties.first; member; member = member.next)
11981                   if(!member.isProperty)
11982                      break;
11983                if(member)
11984                   break;
11985             }
11986          }
11987          for(member = _class.membersAndProperties.first; member; member = member.next)
11988             if(!member.isProperty)
11989                break;
11990          if(member)
11991          {
11992             char pointerName[1024];
11993
11994             Declaration decl;
11995             Initializer initializer;
11996             Expression exp, bytePtr;
11997
11998             strcpy(pointerName, "__ecerePointer_");
11999             FullClassNameCat(pointerName, _class.fullName, false);
12000             {
12001                char className[1024];
12002                strcpy(className, "__ecereClass_");
12003                FullClassNameCat(className, classSym.string, true);
12004                MangleClassName(className);
12005
12006                // Testing This
12007                DeclareClass(classSym, className);
12008             }
12009
12010             // ((byte *) this)
12011             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12012
12013             if(_class.fixed)
12014             {
12015                char string[256];
12016                sprintf(string, "%d", _class.offset);
12017                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12018             }
12019             else
12020             {
12021                // ([bytePtr] + [className]->offset)
12022                exp = QBrackets(MkExpOp(bytePtr, '+',
12023                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12024             }
12025
12026             // (this ? [exp] : 0)
12027             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12028             exp.expType = Type
12029             {
12030                refCount = 1;
12031                kind = pointerType;
12032                type = Type { refCount = 1, kind = voidType };
12033             };
12034
12035             if(function.body)
12036             {
12037                yylloc = function.body.loc;
12038                // ([structName] *) [exp]
12039                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12040                initializer = MkInitializerAssignment(
12041                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12042
12043                // [structName] * [pointerName] = [initializer];
12044                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12045
12046                {
12047                   Context prevContext = curContext;
12048                   curContext = function.body.compound.context;
12049
12050                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12051                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12052
12053                   curContext = prevContext;
12054                }
12055
12056                // WHY?
12057                decl.symbol = null;
12058
12059                if(!function.body.compound.declarations)
12060                   function.body.compound.declarations = MkList();
12061                function.body.compound.declarations->Insert(null, decl);
12062             }
12063          }
12064       }
12065
12066
12067       // Loop through the function and replace undeclared identifiers
12068       // which are a member of the class (methods, properties or data)
12069       // by "this.[member]"
12070    }
12071    else
12072       thisClass = null;
12073
12074    if(id)
12075    {
12076       FreeSpecifier(id._class);
12077       id._class = null;
12078
12079       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12080       {
12081          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12082          id = GetDeclId(initDecl.declarator);
12083
12084          FreeSpecifier(id._class);
12085          id._class = null;
12086       }
12087    }
12088    if(function.body)
12089       topContext = function.body.compound.context;
12090    {
12091       FunctionDefinition oldFunction = curFunction;
12092       curFunction = function;
12093       if(function.body)
12094          ProcessStatement(function.body);
12095
12096       // If this is a property set and no firewatchers has been done yet, add one here
12097       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12098       {
12099          Statement prevCompound = curCompound;
12100          Context prevContext = curContext;
12101
12102          Statement fireWatchers = MkFireWatchersStmt(null, null);
12103          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12104          ListAdd(function.body.compound.statements, fireWatchers);
12105
12106          curCompound = function.body;
12107          curContext = function.body.compound.context;
12108
12109          ProcessStatement(fireWatchers);
12110
12111          curContext = prevContext;
12112          curCompound = prevCompound;
12113
12114       }
12115
12116       curFunction = oldFunction;
12117    }
12118
12119    if(function.declarator)
12120    {
12121       ProcessDeclarator(function.declarator);
12122    }
12123
12124    topContext = oldTopContext;
12125    thisClass = oldThisClass;
12126 }
12127
12128 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12129 static void ProcessClass(OldList definitions, Symbol symbol)
12130 {
12131    ClassDef def;
12132    External external = curExternal;
12133    Class regClass = symbol ? symbol.registered : null;
12134
12135    // Process all functions
12136    for(def = definitions.first; def; def = def.next)
12137    {
12138       if(def.type == functionClassDef)
12139       {
12140          if(def.function.declarator)
12141             curExternal = def.function.declarator.symbol.pointerExternal;
12142          else
12143             curExternal = external;
12144
12145          ProcessFunction((FunctionDefinition)def.function);
12146       }
12147       else if(def.type == declarationClassDef)
12148       {
12149          if(def.decl.type == instDeclaration)
12150          {
12151             thisClass = regClass;
12152             ProcessInstantiationType(def.decl.inst);
12153             thisClass = null;
12154          }
12155          // Testing this
12156          else
12157          {
12158             Class backThisClass = thisClass;
12159             if(regClass) thisClass = regClass;
12160             ProcessDeclaration(def.decl);
12161             thisClass = backThisClass;
12162          }
12163       }
12164       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12165       {
12166          MemberInit defProperty;
12167
12168          // Add this to the context
12169          Symbol thisSymbol = Symbol
12170          {
12171             string = CopyString("this");
12172             type = regClass ? MkClassType(regClass.fullName) : null;
12173          };
12174          globalContext.symbols.Add((BTNode)thisSymbol);
12175
12176          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12177          {
12178             thisClass = regClass;
12179             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12180             thisClass = null;
12181          }
12182
12183          globalContext.symbols.Remove((BTNode)thisSymbol);
12184          FreeSymbol(thisSymbol);
12185       }
12186       else if(def.type == propertyClassDef && def.propertyDef)
12187       {
12188          PropertyDef prop = def.propertyDef;
12189
12190          // Add this to the context
12191          /*
12192          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12193          globalContext.symbols.Add(thisSymbol);
12194          */
12195
12196          thisClass = regClass;
12197          if(prop.setStmt)
12198          {
12199             if(regClass)
12200             {
12201                Symbol thisSymbol
12202                {
12203                   string = CopyString("this");
12204                   type = MkClassType(regClass.fullName);
12205                };
12206                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12207             }
12208
12209             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12210             ProcessStatement(prop.setStmt);
12211          }
12212          if(prop.getStmt)
12213          {
12214             if(regClass)
12215             {
12216                Symbol thisSymbol
12217                {
12218                   string = CopyString("this");
12219                   type = MkClassType(regClass.fullName);
12220                };
12221                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12222             }
12223
12224             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12225             ProcessStatement(prop.getStmt);
12226          }
12227          if(prop.issetStmt)
12228          {
12229             if(regClass)
12230             {
12231                Symbol thisSymbol
12232                {
12233                   string = CopyString("this");
12234                   type = MkClassType(regClass.fullName);
12235                };
12236                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12237             }
12238
12239             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12240             ProcessStatement(prop.issetStmt);
12241          }
12242
12243          thisClass = null;
12244
12245          /*
12246          globalContext.symbols.Remove(thisSymbol);
12247          FreeSymbol(thisSymbol);
12248          */
12249       }
12250       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12251       {
12252          PropertyWatch propertyWatch = def.propertyWatch;
12253
12254          thisClass = regClass;
12255          if(propertyWatch.compound)
12256          {
12257             Symbol thisSymbol
12258             {
12259                string = CopyString("this");
12260                type = regClass ? MkClassType(regClass.fullName) : null;
12261             };
12262
12263             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12264
12265             curExternal = null;
12266             ProcessStatement(propertyWatch.compound);
12267          }
12268          thisClass = null;
12269       }
12270    }
12271 }
12272
12273 void DeclareFunctionUtil(String s)
12274 {
12275    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12276    if(function)
12277    {
12278       char name[1024];
12279       name[0] = 0;
12280       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12281          strcpy(name, "__ecereFunction_");
12282       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12283       DeclareFunction(function, name);
12284    }
12285 }
12286
12287 void ComputeDataTypes()
12288 {
12289    External external;
12290    External temp { };
12291    External after = null;
12292
12293    currentClass = null;
12294
12295    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12296
12297    for(external = ast->first; external; external = external.next)
12298    {
12299       if(external.type == declarationExternal)
12300       {
12301          Declaration decl = external.declaration;
12302          if(decl)
12303          {
12304             OldList * decls = decl.declarators;
12305             if(decls)
12306             {
12307                InitDeclarator initDecl = decls->first;
12308                if(initDecl)
12309                {
12310                   Declarator declarator = initDecl.declarator;
12311                   if(declarator && declarator.type == identifierDeclarator)
12312                   {
12313                      Identifier id = declarator.identifier;
12314                      if(id && id.string)
12315                      {
12316                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12317                         {
12318                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12319                            after = external;
12320                         }
12321                      }
12322                   }
12323                }
12324             }
12325          }
12326        }
12327    }
12328
12329    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12330    ast->Insert(after, temp);
12331    curExternal = temp;
12332
12333    DeclareFunctionUtil("eSystem_New");
12334    DeclareFunctionUtil("eSystem_New0");
12335    DeclareFunctionUtil("eSystem_Renew");
12336    DeclareFunctionUtil("eSystem_Renew0");
12337    DeclareFunctionUtil("eClass_GetProperty");
12338
12339    DeclareStruct("ecere::com::Class", false);
12340    DeclareStruct("ecere::com::Instance", false);
12341    DeclareStruct("ecere::com::Property", false);
12342    DeclareStruct("ecere::com::DataMember", false);
12343    DeclareStruct("ecere::com::Method", false);
12344    DeclareStruct("ecere::com::SerialBuffer", false);
12345    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12346
12347    ast->Remove(temp);
12348
12349    for(external = ast->first; external; external = external.next)
12350    {
12351       afterExternal = curExternal = external;
12352       if(external.type == functionExternal)
12353       {
12354          currentClass = external.function._class;
12355          ProcessFunction(external.function);
12356       }
12357       // There shouldn't be any _class member access here anyways...
12358       else if(external.type == declarationExternal)
12359       {
12360          currentClass = null;
12361          ProcessDeclaration(external.declaration);
12362       }
12363       else if(external.type == classExternal)
12364       {
12365          ClassDefinition _class = external._class;
12366          currentClass = external.symbol.registered;
12367          if(_class.definitions)
12368          {
12369             ProcessClass(_class.definitions, _class.symbol);
12370          }
12371          if(inCompiler)
12372          {
12373             // Free class data...
12374             ast->Remove(external);
12375             delete external;
12376          }
12377       }
12378       else if(external.type == nameSpaceExternal)
12379       {
12380          thisNameSpace = external.id.string;
12381       }
12382    }
12383    currentClass = null;
12384    thisNameSpace = null;
12385
12386    delete temp.symbol;
12387    delete temp;
12388 }