pdecsub.pas 127 KB

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