pdecsub.pas 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { scanner }
  24. tokens,
  25. { symtable }
  26. symconst,symtype,symdef,symsym;
  27. type
  28. tpdflag=(
  29. pd_body, { directive needs a body }
  30. pd_implemen, { directive can be used in implementation section }
  31. pd_interface, { directive can be used in interface section }
  32. pd_object, { directive can be used with object declaration }
  33. pd_record, { directive can be used with record declaration }
  34. pd_procvar, { directive can be used with procvar declaration }
  35. pd_notobject, { directive can not be used with object declaration }
  36. pd_notrecord, { directive can not be used with record declaration }
  37. pd_notobjintf, { directive can not be used with interface declaration }
  38. pd_notprocvar, { directive can not be used with procvar declaration }
  39. pd_dispinterface,{ directive can be used with dispinterface methods }
  40. pd_cppobject, { directive can be used with cppclass }
  41. pd_objcclass, { directive can be used with objcclass }
  42. pd_objcprot, { directive can be used with objcprotocol }
  43. pd_nothelper, { directive can not be used with record/class helper declaration }
  44. pd_javaclass, { directive can be used with Java class }
  45. pd_intfjava { directive can be used with Java interface }
  46. );
  47. tpdflags=set of tpdflag;
  48. // flags of handle_calling_convention routine
  49. thccflag=(
  50. hcc_check, // perform checks and outup errors if found
  51. hcc_insert_hidden_paras // insert hidden parameters
  52. );
  53. thccflags=set of thccflag;
  54. const
  55. hcc_all=[hcc_check,hcc_insert_hidden_paras];
  56. function check_proc_directive(isprocvar:boolean):boolean;
  57. function proc_add_definition(var currpd:tprocdef):boolean;
  58. function proc_get_importname(pd:tprocdef):string;
  59. procedure proc_set_mangledname(pd:tprocdef);
  60. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  61. procedure parse_parameter_dec(pd:tabstractprocdef);
  62. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  63. procedure parse_var_proc_directives(sym:tsym);
  64. procedure parse_object_proc_directives(pd:tabstractprocdef);
  65. procedure parse_record_proc_directives(pd:tabstractprocdef);
  66. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;isgeneric:boolean;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  67. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef;isgeneric:boolean):tprocdef;
  68. procedure parse_proc_dec_finish(pd:tprocdef;isclassmethod:boolean);
  69. { parse a record method declaration (not a (class) constructor/destructor) }
  70. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  71. procedure insert_record_hidden_paras(astruct: trecorddef);
  72. { helper functions - they insert nested objects hierarchy to the symtablestack
  73. with object hierarchy
  74. }
  75. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  76. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  77. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  78. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  79. implementation
  80. uses
  81. SysUtils,
  82. { common }
  83. cutils,
  84. { global }
  85. globtype,globals,verbose,constexp,
  86. systems,
  87. cpuinfo,
  88. { symtable }
  89. symbase,symcpu,symtable,defutil,defcmp,
  90. { parameter handling }
  91. paramgr,cpupara,
  92. { pass 1 }
  93. fmodule,node,htypechk,ncon,ppu,nld,
  94. objcutil,
  95. { parser }
  96. scanner,
  97. pbase,pexpr,ptype,pdecl,pparautl,pgenutil
  98. {$ifdef jvm}
  99. ,pjvm
  100. {$endif}
  101. ;
  102. const
  103. { Please leave this here, this module should NOT use
  104. these variables.
  105. Declaring it as string here results in an error when compiling (PFV) }
  106. current_procinfo = 'error';
  107. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  108. var
  109. _class,hp : tobjectdef;
  110. begin
  111. if obj.typ=recorddef then
  112. begin
  113. symtablestack.push(obj.symtable);
  114. result:=1;
  115. exit;
  116. end;
  117. result:=0;
  118. { insert class hierarchy in the reverse order }
  119. hp:=nil;
  120. repeat
  121. _class:=tobjectdef(obj);
  122. while _class.childof<>hp do
  123. _class:=_class.childof;
  124. hp:=_class;
  125. symtablestack.push(_class.symtable);
  126. inc(result);
  127. until hp=obj;
  128. end;
  129. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  130. begin
  131. result:=0;
  132. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  133. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  134. inc(result,push_child_hierarchy(obj));
  135. end;
  136. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  137. var
  138. _class : tobjectdef;
  139. begin
  140. if obj.typ=recorddef then
  141. begin
  142. symtablestack.pop(obj.symtable);
  143. result:=1;
  144. exit;
  145. end;
  146. result:=0;
  147. _class:=tobjectdef(obj);
  148. while assigned(_class) do
  149. begin
  150. symtablestack.pop(_class.symtable);
  151. _class:=_class.childof;
  152. inc(result);
  153. end;
  154. end;
  155. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  156. begin
  157. result:=pop_child_hierarchy(obj);
  158. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  159. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  160. end;
  161. procedure check_msg_para(p:TObject;arg:pointer);
  162. begin
  163. if (tsym(p).typ<>paravarsym) then
  164. exit;
  165. with tparavarsym(p) do
  166. begin
  167. { Count parameters }
  168. if (paranr>=10) then
  169. inc(plongint(arg)^);
  170. { First parameter must be var }
  171. if (paranr=10) and
  172. (varspez<>vs_var) then
  173. MessagePos(fileinfo,parser_e_ill_msg_param);
  174. end;
  175. end;
  176. procedure set_addr_param_regable(p:TObject;arg:pointer);
  177. begin
  178. if (tsym(p).typ<>paravarsym) then
  179. exit;
  180. with tparavarsym(p) do
  181. begin
  182. if (not needs_finalization) and
  183. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  184. varregable:=vr_addr;
  185. end;
  186. end;
  187. procedure parse_parameter_dec(pd:tabstractprocdef);
  188. {
  189. handle_procvar needs the same changes
  190. }
  191. type
  192. tppv = (pv_none,pv_proc,pv_func);
  193. var
  194. sc : TFPObjectList;
  195. hdef : tdef;
  196. arrayelementdef : tdef;
  197. vs : tparavarsym;
  198. i : longint;
  199. srsym : tsym;
  200. pv : tprocvardef;
  201. varspez : Tvarspez;
  202. defaultvalue : tconstsym;
  203. defaultrequired : boolean;
  204. old_block_type : tblock_type;
  205. currparast : tparasymtable;
  206. parseprocvar : tppv;
  207. locationstr : string;
  208. paranr : integer;
  209. dummytype : ttypesym;
  210. explicit_paraloc,
  211. need_array,
  212. is_univ: boolean;
  213. stoptions : TSingleTypeOptions;
  214. procedure handle_default_para_value;
  215. var
  216. convpd : tprocdef;
  217. doconv : tconverttype;
  218. nodetype : tnodetype;
  219. bt : tblock_type;
  220. begin
  221. { only allowed for types that can be represented by a
  222. constant expression }
  223. if try_to_consume(_EQ) then
  224. begin
  225. if (hdef.typ in [recorddef,variantdef,filedef,formaldef]) or
  226. is_object(hdef) or
  227. ((hdef.typ=arraydef) and
  228. not is_dynamic_array(hdef)) then
  229. Message1(type_e_invalid_default_value,FullTypeName(hdef,nil));
  230. vs:=tparavarsym(sc[0]);
  231. if sc.count>1 then
  232. Message(parser_e_default_value_only_one_para);
  233. if not(vs.varspez in [vs_value,vs_const]) then
  234. Message(parser_e_default_value_val_const);
  235. bt:=block_type;
  236. block_type:=bt_const;
  237. { prefix 'def' to the parameter name }
  238. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo,nodetype);
  239. block_type:=bt;
  240. if assigned(defaultvalue) then
  241. begin
  242. include(defaultvalue.symoptions,sp_internal);
  243. pd.parast.insert(defaultvalue);
  244. { check whether the default value is of the correct
  245. type }
  246. if compare_defs_ext(defaultvalue.constdef,hdef,nodetype,doconv,convpd,[])<=te_convert_operator then
  247. MessagePos2(defaultvalue.fileinfo,type_e_incompatible_types,FullTypeName(defaultvalue.constdef,hdef),FullTypeName(hdef,defaultvalue.constdef));
  248. end;
  249. defaultrequired:=true;
  250. end
  251. else
  252. begin
  253. if defaultrequired then
  254. Message1(parser_e_default_value_expected_for_para,vs.name);
  255. end;
  256. end;
  257. begin
  258. old_block_type:=block_type;
  259. explicit_paraloc:=false;
  260. consume(_LKLAMMER);
  261. { Delphi/Kylix supports nonsense like }
  262. { procedure p(); }
  263. if try_to_consume(_RKLAMMER) and
  264. not(m_tp7 in current_settings.modeswitches) then
  265. exit;
  266. { parsing a proc or procvar ? }
  267. currparast:=tparasymtable(pd.parast);
  268. { reset }
  269. sc:=TFPObjectList.create(false);
  270. defaultrequired:=false;
  271. paranr:=0;
  272. block_type:=bt_var;
  273. is_univ:=false;
  274. repeat
  275. parseprocvar:=pv_none;
  276. if try_to_consume(_VAR) then
  277. varspez:=vs_var
  278. else
  279. if try_to_consume(_CONST) then
  280. varspez:=vs_const
  281. else
  282. if (m_out in current_settings.modeswitches) and
  283. try_to_consume(_OUT) then
  284. varspez:=vs_out
  285. else
  286. if try_to_consume(_CONSTREF) then
  287. varspez:=vs_constref
  288. else
  289. if (m_mac in current_settings.modeswitches) and
  290. try_to_consume(_POINTPOINTPOINT) then
  291. begin
  292. include(pd.procoptions,po_varargs);
  293. break;
  294. end
  295. else
  296. if (m_nested_procvars in current_settings.modeswitches) and
  297. try_to_consume(_PROCEDURE) then
  298. begin
  299. parseprocvar:=pv_proc;
  300. varspez:=vs_const;
  301. end
  302. else
  303. if (m_nested_procvars in current_settings.modeswitches) and
  304. try_to_consume(_FUNCTION) then
  305. begin
  306. parseprocvar:=pv_func;
  307. varspez:=vs_const;
  308. end
  309. else
  310. varspez:=vs_value;
  311. defaultvalue:=nil;
  312. hdef:=nil;
  313. { read identifiers and insert with error type }
  314. sc.clear;
  315. repeat
  316. inc(paranr);
  317. vs:=cparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  318. currparast.insert(vs);
  319. if assigned(vs.owner) then
  320. sc.add(vs)
  321. else
  322. vs.free;
  323. consume(_ID);
  324. until not try_to_consume(_COMMA);
  325. locationstr:='';
  326. { macpas anonymous procvar }
  327. if parseprocvar<>pv_none then
  328. begin
  329. { inline procvar definitions are always nested procvars }
  330. pv:=cprocvardef.create(normal_function_level+1);
  331. if token=_LKLAMMER then
  332. parse_parameter_dec(pv);
  333. if parseprocvar=pv_func then
  334. begin
  335. block_type:=bt_var_type;
  336. consume(_COLON);
  337. single_type(pv.returndef,[]);
  338. block_type:=bt_var;
  339. end;
  340. hdef:=pv;
  341. { possible proc directives }
  342. if check_proc_directive(true) then
  343. begin
  344. dummytype:=ctypesym.create('unnamed',hdef,true);
  345. parse_var_proc_directives(tsym(dummytype));
  346. dummytype.typedef:=nil;
  347. hdef.typesym:=nil;
  348. dummytype.free;
  349. end;
  350. { Add implicit hidden parameters and function result }
  351. handle_calling_convention(pv);
  352. {$ifdef jvm}
  353. { anonymous -> no name }
  354. jvm_create_procvar_class('',pv);
  355. {$endif}
  356. end
  357. else
  358. { read type declaration, force reading for value paras }
  359. if (token=_COLON) or (varspez=vs_value) then
  360. begin
  361. consume(_COLON);
  362. { check for an open array }
  363. need_array:=false;
  364. { bitpacked open array are not yet supported }
  365. if (token=_PACKED) and
  366. not(cs_bitpacking in current_settings.localswitches) then
  367. begin
  368. consume(_PACKED);
  369. need_array:=true;
  370. end;
  371. if (token=_ARRAY) or
  372. need_array then
  373. begin
  374. consume(_ARRAY);
  375. consume(_OF);
  376. { define range and type of range }
  377. hdef:=carraydef.create(0,-1,ptrsinttype);
  378. { array of const ? }
  379. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  380. begin
  381. consume(_CONST);
  382. srsym:=search_system_type('TVARREC');
  383. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  384. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  385. end
  386. else
  387. begin
  388. { define field type }
  389. if m_delphi in current_settings.modeswitches then
  390. stoptions:=[stoAllowSpecialization]
  391. else
  392. stoptions:=[];
  393. single_type(arrayelementdef,stoptions);
  394. tarraydef(hdef).elementdef:=arrayelementdef;
  395. end;
  396. end
  397. else
  398. begin
  399. if (m_mac in current_settings.modeswitches) then
  400. is_univ:=try_to_consume(_UNIV);
  401. if try_to_consume(_TYPE) then
  402. hdef:=ctypedformaltype
  403. else
  404. begin
  405. block_type:=bt_var_type;
  406. single_type(hdef,[stoAllowSpecialization]);
  407. block_type:=bt_var;
  408. end;
  409. { open string ? }
  410. if is_shortstring(hdef) then
  411. begin
  412. case varspez of
  413. vs_var,vs_out:
  414. begin
  415. { not 100% Delphi-compatible: type xstr=string[255] cannot
  416. become an openstring there, while here it can }
  417. if (cs_openstring in current_settings.moduleswitches) and
  418. (tstringdef(hdef).len=255) then
  419. hdef:=openshortstringtype
  420. end;
  421. vs_value:
  422. begin
  423. { value "openstring" parameters don't make sense (the
  424. original string can never be modified, so there's no
  425. use in passing its original length), so change these
  426. into regular shortstring parameters (seems to be what
  427. Delphi also does) }
  428. if is_open_string(hdef) then
  429. hdef:=cshortstringtype;
  430. end;
  431. end;
  432. end;
  433. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  434. begin
  435. if (idtoken=_LOCATION) then
  436. begin
  437. consume(_LOCATION);
  438. locationstr:=cstringpattern;
  439. consume(_CSTRING);
  440. end
  441. else
  442. begin
  443. if explicit_paraloc then
  444. Message(parser_e_paraloc_all_paras);
  445. locationstr:='';
  446. end;
  447. end
  448. else
  449. locationstr:='';
  450. { default parameter }
  451. if (m_default_para in current_settings.modeswitches) then
  452. handle_default_para_value;
  453. end;
  454. end
  455. else
  456. hdef:=cformaltype;
  457. { File types are only allowed for var and out parameters }
  458. if (hdef.typ=filedef) and
  459. not(varspez in [vs_out,vs_var]) then
  460. CGMessage(cg_e_file_must_call_by_reference);
  461. { Dispinterfaces are restricted to using only automatable types }
  462. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  463. not is_automatable(hdef) then
  464. Message1(type_e_not_automatable,hdef.typename);
  465. { univ cannot be used with types whose size is not known at compile
  466. time }
  467. if is_univ and
  468. not is_valid_univ_para_type(hdef) then
  469. Message1(parser_e_invalid_univ_para,hdef.typename);
  470. for i:=0 to sc.count-1 do
  471. begin
  472. vs:=tparavarsym(sc[i]);
  473. vs.univpara:=is_univ;
  474. { update varsym }
  475. vs.vardef:=hdef;
  476. vs.defaultconstsym:=defaultvalue;
  477. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  478. begin
  479. if locationstr<>'' then
  480. begin
  481. if sc.count>1 then
  482. Message(parser_e_paraloc_only_one_para);
  483. if (paranr>1) and not(explicit_paraloc) then
  484. Message(parser_e_paraloc_all_paras);
  485. explicit_paraloc:=true;
  486. include(vs.varoptions,vo_has_explicit_paraloc);
  487. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  488. message(parser_e_illegal_explicit_paraloc);
  489. end
  490. else
  491. if explicit_paraloc then
  492. Message(parser_e_paraloc_all_paras);
  493. end;
  494. end;
  495. until not try_to_consume(_SEMICOLON);
  496. if explicit_paraloc then
  497. begin
  498. pd.has_paraloc_info:=callerside;
  499. include(pd.procoptions,po_explicitparaloc);
  500. end;
  501. { remove parasymtable from stack }
  502. sc.free;
  503. { reset object options }
  504. block_type:=old_block_type;
  505. consume(_RKLAMMER);
  506. end;
  507. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;isgeneric:boolean;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  508. var
  509. hs : string;
  510. orgsp,sp,orgspnongen,spnongen : TIDString;
  511. dummysym,srsym : tsym;
  512. checkstack : psymtablestackitem;
  513. oldfilepos,
  514. classstartfilepos,
  515. procstartfilepos : tfileposinfo;
  516. i,
  517. index : longint;
  518. hadspecialize,
  519. firstpart,
  520. freegenericparams,
  521. found,
  522. searchagain : boolean;
  523. st,
  524. genericst: TSymtable;
  525. aprocsym : tprocsym;
  526. popclass : integer;
  527. ImplIntf : TImplementedInterface;
  528. old_parse_generic : boolean;
  529. old_current_structdef: tabstractrecorddef;
  530. old_current_genericdef,
  531. old_current_specializedef: tstoreddef;
  532. lasttoken,lastidtoken: ttoken;
  533. genericparams : tfphashobjectlist;
  534. procedure parse_operator_name;
  535. begin
  536. if (lasttoken in [first_overloaded..last_overloaded]) then
  537. begin
  538. optoken:=token;
  539. end
  540. else
  541. begin
  542. case lasttoken of
  543. _CARET:
  544. Message1(parser_e_overload_operator_failed,'**');
  545. _ID:
  546. case lastidtoken of
  547. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  548. _EXPLICIT:optoken:=_OP_EXPLICIT;
  549. _INC:optoken:=_OP_INC;
  550. _DEC:optoken:=_OP_DEC;
  551. else
  552. if (m_delphi in current_settings.modeswitches) then
  553. case lastidtoken of
  554. _IMPLICIT:optoken:=_ASSIGNMENT;
  555. _NEGATIVE:optoken:=_MINUS;
  556. _POSITIVE:optoken:=_PLUS;
  557. _LOGICALNOT:optoken:=_OP_NOT;
  558. _IN:optoken:=_OP_IN;
  559. _EQUAL:optoken:=_EQ;
  560. _NOTEQUAL:optoken:=_NE;
  561. _GREATERTHAN:optoken:=_GT;
  562. _GREATERTHANOREQUAL:optoken:=_GTE;
  563. _LESSTHAN:optoken:=_LT;
  564. _LESSTHANOREQUAL:optoken:=_LTE;
  565. _ADD:optoken:=_PLUS;
  566. _SUBTRACT:optoken:=_MINUS;
  567. _MULTIPLY:optoken:=_STAR;
  568. _DIVIDE:optoken:=_SLASH;
  569. _INTDIVIDE:optoken:=_OP_DIV;
  570. _MODULUS:optoken:=_OP_MOD;
  571. _LEFTSHIFT:optoken:=_OP_SHL;
  572. _RIGHTSHIFT:optoken:=_OP_SHR;
  573. _LOGICALAND:optoken:=_OP_AND;
  574. _LOGICALOR:optoken:=_OP_OR;
  575. _LOGICALXOR:optoken:=_OP_XOR;
  576. _BITWISEAND:optoken:=_OP_AND;
  577. _BITWISEOR:optoken:=_OP_OR;
  578. _BITWISEXOR:optoken:=_OP_XOR;
  579. else
  580. Message1(parser_e_overload_operator_failed,'');
  581. end
  582. else
  583. Message1(parser_e_overload_operator_failed,'');
  584. end
  585. else
  586. Message1(parser_e_overload_operator_failed,'');
  587. end;
  588. end;
  589. sp:=overloaded_names[optoken];
  590. orgsp:=sp;
  591. spnongen:=sp;
  592. orgspnongen:=orgsp;
  593. end;
  594. procedure consume_proc_name;
  595. var
  596. s : string;
  597. i : longint;
  598. sym : ttypesym;
  599. begin
  600. lasttoken:=token;
  601. lastidtoken:=idtoken;
  602. if assigned(genericparams) and freegenericparams then
  603. for i:=0 to genericparams.count-1 do
  604. begin
  605. sym:=ttypesym(genericparams[i]);
  606. if tstoreddef(sym.typedef).is_registered then
  607. begin
  608. sym.typedef.free;
  609. sym.typedef:=nil;
  610. end;
  611. sym.free;
  612. end;
  613. genericparams.free;
  614. genericparams:=nil;
  615. hadspecialize:=false;
  616. if potype=potype_operator then
  617. optoken:=NOTOKEN;
  618. if (potype=potype_operator) and (token<>_ID) then
  619. begin
  620. parse_operator_name;
  621. consume(token);
  622. end
  623. else
  624. begin
  625. sp:=pattern;
  626. orgsp:=orgpattern;
  627. spnongen:=sp;
  628. orgspnongen:=orgsp;
  629. if firstpart and
  630. not (m_delphi in current_settings.modeswitches) and
  631. (idtoken=_SPECIALIZE) then
  632. hadspecialize:=true;
  633. consume(_ID);
  634. if (isgeneric or (m_delphi in current_settings.modeswitches)) and
  635. (token in [_LT,_LSHARPBRACKET]) then
  636. begin
  637. consume(token);
  638. if token in [_GT,_RSHARPBRACKET] then
  639. message(type_e_type_id_expected)
  640. else
  641. begin
  642. genericparams:=parse_generic_parameters(true);
  643. if not assigned(genericparams) then
  644. internalerror(2015061201);
  645. if genericparams.count=0 then
  646. internalerror(2015061202);
  647. s:='';
  648. str(genericparams.count,s);
  649. spnongen:=sp;
  650. orgspnongen:=orgsp;
  651. sp:=sp+'$'+s;
  652. orgsp:=orgsp+'$'+s;
  653. end;
  654. if not try_to_consume(_GT) then
  655. consume(_RSHARPBRACKET);
  656. end;
  657. end;
  658. firstpart:=false;
  659. end;
  660. function search_object_name(sp:TIDString;gen_error:boolean):tsym;
  661. var
  662. storepos:tfileposinfo;
  663. srsymtable:TSymtable;
  664. begin
  665. storepos:=current_tokenpos;
  666. current_tokenpos:=procstartfilepos;
  667. searchsym(sp,result,srsymtable);
  668. if not assigned(result) then
  669. begin
  670. if gen_error then
  671. identifier_not_found(orgsp);
  672. result:=generrorsym;
  673. end;
  674. current_tokenpos:=storepos;
  675. end;
  676. procedure consume_generic_interface;
  677. var
  678. genparalist : tfpobjectlist;
  679. prettyname,
  680. specializename : ansistring;
  681. genname,
  682. ugenname : tidstring;
  683. gencount : string;
  684. begin
  685. consume(_LSHARPBRACKET);
  686. genparalist:=tfpobjectlist.create(false);
  687. if not parse_generic_specialization_types(genparalist,nil,prettyname,specializename) then
  688. srsym:=generrorsym
  689. else
  690. begin
  691. str(genparalist.count,gencount);
  692. genname:=sp+'$'+gencount;
  693. { ToDo: handle nested interfaces }
  694. genname:=generate_generic_name(genname,specializename,'');
  695. ugenname:=upper(genname);
  696. srsym:=search_object_name(ugenname,false);
  697. if not assigned(srsym) then
  698. begin
  699. Message1(type_e_generic_declaration_does_not_match,sp+'<'+prettyname+'>');
  700. srsym:=nil;
  701. exit;
  702. end;
  703. end;
  704. genparalist.free;
  705. consume(_RSHARPBRACKET);
  706. end;
  707. function handle_generic_interface:boolean;
  708. var
  709. i : longint;
  710. sym : ttypesym;
  711. typesrsym : tsym;
  712. typesrsymtable : tsymtable;
  713. specializename,
  714. prettyname: ansistring;
  715. error : boolean;
  716. genname,
  717. ugenname : tidstring;
  718. begin
  719. result:=false;
  720. if not assigned(genericparams) then
  721. exit;
  722. specializename:='';
  723. prettyname:='';
  724. error:=false;
  725. for i:=0 to genericparams.count-1 do
  726. begin
  727. sym:=ttypesym(genericparams[i]);
  728. { ToDo: position }
  729. if not searchsym(upper(sym.RealName),typesrsym,typesrsymtable) then
  730. begin
  731. message1(sym_e_id_not_found,sym.name);
  732. error:=true;
  733. continue;
  734. end;
  735. if typesrsym.typ<>typesym then
  736. begin
  737. message(type_e_type_id_expected);
  738. error:=true;
  739. continue;
  740. end;
  741. specializename:=specializename+'$'+ttypesym(typesrsym).typedef.fulltypename;
  742. if i>0 then
  743. prettyname:=prettyname+',';
  744. prettyname:=prettyname+ttypesym(typesrsym).prettyname;
  745. end;
  746. result:=true;
  747. if error then
  748. begin
  749. srsym:=generrorsym;
  750. exit;
  751. end;
  752. { ToDo: handle nested interfaces }
  753. genname:=generate_generic_name(sp,specializename,'');
  754. ugenname:=upper(genname);
  755. srsym:=search_object_name(ugenname,false);
  756. if not assigned(srsym) then
  757. begin
  758. Message1(type_e_generic_declaration_does_not_match,sp+'<'+prettyname+'>');
  759. srsym:=generrorsym;
  760. end;
  761. end;
  762. procedure specialize_generic_interface;
  763. var
  764. node : tnode;
  765. begin
  766. node:=factor(false,[ef_type_only,ef_had_specialize]);
  767. if node.nodetype=typen then
  768. begin
  769. sp:=ttypenode(node).typedef.typesym.name;
  770. end
  771. else
  772. sp:='';
  773. end;
  774. begin
  775. sp:='';
  776. orgsp:='';
  777. spnongen:='';
  778. orgspnongen:='';
  779. { Save the position where this procedure really starts }
  780. procstartfilepos:=current_tokenpos;
  781. old_parse_generic:=parse_generic;
  782. firstpart:=true;
  783. result:=false;
  784. pd:=nil;
  785. aprocsym:=nil;
  786. srsym:=nil;
  787. genericparams:=nil;
  788. freegenericparams:=true;
  789. hadspecialize:=false;
  790. if not assigned(genericdef) then
  791. begin
  792. consume_proc_name;
  793. { examine interface map: function/procedure iname.functionname=locfuncname }
  794. if assigned(astruct) and
  795. (astruct.typ=objectdef) and
  796. assigned(tobjectdef(astruct).ImplementedInterfaces) and
  797. (tobjectdef(astruct).ImplementedInterfaces.count>0) and
  798. (
  799. (token=_POINT) or
  800. (
  801. hadspecialize and
  802. (token=_ID)
  803. )
  804. ) then
  805. begin
  806. if hadspecialize and (token=_ID) then
  807. specialize_generic_interface;
  808. consume(_POINT);
  809. if hadspecialize or not handle_generic_interface then
  810. srsym:=search_object_name(sp,true);
  811. { qualifier is interface? }
  812. ImplIntf:=nil;
  813. if assigned(srsym) and
  814. (srsym.typ=typesym) and
  815. (ttypesym(srsym).typedef.typ=objectdef) then
  816. ImplIntf:=find_implemented_interface(tobjectdef(astruct),tobjectdef(ttypesym(srsym).typedef));
  817. if ImplIntf=nil then
  818. Message(parser_e_interface_id_expected)
  819. else
  820. { in case of a generic or specialized interface we need to use the
  821. name of the def instead of the symbol, so that always the correct
  822. name is used }
  823. if [df_generic,df_specialization]*ttypesym(srsym).typedef.defoptions<>[] then
  824. sp:=tobjectdef(ttypesym(srsym).typedef).objname^;
  825. { must be a directly implemented interface }
  826. if Assigned(ImplIntf.ImplementsGetter) then
  827. Message2(parser_e_implements_no_mapping,ImplIntf.IntfDef.typename,astruct.objrealname^);
  828. consume(_ID);
  829. { Create unique name <interface>.<method> }
  830. hs:=sp+'.'+pattern;
  831. consume(_EQ);
  832. if assigned(ImplIntf) and
  833. (token=_ID) then
  834. ImplIntf.AddMapping(hs,pattern);
  835. consume(_ID);
  836. result:=true;
  837. exit;
  838. end;
  839. { method ? }
  840. srsym:=nil;
  841. if not assigned(astruct) and
  842. (symtablestack.top.symtablelevel=main_program_level) and
  843. try_to_consume(_POINT) then
  844. begin
  845. repeat
  846. classstartfilepos:=procstartfilepos;
  847. searchagain:=false;
  848. { throw the error at the right location }
  849. oldfilepos:=current_filepos;
  850. current_filepos:=procstartfilepos;
  851. if not assigned(astruct) and not assigned(srsym) then
  852. srsym:=search_object_name(sp,true);
  853. current_filepos:=oldfilepos;
  854. { consume proc name }
  855. procstartfilepos:=current_tokenpos;
  856. consume_proc_name;
  857. { qualifier is class name ? }
  858. if (srsym.typ=typesym) and
  859. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  860. begin
  861. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  862. if (token<>_POINT) then
  863. if (potype in [potype_class_constructor,potype_class_destructor]) then
  864. sp:=lower(sp)
  865. else
  866. if (potype=potype_operator) and (optoken=NOTOKEN) then
  867. parse_operator_name;
  868. srsym:=tsym(astruct.symtable.Find(sp));
  869. if assigned(srsym) then
  870. begin
  871. if srsym.typ=procsym then
  872. aprocsym:=tprocsym(srsym)
  873. else
  874. if (srsym.typ=typesym) and
  875. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  876. begin
  877. searchagain:=true;
  878. consume(_POINT);
  879. end
  880. else
  881. begin
  882. { we use a different error message for tp7 so it looks more compatible }
  883. if (m_fpc in current_settings.modeswitches) then
  884. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  885. else
  886. Message(parser_e_methode_id_expected);
  887. { rename the name to an unique name to avoid an
  888. error when inserting the symbol in the symtable }
  889. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  890. end;
  891. end
  892. else
  893. begin
  894. MessagePos(procstartfilepos,parser_e_methode_id_expected);
  895. { recover by making it a normal procedure instead of method }
  896. astruct:=nil;
  897. end;
  898. end
  899. else
  900. MessagePos(classstartfilepos,parser_e_class_id_expected);
  901. until not searchagain;
  902. end
  903. else
  904. begin
  905. { check for constructor/destructor/class operators which are not allowed here }
  906. if (not parse_only) and
  907. ((potype in [potype_constructor,potype_destructor,
  908. potype_class_constructor,potype_class_destructor]) or
  909. ((potype=potype_operator) and (m_delphi in current_settings.modeswitches))) then
  910. Message(parser_e_only_methods_allowed);
  911. repeat
  912. { only 1 class constructor and destructor is allowed in the class and
  913. the check was already done with oo_has_class_constructor or
  914. oo_has_class_destructor -> skip searching
  915. (bug #28801) }
  916. if (potype in [potype_class_constructor,potype_class_destructor]) then
  917. break;
  918. searchagain:=false;
  919. current_tokenpos:=procstartfilepos;
  920. if (potype=potype_operator)and(optoken=NOTOKEN) then
  921. parse_operator_name;
  922. srsym:=tsym(symtablestack.top.Find(sp));
  923. { Also look in the globalsymtable if we didn't found
  924. the symbol in the localsymtable }
  925. if not assigned(srsym) and
  926. not(parse_only) and
  927. (symtablestack.top=current_module.localsymtable) and
  928. assigned(current_module.globalsymtable) then
  929. srsym:=tsym(current_module.globalsymtable.Find(sp));
  930. { Check if overloaded is a procsym }
  931. if assigned(srsym) then
  932. begin
  933. if srsym.typ=procsym then
  934. aprocsym:=tprocsym(srsym)
  935. else
  936. begin
  937. { when the other symbol is a unit symbol then hide the unit
  938. symbol, this is not supported in tp7 }
  939. if not(m_tp7 in current_settings.modeswitches) and
  940. (srsym.typ=unitsym) then
  941. begin
  942. HideSym(srsym);
  943. searchagain:=true;
  944. end
  945. else
  946. if (m_delphi in current_settings.modeswitches) and
  947. (srsym.typ=absolutevarsym) and
  948. ([vo_is_funcret,vo_is_result]*tabstractvarsym(srsym).varoptions=[vo_is_funcret]) then
  949. begin
  950. HideSym(srsym);
  951. searchagain:=true;
  952. end
  953. else
  954. begin
  955. { we use a different error message for tp7 so it looks more compatible }
  956. if (m_fpc in current_settings.modeswitches) then
  957. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  958. else
  959. Message1(sym_e_duplicate_id,srsym.realname);
  960. { rename the name to an unique name to avoid an
  961. error when inserting the symbol in the symtable }
  962. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  963. end;
  964. end;
  965. end;
  966. until not searchagain;
  967. end;
  968. { test again if assigned, it can be reset to recover }
  969. if not assigned(aprocsym) then
  970. begin
  971. { create a new procsym and set the real filepos }
  972. current_tokenpos:=procstartfilepos;
  973. { for operator we have only one procsym for each overloaded
  974. operation }
  975. if (potype=potype_operator) then
  976. begin
  977. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  978. if aprocsym=nil then
  979. aprocsym:=cprocsym.create('$'+sp);
  980. end
  981. else
  982. if (potype in [potype_class_constructor,potype_class_destructor]) then
  983. aprocsym:=cprocsym.create('$'+lower(sp))
  984. else
  985. aprocsym:=cprocsym.create(orgsp);
  986. symtablestack.top.insert(aprocsym);
  987. end;
  988. end;
  989. { to get the correct symtablelevel we must ignore ObjectSymtables }
  990. st:=nil;
  991. checkstack:=symtablestack.stack;
  992. while assigned(checkstack) do
  993. begin
  994. st:=checkstack^.symtable;
  995. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  996. break;
  997. checkstack:=checkstack^.next;
  998. end;
  999. pd:=cprocdef.create(st.symtablelevel+1,not assigned(genericdef));
  1000. pd.struct:=astruct;
  1001. pd.procsym:=aprocsym;
  1002. pd.proctypeoption:=potype;
  1003. if assigned(genericparams) then
  1004. begin
  1005. include(pd.defoptions,df_generic);
  1006. { push the parameter symtable so that constraint definitions are added
  1007. there and not in the owner symtable }
  1008. symtablestack.push(pd.parast);
  1009. { register the parameters }
  1010. for i:=0 to genericparams.count-1 do
  1011. begin
  1012. ttypesym(genericparams[i]).register_sym;
  1013. tstoreddef(ttypesym(genericparams[i]).typedef).register_def;
  1014. end;
  1015. insert_generic_parameter_types(pd,nil,genericparams);
  1016. symtablestack.pop(pd.parast);
  1017. freegenericparams:=false;
  1018. parse_generic:=true;
  1019. { also generate a dummy symbol if none exists already }
  1020. if assigned(astruct) then
  1021. dummysym:=tsym(astruct.symtable.find(spnongen))
  1022. else
  1023. begin
  1024. dummysym:=tsym(symtablestack.top.find(spnongen));
  1025. if not assigned(dummysym) and
  1026. (symtablestack.top=current_module.localsymtable) and
  1027. assigned(current_module.globalsymtable) then
  1028. dummysym:=tsym(current_module.globalsymtable.find(spnongen));
  1029. end;
  1030. if not assigned(dummysym) then
  1031. begin
  1032. dummysym:=ctypesym.create(orgspnongen,cundefineddef.create(true),true);
  1033. if assigned(astruct) then
  1034. astruct.symtable.insert(dummysym)
  1035. else
  1036. symtablestack.top.insert(dummysym);
  1037. end;
  1038. include(dummysym.symoptions,sp_generic_dummy);
  1039. { start token recorder for the declaration }
  1040. pd.init_genericdecl;
  1041. current_scanner.startrecordtokens(pd.genericdecltokenbuf);
  1042. end;
  1043. if assigned(genericdef) and not assigned(genericparams) then
  1044. insert_generic_parameter_types(pd,tstoreddef(genericdef),generictypelist);
  1045. { methods inherit df_generic or df_specialization from the objectdef }
  1046. if assigned(pd.struct) and
  1047. (pd.parast.symtablelevel=normal_function_level) then
  1048. begin
  1049. if (df_generic in pd.struct.defoptions) then
  1050. begin
  1051. include(pd.defoptions,df_generic);
  1052. parse_generic:=true;
  1053. end;
  1054. if (df_specialization in pd.struct.defoptions) then
  1055. begin
  1056. if assigned(current_specializedef) then
  1057. begin
  1058. include(pd.defoptions,df_specialization);
  1059. { Find corresponding genericdef, we need it later to
  1060. replay the tokens to generate the body }
  1061. if not assigned(pd.struct.genericdef) then
  1062. internalerror(200512113);
  1063. genericst:=pd.struct.genericdef.GetSymtable(gs_record);
  1064. if not assigned(genericst) then
  1065. internalerror(200512114);
  1066. { when searching for the correct procdef to use as genericdef we need to ignore
  1067. everything except procdefs so that we can find the correct indices }
  1068. index:=0;
  1069. found:=false;
  1070. for i:=0 to pd.owner.deflist.count-1 do
  1071. begin
  1072. if tdef(pd.owner.deflist[i]).typ<>procdef then
  1073. continue;
  1074. if pd.owner.deflist[i]=pd then
  1075. begin
  1076. found:=true;
  1077. break;
  1078. end;
  1079. inc(index);
  1080. end;
  1081. if not found then
  1082. internalerror(2014052301);
  1083. for i:=0 to genericst.deflist.count-1 do
  1084. begin
  1085. if tdef(genericst.deflist[i]).typ<>procdef then
  1086. continue;
  1087. if index=0 then
  1088. pd.genericdef:=tstoreddef(genericst.deflist[i]);
  1089. dec(index);
  1090. end;
  1091. if not assigned(pd.genericdef) or
  1092. (pd.genericdef.typ<>procdef) then
  1093. internalerror(200512115);
  1094. end
  1095. else
  1096. Message(parser_e_explicit_method_implementation_for_specializations_not_allowed);
  1097. end;
  1098. end;
  1099. { methods need to be exported }
  1100. if assigned(astruct) and
  1101. (
  1102. (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) or
  1103. (symtablestack.top.symtablelevel=main_program_level)
  1104. ) then
  1105. include(pd.procoptions,po_global);
  1106. { symbol options that need to be kept per procdef }
  1107. pd.fileinfo:=procstartfilepos;
  1108. pd.visibility:=symtablestack.top.currentvisibility;
  1109. if symtablestack.top.currentlyoptional then
  1110. include(pd.procoptions,po_optional);
  1111. { parse parameters }
  1112. if token=_LKLAMMER then
  1113. begin
  1114. old_current_structdef:=nil;
  1115. old_current_genericdef:=current_genericdef;
  1116. old_current_specializedef:=nil;
  1117. { Add ObjectSymtable to be able to find nested type definitions }
  1118. popclass:=0;
  1119. if assigned(pd.struct) and
  1120. (pd.parast.symtablelevel>=normal_function_level) and
  1121. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1122. begin
  1123. popclass:=push_nested_hierarchy(pd.struct);
  1124. old_current_structdef:=current_structdef;
  1125. old_current_specializedef:=current_specializedef;
  1126. current_structdef:=pd.struct;
  1127. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1128. current_genericdef:=current_structdef;
  1129. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1130. current_specializedef:=current_structdef;
  1131. end;
  1132. if pd.is_generic then
  1133. current_genericdef:=pd;
  1134. { Add parameter symtable }
  1135. if pd.parast.symtabletype<>staticsymtable then
  1136. symtablestack.push(pd.parast);
  1137. parse_parameter_dec(pd);
  1138. if pd.parast.symtabletype<>staticsymtable then
  1139. symtablestack.pop(pd.parast);
  1140. current_genericdef:=old_current_genericdef;
  1141. if popclass>0 then
  1142. begin
  1143. current_structdef:=old_current_structdef;
  1144. current_specializedef:=old_current_specializedef;
  1145. dec(popclass,pop_nested_hierarchy(pd.struct));
  1146. if popclass<>0 then
  1147. internalerror(201011260); // 11 nov 2010 index 0
  1148. end;
  1149. end;
  1150. parse_generic:=old_parse_generic;
  1151. result:=true;
  1152. end;
  1153. procedure parse_proc_dec_finish(pd:tprocdef;isclassmethod:boolean);
  1154. var
  1155. locationstr: string;
  1156. i: integer;
  1157. found: boolean;
  1158. procedure read_returndef(pd: tprocdef);
  1159. var
  1160. popclass: integer;
  1161. old_parse_generic: boolean;
  1162. old_current_structdef: tabstractrecorddef;
  1163. old_current_genericdef,
  1164. old_current_specializedef: tstoreddef;
  1165. begin
  1166. old_parse_generic:=parse_generic;
  1167. { Add ObjectSymtable to be able to find generic type definitions }
  1168. popclass:=0;
  1169. old_current_structdef:=nil;
  1170. old_current_genericdef:=nil;
  1171. old_current_specializedef:=nil;
  1172. if assigned(pd.struct) and
  1173. (pd.parast.symtablelevel>=normal_function_level) and
  1174. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1175. begin
  1176. popclass:=push_nested_hierarchy(pd.struct);
  1177. parse_generic:=(df_generic in pd.struct.defoptions);
  1178. old_current_structdef:=current_structdef;
  1179. old_current_genericdef:=current_genericdef;
  1180. old_current_specializedef:=current_specializedef;
  1181. current_structdef:=pd.struct;
  1182. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1183. current_genericdef:=current_structdef;
  1184. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1185. current_specializedef:=current_structdef;
  1186. end;
  1187. if pd.is_generic or pd.is_specialization then
  1188. symtablestack.push(pd.parast);
  1189. single_type(pd.returndef,[stoAllowSpecialization]);
  1190. // Issue #24863, enabled only for the main progra commented out for now because it breaks building of RTL and needs extensive
  1191. // testing and/or RTL patching.
  1192. {
  1193. if ((pd.returndef=cvarianttype) or (pd.returndef=colevarianttype)) and
  1194. not(cs_compilesystem in current_settings.moduleswitches) then
  1195. current_module.flags:=current_module.flags or uf_uses_variants;
  1196. }
  1197. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  1198. Message1(type_e_not_automatable,pd.returndef.typename);
  1199. if pd.is_generic or pd.is_specialization then
  1200. symtablestack.pop(pd.parast);
  1201. if popclass>0 then
  1202. begin
  1203. current_structdef:=old_current_structdef;
  1204. current_genericdef:=old_current_genericdef;
  1205. current_specializedef:=old_current_specializedef;
  1206. dec(popclass,pop_nested_hierarchy(pd.struct));
  1207. if popclass<>0 then
  1208. internalerror(201012020);
  1209. end;
  1210. parse_generic:=old_parse_generic;
  1211. end;
  1212. begin
  1213. locationstr:='';
  1214. case pd.proctypeoption of
  1215. potype_procedure:
  1216. begin
  1217. pd.returndef:=voidtype;
  1218. if isclassmethod then
  1219. include(pd.procoptions,po_classmethod);
  1220. end;
  1221. potype_function:
  1222. begin
  1223. if try_to_consume(_COLON) then
  1224. begin
  1225. read_returndef(pd);
  1226. if (target_info.system in [system_m68k_amiga]) then
  1227. begin
  1228. if (idtoken=_LOCATION) then
  1229. begin
  1230. if po_explicitparaloc in pd.procoptions then
  1231. begin
  1232. consume(_LOCATION);
  1233. locationstr:=cstringpattern;
  1234. consume(_CSTRING);
  1235. end
  1236. else
  1237. { I guess this needs a new message... (KB) }
  1238. Message(parser_e_paraloc_all_paras);
  1239. end
  1240. else
  1241. begin
  1242. if po_explicitparaloc in pd.procoptions then
  1243. { assign default locationstr, if none specified }
  1244. { and we've arguments with explicit paraloc }
  1245. locationstr:='D0';
  1246. end;
  1247. end;
  1248. end
  1249. else
  1250. begin
  1251. if (
  1252. parse_only and
  1253. not(is_interface(pd.struct))
  1254. ) or
  1255. (m_repeat_forward in current_settings.modeswitches) then
  1256. begin
  1257. consume(_COLON);
  1258. consume_all_until(_SEMICOLON);
  1259. end;
  1260. end;
  1261. if isclassmethod then
  1262. include(pd.procoptions,po_classmethod);
  1263. end;
  1264. potype_constructor,
  1265. potype_class_constructor:
  1266. begin
  1267. if not isclassmethod and
  1268. assigned(pd) and
  1269. assigned(pd.struct) then
  1270. begin
  1271. { Set return type, class constructors return the
  1272. created instance, object constructors return boolean }
  1273. if is_class(pd.struct) or
  1274. is_record(pd.struct) or
  1275. is_javaclass(pd.struct) then
  1276. pd.returndef:=pd.struct
  1277. else
  1278. if is_objectpascal_helper(pd.struct) then
  1279. pd.returndef:=tobjectdef(pd.struct).extendeddef
  1280. else
  1281. {$ifdef CPU64bitaddr}
  1282. pd.returndef:=bool64type;
  1283. {$else CPU64bitaddr}
  1284. pd.returndef:=bool32type;
  1285. {$endif CPU64bitaddr}
  1286. end
  1287. else
  1288. pd.returndef:=voidtype;
  1289. end;
  1290. potype_class_destructor,
  1291. potype_destructor:
  1292. begin
  1293. if assigned(pd) then
  1294. pd.returndef:=voidtype;
  1295. end;
  1296. potype_operator:
  1297. begin
  1298. { operators always need to be searched in all units (that
  1299. contain operators) }
  1300. include(pd.procoptions,po_overload);
  1301. pd.procsym.owner.includeoption(sto_has_operator);
  1302. if pd.parast.symtablelevel>normal_function_level then
  1303. Message(parser_e_no_local_operator);
  1304. if isclassmethod then
  1305. include(pd.procoptions,po_classmethod);
  1306. if token<>_ID then
  1307. begin
  1308. if not(m_result in current_settings.modeswitches) then
  1309. consume(_ID);
  1310. end
  1311. else
  1312. begin
  1313. pd.resultname:=stringdup(orgpattern);
  1314. consume(_ID);
  1315. end;
  1316. if not try_to_consume(_COLON) then
  1317. begin
  1318. consume(_COLON);
  1319. pd.returndef:=generrordef;
  1320. consume_all_until(_SEMICOLON);
  1321. end
  1322. else
  1323. begin
  1324. read_returndef(pd);
  1325. { check that class operators have either return type of structure or }
  1326. { at least one argument of that type }
  1327. if (po_classmethod in pd.procoptions) and
  1328. (pd.returndef <> pd.struct) then
  1329. begin
  1330. found:=false;
  1331. for i := 0 to pd.parast.SymList.Count - 1 do
  1332. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1333. begin
  1334. found:=true;
  1335. break;
  1336. end;
  1337. if not found then
  1338. if assigned(pd.struct) then
  1339. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName)
  1340. else
  1341. MessagePos(pd.fileinfo,type_e_type_id_expected);
  1342. end;
  1343. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1344. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1345. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1346. message(parser_e_no_such_assignment)
  1347. else if not isoperatoracceptable(pd,optoken) then
  1348. Message(parser_e_overload_impossible);
  1349. end;
  1350. end;
  1351. else
  1352. internalerror(2015052202);
  1353. end;
  1354. { file types can't be function results }
  1355. if assigned(pd) and
  1356. (pd.returndef.typ=filedef) then
  1357. message(parser_e_illegal_function_result);
  1358. { support procedure proc stdcall export; }
  1359. if not(check_proc_directive(false)) then
  1360. begin
  1361. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1362. begin
  1363. message(parser_e_field_not_allowed_here);
  1364. consume_all_until(_SEMICOLON);
  1365. end;
  1366. consume(_SEMICOLON);
  1367. end;
  1368. if locationstr<>'' then
  1369. begin
  1370. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1371. { I guess this needs a new message... (KB) }
  1372. message(parser_e_illegal_explicit_paraloc);
  1373. end;
  1374. end;
  1375. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef;isgeneric:boolean):tprocdef;
  1376. var
  1377. pd : tprocdef;
  1378. old_block_type : tblock_type;
  1379. recover : boolean;
  1380. procedure finish_intf_mapping;
  1381. begin
  1382. if token=_COLON then
  1383. begin
  1384. message(parser_e_field_not_allowed_here);
  1385. consume_all_until(_SEMICOLON);
  1386. end;
  1387. consume(_SEMICOLON);
  1388. end;
  1389. begin
  1390. pd:=nil;
  1391. recover:=false;
  1392. case token of
  1393. _FUNCTION :
  1394. begin
  1395. consume(_FUNCTION);
  1396. if parse_proc_head(astruct,potype_function,isgeneric,nil,nil,pd) then
  1397. begin
  1398. { pd=nil when it is a interface mapping }
  1399. if assigned(pd) then
  1400. parse_proc_dec_finish(pd,isclassmethod)
  1401. else
  1402. finish_intf_mapping;
  1403. end
  1404. else
  1405. begin
  1406. { recover }
  1407. consume(_COLON);
  1408. consume_all_until(_SEMICOLON);
  1409. recover:=true;
  1410. end;
  1411. end;
  1412. _PROCEDURE :
  1413. begin
  1414. consume(_PROCEDURE);
  1415. if parse_proc_head(astruct,potype_procedure,isgeneric,nil,nil,pd) then
  1416. begin
  1417. { pd=nil when it is an interface mapping }
  1418. if assigned(pd) then
  1419. parse_proc_dec_finish(pd,isclassmethod)
  1420. else
  1421. finish_intf_mapping;
  1422. end
  1423. else
  1424. recover:=true;
  1425. end;
  1426. _CONSTRUCTOR :
  1427. begin
  1428. consume(_CONSTRUCTOR);
  1429. if isclassmethod then
  1430. recover:=not parse_proc_head(astruct,potype_class_constructor,false,nil,nil,pd)
  1431. else
  1432. recover:=not parse_proc_head(astruct,potype_constructor,false,nil,nil,pd);
  1433. if not recover then
  1434. parse_proc_dec_finish(pd,isclassmethod);
  1435. end;
  1436. _DESTRUCTOR :
  1437. begin
  1438. consume(_DESTRUCTOR);
  1439. if isclassmethod then
  1440. recover:=not parse_proc_head(astruct,potype_class_destructor,false,nil,nil,pd)
  1441. else
  1442. recover:=not parse_proc_head(astruct,potype_destructor,false,nil,nil,pd);
  1443. if not recover then
  1444. parse_proc_dec_finish(pd,isclassmethod);
  1445. end;
  1446. else
  1447. if (token=_OPERATOR) or
  1448. (isclassmethod and (idtoken=_OPERATOR)) then
  1449. begin
  1450. { we need to set the block type to bt_body, so that operator names
  1451. like ">", "=>" or "<>" are parsed correctly instead of e.g.
  1452. _LSHARPBRACKET and _RSHARPBRACKET for "<>" }
  1453. old_block_type:=block_type;
  1454. block_type:=bt_body;
  1455. consume(_OPERATOR);
  1456. parse_proc_head(astruct,potype_operator,false,nil,nil,pd);
  1457. block_type:=old_block_type;
  1458. if assigned(pd) then
  1459. parse_proc_dec_finish(pd,isclassmethod)
  1460. else
  1461. begin
  1462. { recover }
  1463. try_to_consume(_ID);
  1464. consume(_COLON);
  1465. consume_all_until(_SEMICOLON);
  1466. recover:=true;
  1467. end;
  1468. end;
  1469. end;
  1470. if recover and not(check_proc_directive(false)) then
  1471. begin
  1472. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1473. begin
  1474. message(parser_e_field_not_allowed_here);
  1475. consume_all_until(_SEMICOLON);
  1476. end;
  1477. consume(_SEMICOLON);
  1478. end;
  1479. { we've parsed the final semicolon, so stop recording tokens }
  1480. if assigned(pd) and
  1481. (df_generic in pd.defoptions) and
  1482. assigned(pd.genericdecltokenbuf) then
  1483. current_scanner.stoprecordtokens;
  1484. result:=pd;
  1485. end;
  1486. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  1487. var
  1488. oldparse_only: boolean;
  1489. begin
  1490. oldparse_only:=parse_only;
  1491. parse_only:=true;
  1492. result:=parse_proc_dec(is_classdef,astruct,hadgeneric);
  1493. { this is for error recovery as well as forward }
  1494. { interface mappings, i.e. mapping to a method }
  1495. { which isn't declared yet }
  1496. if assigned(result) then
  1497. begin
  1498. parse_record_proc_directives(result);
  1499. { since records have no inheritance, don't allow non-static
  1500. class methods. Delphi does the same. }
  1501. if (result.proctypeoption<>potype_operator) and
  1502. is_classdef and
  1503. not (po_staticmethod in result.procoptions) then
  1504. MessagePos(result.fileinfo, parser_e_class_methods_only_static_in_records);
  1505. // we can't add hidden params here because record is not yet defined
  1506. // and therefore record size which has influence on paramter passing rules may change too
  1507. // look at record_dec to see where calling conventions are applied (issue #0021044)
  1508. handle_calling_convention(result,[hcc_check]);
  1509. { add definition to procsym }
  1510. proc_add_definition(result);
  1511. end;
  1512. maybe_parse_hint_directives(result);
  1513. parse_only:=oldparse_only;
  1514. end;
  1515. procedure insert_record_hidden_paras(astruct: trecorddef);
  1516. var
  1517. pd: tdef;
  1518. i: longint;
  1519. oldpos : tfileposinfo;
  1520. oldparse_only: boolean;
  1521. begin
  1522. // handle calling conventions of record methods
  1523. oldpos:=current_filepos;
  1524. oldparse_only:=parse_only;
  1525. parse_only:=true;
  1526. { don't keep track of procdefs in a separate list, because the
  1527. compiler may add additional procdefs (e.g. property wrappers for
  1528. the jvm backend) }
  1529. for i := 0 to astruct.symtable.deflist.count - 1 do
  1530. begin
  1531. pd:=tdef(astruct.symtable.deflist[i]);
  1532. if pd.typ<>procdef then
  1533. continue;
  1534. current_filepos:=tprocdef(pd).fileinfo;
  1535. handle_calling_convention(tprocdef(pd),[hcc_insert_hidden_paras]);
  1536. end;
  1537. parse_only:=oldparse_only;
  1538. current_filepos:=oldpos;
  1539. end;
  1540. {****************************************************************************
  1541. Procedure directive handlers
  1542. ****************************************************************************}
  1543. procedure pd_far(pd:tabstractprocdef);
  1544. begin
  1545. pd.declared_far;
  1546. end;
  1547. procedure pd_near(pd:tabstractprocdef);
  1548. begin
  1549. pd.declared_near;
  1550. end;
  1551. procedure pd_export(pd:tabstractprocdef);
  1552. begin
  1553. if pd.typ<>procdef then
  1554. internalerror(200304264);
  1555. if assigned(tprocdef(pd).struct) then
  1556. Message(parser_e_methods_dont_be_export);
  1557. if pd.parast.symtablelevel>normal_function_level then
  1558. Message(parser_e_dont_nest_export);
  1559. end;
  1560. procedure pd_forward(pd:tabstractprocdef);
  1561. begin
  1562. if pd.typ<>procdef then
  1563. internalerror(200304265);
  1564. tprocdef(pd).forwarddef:=true;
  1565. end;
  1566. procedure pd_alias(pd:tabstractprocdef);
  1567. begin
  1568. if pd.typ<>procdef then
  1569. internalerror(200304266);
  1570. consume(_COLON);
  1571. tprocdef(pd).aliasnames.insert(get_stringconst);
  1572. include(pd.procoptions,po_has_public_name);
  1573. end;
  1574. procedure pd_public(pd:tabstractprocdef);
  1575. begin
  1576. if pd.typ<>procdef then
  1577. internalerror(200304266);
  1578. if try_to_consume(_NAME) then
  1579. begin
  1580. tprocdef(pd).aliasnames.insert(get_stringconst);
  1581. include(pd.procoptions,po_has_public_name);
  1582. end;
  1583. end;
  1584. procedure pd_asmname(pd:tabstractprocdef);
  1585. begin
  1586. if pd.typ<>procdef then
  1587. internalerror(200304267);
  1588. if token=_CCHAR then
  1589. begin
  1590. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1591. consume(_CCHAR)
  1592. end
  1593. else
  1594. begin
  1595. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1596. consume(_CSTRING);
  1597. end;
  1598. { we don't need anything else }
  1599. tprocdef(pd).forwarddef:=false;
  1600. end;
  1601. procedure pd_internconst(pd:tabstractprocdef);
  1602. var v:Tconstexprint;
  1603. begin
  1604. if pd.typ<>procdef then
  1605. internalerror(200304268);
  1606. consume(_COLON);
  1607. v:=get_intconst;
  1608. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1609. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1610. else
  1611. Tprocdef(pd).extnumber:=longint(v.svalue);
  1612. end;
  1613. procedure pd_internproc(pd:tabstractprocdef);
  1614. var v:Tconstexprint;
  1615. begin
  1616. if pd.typ<>procdef then
  1617. internalerror(200304268);
  1618. consume(_COLON);
  1619. v:=get_intconst;
  1620. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1621. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1622. else
  1623. Tprocdef(pd).extnumber:=longint(v.svalue);
  1624. { the proc is defined }
  1625. tprocdef(pd).forwarddef:=false;
  1626. end;
  1627. procedure pd_interrupt(pd:tabstractprocdef);
  1628. begin
  1629. if pd.parast.symtablelevel>normal_function_level then
  1630. Message(parser_e_dont_nest_interrupt);
  1631. end;
  1632. procedure pd_abstract(pd:tabstractprocdef);
  1633. begin
  1634. if pd.typ<>procdef then
  1635. internalerror(200304269);
  1636. if is_objectpascal_helper(tprocdef(pd).struct) then
  1637. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1638. if assigned(tprocdef(pd).struct) and
  1639. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1640. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1641. else if (po_virtualmethod in pd.procoptions) then
  1642. begin
  1643. include(pd.procoptions,po_abstractmethod);
  1644. { one more abstract method }
  1645. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1646. end
  1647. else
  1648. Message(parser_e_only_virtual_methods_abstract);
  1649. { the method is defined }
  1650. tprocdef(pd).forwarddef:=false;
  1651. end;
  1652. procedure pd_final(pd:tabstractprocdef);
  1653. begin
  1654. if pd.typ<>procdef then
  1655. internalerror(200910170);
  1656. if is_objectpascal_helper(tprocdef(pd).struct) and
  1657. (m_objfpc in current_settings.modeswitches) then
  1658. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1659. if (po_virtualmethod in pd.procoptions) or
  1660. (is_javaclass(tprocdef(pd).struct) and
  1661. (po_classmethod in pd.procoptions)) then
  1662. include(pd.procoptions,po_finalmethod)
  1663. else
  1664. Message(parser_e_only_virtual_methods_final);
  1665. end;
  1666. procedure pd_enumerator(pd:tabstractprocdef);
  1667. begin
  1668. if pd.typ<>procdef then
  1669. internalerror(200910250);
  1670. if (token = _ID) then
  1671. begin
  1672. if pattern='MOVENEXT' then
  1673. begin
  1674. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1675. message(parser_e_only_one_enumerator_movenext);
  1676. pd.calcparas;
  1677. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1678. (pd.minparacount = 0) then
  1679. begin
  1680. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1681. include(pd.procoptions,po_enumerator_movenext);
  1682. end
  1683. else
  1684. Message(parser_e_enumerator_movenext_is_not_valid)
  1685. end
  1686. else
  1687. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1688. consume(token);
  1689. end
  1690. else
  1691. Message(parser_e_enumerator_identifier_required);
  1692. end;
  1693. procedure pd_virtual(pd:tabstractprocdef);
  1694. {$ifdef WITHDMT}
  1695. var
  1696. pt : tnode;
  1697. {$endif WITHDMT}
  1698. begin
  1699. if (not assigned(pd.owner.defowner) or
  1700. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1701. (po_external in pd.procoptions) then
  1702. Message1(parser_e_proc_dir_conflict,'EXTERNAL');
  1703. if pd.typ<>procdef then
  1704. internalerror(2003042610);
  1705. if (pd.proctypeoption=potype_constructor) and
  1706. is_object(tprocdef(pd).struct) then
  1707. Message(parser_e_constructor_cannot_be_not_virtual);
  1708. if pd.is_generic then
  1709. message(parser_e_genfuncs_cannot_be_virtual);
  1710. if is_objectpascal_helper(tprocdef(pd).struct) and
  1711. (m_objfpc in current_settings.modeswitches) then
  1712. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1713. {$ifdef WITHDMT}
  1714. if is_object(tprocdef(pd).struct) and
  1715. (token<>_SEMICOLON) then
  1716. begin
  1717. { any type of parameter is allowed here! }
  1718. pt:=comp_expr(true);
  1719. if is_constintnode(pt) then
  1720. begin
  1721. include(pd.procoptions,po_msgint);
  1722. pd.messageinf.i:=pt.value;
  1723. end
  1724. else
  1725. Message(parser_e_ill_msg_expr);
  1726. disposetree(pt);
  1727. end;
  1728. {$endif WITHDMT}
  1729. end;
  1730. procedure pd_dispid(pd:tabstractprocdef);
  1731. var pt:Tnode;
  1732. begin
  1733. if pd.typ<>procdef then
  1734. internalerror(200604301);
  1735. pt:=comp_expr([ef_accept_equal]);
  1736. if is_constintnode(pt) then
  1737. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1738. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(longint)),tostr(high(longint)))
  1739. else
  1740. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1741. else
  1742. message(parser_e_dispid_must_be_ord_const);
  1743. pt.free;
  1744. end;
  1745. procedure pd_static(pd:tabstractprocdef);
  1746. begin
  1747. if pd.typ<>procdef then
  1748. internalerror(2013032001);
  1749. if not assigned(tprocdef(pd).struct) then
  1750. internalerror(2013032002);
  1751. include(tprocdef(pd).procsym.symoptions,sp_static);
  1752. { "static" is not allowed for operators or normal methods (except in objects) }
  1753. if (pd.proctypeoption=potype_operator) or
  1754. (
  1755. not (po_classmethod in pd.procoptions) and
  1756. not is_object(tprocdef(pd).struct)
  1757. )
  1758. then
  1759. Message1(parser_e_dir_not_allowed,arraytokeninfo[_STATIC].str);
  1760. include(pd.procoptions,po_staticmethod);
  1761. end;
  1762. procedure pd_override(pd:tabstractprocdef);
  1763. begin
  1764. if pd.typ<>procdef then
  1765. internalerror(2003042611);
  1766. if is_objectpascal_helper(tprocdef(pd).struct) then
  1767. begin
  1768. if m_objfpc in current_settings.modeswitches then
  1769. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1770. end
  1771. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1772. Message(parser_e_no_object_override)
  1773. else if is_objccategory(tprocdef(pd).struct) then
  1774. Message(parser_e_no_category_override)
  1775. else if (po_external in pd.procoptions) and
  1776. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1777. not is_cppclass(tprocdef(pd).struct) and
  1778. not is_java_class_or_interface(tprocdef(pd).struct) then
  1779. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1780. end;
  1781. procedure pd_overload(pd:tabstractprocdef);
  1782. begin
  1783. if pd.typ<>procdef then
  1784. internalerror(2003042612);
  1785. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1786. end;
  1787. procedure pd_message(pd:tabstractprocdef);
  1788. var
  1789. pt : tnode;
  1790. paracnt : longint;
  1791. begin
  1792. if pd.typ<>procdef then
  1793. internalerror(2003042613);
  1794. if is_objectpascal_helper(tprocdef(pd).struct) then
  1795. begin
  1796. if m_objfpc in current_settings.modeswitches then
  1797. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1798. end
  1799. else
  1800. if not is_class(tprocdef(pd).struct) and
  1801. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1802. Message(parser_e_msg_only_for_classes);
  1803. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1804. Message(parser_e_multiple_messages);
  1805. { check parameter type }
  1806. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1807. begin
  1808. if po_external in pd.procoptions then
  1809. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1810. paracnt:=0;
  1811. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1812. if paracnt<>1 then
  1813. Message(parser_e_ill_msg_param);
  1814. end;
  1815. pt:=comp_expr([ef_accept_equal]);
  1816. { message is 1-character long }
  1817. if is_constcharnode(pt) then
  1818. begin
  1819. include(pd.procoptions,po_msgstr);
  1820. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1821. end
  1822. else if pt.nodetype=stringconstn then
  1823. begin
  1824. include(pd.procoptions,po_msgstr);
  1825. if (tstringconstnode(pt).len>255) then
  1826. Message(parser_e_message_string_too_long);
  1827. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1828. end
  1829. else
  1830. if is_constintnode(pt) and
  1831. (is_class(tprocdef(pd).struct) or
  1832. is_objectpascal_helper(tprocdef(pd).struct)) then
  1833. begin
  1834. include(pd.procoptions,po_msgint);
  1835. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1836. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1837. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(Tprocdef(pd).messageinf.i)),tostr(high(Tprocdef(pd).messageinf.i)))
  1838. else
  1839. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1840. end
  1841. else
  1842. Message(parser_e_ill_msg_expr);
  1843. { check whether the selector name is valid in case of Objective-C }
  1844. if (po_msgstr in pd.procoptions) and
  1845. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1846. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1847. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1848. pt.free;
  1849. end;
  1850. procedure pd_reintroduce(pd:tabstractprocdef);
  1851. begin
  1852. if pd.typ<>procdef then
  1853. internalerror(200401211);
  1854. if is_objectpascal_helper(tprocdef(pd).struct) then
  1855. begin
  1856. if m_objfpc in current_settings.modeswitches then
  1857. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1858. end
  1859. else
  1860. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1861. not(is_objccategory(tprocdef(pd).struct)) and
  1862. not(is_javaclass(tprocdef(pd).struct)) then
  1863. Message(parser_e_no_object_reintroduce);
  1864. end;
  1865. procedure pd_syscall(pd:tabstractprocdef);
  1866. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64)}
  1867. var
  1868. vs : tparavarsym;
  1869. sym : tsym;
  1870. symtable : TSymtable;
  1871. v: Tconstexprint;
  1872. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64)}
  1873. begin
  1874. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1875. internalerror(2003042614);
  1876. tprocdef(pd).forwarddef:=false;
  1877. {$ifdef m68k}
  1878. if target_info.system in [system_m68k_amiga] then
  1879. begin
  1880. include(pd.procoptions,po_syscall_legacy);
  1881. if consume_sym(sym,symtable) then
  1882. begin
  1883. if (sym.typ=staticvarsym) and
  1884. (
  1885. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1886. is_32bitint(tabstractvarsym(sym).vardef)
  1887. ) then
  1888. begin
  1889. include(pd.procoptions,po_syscall_has_libsym);
  1890. tcpuprocdef(pd).libsym:=sym;
  1891. if po_syscall_legacy in tprocdef(pd).procoptions then
  1892. begin
  1893. vs:=cparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1894. paramanager.parseparaloc(vs,'A6');
  1895. pd.parast.insert(vs);
  1896. end
  1897. end
  1898. else
  1899. Message(parser_e_32bitint_or_pointer_variable_expected);
  1900. end;
  1901. paramanager.create_funcretloc_info(pd,calleeside);
  1902. paramanager.create_funcretloc_info(pd,callerside);
  1903. v:=get_intconst;
  1904. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1905. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(Tprocdef(pd).extnumber)),tostr(high(Tprocdef(pd).extnumber)))
  1906. else
  1907. Tprocdef(pd).extnumber:=v.uvalue;
  1908. end;
  1909. {$endif m68k}
  1910. {$ifdef powerpc}
  1911. if target_info.system = system_powerpc_amiga then
  1912. begin
  1913. include(pd.procoptions,po_syscall_basesysv);
  1914. if consume_sym(sym,symtable) then
  1915. begin
  1916. if (sym.typ=staticvarsym) and
  1917. (
  1918. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1919. is_32bitint(tabstractvarsym(sym).vardef)
  1920. ) then
  1921. begin
  1922. include(pd.procoptions,po_syscall_has_libsym);
  1923. tcpuprocdef(pd).libsym:=sym;
  1924. vs:=cparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1925. pd.parast.insert(vs);
  1926. end
  1927. else
  1928. Message(parser_e_32bitint_or_pointer_variable_expected);
  1929. end;
  1930. paramanager.create_funcretloc_info(pd,calleeside);
  1931. paramanager.create_funcretloc_info(pd,callerside);
  1932. v:=get_intconst;
  1933. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1934. message(parser_e_range_check_error)
  1935. else
  1936. Tprocdef(pd).extnumber:=v.uvalue;
  1937. end else
  1938. if target_info.system = system_powerpc_morphos then
  1939. begin
  1940. if idtoken=_LEGACY then
  1941. begin
  1942. consume(_LEGACY);
  1943. include(pd.procoptions,po_syscall_legacy);
  1944. end
  1945. else if idtoken=_SYSV then
  1946. begin
  1947. consume(_SYSV);
  1948. include(pd.procoptions,po_syscall_sysv);
  1949. end
  1950. else if idtoken=_BASESYSV then
  1951. begin
  1952. consume(_BASESYSV);
  1953. include(pd.procoptions,po_syscall_basesysv);
  1954. end
  1955. else if idtoken=_SYSVBASE then
  1956. begin
  1957. consume(_SYSVBASE);
  1958. include(pd.procoptions,po_syscall_sysvbase);
  1959. end
  1960. else if idtoken=_R12BASE then
  1961. begin
  1962. consume(_R12BASE);
  1963. include(pd.procoptions,po_syscall_r12base);
  1964. end
  1965. else
  1966. if syscall_convention='LEGACY' then
  1967. include(pd.procoptions,po_syscall_legacy)
  1968. else if syscall_convention='SYSV' then
  1969. include(pd.procoptions,po_syscall_sysv)
  1970. else if syscall_convention='BASESYSV' then
  1971. include(pd.procoptions,po_syscall_basesysv)
  1972. else if syscall_convention='SYSVBASE' then
  1973. include(pd.procoptions,po_syscall_sysvbase)
  1974. else if syscall_convention='R12BASE' then
  1975. include(pd.procoptions,po_syscall_r12base)
  1976. else
  1977. internalerror(2005010404);
  1978. if consume_sym(sym,symtable) then
  1979. begin
  1980. if (sym.typ=staticvarsym) and
  1981. (
  1982. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1983. is_32bitint(tabstractvarsym(sym).vardef)
  1984. ) then
  1985. begin
  1986. include(pd.procoptions,po_syscall_has_libsym);
  1987. tcpuprocdef(pd).libsym:=sym;
  1988. if po_syscall_legacy in tprocdef(pd).procoptions then
  1989. begin
  1990. vs:=cparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1991. paramanager.parseparaloc(vs,'A6');
  1992. pd.parast.insert(vs);
  1993. end
  1994. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1995. begin
  1996. { Nothing to be done for sysv here for now, but this might change }
  1997. end
  1998. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1999. begin
  2000. vs:=cparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  2001. pd.parast.insert(vs);
  2002. end
  2003. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  2004. begin
  2005. vs:=cparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  2006. pd.parast.insert(vs);
  2007. end
  2008. else if po_syscall_r12base in tprocdef(pd).procoptions then
  2009. begin
  2010. vs:=cparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  2011. paramanager.parseparaloc(vs,'R12');
  2012. pd.parast.insert(vs);
  2013. end
  2014. else
  2015. internalerror(2005010501);
  2016. end
  2017. else
  2018. Message(parser_e_32bitint_or_pointer_variable_expected);
  2019. end;
  2020. paramanager.create_funcretloc_info(pd,calleeside);
  2021. paramanager.create_funcretloc_info(pd,callerside);
  2022. v:=get_intconst;
  2023. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  2024. message(parser_e_range_check_error)
  2025. else
  2026. Tprocdef(pd).extnumber:=v.uvalue;
  2027. end;
  2028. {$endif powerpc}
  2029. {$if defined(i386) or defined(x86_64)}
  2030. if target_info.system in [system_i386_aros,system_x86_64_aros] then
  2031. begin
  2032. include(pd.procoptions,po_syscall_sysvbase);
  2033. if consume_sym(sym,symtable) then
  2034. begin
  2035. if (sym.typ=staticvarsym) and
  2036. (
  2037. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  2038. is_32bitint(tabstractvarsym(sym).vardef)
  2039. ) then
  2040. begin
  2041. include(pd.procoptions,po_syscall_has_libsym);
  2042. tcpuprocdef(pd).libsym:=sym;
  2043. vs:=cparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  2044. pd.parast.insert(vs);
  2045. end
  2046. else
  2047. Message(parser_e_32bitint_or_pointer_variable_expected);
  2048. end;
  2049. paramanager.create_funcretloc_info(pd,calleeside);
  2050. paramanager.create_funcretloc_info(pd,callerside);
  2051. v:=get_intconst;
  2052. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  2053. message(parser_e_range_check_error)
  2054. else
  2055. Tprocdef(pd).extnumber:=v.uvalue * sizeof(pint);
  2056. end;
  2057. {$endif}
  2058. end;
  2059. procedure pd_external(pd:tabstractprocdef);
  2060. {
  2061. If import_dll=nil the procedure is assumed to be in another
  2062. object file. In that object file it should have the name to
  2063. which import_name is pointing to. Otherwise, the procedure is
  2064. assumed to be in the DLL to which import_dll is pointing to. In
  2065. that case either import_nr<>0 or import_name<>nil is true, so
  2066. the procedure is either imported by number or by name. (DM)
  2067. }
  2068. var
  2069. hs : string;
  2070. v:Tconstexprint;
  2071. is_java_external: boolean;
  2072. begin
  2073. if pd.typ<>procdef then
  2074. internalerror(2003042615);
  2075. { Allow specifying a separate external name for methods in external Java
  2076. because its identifier naming constraints are laxer than FPC's
  2077. (e.g., case sensitive).
  2078. Limitation: only allows specifying the symbol name and not the package name,
  2079. and only for external classes/interfaces }
  2080. is_java_external:=
  2081. (pd.typ=procdef) and
  2082. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  2083. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  2084. with tprocdef(pd) do
  2085. begin
  2086. forwarddef:=false;
  2087. { forbid local external procedures }
  2088. if parast.symtablelevel>normal_function_level then
  2089. Message(parser_e_no_local_proc_external);
  2090. { If the procedure should be imported from a DLL, a constant string follows.
  2091. This isn't really correct, an contant string expression follows
  2092. so we check if an semicolon follows, else a string constant have to
  2093. follow (FK) }
  2094. if not is_java_external and
  2095. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  2096. begin
  2097. { Always add library prefix and suffix to create an uniform name }
  2098. hs:=get_stringconst;
  2099. if ExtractFileExt(hs)='' then
  2100. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  2101. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  2102. hs:=target_info.sharedlibprefix+hs;
  2103. { the JVM expects java/lang/Object rather than java.lang.Object }
  2104. if target_info.system in systems_jvm then
  2105. Replace(hs,'.','/');
  2106. import_dll:=stringdup(hs);
  2107. include(procoptions,po_has_importdll);
  2108. if (idtoken=_NAME) then
  2109. begin
  2110. consume(_NAME);
  2111. import_name:=stringdup(get_stringconst);
  2112. include(procoptions,po_has_importname);
  2113. if import_name^='' then
  2114. message(parser_e_empty_import_name);
  2115. end;
  2116. if (idtoken=_INDEX) then
  2117. begin
  2118. {After the word index follows the index number in the DLL.}
  2119. consume(_INDEX);
  2120. v:=get_intconst;
  2121. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  2122. message(parser_e_range_check_error)
  2123. else
  2124. import_nr:=longint(v.svalue);
  2125. end;
  2126. { default is to used the realname of the procedure }
  2127. if (import_nr=0) and not assigned(import_name) then
  2128. begin
  2129. import_name:=stringdup(procsym.realname);
  2130. include(procoptions,po_has_importname);
  2131. end;
  2132. end
  2133. else
  2134. begin
  2135. if (idtoken=_NAME) or
  2136. is_java_external then
  2137. begin
  2138. consume(_NAME);
  2139. import_name:=stringdup(get_stringconst);
  2140. include(procoptions,po_has_importname);
  2141. if import_name^='' then
  2142. message(parser_e_empty_import_name);
  2143. end;
  2144. end;
  2145. end;
  2146. end;
  2147. procedure pd_weakexternal(pd:tabstractprocdef);
  2148. begin
  2149. if not(target_info.system in systems_weak_linking) then
  2150. message(parser_e_weak_external_not_supported)
  2151. else
  2152. pd_external(pd);
  2153. end;
  2154. procedure pd_winapi(pd:tabstractprocdef);
  2155. begin
  2156. if not(target_info.system in systems_wince) then
  2157. pd.proccalloption:=pocall_cdecl
  2158. else
  2159. pd.proccalloption:=pocall_stdcall;
  2160. end;
  2161. type
  2162. pd_handler=procedure(pd:tabstractprocdef);
  2163. proc_dir_rec=record
  2164. idtok : ttoken;
  2165. pd_flags : tpdflags;
  2166. handler : pd_handler;
  2167. pocall : tproccalloption;
  2168. pooption : tprocoptions;
  2169. mutexclpocall : tproccalloptions;
  2170. mutexclpotype : tproctypeoptions;
  2171. mutexclpo : tprocoptions;
  2172. end;
  2173. const
  2174. {Should contain the number of procedure directives we support.}
  2175. num_proc_directives=45;
  2176. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  2177. (
  2178. (
  2179. idtok:_ABSTRACT;
  2180. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2181. handler : @pd_abstract;
  2182. pocall : pocall_none;
  2183. pooption : [po_abstractmethod];
  2184. mutexclpocall : [pocall_internproc];
  2185. mutexclpotype : [];
  2186. mutexclpo : [po_exports,po_interrupt,po_inline]
  2187. ),(
  2188. idtok:_ALIAS;
  2189. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2190. handler : @pd_alias;
  2191. pocall : pocall_none;
  2192. pooption : [];
  2193. mutexclpocall : [];
  2194. mutexclpotype : [];
  2195. mutexclpo : [po_external,po_inline]
  2196. ),(
  2197. idtok:_ASMNAME;
  2198. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  2199. handler : @pd_asmname;
  2200. pocall : pocall_cdecl;
  2201. pooption : [po_external];
  2202. mutexclpocall : [pocall_internproc];
  2203. mutexclpotype : [];
  2204. mutexclpo : [po_external,po_inline]
  2205. ),(
  2206. idtok:_ASSEMBLER;
  2207. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2208. handler : nil;
  2209. pocall : pocall_none;
  2210. pooption : [po_assembler];
  2211. mutexclpocall : [];
  2212. mutexclpotype : [];
  2213. mutexclpo : [po_external]
  2214. ),(
  2215. idtok:_C; {same as cdecl for mode mac}
  2216. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2217. handler : nil;
  2218. pocall : pocall_cdecl;
  2219. pooption : [];
  2220. mutexclpocall : [];
  2221. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2222. mutexclpo : [po_assembler,po_external]
  2223. ),(
  2224. idtok:_CDECL;
  2225. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2226. handler : nil;
  2227. pocall : pocall_cdecl;
  2228. pooption : [];
  2229. mutexclpocall : [];
  2230. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2231. mutexclpo : [po_assembler,po_external]
  2232. ),(
  2233. idtok:_DISPID;
  2234. pd_flags : [pd_dispinterface];
  2235. handler : @pd_dispid;
  2236. pocall : pocall_none;
  2237. pooption : [po_dispid];
  2238. mutexclpocall : [pocall_internproc];
  2239. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2240. mutexclpo : [po_interrupt,po_external,po_inline]
  2241. ),(
  2242. idtok:_DYNAMIC;
  2243. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2244. handler : @pd_virtual;
  2245. pocall : pocall_none;
  2246. pooption : [po_virtualmethod];
  2247. mutexclpocall : [pocall_internproc];
  2248. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2249. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  2250. ),(
  2251. idtok:_EXPORT;
  2252. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  2253. handler : @pd_export;
  2254. pocall : pocall_none;
  2255. pooption : [po_exports,po_global];
  2256. mutexclpocall : [pocall_internproc];
  2257. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2258. mutexclpo : [po_external,po_interrupt,po_inline]
  2259. ),(
  2260. idtok:_EXTERNAL;
  2261. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  2262. handler : @pd_external;
  2263. pocall : pocall_none;
  2264. pooption : [po_external];
  2265. mutexclpocall : [pocall_syscall];
  2266. { allowed for external cpp classes }
  2267. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2268. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2269. ),(
  2270. idtok:_FAR;
  2271. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2272. handler : @pd_far;
  2273. pocall : pocall_none;
  2274. pooption : [];
  2275. mutexclpocall : [pocall_internproc];
  2276. mutexclpotype : [];
  2277. mutexclpo : [po_inline]
  2278. ),(
  2279. idtok:_FAR16;
  2280. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  2281. handler : nil;
  2282. pocall : pocall_far16;
  2283. pooption : [];
  2284. mutexclpocall : [];
  2285. mutexclpotype : [];
  2286. mutexclpo : [po_external]
  2287. ),(
  2288. idtok:_FINAL;
  2289. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2290. handler : @pd_final;
  2291. pocall : pocall_none;
  2292. pooption : [po_finalmethod];
  2293. mutexclpocall : [pocall_internproc];
  2294. mutexclpotype : [];
  2295. mutexclpo : [po_exports,po_interrupt,po_inline]
  2296. ),(
  2297. idtok:_FORWARD;
  2298. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2299. handler : @pd_forward;
  2300. pocall : pocall_none;
  2301. pooption : [];
  2302. mutexclpocall : [pocall_internproc];
  2303. mutexclpotype : [];
  2304. mutexclpo : [po_external,po_inline]
  2305. ),(
  2306. idtok:_OLDFPCCALL;
  2307. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2308. handler : nil;
  2309. pocall : pocall_oldfpccall;
  2310. pooption : [];
  2311. mutexclpocall : [];
  2312. mutexclpotype : [];
  2313. mutexclpo : []
  2314. ),(
  2315. idtok:_INLINE;
  2316. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2317. handler : nil;
  2318. pocall : pocall_none;
  2319. pooption : [po_inline];
  2320. mutexclpocall : [pocall_safecall];
  2321. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2322. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck]
  2323. ),(
  2324. idtok:_INTERNCONST;
  2325. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2326. handler : @pd_internconst;
  2327. pocall : pocall_none;
  2328. pooption : [po_internconst];
  2329. mutexclpocall : [];
  2330. mutexclpotype : [potype_operator];
  2331. mutexclpo : []
  2332. ),(
  2333. idtok:_INTERNPROC;
  2334. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2335. handler : @pd_internproc;
  2336. pocall : pocall_internproc;
  2337. pooption : [];
  2338. mutexclpocall : [];
  2339. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2340. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2341. ),(
  2342. idtok:_INTERRUPT;
  2343. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2344. handler : @pd_interrupt;
  2345. pocall : pocall_oldfpccall;
  2346. pooption : [po_interrupt];
  2347. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,pocall_mwpascal,
  2348. pocall_pascal,pocall_far16,pocall_oldfpccall];
  2349. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2350. mutexclpo : [po_external,po_inline,po_exports]
  2351. ),(
  2352. idtok:_IOCHECK;
  2353. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2354. handler : nil;
  2355. pocall : pocall_none;
  2356. pooption : [po_iocheck];
  2357. mutexclpocall : [pocall_internproc];
  2358. mutexclpotype : [];
  2359. mutexclpo : [po_external]
  2360. ),(
  2361. idtok:_LOCAL;
  2362. pd_flags : [pd_implemen,pd_body];
  2363. handler : nil;
  2364. pocall : pocall_none;
  2365. pooption : [po_kylixlocal];
  2366. mutexclpocall : [pocall_internproc,pocall_far16];
  2367. mutexclpotype : [];
  2368. mutexclpo : [po_external,po_exports]
  2369. ),(
  2370. idtok:_MESSAGE;
  2371. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2372. handler : @pd_message;
  2373. pocall : pocall_none;
  2374. pooption : []; { can be po_msgstr or po_msgint }
  2375. mutexclpocall : [pocall_internproc];
  2376. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2377. mutexclpo : [po_interrupt,po_inline]
  2378. ),(
  2379. idtok:_MWPASCAL;
  2380. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2381. handler : nil;
  2382. pocall : pocall_mwpascal;
  2383. pooption : [];
  2384. mutexclpocall : [];
  2385. mutexclpotype : [];
  2386. mutexclpo : []
  2387. ),(
  2388. idtok:_NEAR;
  2389. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2390. handler : @pd_near;
  2391. pocall : pocall_none;
  2392. pooption : [];
  2393. mutexclpocall : [pocall_internproc];
  2394. mutexclpotype : [];
  2395. mutexclpo : []
  2396. ),(
  2397. idtok:_NORETURN;
  2398. pd_flags : [pd_implemen,pd_interface,pd_body,pd_notobjintf];
  2399. handler : nil;
  2400. pocall : pocall_none;
  2401. pooption : [po_noreturn];
  2402. mutexclpocall : [];
  2403. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2404. mutexclpo : [po_interrupt,po_virtualmethod,po_iocheck]
  2405. ),(
  2406. idtok:_NOSTACKFRAME;
  2407. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2408. handler : nil;
  2409. pocall : pocall_none;
  2410. pooption : [po_nostackframe];
  2411. mutexclpocall : [pocall_internproc];
  2412. mutexclpotype : [];
  2413. mutexclpo : []
  2414. ),(
  2415. idtok:_OVERLOAD;
  2416. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava,pd_objcclass,pd_objcprot];
  2417. handler : @pd_overload;
  2418. pocall : pocall_none;
  2419. pooption : [po_overload];
  2420. mutexclpocall : [pocall_internproc];
  2421. mutexclpotype : [];
  2422. mutexclpo : []
  2423. ),(
  2424. idtok:_OVERRIDE;
  2425. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2426. handler : @pd_override;
  2427. pocall : pocall_none;
  2428. pooption : [po_overridingmethod,po_virtualmethod];
  2429. mutexclpocall : [pocall_internproc];
  2430. mutexclpotype : [];
  2431. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2432. ),(
  2433. idtok:_PASCAL;
  2434. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2435. handler : nil;
  2436. pocall : pocall_pascal;
  2437. pooption : [];
  2438. mutexclpocall : [];
  2439. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2440. mutexclpo : [po_external]
  2441. ),(
  2442. idtok:_PUBLIC;
  2443. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2444. handler : @pd_public;
  2445. pocall : pocall_none;
  2446. pooption : [po_public,po_global];
  2447. mutexclpocall : [pocall_internproc];
  2448. mutexclpotype : [];
  2449. mutexclpo : [po_external,po_inline]
  2450. ),(
  2451. idtok:_REGISTER;
  2452. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2453. handler : nil;
  2454. pocall : pocall_register;
  2455. pooption : [];
  2456. mutexclpocall : [];
  2457. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2458. mutexclpo : [po_external]
  2459. ),(
  2460. idtok:_REINTRODUCE;
  2461. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2462. handler : @pd_reintroduce;
  2463. pocall : pocall_none;
  2464. pooption : [po_reintroduce];
  2465. mutexclpocall : [pocall_internproc];
  2466. mutexclpotype : [];
  2467. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2468. ),(
  2469. idtok:_SAFECALL;
  2470. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2471. handler : nil;
  2472. pocall : pocall_safecall;
  2473. pooption : [];
  2474. mutexclpocall : [];
  2475. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2476. mutexclpo : [po_external]
  2477. ),(
  2478. idtok:_SOFTFLOAT;
  2479. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2480. handler : nil;
  2481. pocall : pocall_softfloat;
  2482. pooption : [];
  2483. mutexclpocall : [];
  2484. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2485. { it's available with po_external because the libgcc floating point routines on the arm
  2486. uses this calling convention }
  2487. mutexclpo : []
  2488. ),(
  2489. idtok:_STATIC;
  2490. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2491. handler : @pd_static;
  2492. pocall : pocall_none;
  2493. pooption : [po_staticmethod];
  2494. mutexclpocall : [pocall_internproc];
  2495. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2496. mutexclpo : [po_interrupt,po_exports]
  2497. ),(
  2498. idtok:_STDCALL;
  2499. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2500. handler : nil;
  2501. pocall : pocall_stdcall;
  2502. pooption : [];
  2503. mutexclpocall : [];
  2504. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2505. mutexclpo : [po_external]
  2506. ),(
  2507. idtok:_SYSCALL;
  2508. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2509. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2510. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2511. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2512. handler : @pd_syscall;
  2513. pocall : pocall_syscall;
  2514. pooption : [];
  2515. mutexclpocall : [];
  2516. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2517. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2518. ),(
  2519. idtok:_VIRTUAL;
  2520. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2521. handler : @pd_virtual;
  2522. pocall : pocall_none;
  2523. pooption : [po_virtualmethod];
  2524. mutexclpocall : [pocall_internproc];
  2525. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2526. mutexclpo : [po_interrupt,po_exports,po_overridingmethod,po_inline]
  2527. ),(
  2528. idtok:_CPPDECL;
  2529. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2530. handler : nil;
  2531. pocall : pocall_cppdecl;
  2532. pooption : [];
  2533. mutexclpocall : [];
  2534. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2535. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2536. ),(
  2537. idtok:_VARARGS;
  2538. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2539. handler : nil;
  2540. pocall : pocall_none;
  2541. pooption : [po_varargs];
  2542. mutexclpocall : [pocall_internproc,pocall_register,
  2543. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2544. mutexclpotype : [];
  2545. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2546. ),(
  2547. idtok:_COMPILERPROC;
  2548. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2549. handler : nil;
  2550. pocall : pocall_none;
  2551. pooption : [po_compilerproc];
  2552. mutexclpocall : [];
  2553. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2554. mutexclpo : [po_interrupt]
  2555. ),(
  2556. idtok:_WEAKEXTERNAL;
  2557. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2558. handler : @pd_weakexternal;
  2559. pocall : pocall_none;
  2560. { mark it both external and weak external, so we don't have to
  2561. adapt all code for external symbols to also check for weak external
  2562. }
  2563. pooption : [po_external,po_weakexternal];
  2564. mutexclpocall : [pocall_internproc,pocall_syscall];
  2565. { allowed for external cpp classes }
  2566. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2567. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2568. ),(
  2569. idtok:_WINAPI;
  2570. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2571. handler : @pd_winapi;
  2572. pocall : pocall_none;
  2573. pooption : [];
  2574. mutexclpocall : [pocall_stdcall,pocall_cdecl];
  2575. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2576. mutexclpo : [po_external]
  2577. ),(
  2578. idtok:_ENUMERATOR;
  2579. pd_flags : [pd_interface,pd_object,pd_record];
  2580. handler : @pd_enumerator;
  2581. pocall : pocall_none;
  2582. pooption : [];
  2583. mutexclpocall : [pocall_internproc];
  2584. mutexclpotype : [];
  2585. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2586. ),(
  2587. idtok:_RTLPROC;
  2588. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2589. handler : nil;
  2590. pocall : pocall_none;
  2591. pooption : [po_rtlproc];
  2592. mutexclpocall : [];
  2593. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2594. mutexclpo : [po_interrupt]
  2595. )
  2596. );
  2597. function check_proc_directive(isprocvar:boolean):boolean;
  2598. var
  2599. i : longint;
  2600. begin
  2601. result:=false;
  2602. for i:=1 to num_proc_directives do
  2603. if proc_direcdata[i].idtok=idtoken then
  2604. begin
  2605. if ((not isprocvar) or
  2606. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2607. { don't eat a public directive in classes }
  2608. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2609. result:=true;
  2610. exit;
  2611. end;
  2612. end;
  2613. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2614. {
  2615. Parse the procedure directive, returns true if a correct directive is found
  2616. }
  2617. var
  2618. p : longint;
  2619. found : boolean;
  2620. name : TIDString;
  2621. begin
  2622. parse_proc_direc:=false;
  2623. name:=tokeninfo^[idtoken].str;
  2624. found:=false;
  2625. { Hint directive? Then exit immediatly }
  2626. if (m_hintdirective in current_settings.modeswitches) then
  2627. begin
  2628. case idtoken of
  2629. _LIBRARY,
  2630. _PLATFORM,
  2631. _UNIMPLEMENTED,
  2632. _EXPERIMENTAL,
  2633. _DEPRECATED :
  2634. if (m_delphi in current_settings.modeswitches) and (pd.typ=procdef) then
  2635. begin
  2636. maybe_parse_hint_directives(tprocdef(pd));
  2637. { could the new token still be a directive? }
  2638. if token<>_ID then
  2639. exit;
  2640. name:=tokeninfo^[idtoken].str;
  2641. end
  2642. else
  2643. exit;
  2644. end;
  2645. end;
  2646. { C directive is MacPas only, because it breaks too much existing code
  2647. on other platforms (PFV) }
  2648. if (idtoken=_C) and
  2649. not(m_mac in current_settings.modeswitches) then
  2650. exit;
  2651. { retrieve data for directive if found }
  2652. for p:=1 to num_proc_directives do
  2653. if proc_direcdata[p].idtok=idtoken then
  2654. begin
  2655. found:=true;
  2656. break;
  2657. end;
  2658. { Check if the procedure directive is known }
  2659. if not found then
  2660. begin
  2661. { parsing a procvar type the name can be any
  2662. next variable !! }
  2663. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2664. not(idtoken=_PROPERTY) then
  2665. Message1(parser_w_unknown_proc_directive_ignored,pattern);
  2666. exit;
  2667. end;
  2668. { check if method and directive not for object, like public.
  2669. This needs to be checked also for procvars }
  2670. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2671. (symtablestack.top.symtabletype=ObjectSymtable) and
  2672. { directive allowed for cpp classes? }
  2673. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2674. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2675. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2676. exit;
  2677. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2678. (symtablestack.top.symtabletype=recordsymtable) then
  2679. exit;
  2680. { check if method and directive not for java class }
  2681. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2682. is_javaclass(tdef(symtablestack.top.defowner)) then
  2683. exit;
  2684. { check if method and directive not for java interface }
  2685. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2686. is_javainterface(tdef(symtablestack.top.defowner)) then
  2687. exit;
  2688. { Conflicts between directives ? }
  2689. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2690. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2691. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2692. begin
  2693. Message1(parser_e_proc_dir_conflict,name);
  2694. exit;
  2695. end;
  2696. { set calling convention }
  2697. if proc_direcdata[p].pocall<>pocall_none then
  2698. begin
  2699. if (po_hascallingconvention in pd.procoptions) then
  2700. begin
  2701. Message2(parser_w_proc_overriding_calling,
  2702. proccalloptionStr[pd.proccalloption],
  2703. proccalloptionStr[proc_direcdata[p].pocall]);
  2704. end;
  2705. { check if the target processor supports this calling convention }
  2706. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2707. begin
  2708. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2709. { recover }
  2710. proc_direcdata[p].pocall:=pocall_stdcall;
  2711. end;
  2712. pd.proccalloption:=proc_direcdata[p].pocall;
  2713. include(pd.procoptions,po_hascallingconvention);
  2714. end;
  2715. if pd.typ=procdef then
  2716. begin
  2717. { Check if the directive is only for objects }
  2718. if (pd_object in proc_direcdata[p].pd_flags) and
  2719. not assigned(tprocdef(pd).struct) then
  2720. exit;
  2721. { Check if the directive is only for records }
  2722. if (pd_record in proc_direcdata[p].pd_flags) and
  2723. not assigned(tprocdef(pd).struct) then
  2724. exit;
  2725. { check if method and directive not for interface }
  2726. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2727. is_interface(tprocdef(pd).struct) then
  2728. exit;
  2729. { check if method and directive not for interface }
  2730. if is_dispinterface(tprocdef(pd).struct) and
  2731. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2732. exit;
  2733. { check if method and directive not for objcclass }
  2734. if is_objcclass(tprocdef(pd).struct) and
  2735. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2736. exit;
  2737. { check if method and directive not for objcprotocol }
  2738. if is_objcprotocol(tprocdef(pd).struct) and
  2739. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2740. exit;
  2741. { check if method and directive not for record/class helper }
  2742. if is_objectpascal_helper(tprocdef(pd).struct) and
  2743. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2744. end;
  2745. { consume directive, and turn flag on }
  2746. consume(token);
  2747. parse_proc_direc:=true;
  2748. { Check the pd_flags if the directive should be allowed }
  2749. if (pd_interface in pdflags) and
  2750. not(pd_interface in proc_direcdata[p].pd_flags) then
  2751. begin
  2752. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2753. exit;
  2754. end;
  2755. if (pd_implemen in pdflags) and
  2756. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2757. begin
  2758. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2759. exit;
  2760. end;
  2761. if (pd_procvar in pdflags) and
  2762. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2763. begin
  2764. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2765. exit;
  2766. end;
  2767. { Return the new pd_flags }
  2768. if not(pd_body in proc_direcdata[p].pd_flags) then
  2769. exclude(pdflags,pd_body);
  2770. { Add the correct flag }
  2771. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2772. { Call the handler }
  2773. if pointer(proc_direcdata[p].handler)<>nil then
  2774. proc_direcdata[p].handler(pd);
  2775. end;
  2776. function proc_get_importname(pd:tprocdef):string;
  2777. var
  2778. dllname, importname : string;
  2779. begin
  2780. result:='';
  2781. if not(po_external in pd.procoptions) then
  2782. internalerror(200412151);
  2783. { external name or number is specified }
  2784. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2785. begin
  2786. if assigned(pd.import_dll) then
  2787. dllname:=pd.import_dll^
  2788. else
  2789. dllname:='';
  2790. if assigned(pd.import_name) then
  2791. importname:=pd.import_name^
  2792. else
  2793. importname:='';
  2794. proc_get_importname:=make_dllmangledname(dllname,
  2795. importname,pd.import_nr,pd.proccalloption);
  2796. end
  2797. else
  2798. begin
  2799. { Default names when importing variables }
  2800. case pd.proccalloption of
  2801. pocall_cdecl :
  2802. begin
  2803. if assigned(pd.struct) then
  2804. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2805. else
  2806. result:=target_info.Cprefix+pd.procsym.realname;
  2807. end;
  2808. pocall_cppdecl :
  2809. begin
  2810. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2811. end;
  2812. else
  2813. begin
  2814. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2815. { but according to MacPas mode description
  2816. Cprefix should still be used PM }
  2817. if (m_mac in current_settings.modeswitches) then
  2818. result:=target_info.Cprefix+tprocdef(pd).procsym.realname
  2819. else
  2820. result:=pd.procsym.realname;
  2821. end;
  2822. end;
  2823. end;
  2824. end;
  2825. procedure compilerproc_set_symbol_name(pd: tprocdef);
  2826. begin
  2827. pd.procsym.realname:='$'+lower(pd.procsym.name);
  2828. end;
  2829. procedure proc_set_mangledname(pd:tprocdef);
  2830. var
  2831. s : string;
  2832. begin
  2833. { When the mangledname is already set we aren't allowed to change
  2834. it because it can already be used somewhere (PFV) }
  2835. if not(po_has_mangledname in pd.procoptions) then
  2836. begin
  2837. if (po_external in pd.procoptions) then
  2838. begin
  2839. { External Procedures are only allowed to change the mangledname
  2840. in their first declaration }
  2841. if (pd.forwarddef or (not pd.hasforward)) then
  2842. begin
  2843. s:=proc_get_importname(pd);
  2844. if s<>'' then
  2845. begin
  2846. pd.setmangledname(s);
  2847. end;
  2848. { since this is an external declaration, there won't be an
  2849. implementation that needs to match the original symbol
  2850. again -> immediately convert here }
  2851. if po_compilerproc in pd.procoptions then
  2852. compilerproc_set_symbol_name(pd);
  2853. end
  2854. end
  2855. else
  2856. { Normal procedures }
  2857. begin
  2858. if (po_compilerproc in pd.procoptions) then
  2859. begin
  2860. pd.setmangledname(lower(pd.procsym.name));
  2861. end;
  2862. end;
  2863. end;
  2864. { Public/exported alias names }
  2865. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2866. not(po_has_public_name in pd.procoptions) then
  2867. begin
  2868. case pd.proccalloption of
  2869. pocall_cdecl :
  2870. begin
  2871. if assigned(pd.struct) then
  2872. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2873. else
  2874. begin
  2875. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2876. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2877. (po_exports in pd.procoptions) then
  2878. pd.aliasnames.insert(pd.procsym.realname)
  2879. else
  2880. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2881. end;
  2882. end;
  2883. pocall_cppdecl :
  2884. begin
  2885. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2886. end;
  2887. end;
  2888. { prevent adding the alias a second time }
  2889. include(pd.procoptions,po_has_public_name);
  2890. end;
  2891. end;
  2892. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  2893. begin
  2894. if hcc_check in flags then
  2895. begin
  2896. { set the default calling convention if none provided }
  2897. if (pd.typ=procdef) and
  2898. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2899. is_cppclass(tprocdef(pd).struct)) then
  2900. begin
  2901. { none of the explicit calling conventions should be allowed }
  2902. if (po_hascallingconvention in pd.procoptions) then
  2903. internalerror(2009032501);
  2904. if is_cppclass(tprocdef(pd).struct) then
  2905. pd.proccalloption:=pocall_cppdecl
  2906. else
  2907. pd.proccalloption:=pocall_cdecl;
  2908. end
  2909. else if not(po_hascallingconvention in pd.procoptions) then
  2910. pd.proccalloption:=current_settings.defproccall
  2911. else
  2912. begin
  2913. if pd.proccalloption=pocall_none then
  2914. internalerror(200309081);
  2915. end;
  2916. { handle proccall specific settings }
  2917. case pd.proccalloption of
  2918. pocall_cdecl,
  2919. pocall_cppdecl :
  2920. begin
  2921. { check C cdecl para types }
  2922. check_c_para(pd);
  2923. end;
  2924. pocall_far16 :
  2925. begin
  2926. { Temporary stub, must be rewritten to support OS/2 far16 }
  2927. Message1(parser_w_proc_directive_ignored,'FAR16');
  2928. end;
  2929. end;
  2930. { Inlining is enabled and supported? }
  2931. if (po_inline in pd.procoptions) and
  2932. not(cs_do_inline in current_settings.localswitches) then
  2933. begin
  2934. { Give an error if inline is not supported by the compiler mode,
  2935. otherwise only give a hint that this procedure will not be inlined }
  2936. if not(m_default_inline in current_settings.modeswitches) then
  2937. Message(parser_e_proc_inline_not_supported)
  2938. else
  2939. Message(parser_h_inlining_disabled);
  2940. exclude(pd.procoptions,po_inline);
  2941. end;
  2942. { For varargs directive also cdecl and external must be defined }
  2943. if (po_varargs in pd.procoptions) then
  2944. begin
  2945. { check first for external in the interface, if available there
  2946. then the cdecl must also be there since there is no implementation
  2947. available to contain it }
  2948. if parse_only then
  2949. begin
  2950. { if external is available, then cdecl must also be available,
  2951. procvars don't need external }
  2952. if not((po_external in pd.procoptions) or
  2953. (pd.typ=procvardef) or
  2954. { for objcclasses this is checked later, because the entire
  2955. class may be external. }
  2956. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2957. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal,pocall_stdcall])) then
  2958. Message(parser_e_varargs_need_cdecl_and_external);
  2959. end
  2960. else
  2961. begin
  2962. { both must be defined now }
  2963. if not((po_external in pd.procoptions) or
  2964. (pd.typ=procvardef)) or
  2965. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal,pocall_stdcall])) then
  2966. Message(parser_e_varargs_need_cdecl_and_external);
  2967. end;
  2968. end;
  2969. end;
  2970. if hcc_insert_hidden_paras in flags then
  2971. begin
  2972. { insert hidden high parameters }
  2973. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2974. { insert hidden self parameter }
  2975. insert_self_and_vmt_para(pd);
  2976. { insert funcret parameter if required }
  2977. insert_funcret_para(pd);
  2978. { Make var parameters regable, this must be done after the calling
  2979. convention is set. }
  2980. { this must be done before parentfp is insert, because getting all cases
  2981. where parentfp must be in a memory location isn't catched properly so
  2982. we put parentfp never in a register }
  2983. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2984. { insert parentfp parameter if required }
  2985. insert_parentfp_para(pd);
  2986. end;
  2987. { Calculate parameter tlist }
  2988. pd.calcparas;
  2989. end;
  2990. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2991. {
  2992. Parse the procedure directives. It does not matter if procedure directives
  2993. are written using ;procdir; or ['procdir'] syntax.
  2994. }
  2995. var
  2996. res : boolean;
  2997. begin
  2998. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2999. begin
  3000. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  3001. include(pd.procoptions,po_public);
  3002. include(pd.procoptions,po_has_public_name);
  3003. include(pd.procoptions,po_global);
  3004. end;
  3005. { methods from external class definitions are all external themselves }
  3006. if (pd.typ=procdef) and
  3007. assigned(tprocdef(pd).struct) and
  3008. (tprocdef(pd).struct.typ=objectdef) and
  3009. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  3010. tprocdef(pd).make_external;
  3011. { Class constructors and destructor are static class methods in real. }
  3012. { There are many places in the compiler where either class or static }
  3013. { method flag changes the behavior. It is simplier to add them to }
  3014. { the class constructors/destructors options than to fix all the }
  3015. { occurencies. (Paul) }
  3016. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  3017. begin
  3018. include(pd.procoptions,po_classmethod);
  3019. include(pd.procoptions,po_staticmethod);
  3020. end;
  3021. while token in [_ID,_LECKKLAMMER] do
  3022. begin
  3023. if try_to_consume(_LECKKLAMMER) then
  3024. begin
  3025. repeat
  3026. parse_proc_direc(pd,pdflags);
  3027. until not try_to_consume(_COMMA);
  3028. consume(_RECKKLAMMER);
  3029. { we always expect at least '[];' }
  3030. res:=true;
  3031. end
  3032. else
  3033. begin
  3034. res:=parse_proc_direc(pd,pdflags);
  3035. end;
  3036. { A procedure directive normally followed by a semicolon, but in
  3037. a const section or reading a type we should stop when _EQ is found,
  3038. because a constant/default value follows }
  3039. if res then
  3040. begin
  3041. if (block_type=bt_const_type) and
  3042. (token=_EQ) then
  3043. break;
  3044. { support procedure proc;stdcall export; }
  3045. if not(check_proc_directive((pd.typ=procvardef))) then
  3046. begin
  3047. { support "record p : procedure stdcall end;" and
  3048. "var p : procedure stdcall = nil;" }
  3049. if (pd_procvar in pdflags) and
  3050. (token in [_END,_RKLAMMER,_EQ]) then
  3051. break
  3052. else
  3053. begin
  3054. if (token=_COLON) then
  3055. begin
  3056. Message(parser_e_field_not_allowed_here);
  3057. consume_all_until(_SEMICOLON);
  3058. end;
  3059. consume(_SEMICOLON)
  3060. end;
  3061. end;
  3062. end
  3063. else
  3064. break;
  3065. end;
  3066. { nostackframe requires assembler, but assembler
  3067. may be specified in the implementation part only,
  3068. and in not required if the function is first forward declared
  3069. if it is a procdef that has forwardef set to true
  3070. we postpone the possible error message to the real implementation
  3071. parse_only does not need to be considered as po_nostackframe
  3072. is an implementation only directive }
  3073. if (po_nostackframe in pd.procoptions) and
  3074. not (po_assembler in pd.procoptions) and
  3075. ((pd.typ<>procdef) or not tprocdef(pd).forwarddef) then
  3076. message(parser_e_nostackframe_without_assembler);
  3077. end;
  3078. procedure parse_var_proc_directives(sym:tsym);
  3079. var
  3080. pdflags : tpdflags;
  3081. pd : tabstractprocdef;
  3082. begin
  3083. pdflags:=[pd_procvar];
  3084. pd:=nil;
  3085. case sym.typ of
  3086. fieldvarsym,
  3087. staticvarsym,
  3088. localvarsym,
  3089. paravarsym :
  3090. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  3091. typesym :
  3092. pd:=tabstractprocdef(ttypesym(sym).typedef);
  3093. else
  3094. internalerror(2003042617);
  3095. end;
  3096. if pd.typ<>procvardef then
  3097. internalerror(2003042618);
  3098. { names should never be used anyway }
  3099. parse_proc_directives(pd,pdflags);
  3100. end;
  3101. procedure parse_object_proc_directives(pd:tabstractprocdef);
  3102. var
  3103. pdflags : tpdflags;
  3104. begin
  3105. pdflags:=[pd_object];
  3106. parse_proc_directives(pd,pdflags);
  3107. end;
  3108. procedure parse_record_proc_directives(pd:tabstractprocdef);
  3109. var
  3110. pdflags : tpdflags;
  3111. begin
  3112. pdflags:=[pd_record];
  3113. parse_proc_directives(pd,pdflags);
  3114. end;
  3115. function proc_add_definition(var currpd:tprocdef):boolean;
  3116. function equal_generic_procdefs(fwpd,currpd:tprocdef):boolean;
  3117. var
  3118. i : longint;
  3119. fwtype,
  3120. currtype : ttypesym;
  3121. foundretdef : boolean;
  3122. begin
  3123. result:=false;
  3124. if fwpd.genericparas.count<>currpd.genericparas.count then
  3125. exit;
  3126. { comparing generic declarations is a bit more cumbersome as the
  3127. defs of the generic parameter types are not equal, especially if the
  3128. declaration contains constraints; essentially we have two cases:
  3129. - proc declared in interface of unit (or in class/record/object)
  3130. and defined in implementation; here the fwpd might contain
  3131. constraints while currpd must only contain undefineddefs
  3132. - forward declaration in implementation; this case is not supported
  3133. right now }
  3134. foundretdef:=false;
  3135. for i:=0 to fwpd.genericparas.count-1 do
  3136. begin
  3137. fwtype:=ttypesym(fwpd.genericparas[i]);
  3138. currtype:=ttypesym(currpd.genericparas[i]);
  3139. { if the type in the currpd isn't a pure undefineddef, then we can
  3140. stop right there }
  3141. if (currtype.typedef.typ<>undefineddef) or (df_genconstraint in currtype.typedef.defoptions) then
  3142. exit;
  3143. if not foundretdef then
  3144. begin
  3145. { if the returndef is the same as this parameter's def then this
  3146. needs to be the case for both procdefs }
  3147. foundretdef:=fwpd.returndef=fwtype.typedef;
  3148. if foundretdef xor (currpd.returndef=currtype.typedef) then
  3149. exit;
  3150. end;
  3151. end;
  3152. if compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv,cpo_generic])<>te_exact then
  3153. exit;
  3154. if not foundretdef then
  3155. { the returndef isn't a type parameter, so compare as usual }
  3156. result:=compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact
  3157. else
  3158. result:=true;
  3159. end;
  3160. {
  3161. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  3162. forwarddef is found and reused it returns true
  3163. }
  3164. var
  3165. fwpd : tprocdef;
  3166. currparasym,
  3167. fwparasym : tsym;
  3168. currparacnt,
  3169. fwparacnt,
  3170. curridx,
  3171. fwidx,
  3172. i : longint;
  3173. po_comp : tprocoptions;
  3174. paracompopt: tcompare_paras_options;
  3175. forwardfound : boolean;
  3176. symentry: TSymEntry;
  3177. begin
  3178. forwardfound:=false;
  3179. { check overloaded functions if the same function already exists }
  3180. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  3181. begin
  3182. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  3183. { can happen for internally generated routines }
  3184. if (fwpd=currpd) then
  3185. begin
  3186. result:=true;
  3187. exit;
  3188. end;
  3189. { Skip overloaded definitions that are declared in other units }
  3190. if fwpd.procsym<>currpd.procsym then
  3191. continue;
  3192. { check the parameters, for delphi/tp it is possible to
  3193. leave the parameters away in the implementation (forwarddef=false).
  3194. But for an overload declared function this is not allowed }
  3195. if { check if empty implementation arguments match is allowed }
  3196. (
  3197. not(m_repeat_forward in current_settings.modeswitches) and
  3198. not(currpd.forwarddef) and
  3199. is_bareprocdef(currpd) and
  3200. not(po_overload in fwpd.procoptions)
  3201. ) or
  3202. (
  3203. fwpd.is_generic and
  3204. currpd.is_generic and
  3205. equal_generic_procdefs(fwpd,currpd)
  3206. ) or
  3207. { check arguments, we need to check only the user visible parameters. The hidden parameters
  3208. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV)
  3209. don't check default values here, because routines that are the same except for their default
  3210. values should be reported as mismatches (since you can't overload based on different default
  3211. parameter values) }
  3212. (
  3213. (compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  3214. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  3215. ) then
  3216. begin
  3217. { Check if we've found the forwarddef, if found then
  3218. we need to update the forward def with the current
  3219. implementation settings }
  3220. if fwpd.forwarddef then
  3221. begin
  3222. forwardfound:=true;
  3223. if not(m_repeat_forward in current_settings.modeswitches) and
  3224. (fwpd.proccalloption<>currpd.proccalloption) then
  3225. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  3226. else
  3227. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  3228. { Check calling convention }
  3229. if (fwpd.proccalloption<>currpd.proccalloption) then
  3230. begin
  3231. { In delphi it is possible to specify the calling
  3232. convention in the interface or implementation if
  3233. there was no convention specified in the other
  3234. part }
  3235. if (m_delphi in current_settings.modeswitches) then
  3236. begin
  3237. if not(po_hascallingconvention in currpd.procoptions) then
  3238. currpd.proccalloption:=fwpd.proccalloption
  3239. else
  3240. if not(po_hascallingconvention in fwpd.procoptions) then
  3241. fwpd.proccalloption:=currpd.proccalloption
  3242. else
  3243. begin
  3244. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  3245. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3246. { restore interface settings }
  3247. currpd.proccalloption:=fwpd.proccalloption;
  3248. end;
  3249. end
  3250. else
  3251. begin
  3252. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  3253. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3254. { restore interface settings }
  3255. currpd.proccalloption:=fwpd.proccalloption;
  3256. end;
  3257. end;
  3258. { Check static }
  3259. if (po_staticmethod in fwpd.procoptions) then
  3260. begin
  3261. if not (po_staticmethod in currpd.procoptions) then
  3262. begin
  3263. include(currpd.procoptions, po_staticmethod);
  3264. if (po_classmethod in currpd.procoptions) then
  3265. begin
  3266. { remove self from the hidden paras }
  3267. symentry:=currpd.parast.Find('self');
  3268. if symentry<>nil then
  3269. begin
  3270. currpd.parast.Delete(symentry);
  3271. currpd.calcparas;
  3272. end;
  3273. end;
  3274. end;
  3275. end;
  3276. { Check if the procedure type and return type are correct,
  3277. also the parameters must match also with the type and that
  3278. if the implementation has default parameters, the interface
  3279. also has them and that if they both have them, that they
  3280. have the same value }
  3281. if ((m_repeat_forward in current_settings.modeswitches) or
  3282. not is_bareprocdef(currpd)) and
  3283. (
  3284. (
  3285. fwpd.is_generic and
  3286. currpd.is_generic and
  3287. not equal_generic_procdefs(fwpd,currpd)
  3288. ) or
  3289. (
  3290. (
  3291. not fwpd.is_generic or
  3292. not currpd.is_generic
  3293. ) and
  3294. (
  3295. (compare_paras(fwpd.paras,currpd.paras,cp_all,paracompopt)<>te_exact) or
  3296. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)
  3297. )
  3298. )
  3299. ) then
  3300. begin
  3301. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  3302. fwpd.fullprocname(false));
  3303. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3304. break;
  3305. end;
  3306. { Check if both are declared forward }
  3307. if fwpd.forwarddef and currpd.forwarddef then
  3308. begin
  3309. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  3310. currpd.fullprocname(false));
  3311. end;
  3312. { internconst or internproc only need to be defined once }
  3313. if (fwpd.proccalloption=pocall_internproc) then
  3314. currpd.proccalloption:=fwpd.proccalloption
  3315. else
  3316. if (currpd.proccalloption=pocall_internproc) then
  3317. fwpd.proccalloption:=currpd.proccalloption;
  3318. { Check procedure options, Delphi requires that class is
  3319. repeated in the implementation for class methods }
  3320. if (m_fpc in current_settings.modeswitches) then
  3321. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  3322. else
  3323. po_comp:=[po_classmethod,po_methodpointer];
  3324. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  3325. (fwpd.proctypeoption <> currpd.proctypeoption) or
  3326. { if the implementation version has an "overload" modifier,
  3327. the interface version must also have it (otherwise we can
  3328. get annoying crashes due to interface crc changes) }
  3329. (not(po_overload in fwpd.procoptions) and
  3330. (po_overload in currpd.procoptions)) then
  3331. begin
  3332. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  3333. fwpd.fullprocname(false));
  3334. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  3335. { This error is non-fatal, we can recover }
  3336. end;
  3337. { Forward declaration is external? }
  3338. if (po_external in fwpd.procoptions) then
  3339. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  3340. { Check parameters }
  3341. if (m_repeat_forward in current_settings.modeswitches) or
  3342. (currpd.minparacount>0) then
  3343. begin
  3344. { If mangled names are equal then they have the same amount of arguments }
  3345. { We can check the names of the arguments }
  3346. { both symtables are in the same order from left to right }
  3347. curridx:=0;
  3348. fwidx:=0;
  3349. currparacnt:=currpd.parast.SymList.Count;
  3350. fwparacnt:=fwpd.parast.SymList.Count;
  3351. repeat
  3352. { skip default parameter constsyms }
  3353. while (curridx<currparacnt) and
  3354. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  3355. inc(curridx);
  3356. while (fwidx<fwparacnt) and
  3357. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  3358. inc(fwidx);
  3359. { stop when one of the two lists is at the end }
  3360. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  3361. break;
  3362. { compare names of parameters, ignore implictly
  3363. renamed parameters }
  3364. currparasym:=tsym(currpd.parast.SymList[curridx]);
  3365. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  3366. if not(sp_implicitrename in currparasym.symoptions) and
  3367. not(sp_implicitrename in fwparasym.symoptions) then
  3368. begin
  3369. if (currparasym.name<>fwparasym.name) then
  3370. begin
  3371. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  3372. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  3373. break;
  3374. end;
  3375. end;
  3376. { next parameter }
  3377. inc(curridx);
  3378. inc(fwidx);
  3379. until false;
  3380. end;
  3381. { Everything is checked, now we can update the forward declaration
  3382. with the new data from the implementation }
  3383. fwpd.forwarddef:=currpd.forwarddef;
  3384. fwpd.hasforward:=true;
  3385. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  3386. { marked as local but exported from unit? }
  3387. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  3388. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  3389. if fwpd.extnumber=$ffff then
  3390. fwpd.extnumber:=currpd.extnumber;
  3391. while not currpd.aliasnames.empty do
  3392. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  3393. { update fileinfo so position references the implementation,
  3394. also update funcretsym if it is already generated }
  3395. fwpd.fileinfo:=currpd.fileinfo;
  3396. if assigned(fwpd.funcretsym) then
  3397. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  3398. if assigned(currpd.deprecatedmsg) then
  3399. begin
  3400. stringdispose(fwpd.deprecatedmsg);
  3401. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  3402. end;
  3403. { import names }
  3404. if assigned(currpd.import_dll) then
  3405. begin
  3406. stringdispose(fwpd.import_dll);
  3407. fwpd.import_dll:=stringdup(currpd.import_dll^);
  3408. end;
  3409. if assigned(currpd.import_name) then
  3410. begin
  3411. stringdispose(fwpd.import_name);
  3412. fwpd.import_name:=stringdup(currpd.import_name^);
  3413. end;
  3414. fwpd.import_nr:=currpd.import_nr;
  3415. { for compilerproc defines we need to rename and update the
  3416. symbolname to lowercase so users can' access it (can't do
  3417. it immediately, because then the implementation symbol
  3418. won't be matched) }
  3419. if po_compilerproc in fwpd.procoptions then
  3420. compilerproc_set_symbol_name(fwpd);
  3421. { Release current procdef }
  3422. currpd.owner.deletedef(currpd);
  3423. currpd:=fwpd;
  3424. end
  3425. else
  3426. begin
  3427. { abstract methods aren't forward defined, but this }
  3428. { needs another error message }
  3429. if (po_abstractmethod in fwpd.procoptions) then
  3430. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  3431. else
  3432. begin
  3433. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  3434. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3435. end;
  3436. end;
  3437. { we found one proc with the same arguments, there are no others
  3438. so we can stop }
  3439. break;
  3440. end;
  3441. { check for allowing overload directive }
  3442. if not(m_fpc in current_settings.modeswitches) then
  3443. begin
  3444. { overload directive turns on overloading }
  3445. if ((po_overload in currpd.procoptions) or
  3446. (po_overload in fwpd.procoptions)) then
  3447. begin
  3448. { check if all procs have overloading, but not if the proc is a method or
  3449. already declared forward, then the check is already done }
  3450. if not(fwpd.hasforward or
  3451. assigned(currpd.struct) or
  3452. (currpd.forwarddef<>fwpd.forwarddef) or
  3453. ((po_overload in currpd.procoptions) and
  3454. (po_overload in fwpd.procoptions))) then
  3455. begin
  3456. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3457. break;
  3458. end
  3459. end
  3460. else
  3461. begin
  3462. if not(fwpd.forwarddef) then
  3463. begin
  3464. if (m_tp7 in current_settings.modeswitches) then
  3465. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  3466. else
  3467. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3468. break;
  3469. end;
  3470. end;
  3471. end; { equal arguments }
  3472. end;
  3473. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  3474. list }
  3475. if not forwardfound then
  3476. begin
  3477. { can happen in Delphi mode }
  3478. if (currpd.proctypeoption = potype_function) and
  3479. is_void(currpd.returndef) then
  3480. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  3481. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  3482. end;
  3483. proc_add_definition:=forwardfound;
  3484. end;
  3485. end.