123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457 |
- unit TCGenerics;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, fpcunit, testregistry,
- TCModules, FPPas2Js, PScanner, PasResolveEval;
- type
- { TTestGenerics }
- TTestGenerics = class(TCustomTestModule)
- Published
- // generic record
- Procedure TestGen_RecordEmpty;
- Procedure TestGen_Record_ClassProc;
- Procedure TestGen_Record_ClassVarRecord_Program;
- Procedure TestGen_Record_ClassVarRecord_UnitImpl;
- Procedure TestGen_Record_RTTI_UnitImpl;
- // ToDo: delay RTTI with anonymous array a:array of T, array[1..2] of T
- // ToDo: type alias type as parameter, TBird = type word;
- // generic class
- Procedure TestGen_ClassEmpty;
- Procedure TestGen_Class_EmptyMethod;
- Procedure TestGen_Class_TList;
- Procedure TestGen_Class_TCustomList; // ToDo: with Self do Result:=Method()
- Procedure TestGen_ClassAncestor;
- Procedure TestGen_Class_TypeInfo;
- Procedure TestGen_Class_TypeOverload; // ToDo TBird, TBird<T>, TBird<S,T>
- Procedure TestGen_Class_ClassProperty;
- Procedure TestGen_Class_ClassProc;
- //Procedure TestGen_Record_ReferGenClass_DelphiFail; TBird<T> = class x:TBird; end;
- Procedure TestGen_Class_ClassConstructor;
- Procedure TestGen_Class_TypeCastSpecializesWarn;
- Procedure TestGen_Class_TypeCastSpecializesJSValueNoWarn;
- procedure TestGen_Class_OverloadsInUnit;
- procedure TestGen_ClassForward_CircleRTTI;
- procedure TestGen_Class_Nested_RTTI;
- Procedure TestGen_Class_ClassVarRecord_UnitImpl;
- // generic external class
- procedure TestGen_ExtClass_VarArgsOfType;
- procedure TestGen_ExtClass_Array;
- procedure TestGen_ExtClass_GenJSValueAssign;
- procedure TestGen_ExtClass_AliasMemberType;
- Procedure TestGen_ExtClass_RTTI;
- procedure TestGen_ExtClass_UnitImplRec;
- // class interfaces
- procedure TestGen_ClassInterface_Corba;
- procedure TestGen_ClassInterface_InterfacedObject;
- procedure TestGen_ClassInterface_COM_RTTI;
- procedure TestGen_ClassInterface_Helper;
- // statements
- Procedure TestGen_InlineSpec_Constructor;
- Procedure TestGen_CallUnitImplProc;
- Procedure TestGen_IntAssignTemplVar;
- Procedure TestGen_TypeCastDotField;
- // generic helper
- procedure TestGen_HelperForArray;
- // generic functions
- procedure TestGenProc_Function_ObjFPC;
- procedure TestGenProc_Function_Delphi;
- procedure TestGenProc_Overload;
- procedure TestGenProc_Forward;
- procedure TestGenProc_Infer_OverloadForward;
- procedure TestGenProc_TypeInfo;
- procedure TestGenProc_Infer_Widen;
- procedure TestGenProc_Infer_PassAsArg;
- // ToDo: FuncName:= instead of Result:=
- // generic methods
- procedure TestGenMethod_ObjFPC;
- // generic array
- procedure TestGen_Array_OtherUnit;
- procedure TestGen_ArrayOfUnitImplRec;
- // generic procedure type
- procedure TestGen_ProcType_ProcLocal;
- procedure TestGen_ProcType_Local_RTTI_Fail;
- procedure TestGen_ProcType_ParamUnitImpl;
- end;
- implementation
- { TTestGenerics }
- procedure TTestGenerics.TestGen_RecordEmpty;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' generic TRecA<T> = record',
- ' end;',
- 'var a,b: specialize TRecA<word>;',
- 'begin',
- ' if a=b then ;']);
- ConvertProgram;
- CheckSource('TestGen_RecordEmpty',
- LinesToStr([ // statements
- 'rtl.recNewT(this, "TRecA$G1", function () {',
- ' this.$eq = function (b) {',
- ' return true;',
- ' };',
- ' this.$assign = function (s) {',
- ' return this;',
- ' };',
- '});',
- 'this.a = this.TRecA$G1.$new();',
- 'this.b = this.TRecA$G1.$new();',
- '']),
- LinesToStr([ // $mod.$main
- 'if ($mod.a.$eq($mod.b)) ;'
- ]));
- end;
- procedure TTestGenerics.TestGen_Record_ClassProc;
- begin
- StartProgram(false);
- Add([
- '{$modeswitch AdvancedRecords}',
- 'type',
- ' generic TPoint<T> = record',
- ' class var x: T;',
- ' class procedure Fly; static;',
- ' end;',
- 'class procedure Tpoint.Fly;',
- 'begin',
- ' x:=x+3;',
- ' tpoint.x:=tpoint.x+4;',
- ' Fly;',
- ' tpoint.Fly;',
- 'end;',
- 'var p: specialize TPoint<word>;',
- 'begin',
- ' p.x:=p.x+10;',
- ' p.Fly;',
- ' p.Fly();',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Record_ClassProc',
- LinesToStr([ // statements
- 'rtl.recNewT(this, "TPoint$G1", function () {',
- ' this.x = 0;',
- ' this.$eq = function (b) {',
- ' return true;',
- ' };',
- ' this.$assign = function (s) {',
- ' return this;',
- ' };',
- ' this.Fly = function () {',
- ' $mod.TPoint$G1.x = $mod.TPoint$G1.x + 3;',
- ' $mod.TPoint$G1.x = $mod.TPoint$G1.x + 4;',
- ' $mod.TPoint$G1.Fly();',
- ' $mod.TPoint$G1.Fly();',
- ' };',
- '}, true);',
- 'this.p = this.TPoint$G1.$new();',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.TPoint$G1.x = $mod.p.x + 10;',
- '$mod.p.Fly();',
- '$mod.p.Fly();',
- '']));
- end;
- procedure TTestGenerics.TestGen_Record_ClassVarRecord_Program;
- begin
- StartProgram(false);
- Add([
- '{$modeswitch AdvancedRecords}',
- 'type',
- ' generic TAnt<T> = record',
- ' class var x: T;',
- ' end;',
- ' TBird = record',
- ' b: word;',
- ' end;',
- 'var f: specialize TAnt<TBird>;',
- 'begin',
- ' f.x.b:=f.x.b+10;',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Record_ClassVarRecord_Program',
- LinesToStr([ // statements
- 'rtl.recNewT(this, "TBird", function () {',
- ' this.b = 0;',
- ' this.$eq = function (b) {',
- ' return this.b === b.b;',
- ' };',
- ' this.$assign = function (s) {',
- ' this.b = s.b;',
- ' return this;',
- ' };',
- '});',
- 'rtl.recNewT(this, "TAnt$G1", function () {',
- ' this.x = $mod.TBird.$new();',
- ' this.$eq = function (b) {',
- ' return true;',
- ' };',
- ' this.$assign = function (s) {',
- ' return this;',
- ' };',
- '}, true);',
- 'this.f = this.TAnt$G1.$new();',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.f.x.b = $mod.f.x.b + 10;',
- '']));
- end;
- procedure TTestGenerics.TestGen_Record_ClassVarRecord_UnitImpl;
- begin
- StartProgram(true,[supTObject]);
- AddModuleWithIntfImplSrc('UnitA.pas',
- LinesToStr([
- '{$modeswitch AdvancedRecords}',
- 'type',
- ' generic TAnt<T> = record',
- ' class var x: T;',
- ' class var a: array[1..2] of T;',
- ' end;',
- '']),
- LinesToStr([
- 'type',
- ' TBird = record',
- ' b: word;',
- ' end;',
- 'var f: specialize TAnt<TBird>;',
- 'begin',
- ' f.x.b:=f.x.b+10;',
- '']));
- Add([
- 'uses UnitA;',
- 'begin',
- 'end.']);
- ConvertProgram;
- CheckUnit('UnitA.pas',
- LinesToStr([ // statements
- 'rtl.module("UnitA", ["system"], function () {',
- ' var $mod = this;',
- ' var $impl = $mod.$impl;',
- ' rtl.recNewT(this, "TAnt$G1", function () {',
- ' this.$initSpec = function () {',
- ' this.x = $impl.TBird.$new();',
- ' this.a = rtl.arraySetLength(null, $impl.TBird, 2);',
- ' };',
- ' this.$eq = function (b) {',
- ' return true;',
- ' };',
- ' this.$assign = function (s) {',
- ' return this;',
- ' };',
- ' }, true);',
- ' $mod.$implcode = function () {',
- ' rtl.recNewT($impl, "TBird", function () {',
- ' this.b = 0;',
- ' this.$eq = function (b) {',
- ' return this.b === b.b;',
- ' };',
- ' this.$assign = function (s) {',
- ' this.b = s.b;',
- ' return this;',
- ' };',
- ' });',
- ' $impl.f = $mod.TAnt$G1.$new();',
- ' };',
- ' $mod.$init = function () {',
- ' $impl.f.x.b = $impl.f.x.b + 10;',
- ' };',
- '}, []);']));
- CheckSource('TestGen_Record_ClassVarRecord_UnitImpl',
- LinesToStr([ // statements
- 'pas.UnitA.TAnt$G1.$initSpec();',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_Record_RTTI_UnitImpl;
- begin
- WithTypeInfo:=true;
- StartUnit(true);
- Add([
- 'interface',
- '{$modeswitch AdvancedRecords}',
- 'type',
- ' generic TAnt<T> = record',
- ' class var x: T;',
- //' class var a,b: array of T;',
- ' end;',
- 'implementation',
- 'type',
- ' TBird = record',
- ' b: word;',
- ' end;',
- 'var f: specialize TAnt<TBird>;',
- ' p: pointer;',
- 'begin',
- ' p:=typeinfo(f);',
- '']);
- ConvertUnit;
- CheckSource('TestGen_Record_RTTI_UnitImpl',
- LinesToStr([ // statements
- 'var $impl = $mod.$impl;',
- 'rtl.recNewT(this, "TAnt$G1", function () {',
- ' var $r = $mod.$rtti.$Record("TAnt<Test1.TBird>", {});',
- ' this.$initSpec = function () {',
- ' this.x = $impl.TBird.$new();',
- ' $r.addField("x", $mod.$rtti["TBird"]);',
- ' };',
- ' this.$eq = function (b) {',
- ' return true;',
- ' };',
- ' this.$assign = function (s) {',
- ' return this;',
- ' };',
- '}, true);',
- '']),
- LinesToStr([ // $mod.$init
- '$impl.p = $mod.$rtti["TAnt<Test1.TBird>"];',
- '']),
- LinesToStr([ // statements
- 'rtl.recNewT($impl, "TBird", function () {',
- ' this.b = 0;',
- ' this.$eq = function (b) {',
- ' return this.b === b.b;',
- ' };',
- ' this.$assign = function (s) {',
- ' this.b = s.b;',
- ' return this;',
- ' };',
- ' var $r = $mod.$rtti.$Record("TBird", {});',
- ' $r.addField("b", rtl.word);',
- '});',
- '$impl.f = $mod.TAnt$G1.$new();',
- '$impl.p = null;',
- '']));
- end;
- procedure TTestGenerics.TestGen_ClassEmpty;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class end;',
- ' generic TBird<T> = class',
- ' end;',
- 'var a,b: specialize TBird<word>;',
- 'begin',
- ' if a=b then ;']);
- ConvertProgram;
- CheckSource('TestGen_ClassEmpty',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
- '}, "TBird<System.Word>");',
- 'this.a = null;',
- 'this.b = null;',
- '']),
- LinesToStr([ // $mod.$main
- 'if ($mod.a === $mod.b) ;'
- ]));
- end;
- procedure TTestGenerics.TestGen_Class_EmptyMethod;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class end;',
- ' generic TBird<T> = class',
- ' function Fly(w: T): T;',
- ' end;',
- 'function TBird.Fly(w: T): T;',
- 'begin',
- 'end;',
- 'var a: specialize TBird<word>;',
- 'begin',
- ' if a.Fly(3)=4 then ;']);
- ConvertProgram;
- CheckSource('TestGen_Class_EmptyMethod',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
- ' this.Fly = function (w) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- '}, "TBird<System.Word>");',
- 'this.a = null;',
- '']),
- LinesToStr([ // $mod.$main
- ' if ($mod.a.Fly(3) === 4) ;'
- ]));
- end;
- procedure TTestGenerics.TestGen_Class_TList;
- begin
- StartProgram(false);
- Add([
- '{$mode objfpc}',
- 'type',
- ' TObject = class end;',
- ' generic TList<T> = class',
- ' strict private',
- ' FItems: array of T;',
- ' function GetItems(Index: longint): T;',
- ' procedure SetItems(Index: longint; Value: T);',
- ' public',
- ' procedure Alter(w: T);',
- ' property Items[Index: longint]: T read GetItems write SetItems; default;',
- ' end;',
- ' TWordList = specialize TList<word>;',
- 'function TList.GetItems(Index: longint): T;',
- 'begin',
- ' Result:=FItems[Index];',
- 'end;',
- 'procedure TList.SetItems(Index: longint; Value: T);',
- 'begin',
- ' FItems[Index]:=Value;',
- 'end;',
- 'procedure TList.Alter(w: T);',
- 'begin',
- ' SetLength(FItems,length(FItems)+1);',
- ' Insert(w,FItems,2);',
- ' Delete(FItems,2,3);',
- 'end;',
- 'var l: TWordList;',
- ' w: word;',
- 'begin',
- ' l[1]:=w;',
- ' w:=l[2];',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Class_TList',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TList$G1", this.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.FItems = [];',
- ' };',
- ' this.$final = function () {',
- ' this.FItems = undefined;',
- ' $mod.TObject.$final.call(this);',
- ' };',
- ' this.GetItems = function (Index) {',
- ' var Result = 0;',
- ' Result = this.FItems[Index];',
- ' return Result;',
- ' };',
- ' this.SetItems = function (Index, Value) {',
- ' this.FItems[Index] = Value;',
- ' };',
- ' this.Alter = function (w) {',
- ' this.FItems = rtl.arraySetLength(this.FItems, 0, rtl.length(this.FItems) + 1);',
- ' this.FItems.splice(2, 0, w);',
- ' this.FItems.splice(2, 3);',
- ' };',
- '}, "TList<System.Word>");',
- 'this.l = null;',
- 'this.w = 0;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.l.SetItems(1, $mod.w);',
- '$mod.w = $mod.l.GetItems(2);',
- '']));
- end;
- procedure TTestGenerics.TestGen_Class_TCustomList;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- 'type',
- ' TObject = class end;',
- ' TCustomList<T> = class',
- ' public',
- ' function PrepareAddingItem: word; virtual;',
- ' end;',
- ' TList<T> = class(TCustomList<T>)',
- ' public',
- ' function Add: word;',
- ' end;',
- ' TWordList = TList<word>;',
- 'function TCustomList<T>.PrepareAddingItem: word;',
- 'begin',
- 'end;',
- 'function TList<T>.Add: word;',
- 'begin',
- ' Result:=PrepareAddingItem;',
- ' Result:=Self.PrepareAddingItem;',
- //' with Self do Result:=PrepareAddingItem;',
- 'end;',
- 'var l: TWordList;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Class_TCustomList',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TCustomList$G2", this.TObject, function () {',
- ' this.PrepareAddingItem = function () {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- '}, "TCustomList<System.Word>");',
- 'rtl.createClass(this, "TList$G1", this.TCustomList$G2, function () {',
- ' this.Add = function () {',
- ' var Result = 0;',
- ' Result = this.PrepareAddingItem();',
- ' Result = this.PrepareAddingItem();',
- ' return Result;',
- ' };',
- '}, "TList<System.Word>");',
- 'this.l = null;',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_ClassAncestor;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class end;',
- ' generic TBird<T> = class',
- ' end;',
- ' generic TEagle<T> = class(specialize TBird<T>)',
- ' end;',
- 'var a: specialize TEagle<word>;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ClassAncestor',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TBird$G2", this.TObject, function () {',
- '}, "TBird<System.Word>");',
- 'rtl.createClass(this, "TEagle$G1", this.TBird$G2, function () {',
- '}, "TEagle<System.Word>");',
- 'this.a = null;',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_Class_TypeInfo;
- begin
- WithTypeInfo:=true;
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class end;',
- ' generic TBird<T> = class',
- ' published',
- ' m: T;',
- ' end;',
- ' TEagle = specialize TBird<word>;',
- 'var',
- ' b: specialize TBird<word>;',
- ' p: pointer;',
- 'begin',
- ' p:=typeinfo(TEagle);',
- ' p:=typeinfo(b);',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Class_TypeInfo',
- LinesToStr([ // statements
- '$mod.$rtti.$Class("TBird<System.Word>");',
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.m = 0;',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addField("m", rtl.word);',
- '}, "TBird<System.Word>");',
- 'this.b = null;',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.$rtti["TBird<System.Word>"];',
- '$mod.p = $mod.b.$rtti;',
- '']));
- end;
- procedure TTestGenerics.TestGen_Class_TypeOverload;
- begin
- exit;// ToDo
- StartProgram(false);
- Add([
- '{$mode delphi}',
- 'type',
- ' TObject = class end;',
- ' TBird = word;',
- ' TBird<T> = class',
- ' m: T;',
- ' end;',
- ' TEagle = TBird<word>;',
- 'var',
- ' b: TBird<word>;',
- ' e: TEagle;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Class_TypeOverload',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_Class_ClassProperty;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- 'type',
- ' TObject = class end;',
- ' TBird<T> = class',
- ' private',
- ' class var fSize: T;',
- ' public',
- ' class property Size: T read fSize write fSize;',
- ' end;',
- ' TEagle = TBird<word>;',
- 'begin',
- ' TBird<word>.Size:=3+TBird<word>.Size;',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Class_ClassProperty',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
- ' this.fSize = 0;',
- '}, "TBird<System.Word>");',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.TBird$G1.fSize = 3 + $mod.TBird$G1.fSize;',
- '']));
- end;
- procedure TTestGenerics.TestGen_Class_ClassProc;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class end;',
- ' generic TPoint<T> = class',
- ' class var x: T;',
- ' class procedure Fly; static;',
- ' class procedure Run;',
- ' end;',
- 'class procedure Tpoint.Fly;',
- 'begin',
- ' x:=x+3;',
- ' tpoint.x:=tpoint.x+4;',
- ' Fly;',
- ' tpoint.Fly;',
- ' Run;',
- ' tpoint.Run;',
- 'end;',
- 'class procedure TPoint.Run;',
- 'begin',
- ' x:=x+5;',
- ' tpoint.x:=tpoint.x+6;',
- ' Fly;',
- ' tpoint.Fly;',
- ' Run;',
- ' tpoint.Run;',
- 'end;',
- 'var p: specialize TPoint<word>;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Class_ClassProc',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TPoint$G1", this.TObject, function () {',
- ' this.x = 0;',
- ' this.Fly = function () {',
- ' $mod.TPoint$G1.x = $mod.TPoint$G1.x + 3;',
- ' $mod.TPoint$G1.x = $mod.TPoint$G1.x + 4;',
- ' $mod.TPoint$G1.Fly();',
- ' $mod.TPoint$G1.Fly();',
- ' $mod.TPoint$G1.Run();',
- ' $mod.TPoint$G1.Run();',
- ' };',
- ' this.Run = function () {',
- ' $mod.TPoint$G1.x = this.x + 5;',
- ' $mod.TPoint$G1.x = $mod.TPoint$G1.x + 6;',
- ' this.Fly();',
- ' $mod.TPoint$G1.Fly();',
- ' this.Run();',
- ' $mod.TPoint$G1.Run();',
- ' };',
- '}, "TPoint<System.Word>");',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_Class_ClassConstructor;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class end;',
- ' generic TPoint<T> = class',
- ' class var x: T;',
- ' class procedure Fly; static;',
- ' class constructor Init;',
- ' end;',
- 'var count: word;',
- 'class procedure Tpoint.Fly;',
- 'begin',
- 'end;',
- 'class constructor tpoint.init;',
- 'begin',
- ' count:=count+1;',
- ' x:=3;',
- ' tpoint.x:=4;',
- ' fly;',
- ' tpoint.fly;',
- 'end;',
- 'var',
- ' r: specialize TPoint<word>;',
- ' s: specialize TPoint<smallint>;',
- 'begin',
- ' r.x:=10;',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Class_ClassConstructor',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.count = 0;',
- 'rtl.createClass(this, "TPoint$G1", this.TObject, function () {',
- ' this.x = 0;',
- ' this.Fly = function () {',
- ' };',
- '}, "TPoint<System.Word>");',
- 'this.r = null;',
- 'rtl.createClass(this, "TPoint$G2", this.TObject, function () {',
- ' this.x = 0;',
- ' this.Fly = function () {',
- ' };',
- '}, "TPoint<System.SmallInt>");',
- 'this.s = null;',
- '']),
- LinesToStr([ // $mod.$main
- '(function () {',
- ' $mod.count = $mod.count + 1;',
- ' $mod.TPoint$G1.x = 3;',
- ' $mod.TPoint$G1.x = 4;',
- ' $mod.TPoint$G1.Fly();',
- ' $mod.TPoint$G1.Fly();',
- '})();',
- '(function () {',
- ' $mod.count = $mod.count + 1;',
- ' $mod.TPoint$G2.x = 3;',
- ' $mod.TPoint$G2.x = 4;',
- ' $mod.TPoint$G2.Fly();',
- ' $mod.TPoint$G2.Fly();',
- '})();',
- '$mod.TPoint$G1.x = 10;',
- '']));
- end;
- procedure TTestGenerics.TestGen_Class_TypeCastSpecializesWarn;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- 'type',
- ' TObject = class end;',
- ' TBird<T> = class F: T; end;',
- ' TBirdWord = TBird<Word>;',
- ' TBirdChar = TBird<Char>;',
- 'var',
- ' w: TBirdWord;',
- ' c: TBirdChar;',
- 'begin',
- ' w:=TBirdWord(c);',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Class_TypeCastSpecializesWarn',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.F = 0;',
- ' };',
- '}, "TBird<System.Word>");',
- 'rtl.createClass(this, "TBird$G2", this.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.F = "";',
- ' };',
- '}, "TBird<System.Char>");',
- 'this.w = null;',
- 'this.c = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.w = $mod.c;',
- '']));
- CheckHint(mtWarning,nClassTypesAreNotRelatedXY,'Class types "TBird<System.Char>" and "TBird<System.Word>" are not related');
- CheckResolverUnexpectedHints();
- end;
- procedure TTestGenerics.TestGen_Class_TypeCastSpecializesJSValueNoWarn;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- 'type',
- ' TObject = class end;',
- ' TBird<T> = class F: T; end;',
- ' TBirdWord = TBird<Word>;',
- ' TBirdAny = TBird<JSValue>;',
- 'var',
- ' w: TBirdWord;',
- ' a: TBirdAny;',
- 'begin',
- ' w:=TBirdWord(a);',
- ' a:=TBirdAny(w);',
- '']);
- ConvertProgram;
- CheckSource('TestGen_Class_TypeCastSpecializesJSValueNoWarn',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.F = 0;',
- ' };',
- '}, "TBird<System.Word>");',
- 'rtl.createClass(this, "TBird$G2", this.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.F = undefined;',
- ' };',
- '}, "TBird<System.JSValue>");',
- 'this.w = null;',
- 'this.a = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.w = $mod.a;',
- '$mod.a = $mod.w;',
- '']));
- CheckResolverUnexpectedHints();
- end;
- procedure TTestGenerics.TestGen_Class_OverloadsInUnit;
- begin
- StartProgram(true,[supTObject]);
- AddModuleWithIntfImplSrc('UnitA.pas',
- LinesToStr([
- 'type',
- ' generic TBird<T> = class',
- ' const c = 13;',
- ' constructor Create(w: T);',
- ' constructor Create(b: boolean);',
- ' end;',
- '']),
- LinesToStr([
- 'constructor TBird.Create(w: T);',
- 'const c = 14;',
- 'begin',
- 'end;',
- 'constructor TBird.Create(b: boolean);',
- 'const c = 15;',
- 'begin',
- 'end;',
- '']));
- Add([
- 'uses UnitA;',
- 'type',
- ' TWordBird = specialize TBird<word>;',
- ' TDoubleBird = specialize TBird<double>;',
- 'var',
- ' wb: TWordBird;',
- ' db: TDoubleBird;',
- 'begin',
- ' wb:=TWordBird.Create(3);',
- ' wb:=TWordBird.Create(true);',
- ' db:=TDoubleBird.Create(1.3);',
- ' db:=TDoubleBird.Create(true);',
- '']);
- ConvertProgram;
- CheckUnit('UnitA.pas',
- LinesToStr([ // statements
- 'rtl.module("UnitA", ["system"], function () {',
- ' var $mod = this;',
- ' rtl.createClass(this, "TBird$G1", pas.system.TObject, function () {',
- ' this.c = 13;',
- ' var c$1 = 14;',
- ' this.Create$1 = function (w) {',
- ' return this;',
- ' };',
- ' var c$2 = 15;',
- ' this.Create$2 = function (b) {',
- ' return this;',
- ' };',
- ' }, "TBird<System.Word>");',
- ' rtl.createClass(this, "TBird$G2", pas.system.TObject, function () {',
- ' this.c = 13;',
- ' var c$1 = 14;',
- ' this.Create$1 = function (w) {',
- ' return this;',
- ' };',
- ' var c$2 = 15;',
- ' this.Create$2 = function (b) {',
- ' return this;',
- ' };',
- ' }, "TBird<System.Double>");',
- '});',
- '']));
- CheckSource('TestGen_Class_OverloadsInUnit',
- LinesToStr([ // statements
- 'this.wb = null;',
- 'this.db = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.wb = pas.UnitA.TBird$G1.$create("Create$1", [3]);',
- '$mod.wb = pas.UnitA.TBird$G1.$create("Create$2", [true]);',
- '$mod.db = pas.UnitA.TBird$G2.$create("Create$1", [1.3]);',
- '$mod.db = pas.UnitA.TBird$G2.$create("Create$2", [true]);',
- '']));
- end;
- procedure TTestGenerics.TestGen_ClassForward_CircleRTTI;
- begin
- WithTypeInfo:=true;
- StartProgram(false);
- Add([
- '{$mode objfpc}',
- 'type',
- ' TObject = class end;',
- ' {$M+}',
- ' TPersistent = class end;',
- ' {$M-}',
- ' generic TAnt<T> = class;',
- ' generic TFish<U> = class(TPersistent)',
- ' private type AliasU = U;',
- ' published',
- ' a: specialize TAnt<AliasU>;',
- ' end;',
- ' generic TAnt<T> = class(TPersistent)',
- ' private type AliasT = T;',
- ' published',
- ' f: specialize TFish<AliasT>;',
- ' end;',
- 'var',
- ' WordFish: specialize TFish<word>;',
- ' p: pointer;',
- 'begin',
- ' p:=typeinfo(specialize TAnt<word>);',
- ' p:=typeinfo(specialize TFish<word>);',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ClassForward_CircleRTTI',
- LinesToStr([ // statements
- '$mod.$rtti.$Class("TAnt<System.Word>");',
- '$mod.$rtti.$Class("TFish<System.Word>");',
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TPersistent", this.TObject, function () {',
- '});',
- 'rtl.createClass(this, "TAnt$G2", this.TPersistent, function () {',
- ' this.$init = function () {',
- ' $mod.TPersistent.$init.call(this);',
- ' this.f = null;',
- ' };',
- ' this.$final = function () {',
- ' this.f = undefined;',
- ' $mod.TPersistent.$final.call(this);',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addField("f", $mod.$rtti["TFish<System.Word>"]);',
- '}, "TAnt<System.Word>");',
- 'rtl.createClass(this, "TFish$G2", this.TPersistent, function () {',
- ' this.$init = function () {',
- ' $mod.TPersistent.$init.call(this);',
- ' this.a = null;',
- ' };',
- ' this.$final = function () {',
- ' this.a = undefined;',
- ' $mod.TPersistent.$final.call(this);',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addField("a", $mod.$rtti["TAnt<System.Word>"]);',
- '}, "TFish<System.Word>");',
- 'this.WordFish = null;',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.$rtti["TAnt<System.Word>"];',
- '$mod.p = $mod.$rtti["TFish<System.Word>"];',
- '']));
- end;
- procedure TTestGenerics.TestGen_Class_Nested_RTTI;
- begin
- WithTypeInfo:=true;
- StartProgram(true,[supTObject]);
- AddModuleWithIntfImplSrc('UnitA.pas',
- LinesToStr([
- 'type',
- ' generic TAnt<T> = class',
- ' type',
- ' TLeg = class',
- ' published',
- ' Size: T;',
- ' end;',
- ' end;',
- ' TBoolAnt = specialize TAnt<boolean>;',
- '']),
- LinesToStr([
- '']));
- Add([
- 'uses UnitA;',
- 'var',
- ' BoolLeg: TBoolAnt.TLeg;',
- 'begin',
- ' if typeinfo(TBoolAnt.TLeg)=nil then ;',
- '']);
- ConvertProgram;
- CheckUnit('UnitA.pas',
- LinesToStr([ // statements
- 'rtl.module("UnitA", ["system"], function () {',
- ' var $mod = this;',
- ' $mod.$rtti.$Class("TAnt<System.Boolean>");',
- ' rtl.createClass(this, "TAnt$G1", pas.system.TObject, function () {',
- ' rtl.createClass(this, "TLeg", pas.system.TObject, function () {',
- ' this.$init = function () {',
- ' pas.system.TObject.$init.call(this);',
- ' this.Size = false;',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addField("Size", rtl.boolean);',
- ' }, "TAnt<System.Boolean>.TLeg");',
- ' }, "TAnt<System.Boolean>");',
- '});']));
- CheckSource('TestGen_Class_Nested_RTTI',
- LinesToStr([ // statements
- 'this.BoolLeg = null;',
- '']),
- LinesToStr([ // $mod.$main
- 'if (pas.UnitA.$rtti["TAnt<System.Boolean>.TLeg"] === null) ;',
- '']));
- end;
- procedure TTestGenerics.TestGen_Class_ClassVarRecord_UnitImpl;
- begin
- StartProgram(true,[supTObject]);
- AddModuleWithIntfImplSrc('UnitA.pas',
- LinesToStr([
- 'type',
- ' generic TAnt<T> = class',
- ' public',
- ' class var x: T;',
- ' class var a: array[1..2] of T;',
- ' end;',
- '']),
- LinesToStr([
- 'type',
- ' TBird = record',
- ' b: word;',
- ' end;',
- 'var f: specialize TAnt<TBird>;',
- 'begin',
- ' f.x.b:=f.x.b+10;',
- '']));
- Add([
- 'uses UnitA;',
- 'begin',
- 'end.']);
- ConvertProgram;
- CheckUnit('UnitA.pas',
- LinesToStr([ // statements
- 'rtl.module("UnitA", ["system"], function () {',
- ' var $mod = this;',
- ' var $impl = $mod.$impl;',
- ' rtl.createClass(this, "TAnt$G1", pas.system.TObject, function () {',
- ' this.$initSpec = function () {',
- ' this.x = $impl.TBird.$new();',
- ' this.a = rtl.arraySetLength(null, $impl.TBird, 2);',
- ' };',
- ' }, "TAnt<UnitA.TBird>");',
- ' $mod.$implcode = function () {',
- ' rtl.recNewT($impl, "TBird", function () {',
- ' this.b = 0;',
- ' this.$eq = function (b) {',
- ' return this.b === b.b;',
- ' };',
- ' this.$assign = function (s) {',
- ' this.b = s.b;',
- ' return this;',
- ' };',
- ' });',
- ' $impl.f = null;',
- ' };',
- ' $mod.$init = function () {',
- ' $impl.f.x.b = $impl.f.x.b + 10;',
- ' };',
- '}, []);',
- '']));
- CheckSource('TestGen_Class_ClassVarRecord_UnitImpl',
- LinesToStr([ // statements
- 'pas.UnitA.TAnt$G1.$initSpec();',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_ExtClass_VarArgsOfType;
- begin
- StartProgram(false);
- Add([
- '{$mode objfpc}',
- '{$modeswitch externalclass}',
- 'type',
- ' TJSObject = class external name ''Object''',
- ' end;',
- ' generic TGJSSet<T> = class external name ''Set''',
- ' constructor new(aElement1: T); varargs of T; overload;',
- ' function bind(thisArg: TJSObject): T; varargs of T;',
- ' end;',
- ' TJSWordSet = specialize TGJSSet<word>;',
- 'var',
- ' s: TJSWordSet;',
- ' w: word;',
- 'begin',
- ' s:=TJSWordSet.new(3);',
- ' s:=TJSWordSet.new(3,5);',
- ' w:=s.bind(nil);',
- ' w:=s.bind(nil,6);',
- ' w:=s.bind(nil,7,8);',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ExtClass_VarArgsOfType',
- LinesToStr([ // statements
- 'this.s = null;',
- 'this.w = 0;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.s = new Set(3);',
- '$mod.s = new Set(3, 5);',
- '$mod.w = $mod.s.bind(null);',
- '$mod.w = $mod.s.bind(null, 6);',
- '$mod.w = $mod.s.bind(null, 7, 8);',
- '']));
- end;
- procedure TTestGenerics.TestGen_ExtClass_Array;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- '{$ModeSwitch externalclass}',
- 'type',
- ' NativeInt = longint;',
- ' TJSGenArray<T> = Class external name ''Array''',
- ' private',
- ' function GetElements(Index: NativeInt): T; external name ''[]'';',
- ' procedure SetElements(Index: NativeInt; const AValue: T); external name ''[]'';',
- ' public',
- ' type TSelfType = TJSGenArray<T>;',
- ' public',
- ' FLength : NativeInt; external name ''length'';',
- ' constructor new; overload;',
- ' constructor new(aLength : NativeInt); overload;',
- ' class function _of() : TSelfType; varargs; external name ''of'';',
- ' function fill(aValue : T) : TSelfType; overload;',
- ' function fill(aValue : T; aStartIndex : NativeInt) : TSelfType; overload;',
- ' function fill(aValue : T; aStartIndex,aEndIndex : NativeInt) : TSelfType; overload;',
- ' property Length : NativeInt Read FLength Write FLength;',
- ' property Elements[Index: NativeInt]: T read GetElements write SetElements; default;',
- ' end;',
- ' TJSWordArray = TJSGenArray<word>;',
- 'var',
- ' wa: TJSWordArray;',
- ' w: word;',
- 'begin',
- ' wa:=TJSWordArray.new;',
- ' wa:=TJSWordArray.new(3);',
- ' wa:=TJSWordArray._of(4,5);',
- ' wa:=wa.fill(7);',
- ' wa:=wa.fill(7,8,9);',
- ' w:=wa.length;',
- ' wa.length:=10;',
- ' wa[11]:=w;',
- ' w:=wa[12];',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ExtClass_Array',
- LinesToStr([ // statements
- 'this.wa = null;',
- 'this.w = 0;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.wa = new Array();',
- '$mod.wa = new Array(3);',
- '$mod.wa = Array.of(4, 5);',
- '$mod.wa = $mod.wa.fill(7);',
- '$mod.wa = $mod.wa.fill(7, 8, 9);',
- '$mod.w = $mod.wa.length;',
- '$mod.wa.length = 10;',
- '$mod.wa[11] = $mod.w;',
- '$mod.w = $mod.wa[12];',
- '']));
- end;
- procedure TTestGenerics.TestGen_ExtClass_GenJSValueAssign;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- '{$modeswitch externalclass}',
- 'type',
- ' TExt<T> = class external name ''Ext''',
- ' F: T;',
- ' end;',
- ' TExtWord = TExt<Word>;',
- ' TExtAny = TExt<JSValue>;',
- 'procedure Run(e: TExtAny);',
- 'begin end;',
- 'var',
- ' w: TExtWord;',
- ' a: TExtAny;',
- 'begin',
- ' a:=w;',
- ' Run(w);',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ExtClass_GenJSValueAssign',
- LinesToStr([ // statements
- 'this.Run = function (e) {',
- '};',
- 'this.w = null;',
- 'this.a = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.a = $mod.w;',
- '$mod.Run($mod.w);',
- '']));
- CheckResolverUnexpectedHints();
- end;
- procedure TTestGenerics.TestGen_ExtClass_AliasMemberType;
- begin
- StartProgram(false);
- Add([
- '{$mode objfpc}',
- '{$modeswitch externalclass}',
- 'type',
- ' generic TExt<T> = class external name ''Ext''',
- ' public type TRun = reference to function(a: T): T;',
- ' end;',
- ' TExtWord = specialize TExt<word>;',
- ' TExtWordRun = TExtWord.TRun;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ExtClass_AliasMemberType',
- LinesToStr([ // statements
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_ExtClass_RTTI;
- begin
- WithTypeInfo:=true;
- StartProgram(false);
- Add([
- '{$mode objfpc}',
- '{$modeswitch externalclass}',
- 'type',
- ' generic TGJSSET<T> = class external name ''SET''',
- ' A: T;',
- ' end;',
- ' TJSSet = specialize TGJSSET<JSValue>;',
- ' TJSSetEventProc = reference to procedure(value : JSValue; key: NativeInt; set_: TJSSet);',
- 'var p: Pointer;',
- 'begin',
- ' p:=typeinfo(TJSSetEventProc);',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ExtClass_RTTI',
- LinesToStr([ // statements
- 'this.$rtti.$ExtClass("TGJSSET<System.JSValue>", {',
- ' jsclass: "SET"',
- '});',
- 'this.$rtti.$RefToProcVar("TJSSetEventProc", {',
- ' procsig: rtl.newTIProcSig([["value", rtl.jsvalue], ["key", rtl.nativeint], ["set_", this.$rtti["TGJSSET<System.JSValue>"]]])',
- '});',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.$rtti["TJSSetEventProc"];',
- '']));
- end;
- procedure TTestGenerics.TestGen_ExtClass_UnitImplRec;
- begin
- WithTypeInfo:=true;
- StartProgram(true,[supTObject]);
- AddModuleWithIntfImplSrc('UnitA.pas',
- LinesToStr([
- '{$mode objfpc}',
- '{$modeswitch externalclass}',
- 'type',
- ' generic TAnt<T> = class external name ''SET''',
- ' x: T;',
- ' end;',
- '']),
- LinesToStr([
- 'type',
- ' TBird = record',
- ' b: word;',
- ' end;',
- 'var',
- ' f: specialize TAnt<TBird>;',
- 'begin',
- ' f.x.b:=f.x.b+10;',
- '']));
- Add([
- 'uses UnitA;',
- 'begin',
- 'end.']);
- ConvertProgram;
- CheckUnit('UnitA.pas',
- LinesToStr([ // statements
- 'rtl.module("UnitA", ["system"], function () {',
- ' var $mod = this;',
- ' var $impl = $mod.$impl;',
- ' this.$rtti.$ExtClass("TAnt<UnitA.TBird>", {',
- ' jsclass: "SET"',
- ' });',
- ' $mod.$implcode = function () {',
- ' rtl.recNewT($impl, "TBird", function () {',
- ' this.b = 0;',
- ' this.$eq = function (b) {',
- ' return this.b === b.b;',
- ' };',
- ' this.$assign = function (s) {',
- ' this.b = s.b;',
- ' return this;',
- ' };',
- ' var $r = $mod.$rtti.$Record("TBird", {});',
- ' $r.addField("b", rtl.word);',
- ' });',
- ' $impl.f = null;',
- ' };',
- ' $mod.$init = function () {',
- ' $impl.f.x.b = $impl.f.x.b + 10;',
- ' };',
- '}, []);']));
- CheckSource('TestGen_Class_ClassVarRecord_UnitImpl',
- LinesToStr([ // statements
- //'pas.UnitA.TAnt$G1.$initSpec();',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_ClassInterface_Corba;
- begin
- StartProgram(false);
- Add([
- '{$interfaces corba}',
- 'type',
- ' IUnknown = interface;',
- ' IUnknown = interface',
- ' [''{00000000-0000-0000-C000-000000000046}'']',
- ' end;',
- ' IInterface = IUnknown;',
- ' generic IBird<T> = interface(IInterface)',
- ' function GetSize: T;',
- ' procedure SetSize(i: T);',
- ' property Size: T read GetSize write SetSize;',
- ' procedure DoIt(i: T);',
- ' end;',
- ' TObject = class',
- ' end;',
- ' generic TBird<T> = class(TObject,specialize IBird<T>)',
- ' function GetSize: T; virtual; abstract;',
- ' procedure SetSize(i: T); virtual; abstract;',
- ' procedure DoIt(i: T); virtual; abstract;',
- ' end;',
- ' IWordBird = specialize IBird<Word>;',
- ' TWordBird = specialize TBird<Word>;',
- 'var',
- ' BirdIntf: IWordBird;',
- 'begin',
- ' BirdIntf.Size:=BirdIntf.Size;',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ClassInterface_Corba',
- LinesToStr([ // statements
- 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createInterface(',
- ' this,',
- ' "IBird$G2",',
- ' "{33AB51C6-6240-3BDF-B4B0-D48A593EAB0A}",',
- ' ["GetSize", "SetSize", "DoIt"],',
- ' this.IUnknown,',
- ' "IBird<System.Word>"',
- ');',
- 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
- ' rtl.addIntf(this, $mod.IBird$G2);',
- '}, "TBird<System.Word>");',
- 'this.BirdIntf = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
- '']));
- end;
- procedure TTestGenerics.TestGen_ClassInterface_InterfacedObject;
- begin
- StartProgram(true,[supTInterfacedObject]);
- Add([
- '{$mode delphi}',
- 'type',
- ' IComparer<T> = interface [''{505778ED-F783-4456-9691-32F419CC5E18}'']',
- ' function Compare(const Left, Right: T): Integer; overload;',
- ' end;',
- ' TComparer<T> = class(TInterfacedObject, IComparer<T>)',
- ' function Compare(const Left, Right: T): Integer;',
- ' end;',
- 'function TComparer<T>.Compare(const Left, Right: T): Integer; begin end;',
- 'var',
- ' aComparer : IComparer<Integer>;',
- 'begin',
- ' aComparer:=TComparer<Integer>.Create;',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ClassInterface_InterfacedObject',
- LinesToStr([ // statements
- 'rtl.createInterface(',
- ' this,',
- ' "IComparer$G2",',
- ' "{505778ED-F783-4456-9691-32F419CC5E18}",',
- ' ["Compare"],',
- ' pas.system.IUnknown,',
- ' "IComparer<System.Longint>"',
- ');',
- 'this.aComparer = null;',
- 'rtl.createClass(this, "TComparer$G1", pas.system.TInterfacedObject, function () {',
- ' this.Compare = function (Left, Right) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- ' rtl.addIntf(this, $mod.IComparer$G2);',
- ' rtl.addIntf(this, pas.system.IUnknown);',
- '}, "TComparer<System.Longint>");',
- '']),
- LinesToStr([ // $mod.$main
- 'rtl.setIntfP($mod, "aComparer", rtl.queryIntfT($mod.TComparer$G1.$create("Create"), $mod.IComparer$G2), true);',
- '']));
- end;
- procedure TTestGenerics.TestGen_ClassInterface_COM_RTTI;
- begin
- StartProgram(true,[supTInterfacedObject]);
- Add([
- '{$mode delphi}',
- 'type',
- ' TBird = class',
- ' function Fly<T: IInterface>: T;',
- ' end;',
- ' IAnt = interface',
- ' procedure InterfaceProc;',
- ' end;',
- 'function TBird.Fly<T>: T;',
- 'begin',
- ' if TypeInfo(T)=nil then ;',
- 'end;',
- 'var Bird: TBird;',
- ' Ant: IAnt;',
- 'begin',
- ' Ant := Bird.Fly<IAnt>;',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ClassInterface_COM_RTTI',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
- ' this.Fly$G1 = function () {',
- ' var Result = null;',
- ' if ($mod.$rtti["IAnt"] === null) ;',
- ' return Result;',
- ' };',
- '});',
- 'rtl.createInterface(this, "IAnt", "{B9D0FF27-A446-3A1B-AA85-F167837AA297}", ["InterfaceProc"], pas.system.IUnknown);',
- 'this.Bird = null;',
- 'this.Ant = null;',
- '']),
- LinesToStr([ // $mod.$main
- 'rtl.setIntfP($mod, "Ant", $mod.Bird.Fly$G1(), true);',
- '']));
- end;
- procedure TTestGenerics.TestGen_ClassInterface_Helper;
- begin
- StartProgram(true,[supTInterfacedObject]);
- Add([
- '{$mode objfpc}',
- '{$ModeSwitch typehelpers}',
- 'type',
- ' IAnt = interface',
- ' procedure InterfaceProc;',
- ' end;',
- ' TBird = type helper for IAnt',
- ' generic procedure Fly<T>(a: T);',
- ' end;',
- 'generic procedure TBird.Fly<T>(a: T);',
- 'begin',
- 'end;',
- 'var ',
- ' Ant: IAnt;',
- 'begin',
- ' Ant.specialize Fly<word>(3);',
- '']);
- ConvertProgram;
- CheckSource('TestGen_ClassInterface_COM_RTTI',
- LinesToStr([ // statements
- 'rtl.createInterface(this, "IAnt", "{B9D0FF27-A446-3A1B-AA85-F167837AA297}", ["InterfaceProc"], pas.system.IUnknown);',
- 'rtl.createHelper(this, "TBird", null, function () {',
- ' this.Fly$G1 = function (a) {',
- ' };',
- '});',
- 'this.Ant = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.TBird.Fly$G1.call({',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.Ant;',
- ' },',
- ' set: function (v) {',
- ' rtl.setIntfP(this.p, "Ant", v);',
- ' }',
- '}, 3);',
- '']));
- end;
- procedure TTestGenerics.TestGen_InlineSpec_Constructor;
- begin
- StartProgram(false);
- Add([
- '{$mode objfpc}',
- 'type',
- ' TObject = class',
- ' public',
- ' constructor Create;',
- ' end;',
- ' generic TBird<T> = class',
- ' end;',
- 'constructor TObject.Create; begin end;',
- 'var b: specialize TBird<word>;',
- 'begin',
- ' b:=specialize TBird<word>.Create;',
- '']);
- ConvertProgram;
- CheckSource('TestGen_InlineSpec_Constructor',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function () {',
- ' return this;',
- ' };',
- '});',
- 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
- '}, "TBird<System.Word>");',
- 'this.b = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.b = $mod.TBird$G1.$create("Create");',
- '']));
- end;
- procedure TTestGenerics.TestGen_CallUnitImplProc;
- begin
- AddModuleWithIntfImplSrc('UnitA.pas',
- LinesToStr([
- 'type',
- ' generic TBird<T> = class',
- ' procedure Fly;',
- ' end;',
- 'var b: specialize TBird<boolean>;',
- '']),
- LinesToStr([
- 'procedure DoIt;',
- 'var b: specialize TBird<word>;',
- 'begin',
- ' b:=specialize TBird<word>.Create;',
- ' b.Fly;',
- 'end;',
- 'procedure TBird.Fly;',
- 'begin',
- ' DoIt;',
- 'end;',
- '']));
- StartProgram(true,[supTObject]);
- Add('uses UnitA;');
- Add('begin');
- ConvertProgram;
- CheckUnit('UnitA.pas',
- LinesToStr([ // statements
- 'rtl.module("UnitA", ["system"], function () {',
- ' var $mod = this;',
- ' var $impl = $mod.$impl;',
- ' rtl.createClass(this, "TBird$G1", pas.system.TObject, function () {',
- ' this.Fly = function () {',
- ' $impl.DoIt();',
- ' };',
- ' }, "TBird<System.Boolean>");',
- ' this.b = null;',
- ' rtl.createClass(this, "TBird$G2", pas.system.TObject, function () {',
- ' this.Fly = function () {',
- ' $impl.DoIt();',
- ' };',
- ' }, "TBird<System.Word>");',
- ' $mod.$implcode = function () {',
- ' $impl.DoIt = function () {',
- ' var b = null;',
- ' b = $mod.TBird$G2.$create("Create");',
- ' b.Fly();',
- ' };',
- ' };',
- '}, []);',
- '']));
- end;
- procedure TTestGenerics.TestGen_IntAssignTemplVar;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class end;',
- ' generic TBird<T> = class',
- ' m: T;',
- ' procedure Fly;',
- ' end;',
- 'var b: specialize TBird<word>;',
- 'procedure TBird.Fly;',
- 'var i: nativeint;',
- 'begin',
- ' i:=m;',
- 'end;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestGen_IntAssignTemplVar',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.m = 0;',
- ' };',
- ' this.Fly = function () {',
- ' var i = 0;',
- ' i = this.m;',
- ' };',
- '}, "TBird<System.Word>");',
- 'this.b = null;',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_TypeCastDotField;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class end;',
- ' generic TBird<T> = class',
- ' Field: T;',
- ' procedure Fly;',
- ' end;',
- 'var',
- ' o: TObject;',
- ' b: specialize TBird<word>;',
- 'procedure TBird.Fly;',
- 'begin',
- ' specialize TBird<word>(o).Field:=3;',
- ' if 4=specialize TBird<word>(o).Field then ;',
- 'end;',
- 'begin',
- ' specialize TBird<word>(o).Field:=5;',
- ' if 6=specialize TBird<word>(o).Field then ;',
- '']);
- ConvertProgram;
- CheckSource('TestGen_TypeCastDotField',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.o = null;',
- 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.Field = 0;',
- ' };',
- ' this.Fly = function () {',
- ' $mod.o.Field = 3;',
- ' if (4 === $mod.o.Field) ;',
- ' };',
- '}, "TBird<System.Word>");',
- 'this.b = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.o.Field = 5;',
- 'if (6 === $mod.o.Field) ;',
- '']));
- end;
- procedure TTestGenerics.TestGen_HelperForArray;
- begin
- StartProgram(false);
- Add([
- '{$ModeSwitch typehelpers}',
- 'type',
- ' generic TArr<T> = array[1..2] of T;',
- ' TWordArrHelper = type helper for specialize TArr<word>',
- ' procedure Fly(w: word);',
- ' end;',
- 'procedure TWordArrHelper.Fly(w: word);',
- 'begin',
- 'end;',
- 'var',
- ' a: specialize TArr<word>;',
- 'begin',
- ' a.Fly(3);',
- '']);
- ConvertProgram;
- CheckSource('TestGen_HelperForArray',
- LinesToStr([ // statements
- 'rtl.createHelper(this, "TWordArrHelper", null, function () {',
- ' this.Fly = function (w) {',
- ' };',
- '});',
- 'this.a = rtl.arraySetLength(null, 0, 2);',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.TWordArrHelper.Fly.call({',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.a;',
- ' },',
- ' set: function (v) {',
- ' this.p.a = v;',
- ' }',
- '}, 3);',
- '']));
- end;
- procedure TTestGenerics.TestGenProc_Function_ObjFPC;
- begin
- StartProgram(false);
- Add([
- 'generic function Run<T>(a: T): T;',
- 'var i: T;',
- 'begin',
- ' a:=i;',
- ' Result:=a;',
- 'end;',
- 'var w: word;',
- 'begin',
- ' w:=specialize Run<word>(3);',
- '']);
- ConvertProgram;
- CheckSource('TestGenProc_Function_ObjFPC',
- LinesToStr([ // statements
- 'this.w = 0;',
- 'this.Run$G1 = function (a) {',
- ' var Result = 0;',
- ' var i = 0;',
- ' a = i;',
- ' Result = a;',
- ' return Result;',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.w = $mod.Run$G1(3);',
- '']));
- end;
- procedure TTestGenerics.TestGenProc_Function_Delphi;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- 'function Run<T>(a: T): T;',
- 'var i: T;',
- 'begin',
- ' a:=i;',
- ' Result:=a;',
- 'end;',
- 'var w: word;',
- 'begin',
- ' w:=Run<word>(3);',
- '']);
- ConvertProgram;
- CheckSource('TestGenProc_Function_Delphi',
- LinesToStr([ // statements
- 'this.w = 0;',
- 'this.Run$G1 = function (a) {',
- ' var Result = 0;',
- ' var i = 0;',
- ' a = i;',
- ' Result = a;',
- ' return Result;',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.w = $mod.Run$G1(3);',
- '']));
- end;
- procedure TTestGenerics.TestGenProc_Overload;
- begin
- StartProgram(false);
- Add([
- 'generic procedure DoIt<T>(a: T; w: word); overload;',
- 'begin',
- 'end;',
- 'generic procedure DoIt<T>(a: T; b: boolean); overload;',
- 'begin',
- 'end;',
- 'begin',
- ' specialize DoIt<word>(3,4);',
- ' specialize DoIt<boolean>(false,5);',
- ' specialize DoIt<word>(6,true);',
- ' specialize DoIt<double>(7.3,true);',
- '']);
- ConvertProgram;
- CheckSource('TestGenProc_Overload',
- LinesToStr([ // statements
- 'this.DoIt$G1 = function (a, w) {',
- '};',
- 'this.DoIt$G2 = function (a, w) {',
- '};',
- 'this.DoIt$1G1 = function (a, b) {',
- '};',
- 'this.DoIt$1G2 = function (a, b) {',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.DoIt$G1(3, 4);',
- '$mod.DoIt$G2(false, 5);',
- '$mod.DoIt$1G1(6, true);',
- '$mod.DoIt$1G2(7.3, true);',
- '']));
- end;
- procedure TTestGenerics.TestGenProc_Forward;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- 'procedure Run<S>(a: S; b: boolean); forward;',
- 'procedure Run<S>(a: S; b: boolean);',
- 'begin',
- ' Run<word>(1,true);',
- 'end;',
- 'begin',
- ' Run(1.3,true);',
- '']);
- ConvertProgram;
- CheckSource('TestGenProc_infer_OverloadForward',
- LinesToStr([ // statements
- 'this.Run$G1 = function (a, b) {',
- ' $mod.Run$G1(1, true);',
- '};',
- 'this.Run$G2 = function (a, b) {',
- ' $mod.Run$G1(1, true);',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Run$G2(1.3, true);',
- '']));
- end;
- procedure TTestGenerics.TestGenProc_Infer_OverloadForward;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- 'procedure {#A}Run<S>(a: S; b: boolean); forward; overload;',
- 'procedure {#B}Run<T>(a: T; w: word); forward; overload;',
- 'procedure {#C}Run<U>(a: U; b: U); forward; overload;',
- 'procedure {#A2}Run<S>(a: S; b: boolean); overload;',
- 'begin',
- ' {@A}Run(1,true);', // non generic take precedence
- ' {@B}Run(2,word(3));', // non generic take precedence
- ' {@C}Run(''foo'',''bar'');',
- 'end;',
- 'procedure {#B2}Run<T>(a: T; w: word); overload;',
- 'begin',
- 'end;',
- 'procedure {#C2}Run<U>(a: U; b: U); overload;',
- 'begin',
- 'end;',
- 'begin',
- ' {@A}Run(1,true);', // non generic take precedence
- ' {@B}Run(2,word(3));', // non generic take precedence
- ' {@C}Run(''foo'',''bar'');',
- '']);
- ConvertProgram;
- CheckSource('TestGenProc_infer_OverloadForward',
- LinesToStr([ // statements
- 'this.Run$G1 = function (a, b) {',
- ' $mod.Run$G1(1, true);',
- ' $mod.Run$1G1(2, 3);',
- ' $mod.Run$2G1("foo", "bar");',
- '};',
- 'this.Run$1G1 = function (a, w) {',
- '};',
- 'this.Run$2G1 = function (a, b) {',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Run$G1(1, true);',
- '$mod.Run$1G1(2, 3);',
- '$mod.Run$2G1("foo", "bar");',
- '']));
- end;
- procedure TTestGenerics.TestGenProc_TypeInfo;
- begin
- WithTypeInfo:=true;
- StartProgram(true,[supTypeInfo]);
- Add([
- '{$modeswitch implicitfunctionspecialization}',
- 'generic procedure Run<S>(a: S);',
- 'var',
- ' p: TTypeInfo;',
- 'begin',
- ' p:=TypeInfo(S);',
- ' p:=TypeInfo(a);',
- 'end;',
- 'begin',
- ' Run(word(3));',
- ' Run(''foo'');',
- '']);
- ConvertProgram;
- CheckSource('TestGenProc_TypeInfo',
- LinesToStr([ // statements
- 'this.Run$G1 = function (a) {',
- ' var p = null;',
- ' p = rtl.word;',
- ' p = rtl.word;',
- '};',
- 'this.Run$G2 = function (a) {',
- ' var p = null;',
- ' p = rtl.string;',
- ' p = rtl.string;',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Run$G1(3);',
- '$mod.Run$G2("foo");',
- '']));
- end;
- procedure TTestGenerics.TestGenProc_Infer_Widen;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- 'procedure Run<S>(a: S; b: S);',
- 'begin',
- 'end;',
- 'begin',
- ' Run(word(1),longint(2));',
- ' Run(byte(2),smallint(2));',
- ' Run(longword(3),longint(2));',
- ' Run(nativeint(4),longint(2));',
- ' Run(nativeint(5),nativeuint(2));',
- ' Run(''a'',''foo'');',
- ' Run(''bar'',''c'');',
- '']);
- ConvertProgram;
- CheckSource('TestGenProc_Infer_Widen',
- LinesToStr([ // statements
- 'this.Run$G1 = function (a, b) {',
- '};',
- 'this.Run$G2 = function (a, b) {',
- '};',
- 'this.Run$G3 = function (a, b) {',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Run$G1(1, 2);',
- '$mod.Run$G1(2, 2);',
- '$mod.Run$G2(3, 2);',
- '$mod.Run$G2(4, 2);',
- '$mod.Run$G2(5, 2);',
- '$mod.Run$G3("a", "foo");',
- '$mod.Run$G3("bar", "c");',
- '']));
- end;
- procedure TTestGenerics.TestGenProc_Infer_PassAsArg;
- begin
- StartProgram(false);
- Add([
- '{$mode delphi}',
- 'function Run<T>(a: T): T;',
- 'var b: T;',
- 'begin',
- ' Run(Run<word>(3));',
- ' Run(Run(word(4)));',
- 'end;',
- 'begin',
- ' Run(Run<word>(5));',
- ' Run(Run(word(6)));',
- '']);
- ConvertProgram;
- CheckSource('TestGenProc_Infer_PassAsArg',
- LinesToStr([ // statements
- 'this.Run$G1 = function (a) {',
- ' var Result = 0;',
- ' var b = 0;',
- ' $mod.Run$G1($mod.Run$G1(3));',
- ' $mod.Run$G1($mod.Run$G1(4));',
- ' return Result;',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Run$G1($mod.Run$G1(5));',
- '$mod.Run$G1($mod.Run$G1(6));',
- '']));
- end;
- procedure TTestGenerics.TestGenMethod_ObjFPC;
- begin
- StartProgram(false);
- Add([
- '{$mode objfpc}',
- '{$ModeSwitch implicitfunctionspecialization}',
- 'type',
- ' TObject = class',
- ' generic procedure {#A}Run<S>(a: S; b: boolean); overload;',
- ' generic procedure {#B}Run<T>(a: T; w: word); overload;',
- ' generic procedure {#C}Run<U>(a: U; b: U); overload;',
- ' end; ',
- 'generic procedure {#A2}TObject.Run<S>(a: S; b: boolean); overload;',
- 'begin',
- ' {@A}Run(1,true);', // non generic take precedence
- ' {@B}Run(2,word(3));', // non generic take precedence
- ' {@C}Run(''foo'',''bar'');',
- 'end;',
- 'generic procedure {#B2}TObject.Run<T>(a: T; w: word); overload;',
- 'begin',
- 'end;',
- 'generic procedure {#C2}TObject.Run<U>(a: U; b: U); overload;',
- 'begin',
- 'end;',
- 'var o: TObject;',
- 'begin',
- ' o.{@A}Run(1,true);', // non generic take precedence
- ' o.{@B}Run(2,word(3));', // non generic take precedence
- ' o.{@C}Run(''foo'',''bar'');',
- '']);
- ConvertProgram;
- CheckSource('TestGenMethod_ObjFPC',
- LinesToStr([ // statements
- 'rtl.createClass(this, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Run$G1 = function (a, b) {',
- ' this.Run$G1(1, true);',
- ' this.Run$1G1(2, 3);',
- ' this.Run$2G1("foo", "bar");',
- ' };',
- ' this.Run$1G1 = function (a, w) {',
- ' };',
- ' this.Run$2G1 = function (a, b) {',
- ' };',
- '});',
- 'this.o = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.o.Run$G1(1, true);',
- '$mod.o.Run$1G1(2, 3);',
- '$mod.o.Run$2G1("foo", "bar");',
- '']));
- end;
- procedure TTestGenerics.TestGen_Array_OtherUnit;
- begin
- WithTypeInfo:=true;
- StartProgram(true,[supTObject]);
- AddModuleWithIntfImplSrc('UnitA.pas',
- LinesToStr([
- 'type',
- ' generic TDyn<T> = array of T;',
- ' generic TStatic<T> = array[1..2] of T;',
- '']),
- '');
- AddModuleWithIntfImplSrc('UnitB.pas',
- LinesToStr([
- 'uses UnitA;',
- 'type',
- ' TAnt = class end;',
- ' TAntArray = specialize TDyn<TAnt>;',
- 'procedure Run;',
- '']),
- LinesToStr([
- 'procedure Run;',
- 'begin',
- ' if typeinfo(TAntArray)=nil then ;',
- 'end;',
- '']));
- Add([
- 'uses UnitB;',
- 'begin',
- ' Run;',
- '']);
- ConvertProgram;
- CheckUnit('UnitA.pas',
- LinesToStr([ // statements
- 'rtl.module("UnitA", ["system"], function () {',
- ' var $mod = this;',
- ' this.$rtti.$DynArray("TDyn<UnitB.TAnt>", {});',
- '});']));
- CheckUnit('UnitB.pas',
- LinesToStr([ // statements
- 'rtl.module("UnitB", ["system", "UnitA"], function () {',
- ' var $mod = this;',
- ' rtl.createClass(this, "TAnt", pas.system.TObject, function () {',
- ' });',
- ' this.Run = function () {',
- ' if (pas.UnitA.$rtti["TDyn<UnitB.TAnt>"] === null) ;',
- ' };',
- '});']));
- CheckSource('TestGen_Array_OtherUnit',
- LinesToStr([ // statements
- 'pas.UnitA.$rtti["TDyn<UnitB.TAnt>"].eltype = pas.UnitB.$rtti["TAnt"];',
- '']),
- LinesToStr([ // $mod.$main
- ' pas.UnitB.Run();',
- '']));
- end;
- procedure TTestGenerics.TestGen_ArrayOfUnitImplRec;
- begin
- WithTypeInfo:=true;
- StartProgram(true,[supTObject]);
- AddModuleWithIntfImplSrc('UnitA.pas',
- LinesToStr([
- 'type',
- ' generic TDyn<T> = array of T;',
- ' generic TStatic<T> = array[1..2] of T;',
- '']),
- LinesToStr([
- 'type',
- ' TBird = record',
- ' b: word;',
- ' end;',
- ' TAnt = class end;',
- ' TAntArray = specialize TDyn<TAnt>;',
- 'var',
- ' d: specialize TDyn<TBird>;',
- ' s: specialize TStatic<TBird>;',
- ' p: pointer;',
- 'begin',
- ' d[0].b:=s[1].b;',
- ' s:=s;',
- ' p:=typeinfo(TAntArray);',
- '']));
- Add([
- 'uses UnitA;',
- 'begin',
- '']);
- ConvertProgram;
- CheckUnit('UnitA.pas',
- LinesToStr([ // statements
- 'rtl.module("UnitA", ["system"], function () {',
- ' var $mod = this;',
- ' var $impl = $mod.$impl;',
- ' this.$rtti.$DynArray("TDyn<UnitA.TAnt>", {});',
- ' this.$rtti.$DynArray("TDyn<UnitA.TBird>", {});',
- ' this.TStatic$G1$clone = function (a) {',
- ' var r = [];',
- ' for (var i = 0; i < 2; i++) r.push($impl.TBird.$clone(a[i]));',
- ' return r;',
- ' };',
- ' this.$rtti.$StaticArray("TStatic<UnitA.TBird>", {',
- ' dims: [2]',
- ' });',
- ' $mod.$implcode = function () {',
- ' rtl.recNewT($impl, "TBird", function () {',
- ' this.b = 0;',
- ' this.$eq = function (b) {',
- ' return this.b === b.b;',
- ' };',
- ' this.$assign = function (s) {',
- ' this.b = s.b;',
- ' return this;',
- ' };',
- ' var $r = $mod.$rtti.$Record("TBird", {});',
- ' $r.addField("b", rtl.word);',
- ' });',
- ' rtl.createClass($impl, "TAnt", pas.system.TObject, function () {',
- ' });',
- ' $impl.d = [];',
- ' $impl.s = rtl.arraySetLength(null, $impl.TBird, 2);',
- ' $impl.p = null;',
- ' };',
- ' $mod.$init = function () {',
- ' $impl.d[0].b = $impl.s[0].b;',
- ' $impl.s = $mod.TStatic$G1$clone($impl.s);',
- ' $impl.p = $mod.$rtti["TDyn<UnitA.TAnt>"];',
- ' };',
- '}, []);']));
- CheckSource('TestGen_ArrayOfUnitImplRec',
- LinesToStr([ // statements
- 'pas.UnitA.$rtti["TDyn<UnitA.TAnt>"].eltype = pas.UnitA.$rtti["TAnt"];',
- 'pas.UnitA.$rtti["TDyn<UnitA.TBird>"].eltype = pas.UnitA.$rtti["TBird"];',
- 'pas.UnitA.$rtti["TStatic<UnitA.TBird>"].eltype = pas.UnitA.$rtti["TBird"];',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_ProcType_ProcLocal;
- begin
- StartProgram(false);
- Add([
- 'procedure Fly(w: word);',
- 'begin',
- 'end;',
- 'procedure Run(w: word);',
- 'type generic TProc<T> = procedure(a: T);',
- 'var p: specialize TProc<word>;',
- 'begin',
- ' p:=@Fly;',
- ' p(w);',
- 'end;',
- 'begin',
- 'end.']);
- ConvertProgram;
- CheckSource('TestGen_ProcType_ProcLocal',
- LinesToStr([ // statements
- 'this.Fly = function (w) {',
- '};',
- 'this.Run = function (w) {',
- ' var p = null;',
- ' p = $mod.Fly;',
- ' p(w);',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestGenerics.TestGen_ProcType_Local_RTTI_Fail;
- begin
- WithTypeInfo:=true;
- StartProgram(false);
- Add([
- 'procedure Fly(w: word);',
- 'begin',
- 'end;',
- 'procedure Run(w: word);',
- 'type generic TProc<T> = procedure(a: T);',
- 'var',
- ' p: specialize TProc<word>;',
- ' t: Pointer;',
- 'begin',
- ' p:=@Fly;',
- ' p(w);',
- ' t:=typeinfo(p);',
- 'end;',
- 'begin',
- 'end.']);
- SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
- ConvertProgram;
- end;
- procedure TTestGenerics.TestGen_ProcType_ParamUnitImpl;
- begin
- WithTypeInfo:=true;
- StartProgram(true,[supTObject]);
- AddModuleWithIntfImplSrc('UnitA.pas',
- LinesToStr([
- 'type',
- ' generic TAnt<T> = function(const a: T): T;',
- '']),
- LinesToStr([
- 'type',
- ' TBird = record',
- ' b: word;',
- ' end;',
- 'var',
- ' f: specialize TAnt<TBird>;',
- ' b: TBird;',
- ' p: pointer;',
- 'begin',
- ' b:=f(b);',
- ' p:=typeinfo(f);',
- '']));
- Add([
- 'uses UnitA;',
- 'begin',
- 'end.']);
- ConvertProgram;
- CheckUnit('UnitA.pas',
- LinesToStr([ // statements
- 'rtl.module("UnitA", ["system"], function () {',
- ' var $mod = this;',
- ' var $impl = $mod.$impl;',
- ' this.$rtti.$ProcVar("TAnt<UnitA.TBird>", {',
- ' init: function () {',
- ' this.procsig = rtl.newTIProcSig([["a", $mod.$rtti["TBird"], 2]], $mod.$rtti["TBird"]);',
- ' }',
- ' });',
- ' $mod.$implcode = function () {',
- ' rtl.recNewT($impl, "TBird", function () {',
- ' this.b = 0;',
- ' this.$eq = function (b) {',
- ' return this.b === b.b;',
- ' };',
- ' this.$assign = function (s) {',
- ' this.b = s.b;',
- ' return this;',
- ' };',
- ' var $r = $mod.$rtti.$Record("TBird", {});',
- ' $r.addField("b", rtl.word);',
- ' });',
- ' $impl.f = null;',
- ' $impl.b = $impl.TBird.$new();',
- ' $impl.p = null;',
- ' };',
- ' $mod.$init = function () {',
- ' $impl.b.$assign($impl.f($impl.b));',
- ' $impl.p = $mod.$rtti["TAnt<UnitA.TBird>"];',
- ' };',
- '}, []);']));
- CheckSource('TestGen_Class_ClassVarRecord_UnitImpl',
- LinesToStr([ // statements
- 'pas.UnitA.$rtti["TAnt<UnitA.TBird>"].init();',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- Initialization
- RegisterTests([TTestGenerics]);
- end.
|