2
0

pdecsub.pas 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing of the procedures/functions
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecsub;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,symconst,symtype,symdef,symsym;
  23. type
  24. tpdflag=(
  25. pd_body, { directive needs a body }
  26. pd_implemen, { directive can be used implementation section }
  27. pd_interface, { directive can be used interface section }
  28. pd_object, { directive can be used object declaration }
  29. pd_procvar, { directive can be used procvar declaration }
  30. pd_notobject, { directive can not be used object declaration }
  31. pd_notobjintf, { directive can not be used interface declaration }
  32. pd_notprocvar { directive can not be used procvar declaration }
  33. );
  34. tpdflags=set of tpdflag;
  35. function is_proc_directive(tok:ttoken;isprocvar:boolean):boolean;
  36. procedure calc_parast(pd:tabstractprocdef);
  37. procedure insert_funcret_local(pd:tprocdef);
  38. function proc_add_definition(var pd:tprocdef):boolean;
  39. procedure handle_calling_convention(pd:tabstractprocdef);
  40. procedure parse_parameter_dec(pd:tabstractprocdef);
  41. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  42. procedure parse_var_proc_directives(sym:tsym);
  43. procedure parse_object_proc_directives(pd:tabstractprocdef);
  44. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  45. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  46. implementation
  47. uses
  48. {$ifdef delphi}
  49. sysutils,
  50. {$else delphi}
  51. strings,
  52. {$endif delphi}
  53. { common }
  54. cutils,cclasses,
  55. { global }
  56. globtype,globals,verbose,
  57. systems,
  58. cpuinfo,
  59. { aasm }
  60. aasmbase,
  61. { symtable }
  62. symbase,symtable,defutil,defcmp,paramgr,
  63. { pass 1 }
  64. node,htypechk,
  65. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  66. { parser }
  67. fmodule,scanner,
  68. pbase,pexpr,ptype,pdecl,
  69. { linking }
  70. import,gendef
  71. ;
  72. const
  73. { Please leave this here, this module should NOT use
  74. these variables.
  75. Declaring it as string here results in an error when compiling (PFV) }
  76. current_procinfo = 'error';
  77. procedure insert_funcret_para(pd:tabstractprocdef);
  78. var
  79. storepos : tfileposinfo;
  80. vs : tvarsym;
  81. begin
  82. if not is_void(pd.rettype.def) and
  83. paramanager.ret_in_param(pd.rettype.def,pd.proccalloption) then
  84. begin
  85. storepos:=akttokenpos;
  86. if pd.deftype=procdef then
  87. akttokenpos:=tprocdef(pd).fileinfo;
  88. { Generate result variable accessing function result }
  89. vs:=tvarsym.create('$result',vs_var,pd.rettype);
  90. include(vs.varoptions,vo_is_funcret);
  91. include(vs.varoptions,vo_regable);
  92. pd.parast.insert(vs);
  93. { For left to right add it at the end to be delphi compatible }
  94. if pd.proccalloption in pushleftright_pocalls then
  95. pd.concatpara(nil,vs.vartype,vs,nil,true)
  96. else
  97. pd.insertpara(vs.vartype,vs,nil,true);
  98. { Store the this symbol as funcretsym for procedures }
  99. if pd.deftype=procdef then
  100. tprocdef(pd).funcretsym:=vs;
  101. akttokenpos:=storepos;
  102. end;
  103. end;
  104. procedure insert_parentfp_para(pd:tabstractprocdef);
  105. var
  106. storepos : tfileposinfo;
  107. vs : tvarsym;
  108. begin
  109. if pd.parast.symtablelevel>normal_function_level then
  110. begin
  111. storepos:=akttokenpos;
  112. if pd.deftype=procdef then
  113. akttokenpos:=tprocdef(pd).fileinfo;
  114. { Generate result variable accessing function result }
  115. vs:=tvarsym.create('$parentfp',vs_var,voidpointertype);
  116. include(vs.varoptions,vo_is_parentfp);
  117. pd.parast.insert(vs);
  118. pd.insertpara(vs.vartype,vs,nil,true);
  119. akttokenpos:=storepos;
  120. end;
  121. end;
  122. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  123. var
  124. storepos : tfileposinfo;
  125. vs : tvarsym;
  126. tt : ttype;
  127. vsp : tvarspez;
  128. begin
  129. if (pd.deftype=procvardef) and
  130. pd.is_methodpointer then
  131. begin
  132. { Generate self variable }
  133. tt:=voidpointertype;
  134. vs:=tvarsym.create('$self',vs_value,tt);
  135. include(vs.varoptions,vo_is_self);
  136. { Insert as hidden parameter }
  137. pd.parast.insert(vs);
  138. pd.insertpara(vs.vartype,vs,nil,true);
  139. end
  140. else
  141. begin
  142. if (pd.deftype=procdef) and
  143. assigned(tprocdef(pd)._class) and
  144. (pd.parast.symtablelevel=normal_function_level) then
  145. begin
  146. storepos:=akttokenpos;
  147. akttokenpos:=tprocdef(pd).fileinfo;
  148. { Generate VMT variable for constructor/destructor }
  149. if pd.proctypeoption in [potype_constructor,potype_destructor] then
  150. begin
  151. { can't use classrefdef as type because inheriting
  152. will then always file because of a type mismatch }
  153. tt:=voidpointertype;
  154. vs:=tvarsym.create('$vmt',vs_value,tt);
  155. include(vs.varoptions,vo_is_vmt);
  156. { Insert as hidden parameter }
  157. pd.parast.insert(vs);
  158. pd.insertpara(vs.vartype,vs,nil,true);
  159. end;
  160. { Generate self variable, for classes we need
  161. to use the generic voidpointer to be compatible with
  162. methodpointers }
  163. vsp:=vs_value;
  164. if (po_staticmethod in pd.procoptions) or
  165. (po_classmethod in pd.procoptions) then
  166. begin
  167. tt.setdef(tprocdef(pd)._class);
  168. tt.setdef(tclassrefdef.create(tt));
  169. end
  170. else
  171. begin
  172. if is_object(tprocdef(pd)._class) then
  173. vsp:=vs_var;
  174. tt.setdef(tprocdef(pd)._class);
  175. end;
  176. vs:=tvarsym.create('$self',vsp,tt);
  177. include(vs.varoptions,vo_is_self);
  178. include(vs.varoptions,vo_regable);
  179. { Insert as hidden parameter }
  180. pd.parast.insert(vs);
  181. pd.insertpara(vs.vartype,vs,nil,true);
  182. akttokenpos:=storepos;
  183. end;
  184. end;
  185. end;
  186. procedure insert_funcret_local(pd:tprocdef);
  187. var
  188. storepos : tfileposinfo;
  189. vs : tvarsym;
  190. sl : tsymlist;
  191. begin
  192. if not is_void(pd.rettype.def) then
  193. begin
  194. storepos:=akttokenpos;
  195. akttokenpos:=pd.fileinfo;
  196. { We always need a localsymtable }
  197. if not assigned(pd.localst) then
  198. pd.insert_localst;
  199. { We need to insert a varsym for the result in the localst
  200. when it is returning in a register }
  201. if not paramanager.ret_in_param(pd.rettype.def,pd.proccalloption) then
  202. begin
  203. vs:=tvarsym.create('$result',vs_value,pd.rettype);
  204. include(vs.varoptions,vo_is_funcret);
  205. if tstoreddef(pd.rettype.def).is_intregable then
  206. include(vs.varoptions,vo_regable);
  207. if tstoreddef(pd.rettype.def).is_fpuregable then
  208. include(vs.varoptions,vo_fpuregable);
  209. pd.localst.insert(vs);
  210. pd.funcretsym:=vs;
  211. end;
  212. { insert the name of the procedure as alias for the function result,
  213. we can't use realname because that will not work for compilerprocs
  214. as the name is lowercase and unreachable from the code }
  215. if pd.resultname='' then
  216. pd.resultname:=pd.procsym.name;
  217. sl:=tsymlist.create;
  218. sl.addsym(sl_load,pd.funcretsym);
  219. vs:=tabsolutesym.create_ref(pd.resultname,pd.rettype,sl);
  220. include(vs.varoptions,vo_is_funcret);
  221. pd.localst.insert(vs);
  222. { insert result also if support is on }
  223. if (m_result in aktmodeswitches) then
  224. begin
  225. sl:=tsymlist.create;
  226. sl.addsym(sl_load,pd.funcretsym);
  227. vs:=tabsolutesym.create_ref('RESULT',pd.rettype,sl);
  228. include(vs.varoptions,vo_is_funcret);
  229. include(vs.varoptions,vo_is_result);
  230. pd.localst.insert(vs);
  231. end;
  232. akttokenpos:=storepos;
  233. end;
  234. end;
  235. procedure insert_hidden_para(pd:tabstractprocdef);
  236. var
  237. currpara : tparaitem;
  238. hvs : tvarsym;
  239. begin
  240. { walk from right to left, so we can insert the
  241. high parameters after the current parameter }
  242. currpara:=tparaitem(pd.para.last);
  243. while assigned(currpara) do
  244. begin
  245. { needs high parameter ? }
  246. if paramanager.push_high_param(currpara.paratyp,currpara.paratype.def,pd.proccalloption) then
  247. begin
  248. if assigned(currpara.parasym) then
  249. begin
  250. hvs:=tvarsym.create('$high'+tvarsym(currpara.parasym).name,vs_const,sinttype);
  251. include(hvs.varoptions,vo_is_high_value);
  252. tvarsym(currpara.parasym).owner.insert(hvs);
  253. end
  254. else
  255. hvs:=nil;
  256. pd.concatpara(currpara,sinttype,hvs,nil,true);
  257. end
  258. else
  259. begin
  260. { Give a warning that cdecl routines does not include high()
  261. support }
  262. if (pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  263. paramanager.push_high_param(currpara.paratyp,currpara.paratype.def,pocall_default) then
  264. begin
  265. if is_open_string(currpara.paratype.def) then
  266. Message(parser_w_cdecl_no_openstring);
  267. if not (po_external in pd.procoptions) then
  268. Message(parser_w_cdecl_has_no_high);
  269. end;
  270. end;
  271. currpara:=tparaitem(currpara.previous);
  272. end;
  273. end;
  274. procedure check_c_para(p:tnamedindexitem;arg:pointer);
  275. begin
  276. if (tsym(p).typ<>varsym) then
  277. exit;
  278. with tvarsym(p) do
  279. begin
  280. case vartype.def.deftype of
  281. arraydef :
  282. begin
  283. if not is_variant_array(vartype.def) and
  284. not is_array_of_const(vartype.def) then
  285. begin
  286. if (varspez<>vs_var) then
  287. Message(parser_h_c_arrays_are_references);
  288. end;
  289. if is_array_of_const(vartype.def) and
  290. assigned(indexnext) and
  291. (tsym(indexnext).typ=varsym) and
  292. not(vo_is_high_value in tvarsym(indexnext).varoptions) then
  293. Message(parser_e_C_array_of_const_must_be_last);
  294. end;
  295. end;
  296. end;
  297. end;
  298. procedure parse_parameter_dec(pd:tabstractprocdef);
  299. {
  300. handle_procvar needs the same changes
  301. }
  302. var
  303. is_procvar : boolean;
  304. sc : tsinglelist;
  305. tt : ttype;
  306. arrayelementtype : ttype;
  307. vs : tvarsym;
  308. srsym : tsym;
  309. hs1 : string;
  310. varspez : Tvarspez;
  311. defaultvalue : tconstsym;
  312. defaultrequired : boolean;
  313. old_object_option : tsymoptions;
  314. currparast : tparasymtable;
  315. explicit_paraloc : boolean;
  316. locationstr : string;
  317. begin
  318. explicit_paraloc:=false;
  319. consume(_LKLAMMER);
  320. { Delphi/Kylix supports nonsense like }
  321. { procedure p(); }
  322. if try_to_consume(_RKLAMMER) and
  323. not(m_tp7 in aktmodeswitches) then
  324. exit;
  325. { parsing a proc or procvar ? }
  326. is_procvar:=(pd.deftype=procvardef);
  327. currparast:=tparasymtable(pd.parast);
  328. { reset }
  329. sc:=tsinglelist.create;
  330. defaultrequired:=false;
  331. { the variables are always public }
  332. old_object_option:=current_object_option;
  333. current_object_option:=[sp_public];
  334. inc(testcurobject);
  335. repeat
  336. if try_to_consume(_VAR) then
  337. varspez:=vs_var
  338. else
  339. if try_to_consume(_CONST) then
  340. varspez:=vs_const
  341. else
  342. if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  343. begin
  344. consume(_OUT);
  345. varspez:=vs_out
  346. end
  347. else
  348. if (token=_POINTPOINTPOINT) and (m_mac in aktmodeswitches) then
  349. begin
  350. consume(_POINTPOINTPOINT);
  351. include(pd.procoptions,po_varargs);
  352. break;
  353. end
  354. else
  355. varspez:=vs_value;
  356. defaultvalue:=nil;
  357. tt.reset;
  358. { read identifiers and insert with error type }
  359. sc.reset;
  360. repeat
  361. vs:=tvarsym.create(orgpattern,varspez,generrortype);
  362. currparast.insert(vs);
  363. if assigned(vs.owner) then
  364. sc.insert(vs)
  365. else
  366. vs.free;
  367. consume(_ID);
  368. until not try_to_consume(_COMMA);
  369. locationstr:='';
  370. { read type declaration, force reading for value and const paras }
  371. if (token=_COLON) or (varspez=vs_value) then
  372. begin
  373. consume(_COLON);
  374. { check for an open array }
  375. if token=_ARRAY then
  376. begin
  377. consume(_ARRAY);
  378. consume(_OF);
  379. { define range and type of range }
  380. tt.setdef(tarraydef.create(0,-1,s32inttype));
  381. { array of const ? }
  382. if (token=_CONST) and (m_objpas in aktmodeswitches) then
  383. begin
  384. consume(_CONST);
  385. srsym:=searchsymonlyin(systemunit,'TVARREC');
  386. if not assigned(srsym) then
  387. InternalError(200404181);
  388. tarraydef(tt.def).setelementtype(ttypesym(srsym).restype);
  389. tarraydef(tt.def).IsArrayOfConst:=true;
  390. end
  391. else
  392. begin
  393. { define field type }
  394. single_type(arrayelementtype,hs1,false);
  395. tarraydef(tt.def).setelementtype(arrayelementtype);
  396. end;
  397. end
  398. else
  399. begin
  400. { open string ? }
  401. if (varspez=vs_var) and
  402. (
  403. (
  404. ((token=_STRING) or (idtoken=_SHORTSTRING)) and
  405. (cs_openstring in aktmoduleswitches) and
  406. not(cs_ansistrings in aktlocalswitches)
  407. ) or
  408. (idtoken=_OPENSTRING)) then
  409. begin
  410. consume(token);
  411. tt:=openshortstringtype;
  412. hs1:='openstring';
  413. end
  414. else
  415. begin
  416. { everything else }
  417. if (m_mac in aktmodeswitches) then
  418. try_to_consume(_UNIV); {currently does nothing}
  419. single_type(tt,hs1,false);
  420. end;
  421. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  422. begin
  423. if (idtoken=_LOCATION) then
  424. begin
  425. consume(_LOCATION);
  426. locationstr:=pattern;
  427. consume(_CSTRING);
  428. end
  429. else
  430. begin
  431. if explicit_paraloc then
  432. Message(parser_e_paraloc_all_paras);
  433. locationstr:='';
  434. end;
  435. end
  436. else
  437. locationstr:='';
  438. { default parameter }
  439. if (m_default_para in aktmodeswitches) then
  440. begin
  441. if try_to_consume(_EQUAL) then
  442. begin
  443. vs:=tvarsym(sc.first);
  444. if assigned(vs.listnext) then
  445. Message(parser_e_default_value_only_one_para);
  446. { prefix 'def' to the parameter name }
  447. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  448. include(defaultvalue.symoptions,sp_internal);
  449. if assigned(defaultvalue) then
  450. tprocdef(pd).parast.insert(defaultvalue);
  451. defaultrequired:=true;
  452. end
  453. else
  454. begin
  455. if defaultrequired then
  456. Message1(parser_e_default_value_expected_for_para,vs.name);
  457. end;
  458. end;
  459. end;
  460. end
  461. else
  462. begin
  463. {$ifndef UseNiceNames}
  464. hs1:='$$$';
  465. {$else UseNiceNames}
  466. hs1:='var';
  467. {$endif UseNiceNames}
  468. tt:=cformaltype;
  469. end;
  470. { File types are only allowed for var parameters }
  471. if (tt.def.deftype=filedef) and
  472. (varspez<>vs_var) then
  473. CGMessage(cg_e_file_must_call_by_reference);
  474. vs:=tvarsym(sc.first);
  475. while assigned(vs) do
  476. begin
  477. { update varsym }
  478. vs.vartype:=tt;
  479. { For proc vars we only need the definitions }
  480. if not is_procvar then
  481. begin
  482. if (varspez in [vs_var,vs_const,vs_out]) and
  483. paramanager.push_addr_param(varspez,tt.def,pd.proccalloption) then
  484. include(vs.varoptions,vo_regable);
  485. end;
  486. pd.concatpara(nil,tt,vs,defaultvalue,false);
  487. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  488. begin
  489. if locationstr<>'' then
  490. begin
  491. if assigned(sc.first.listnext) then
  492. Message(parser_e_paraloc_only_one_para);
  493. if (pd.para.first<>pd.para.last) and not(explicit_paraloc) then
  494. Message(parser_e_paraloc_all_paras);
  495. explicit_paraloc:=true;
  496. if not(paramanager.parseparaloc(tparaitem(pd.para.last),upper(locationstr))) then
  497. message(parser_e_illegal_explicit_paraloc);
  498. end
  499. else
  500. if explicit_paraloc then
  501. Message(parser_e_paraloc_all_paras);
  502. end;
  503. vs:=tvarsym(vs.listnext);
  504. end;
  505. until not try_to_consume(_SEMICOLON);
  506. if explicit_paraloc then
  507. begin
  508. pd.has_paraloc_info:=true;
  509. include(pd.procoptions,po_explicitparaloc);
  510. end;
  511. { remove parasymtable from stack }
  512. sc.free;
  513. { reset object options }
  514. dec(testcurobject);
  515. current_object_option:=old_object_option;
  516. consume(_RKLAMMER);
  517. end;
  518. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  519. var
  520. orgsp,sp : stringid;
  521. sym : tsym;
  522. srsymtable : tsymtable;
  523. storepos,
  524. procstartfilepos : tfileposinfo;
  525. searchagain : boolean;
  526. i : longint;
  527. st : tsymtable;
  528. aprocsym : tprocsym;
  529. begin
  530. { Save the position where this procedure really starts }
  531. procstartfilepos:=akttokenpos;
  532. result:=false;
  533. pd:=nil;
  534. aprocsym:=nil;
  535. if (potype=potype_operator) then
  536. begin
  537. sp:=overloaded_names[optoken];
  538. orgsp:=sp;
  539. end
  540. else
  541. begin
  542. sp:=pattern;
  543. orgsp:=orgpattern;
  544. consume(_ID);
  545. end;
  546. { examine interface map: function/procedure iname.functionname=locfuncname }
  547. if assigned(aclass) and
  548. assigned(aclass.implementedinterfaces) and
  549. (aclass.implementedinterfaces.count>0) and
  550. try_to_consume(_POINT) then
  551. begin
  552. storepos:=akttokenpos;
  553. akttokenpos:=procstartfilepos;
  554. { get interface syms}
  555. searchsym(sp,sym,srsymtable);
  556. if not assigned(sym) then
  557. begin
  558. identifier_not_found(orgsp);
  559. sym:=generrorsym;
  560. end;
  561. akttokenpos:=storepos;
  562. { qualifier is interface? }
  563. if (sym.typ=typesym) and
  564. (ttypesym(sym).restype.def.deftype=objectdef) then
  565. i:=aclass.implementedinterfaces.searchintf(ttypesym(sym).restype.def)
  566. else
  567. i:=-1;
  568. if (i=-1) then
  569. Message(parser_e_interface_id_expected);
  570. consume(_ID);
  571. consume(_EQUAL);
  572. if (token=_ID) then
  573. aclass.implementedinterfaces.addmappings(i,sp,pattern);
  574. consume(_ID);
  575. result:=true;
  576. exit;
  577. end;
  578. { method ? }
  579. if not assigned(aclass) and
  580. (potype<>potype_operator) and
  581. (symtablestack.symtablelevel=main_program_level) and
  582. try_to_consume(_POINT) then
  583. begin
  584. { search for object name }
  585. storepos:=akttokenpos;
  586. akttokenpos:=procstartfilepos;
  587. searchsym(sp,sym,srsymtable);
  588. if not assigned(sym) then
  589. begin
  590. identifier_not_found(orgsp);
  591. sym:=generrorsym;
  592. end;
  593. akttokenpos:=storepos;
  594. { consume proc name }
  595. sp:=pattern;
  596. orgsp:=orgpattern;
  597. procstartfilepos:=akttokenpos;
  598. consume(_ID);
  599. { qualifier is class name ? }
  600. if (sym.typ=typesym) and
  601. (ttypesym(sym).restype.def.deftype=objectdef) then
  602. begin
  603. aclass:=tobjectdef(ttypesym(sym).restype.def);
  604. aprocsym:=tprocsym(aclass.symtable.search(sp));
  605. { we solve this below }
  606. if assigned(aprocsym) then
  607. begin
  608. if aprocsym.typ<>procsym then
  609. begin
  610. { we use a different error message for tp7 so it looks more compatible }
  611. if (m_fpc in aktmodeswitches) then
  612. Message1(parser_e_overloaded_no_procedure,aprocsym.realname)
  613. else
  614. Message(parser_e_methode_id_expected);
  615. { rename the name to an unique name to avoid an
  616. error when inserting the symbol in the symtable }
  617. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  618. aprocsym:=nil;
  619. end;
  620. end
  621. else
  622. begin
  623. Message(parser_e_methode_id_expected);
  624. { recover by making it a normal procedure instead of method }
  625. aclass:=nil;
  626. end;
  627. end
  628. else
  629. Message(parser_e_class_id_expected);
  630. end
  631. else
  632. begin
  633. { check for constructor/destructor which is not allowed here }
  634. if (not parse_only) and
  635. (potype in [potype_constructor,potype_destructor]) then
  636. Message(parser_e_constructors_always_objects);
  637. repeat
  638. searchagain:=false;
  639. akttokenpos:=procstartfilepos;
  640. aprocsym:=tprocsym(symtablestack.search(sp));
  641. if not(parse_only) and
  642. not assigned(aprocsym) and
  643. (symtablestack.symtabletype=staticsymtable) and
  644. assigned(symtablestack.next) and
  645. (symtablestack.next.unitid=0) then
  646. begin
  647. { The procedure we prepare for is in the implementation
  648. part of the unit we compile. It is also possible that we
  649. are compiling a program, which is also some kind of
  650. implementaion part.
  651. We need to find out if the procedure is global. If it is
  652. global, it is in the global symtable.}
  653. aprocsym:=tprocsym(symtablestack.next.search(sp));
  654. end;
  655. { Check if overloaded is a procsym }
  656. if assigned(aprocsym) and
  657. (aprocsym.typ<>procsym) then
  658. begin
  659. { when the other symbol is a unit symbol then hide the unit
  660. symbol }
  661. if (aprocsym.typ=unitsym) then
  662. begin
  663. aprocsym.owner.rename(aprocsym.name,'hidden'+aprocsym.name);
  664. searchagain:=true;
  665. end
  666. else
  667. begin
  668. { we use a different error message for tp7 so it looks more compatible }
  669. if (m_fpc in aktmodeswitches) then
  670. Message1(parser_e_overloaded_no_procedure,aprocsym.realname)
  671. else
  672. DuplicateSym(aprocsym);
  673. { rename the name to an unique name to avoid an
  674. error when inserting the symbol in the symtable }
  675. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  676. { generate a new aktprocsym }
  677. aprocsym:=nil;
  678. end;
  679. end;
  680. until not searchagain;
  681. end;
  682. { test again if assigned, it can be reset to recover }
  683. if not assigned(aprocsym) then
  684. begin
  685. { create a new procsym and set the real filepos }
  686. akttokenpos:=procstartfilepos;
  687. { for operator we have only one procsym for each overloaded
  688. operation }
  689. if (potype=potype_operator) then
  690. begin
  691. Aprocsym:=Tprocsym(symtablestack.search(sp));
  692. if Aprocsym=nil then
  693. Aprocsym:=tprocsym.create('$'+sp);
  694. end
  695. else
  696. aprocsym:=tprocsym.create(orgsp);
  697. symtablestack.insert(aprocsym);
  698. end;
  699. { to get the correct symtablelevel we must ignore objectsymtables }
  700. st:=symtablestack;
  701. while not(st.symtabletype in [staticsymtable,globalsymtable,localsymtable]) do
  702. st:=st.next;
  703. pd:=tprocdef.create(st.symtablelevel+1);
  704. pd._class:=aclass;
  705. pd.procsym:=aprocsym;
  706. pd.proctypeoption:=potype;
  707. { methods need to be exported }
  708. if assigned(aclass) and
  709. (
  710. (symtablestack.symtabletype=objectsymtable) or
  711. (symtablestack.symtablelevel=main_program_level)
  712. ) then
  713. include(pd.procoptions,po_public);
  714. { symbol options that need to be kept per procdef }
  715. pd.fileinfo:=procstartfilepos;
  716. pd.symoptions:=current_object_option;
  717. { parse parameters }
  718. if token=_LKLAMMER then
  719. parse_parameter_dec(pd);
  720. result:=true;
  721. end;
  722. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  723. var
  724. pd : tprocdef;
  725. hs : string;
  726. isclassmethod : boolean;
  727. begin
  728. pd:=nil;
  729. { read class method }
  730. if try_to_consume(_CLASS) then
  731. begin
  732. { class method only allowed for procedures and functions }
  733. if not(token in [_FUNCTION,_PROCEDURE]) then
  734. Message(parser_e_procedure_or_function_expected);
  735. isclassmethod:=true;
  736. end
  737. else
  738. isclassmethod:=false;
  739. case token of
  740. _FUNCTION :
  741. begin
  742. consume(_FUNCTION);
  743. if parse_proc_head(aclass,potype_none,pd) then
  744. begin
  745. { pd=nil when it is a interface mapping }
  746. if assigned(pd) then
  747. begin
  748. if try_to_consume(_COLON) then
  749. begin
  750. inc(testcurobject);
  751. single_type(pd.rettype,hs,false);
  752. pd.test_if_fpu_result;
  753. dec(testcurobject);
  754. end
  755. else
  756. begin
  757. if (
  758. parse_only and
  759. not(is_interface(pd._class))
  760. ) or
  761. (m_repeat_forward in aktmodeswitches) then
  762. begin
  763. consume(_COLON);
  764. consume_all_until(_SEMICOLON);
  765. end;
  766. end;
  767. if isclassmethod then
  768. include(pd.procoptions,po_classmethod);
  769. end;
  770. end
  771. else
  772. begin
  773. { recover }
  774. consume(_COLON);
  775. consume_all_until(_SEMICOLON);
  776. end;
  777. end;
  778. _PROCEDURE :
  779. begin
  780. consume(_PROCEDURE);
  781. if parse_proc_head(aclass,potype_none,pd) then
  782. begin
  783. { pd=nil when it is a interface mapping }
  784. if assigned(pd) then
  785. begin
  786. pd.rettype:=voidtype;
  787. if isclassmethod then
  788. include(pd.procoptions,po_classmethod);
  789. end;
  790. end;
  791. end;
  792. _CONSTRUCTOR :
  793. begin
  794. consume(_CONSTRUCTOR);
  795. parse_proc_head(aclass,potype_constructor,pd);
  796. if assigned(pd) and
  797. assigned(pd._class) then
  798. begin
  799. { Set return type, class constructors return the
  800. created instance, object constructors return boolean }
  801. if is_class(pd._class) then
  802. pd.rettype.setdef(pd._class)
  803. else
  804. pd.rettype:=booltype;
  805. end;
  806. end;
  807. _DESTRUCTOR :
  808. begin
  809. consume(_DESTRUCTOR);
  810. parse_proc_head(aclass,potype_destructor,pd);
  811. if assigned(pd) then
  812. pd.rettype:=voidtype;
  813. end;
  814. _OPERATOR :
  815. begin
  816. consume(_OPERATOR);
  817. if (token in [first_overloaded..last_overloaded]) then
  818. begin
  819. optoken:=token;
  820. end
  821. else
  822. begin
  823. Message(parser_e_overload_operator_failed);
  824. { Use the dummy NOTOKEN that is also declared
  825. for the overloaded_operator[] }
  826. optoken:=NOTOKEN;
  827. end;
  828. consume(token);
  829. parse_proc_head(aclass,potype_operator,pd);
  830. if assigned(pd) then
  831. begin
  832. if pd.parast.symtablelevel>normal_function_level then
  833. Message(parser_e_no_local_operator);
  834. if token<>_ID then
  835. begin
  836. if not(m_result in aktmodeswitches) then
  837. consume(_ID);
  838. end
  839. else
  840. begin
  841. pd.resultname:=orgpattern;
  842. consume(_ID);
  843. end;
  844. if not try_to_consume(_COLON) then
  845. begin
  846. consume(_COLON);
  847. pd.rettype:=generrortype;
  848. consume_all_until(_SEMICOLON);
  849. end
  850. else
  851. begin
  852. single_type(pd.rettype,hs,false);
  853. pd.test_if_fpu_result;
  854. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  855. ((pd.rettype.def.deftype<>orddef) or
  856. (torddef(pd.rettype.def).typ<>bool8bit)) then
  857. Message(parser_e_comparative_operator_return_boolean);
  858. if (optoken=_ASSIGNMENT) and
  859. equal_defs(pd.rettype.def,
  860. tvarsym(pd.parast.symindex.first).vartype.def) then
  861. message(parser_e_no_such_assignment)
  862. else if not isoperatoracceptable(pd,optoken) then
  863. Message(parser_e_overload_impossible);
  864. end;
  865. end
  866. else
  867. begin
  868. { recover }
  869. try_to_consume(_ID);
  870. consume(_COLON);
  871. consume_all_until(_SEMICOLON);
  872. end;
  873. end;
  874. end;
  875. { support procedure proc stdcall export; }
  876. if not(is_proc_directive(token,false)) then
  877. consume(_SEMICOLON);
  878. result:=pd;
  879. end;
  880. {****************************************************************************
  881. Procedure directive handlers
  882. ****************************************************************************}
  883. procedure pd_far(pd:tabstractprocdef);
  884. begin
  885. Message1(parser_w_proc_directive_ignored,'FAR');
  886. end;
  887. procedure pd_near(pd:tabstractprocdef);
  888. begin
  889. Message1(parser_w_proc_directive_ignored,'NEAR');
  890. end;
  891. procedure pd_export(pd:tabstractprocdef);
  892. begin
  893. if pd.deftype<>procdef then
  894. internalerror(200304264);
  895. if assigned(tprocdef(pd)._class) then
  896. Message(parser_e_methods_dont_be_export);
  897. if pd.parast.symtablelevel>normal_function_level then
  898. Message(parser_e_dont_nest_export);
  899. { only os/2 and emx need this }
  900. if target_info.system in [system_i386_os2,system_i386_emx] then
  901. begin
  902. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  903. if cs_link_deffile in aktglobalswitches then
  904. deffile.AddExport(tprocdef(pd).mangledname);
  905. end;
  906. end;
  907. procedure pd_forward(pd:tabstractprocdef);
  908. begin
  909. if pd.deftype<>procdef then
  910. internalerror(200304265);
  911. tprocdef(pd).forwarddef:=true;
  912. end;
  913. procedure pd_alias(pd:tabstractprocdef);
  914. begin
  915. if pd.deftype<>procdef then
  916. internalerror(200304266);
  917. consume(_COLON);
  918. tprocdef(pd).aliasnames.insert(get_stringconst);
  919. end;
  920. procedure pd_asmname(pd:tabstractprocdef);
  921. begin
  922. if pd.deftype<>procdef then
  923. internalerror(200304267);
  924. tprocdef(pd).setmangledname(target_info.Cprefix+pattern);
  925. if token=_CCHAR then
  926. consume(_CCHAR)
  927. else
  928. consume(_CSTRING);
  929. { we don't need anything else }
  930. tprocdef(pd).forwarddef:=false;
  931. end;
  932. procedure pd_inline(pd:tabstractprocdef);
  933. var
  934. hp : tparaitem;
  935. begin
  936. { check if there is an array of const }
  937. hp:=tparaitem(pd.para.first);
  938. while assigned(hp) do
  939. begin
  940. if assigned(hp.paratype.def) and
  941. (hp.paratype.def.deftype=arraydef) then
  942. begin
  943. with tarraydef(hp.paratype.def) do
  944. if IsVariant or IsConstructor {or IsArrayOfConst} then
  945. begin
  946. Message1(parser_w_not_supported_for_inline,'array of const');
  947. Message(parser_w_inlining_disabled);
  948. pd.proccalloption:=pocall_default;
  949. end;
  950. end;
  951. hp:=tparaitem(hp.next);
  952. end;
  953. end;
  954. procedure pd_intern(pd:tabstractprocdef);
  955. begin
  956. if pd.deftype<>procdef then
  957. internalerror(200304268);
  958. consume(_COLON);
  959. tprocdef(pd).extnumber:=get_intconst;
  960. end;
  961. procedure pd_interrupt(pd:tabstractprocdef);
  962. begin
  963. if pd.parast.symtablelevel>normal_function_level then
  964. Message(parser_e_dont_nest_interrupt);
  965. end;
  966. procedure pd_abstract(pd:tabstractprocdef);
  967. begin
  968. if pd.deftype<>procdef then
  969. internalerror(200304269);
  970. if (po_virtualmethod in pd.procoptions) then
  971. include(pd.procoptions,po_abstractmethod)
  972. else
  973. Message(parser_e_only_virtual_methods_abstract);
  974. { the method is defined }
  975. tprocdef(pd).forwarddef:=false;
  976. end;
  977. procedure pd_virtual(pd:tabstractprocdef);
  978. {$ifdef WITHDMT}
  979. var
  980. pt : tnode;
  981. {$endif WITHDMT}
  982. begin
  983. if pd.deftype<>procdef then
  984. internalerror(2003042610);
  985. if (pd.proctypeoption=potype_constructor) and
  986. is_object(tprocdef(pd)._class) then
  987. Message(parser_e_constructor_cannot_be_not_virtual);
  988. {$ifdef WITHDMT}
  989. if is_object(tprocdef(pd)._class) and
  990. (token<>_SEMICOLON) then
  991. begin
  992. { any type of parameter is allowed here! }
  993. pt:=comp_expr(true);
  994. if is_constintnode(pt) then
  995. begin
  996. include(pd.procoptions,po_msgint);
  997. pd.messageinf.i:=pt^.value;
  998. end
  999. else
  1000. Message(parser_e_ill_msg_expr);
  1001. disposetree(pt);
  1002. end;
  1003. {$endif WITHDMT}
  1004. end;
  1005. procedure pd_static(pd:tabstractprocdef);
  1006. begin
  1007. if (cs_static_keyword in aktmoduleswitches) then
  1008. begin
  1009. if pd.deftype=procdef then
  1010. include(tprocdef(pd).procsym.symoptions,sp_static);
  1011. include(pd.procoptions,po_staticmethod);
  1012. end;
  1013. end;
  1014. procedure pd_override(pd:tabstractprocdef);
  1015. begin
  1016. if pd.deftype<>procdef then
  1017. internalerror(2003042611);
  1018. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1019. Message(parser_e_no_object_override);
  1020. end;
  1021. procedure pd_overload(pd:tabstractprocdef);
  1022. begin
  1023. if pd.deftype<>procdef then
  1024. internalerror(2003042612);
  1025. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1026. end;
  1027. procedure pd_message(pd:tabstractprocdef);
  1028. var
  1029. pt : tnode;
  1030. begin
  1031. if pd.deftype<>procdef then
  1032. internalerror(2003042613);
  1033. if not is_class(tprocdef(pd)._class) then
  1034. Message(parser_e_msg_only_for_classes);
  1035. { check parameter type }
  1036. if ((pd.minparacount<>1) or
  1037. (pd.maxparacount<>1) or
  1038. (TParaItem(pd.Para.first).paratyp<>vs_var)) then
  1039. Message(parser_e_ill_msg_param);
  1040. pt:=comp_expr(true);
  1041. if pt.nodetype=stringconstn then
  1042. begin
  1043. include(pd.procoptions,po_msgstr);
  1044. tprocdef(pd).messageinf.str:=strnew(tstringconstnode(pt).value_str);
  1045. end
  1046. else
  1047. if is_constintnode(pt) then
  1048. begin
  1049. include(pd.procoptions,po_msgint);
  1050. tprocdef(pd).messageinf.i:=tordconstnode(pt).value;
  1051. end
  1052. else
  1053. Message(parser_e_ill_msg_expr);
  1054. pt.free;
  1055. end;
  1056. procedure pd_reintroduce(pd:tabstractprocdef);
  1057. begin
  1058. if pd.deftype<>procdef then
  1059. internalerror(200401211);
  1060. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1061. Message(parser_e_no_object_reintroduce);
  1062. end;
  1063. procedure pd_syscall(pd:tabstractprocdef);
  1064. var
  1065. sym : tsym;
  1066. symtable : tsymtable;
  1067. begin
  1068. if pd.deftype<>procdef then
  1069. internalerror(2003042614);
  1070. tprocdef(pd).forwarddef:=false;
  1071. {$ifdef powerpc}
  1072. if target_info.system in [system_powerpc_morphos,system_m68k_amiga] then
  1073. begin
  1074. pd.has_paraloc_info:=true;
  1075. include(pd.procoptions,po_explicitparaloc);
  1076. if consume_sym(sym,symtable) then
  1077. begin
  1078. if (sym.typ=varsym) and
  1079. (is_voidpointer(tvarsym(sym).vartype.def) or
  1080. is_32bitint(tvarsym(sym).vartype.def)) then
  1081. begin
  1082. tprocdef(pd).libsym:=sym;
  1083. pd.concatpara(nil,tvarsym(sym).vartype,tvarsym(sym),nil,true);
  1084. paramanager.parseparaloc(tparaitem(pd.para.last),'A6');
  1085. end
  1086. else
  1087. Message(parser_e_32bitint_or_pointer_variable_expected);
  1088. end;
  1089. end;
  1090. {$endif powerpc}
  1091. tprocdef(pd).extnumber:=get_intconst;
  1092. end;
  1093. procedure pd_external(pd:tabstractprocdef);
  1094. {
  1095. If import_dll=nil the procedure is assumed to be in another
  1096. object file. In that object file it should have the name to
  1097. which import_name is pointing to. Otherwise, the procedure is
  1098. assumed to be in the DLL to which import_dll is pointing to. In
  1099. that case either import_nr<>0 or import_name<>nil is true, so
  1100. the procedure is either imported by number or by name. (DM)
  1101. }
  1102. var
  1103. import_dll,
  1104. import_name : string;
  1105. import_nr : word;
  1106. hpd : tprocdef;
  1107. begin
  1108. if pd.deftype<>procdef then
  1109. internalerror(2003042615);
  1110. tprocdef(pd).forwarddef:=false;
  1111. { forbid local external procedures }
  1112. if pd.parast.symtablelevel>normal_function_level then
  1113. Message(parser_e_no_local_external);
  1114. { If the procedure should be imported from a DLL, a constant string follows.
  1115. This isn't really correct, an contant string expression follows
  1116. so we check if an semicolon follows, else a string constant have to
  1117. follow (FK) }
  1118. import_nr:=0;
  1119. import_name:='';
  1120. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1121. begin
  1122. import_dll:=get_stringconst;
  1123. if (idtoken=_NAME) then
  1124. begin
  1125. consume(_NAME);
  1126. import_name:=get_stringconst;
  1127. end;
  1128. if (idtoken=_INDEX) then
  1129. begin
  1130. {After the word index follows the index number in the DLL.}
  1131. consume(_INDEX);
  1132. import_nr:=get_intconst;
  1133. end;
  1134. { default is to used the realname of the procedure }
  1135. if (import_nr=0) and (import_name='') then
  1136. import_name:=tprocdef(pd).procsym.realname;
  1137. { create importlib if not already done }
  1138. if not(current_module.uses_imports) then
  1139. begin
  1140. current_module.uses_imports:=true;
  1141. importlib.preparelib(current_module.modulename^);
  1142. end;
  1143. if not(m_repeat_forward in aktmodeswitches) then
  1144. begin
  1145. { we can only have one overloaded here ! }
  1146. if tprocsym(tprocdef(pd).procsym).procdef_count>1 then
  1147. hpd:=tprocsym(tprocdef(pd).procsym).procdef[2]
  1148. else
  1149. hpd:=tprocdef(pd);
  1150. end
  1151. else
  1152. hpd:=tprocdef(pd);
  1153. importlib.importprocedure(hpd,import_dll,import_nr,import_name);
  1154. end
  1155. else
  1156. begin
  1157. if (idtoken=_NAME) then
  1158. begin
  1159. consume(_NAME);
  1160. import_name:=get_stringconst;
  1161. tprocdef(pd).setmangledname(import_name);
  1162. end;
  1163. end;
  1164. end;
  1165. type
  1166. pd_handler=procedure(pd:tabstractprocdef);
  1167. proc_dir_rec=record
  1168. idtok : ttoken;
  1169. pd_flags : tpdflags;
  1170. handler : pd_handler;
  1171. pocall : tproccalloption;
  1172. pooption : tprocoptions;
  1173. mutexclpocall : tproccalloptions;
  1174. mutexclpotype : tproctypeoptions;
  1175. mutexclpo : tprocoptions;
  1176. end;
  1177. const
  1178. {Should contain the number of procedure directives we support.}
  1179. num_proc_directives=37;
  1180. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1181. (
  1182. (
  1183. idtok:_ABSTRACT;
  1184. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1185. handler : {$ifdef FPCPROCVAR}@{$endif}pd_abstract;
  1186. pocall : pocall_none;
  1187. pooption : [po_abstractmethod];
  1188. mutexclpocall : [pocall_internproc,pocall_inline];
  1189. mutexclpotype : [];
  1190. mutexclpo : [po_exports,po_interrupt,po_external]
  1191. ),(
  1192. idtok:_ALIAS;
  1193. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1194. handler : {$ifdef FPCPROCVAR}@{$endif}pd_alias;
  1195. pocall : pocall_none;
  1196. pooption : [];
  1197. mutexclpocall : [pocall_inline];
  1198. mutexclpotype : [];
  1199. mutexclpo : [po_external]
  1200. ),(
  1201. idtok:_ASMNAME;
  1202. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1203. handler : {$ifdef FPCPROCVAR}@{$endif}pd_asmname;
  1204. pocall : pocall_cdecl;
  1205. pooption : [po_external];
  1206. mutexclpocall : [pocall_internproc,pocall_inline];
  1207. mutexclpotype : [];
  1208. mutexclpo : [po_external]
  1209. ),(
  1210. idtok:_ASSEMBLER;
  1211. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1212. handler : nil;
  1213. pocall : pocall_none;
  1214. pooption : [po_assembler];
  1215. mutexclpocall : [];
  1216. mutexclpotype : [];
  1217. mutexclpo : [po_external]
  1218. ),(
  1219. idtok:_C; {same as cdecl for mode mac}
  1220. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1221. handler : nil;
  1222. pocall : pocall_cdecl;
  1223. pooption : [];
  1224. mutexclpocall : [];
  1225. mutexclpotype : [potype_constructor,potype_destructor];
  1226. mutexclpo : [po_assembler,po_external]
  1227. ),(
  1228. idtok:_CDECL;
  1229. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1230. handler : nil;
  1231. pocall : pocall_cdecl;
  1232. pooption : [];
  1233. mutexclpocall : [];
  1234. mutexclpotype : [potype_constructor,potype_destructor];
  1235. mutexclpo : [po_assembler,po_external]
  1236. ),(
  1237. idtok:_DYNAMIC;
  1238. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1239. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1240. pocall : pocall_none;
  1241. pooption : [po_virtualmethod];
  1242. mutexclpocall : [pocall_internproc,pocall_inline];
  1243. mutexclpotype : [];
  1244. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod]
  1245. ),(
  1246. idtok:_EXPORT;
  1247. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1248. handler : {$ifdef FPCPROCVAR}@{$endif}pd_export;
  1249. pocall : pocall_none;
  1250. pooption : [po_exports,po_public];
  1251. mutexclpocall : [pocall_internproc,pocall_inline];
  1252. mutexclpotype : [potype_constructor,potype_destructor];
  1253. mutexclpo : [po_external,po_interrupt]
  1254. ),(
  1255. idtok:_EXTERNAL;
  1256. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf];
  1257. handler : {$ifdef FPCPROCVAR}@{$endif}pd_external;
  1258. pocall : pocall_none;
  1259. pooption : [po_external];
  1260. mutexclpocall : [pocall_internproc,pocall_inline,pocall_syscall];
  1261. mutexclpotype : [potype_constructor,potype_destructor];
  1262. mutexclpo : [po_exports,po_interrupt,po_assembler]
  1263. ),(
  1264. idtok:_FAR;
  1265. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1266. handler : {$ifdef FPCPROCVAR}@{$endif}pd_far;
  1267. pocall : pocall_none;
  1268. pooption : [];
  1269. mutexclpocall : [pocall_internproc,pocall_inline];
  1270. mutexclpotype : [];
  1271. mutexclpo : []
  1272. ),(
  1273. idtok:_FAR16;
  1274. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1275. handler : nil;
  1276. pocall : pocall_far16;
  1277. pooption : [];
  1278. mutexclpocall : [];
  1279. mutexclpotype : [];
  1280. mutexclpo : [po_external]
  1281. ),(
  1282. idtok:_FORWARD;
  1283. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1284. handler : {$ifdef FPCPROCVAR}@{$endif}pd_forward;
  1285. pocall : pocall_none;
  1286. pooption : [];
  1287. mutexclpocall : [pocall_internproc,pocall_inline];
  1288. mutexclpotype : [];
  1289. mutexclpo : [po_external]
  1290. ),(
  1291. idtok:_OLDFPCCALL;
  1292. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1293. handler : nil;
  1294. pocall : pocall_oldfpccall;
  1295. pooption : [];
  1296. mutexclpocall : [];
  1297. mutexclpotype : [];
  1298. mutexclpo : []
  1299. ),(
  1300. idtok:_INLINE;
  1301. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1302. handler : {$ifdef FPCPROCVAR}@{$endif}pd_inline;
  1303. pocall : pocall_inline;
  1304. pooption : [];
  1305. mutexclpocall : [];
  1306. mutexclpotype : [potype_constructor,potype_destructor];
  1307. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1308. ),(
  1309. idtok:_INTERNCONST;
  1310. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1311. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1312. pocall : pocall_none;
  1313. pooption : [po_internconst];
  1314. mutexclpocall : [];
  1315. mutexclpotype : [potype_operator];
  1316. mutexclpo : []
  1317. ),(
  1318. idtok:_INTERNPROC;
  1319. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1320. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1321. pocall : pocall_internproc;
  1322. pooption : [];
  1323. mutexclpocall : [];
  1324. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1325. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1326. ),(
  1327. idtok:_INTERRUPT;
  1328. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1329. handler : {$ifdef FPCPROCVAR}@{$endif}pd_interrupt;
  1330. pocall : pocall_none;
  1331. pooption : [po_interrupt];
  1332. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1333. pocall_inline,pocall_pascal,pocall_far16,pocall_oldfpccall];
  1334. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1335. mutexclpo : [po_external]
  1336. ),(
  1337. idtok:_IOCHECK;
  1338. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1339. handler : nil;
  1340. pocall : pocall_none;
  1341. pooption : [po_iocheck];
  1342. mutexclpocall : [pocall_internproc];
  1343. mutexclpotype : [];
  1344. mutexclpo : [po_external]
  1345. ),(
  1346. idtok:_MESSAGE;
  1347. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1348. handler : {$ifdef FPCPROCVAR}@{$endif}pd_message;
  1349. pocall : pocall_none;
  1350. pooption : []; { can be po_msgstr or po_msgint }
  1351. mutexclpocall : [pocall_inline,pocall_internproc];
  1352. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1353. mutexclpo : [po_interrupt,po_external]
  1354. ),(
  1355. idtok:_NEAR;
  1356. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1357. handler : {$ifdef FPCPROCVAR}@{$endif}pd_near;
  1358. pocall : pocall_none;
  1359. pooption : [];
  1360. mutexclpocall : [pocall_internproc];
  1361. mutexclpotype : [];
  1362. mutexclpo : []
  1363. ),(
  1364. idtok:_NOSTACKFRAME;
  1365. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1366. handler : nil;
  1367. pocall : pocall_none;
  1368. pooption : [po_nostackframe];
  1369. mutexclpocall : [pocall_internproc];
  1370. mutexclpotype : [];
  1371. mutexclpo : []
  1372. ),(
  1373. idtok:_OVERLOAD;
  1374. pd_flags : [pd_implemen,pd_interface,pd_body];
  1375. handler : {$ifdef FPCPROCVAR}@{$endif}pd_overload;
  1376. pocall : pocall_none;
  1377. pooption : [po_overload];
  1378. mutexclpocall : [pocall_internproc];
  1379. mutexclpotype : [];
  1380. mutexclpo : []
  1381. ),(
  1382. idtok:_OVERRIDE;
  1383. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1384. handler : {$ifdef FPCPROCVAR}@{$endif}pd_override;
  1385. pocall : pocall_none;
  1386. pooption : [po_overridingmethod,po_virtualmethod];
  1387. mutexclpocall : [pocall_inline,pocall_internproc];
  1388. mutexclpotype : [];
  1389. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1390. ),(
  1391. idtok:_PASCAL;
  1392. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1393. handler : nil;
  1394. pocall : pocall_pascal;
  1395. pooption : [];
  1396. mutexclpocall : [];
  1397. mutexclpotype : [potype_constructor,potype_destructor];
  1398. mutexclpo : [po_external]
  1399. ),(
  1400. idtok:_PUBLIC;
  1401. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1402. handler : nil;
  1403. pocall : pocall_none;
  1404. pooption : [po_public];
  1405. mutexclpocall : [pocall_internproc,pocall_inline];
  1406. mutexclpotype : [];
  1407. mutexclpo : [po_external]
  1408. ),(
  1409. idtok:_REGISTER;
  1410. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1411. handler : nil;
  1412. pocall : pocall_register;
  1413. pooption : [];
  1414. mutexclpocall : [];
  1415. mutexclpotype : [potype_constructor,potype_destructor];
  1416. mutexclpo : [po_external]
  1417. ),(
  1418. idtok:_REINTRODUCE;
  1419. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1420. handler : {$ifdef FPCPROCVAR}@{$endif}pd_reintroduce;
  1421. pocall : pocall_none;
  1422. pooption : [po_reintroduce];
  1423. mutexclpocall : [pocall_inline,pocall_internproc];
  1424. mutexclpotype : [];
  1425. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod]
  1426. ),(
  1427. idtok:_SAFECALL;
  1428. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1429. handler : nil;
  1430. pocall : pocall_safecall;
  1431. pooption : [];
  1432. mutexclpocall : [];
  1433. mutexclpotype : [potype_constructor,potype_destructor];
  1434. mutexclpo : [po_external]
  1435. ),(
  1436. idtok:_SAVEREGISTERS;
  1437. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1438. handler : nil;
  1439. pocall : pocall_none;
  1440. pooption : [po_saveregisters];
  1441. mutexclpocall : [pocall_internproc];
  1442. mutexclpotype : [potype_constructor,potype_destructor];
  1443. mutexclpo : [po_external]
  1444. ),(
  1445. idtok:_SOFTFLOAT;
  1446. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1447. handler : nil;
  1448. pocall : pocall_softfloat;
  1449. pooption : [];
  1450. mutexclpocall : [];
  1451. mutexclpotype : [potype_constructor,potype_destructor];
  1452. { it's available with po_external because the libgcc floating point routines on the arm
  1453. uses this calling convention }
  1454. mutexclpo : []
  1455. ),(
  1456. idtok:_STATIC;
  1457. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1458. handler : {$ifdef FPCPROCVAR}@{$endif}pd_static;
  1459. pocall : pocall_none;
  1460. pooption : [po_staticmethod];
  1461. mutexclpocall : [pocall_inline,pocall_internproc];
  1462. mutexclpotype : [potype_constructor,potype_destructor];
  1463. mutexclpo : [po_external,po_interrupt,po_exports]
  1464. ),(
  1465. idtok:_STDCALL;
  1466. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1467. handler : nil;
  1468. pocall : pocall_stdcall;
  1469. pooption : [];
  1470. mutexclpocall : [];
  1471. mutexclpotype : [potype_constructor,potype_destructor];
  1472. mutexclpo : [po_external]
  1473. ),(
  1474. idtok:_SYSCALL;
  1475. pd_flags : [pd_interface,pd_implemen,pd_notobject,pd_notobjintf];
  1476. handler : {$ifdef FPCPROCVAR}@{$endif}pd_syscall;
  1477. pocall : pocall_syscall;
  1478. pooption : [];
  1479. mutexclpocall : [];
  1480. mutexclpotype : [potype_constructor,potype_destructor];
  1481. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1482. ),(
  1483. idtok:_VIRTUAL;
  1484. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1485. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1486. pocall : pocall_none;
  1487. pooption : [po_virtualmethod];
  1488. mutexclpocall : [pocall_inline,pocall_internproc];
  1489. mutexclpotype : [];
  1490. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod]
  1491. ),(
  1492. idtok:_CPPDECL;
  1493. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1494. handler : nil;
  1495. pocall : pocall_cppdecl;
  1496. pooption : [];
  1497. mutexclpocall : [];
  1498. mutexclpotype : [potype_constructor,potype_destructor];
  1499. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1500. ),(
  1501. idtok:_VARARGS;
  1502. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1503. handler : nil;
  1504. pocall : pocall_none;
  1505. pooption : [po_varargs];
  1506. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1507. pocall_inline,pocall_far16,pocall_oldfpccall];
  1508. mutexclpotype : [];
  1509. mutexclpo : [po_assembler,po_interrupt]
  1510. ),(
  1511. idtok:_COMPILERPROC;
  1512. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1513. handler : nil;
  1514. pocall : pocall_compilerproc;
  1515. pooption : [];
  1516. mutexclpocall : [];
  1517. mutexclpotype : [potype_constructor,potype_destructor];
  1518. mutexclpo : [po_interrupt]
  1519. )
  1520. );
  1521. function is_proc_directive(tok:ttoken;isprocvar:boolean):boolean;
  1522. var
  1523. i : longint;
  1524. begin
  1525. is_proc_directive:=false;
  1526. for i:=1 to num_proc_directives do
  1527. if proc_direcdata[i].idtok=idtoken then
  1528. begin
  1529. if (not isprocvar) or
  1530. (pd_procvar in proc_direcdata[i].pd_flags) then
  1531. is_proc_directive:=true;
  1532. exit;
  1533. end;
  1534. end;
  1535. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  1536. {
  1537. Parse the procedure directive, returns true if a correct directive is found
  1538. }
  1539. var
  1540. p : longint;
  1541. found : boolean;
  1542. name : stringid;
  1543. begin
  1544. parse_proc_direc:=false;
  1545. name:=tokeninfo^[idtoken].str;
  1546. found:=false;
  1547. { Hint directive? Then exit immediatly }
  1548. if (m_hintdirective in aktmodeswitches) then
  1549. begin
  1550. case idtoken of
  1551. _LIBRARY,
  1552. _PLATFORM,
  1553. _UNIMPLEMENTED,
  1554. _DEPRECATED :
  1555. exit;
  1556. end;
  1557. end;
  1558. { C directive is MAC only, because it breaks too much existing code
  1559. on other platforms (PFV) }
  1560. if (idtoken=_C) and
  1561. not(m_mac in aktmodeswitches) then
  1562. exit;
  1563. { retrieve data for directive if found }
  1564. for p:=1 to num_proc_directives do
  1565. if proc_direcdata[p].idtok=idtoken then
  1566. begin
  1567. found:=true;
  1568. break;
  1569. end;
  1570. { Check if the procedure directive is known }
  1571. if not found then
  1572. begin
  1573. { parsing a procvar type the name can be any
  1574. next variable !! }
  1575. if (pdflags * [pd_procvar,pd_object])=[] then
  1576. Message1(parser_w_unknown_proc_directive_ignored,name);
  1577. exit;
  1578. end;
  1579. { static needs a special treatment }
  1580. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1581. exit;
  1582. { Conflicts between directives ? }
  1583. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1584. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  1585. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1586. begin
  1587. Message1(parser_e_proc_dir_conflict,name);
  1588. exit;
  1589. end;
  1590. { set calling convention }
  1591. if proc_direcdata[p].pocall<>pocall_none then
  1592. begin
  1593. if (po_hascallingconvention in pd.procoptions) then
  1594. begin
  1595. Message2(parser_w_proc_overriding_calling,
  1596. proccalloptionStr[pd.proccalloption],
  1597. proccalloptionStr[proc_direcdata[p].pocall]);
  1598. end;
  1599. { check if the target processor supports this calling convention }
  1600. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  1601. begin
  1602. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  1603. { recover }
  1604. proc_direcdata[p].pocall:=pocall_stdcall;
  1605. end;
  1606. pd.proccalloption:=proc_direcdata[p].pocall;
  1607. include(pd.procoptions,po_hascallingconvention);
  1608. end;
  1609. { check if method and directive not for object, like public.
  1610. This needs to be checked also for procvars }
  1611. if (pd_notobject in proc_direcdata[p].pd_flags) and
  1612. (pd.owner.symtabletype=objectsymtable) then
  1613. exit;
  1614. if pd.deftype=procdef then
  1615. begin
  1616. { Check if the directive is only for objects }
  1617. if (pd_object in proc_direcdata[p].pd_flags) and
  1618. not assigned(tprocdef(pd)._class) then
  1619. exit;
  1620. { check if method and directive not for interface }
  1621. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  1622. is_interface(tprocdef(pd)._class) then
  1623. exit;
  1624. end;
  1625. { consume directive, and turn flag on }
  1626. consume(token);
  1627. parse_proc_direc:=true;
  1628. { Check the pd_flags if the directive should be allowed }
  1629. if (pd_interface in pdflags) and
  1630. not(pd_interface in proc_direcdata[p].pd_flags) then
  1631. begin
  1632. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1633. exit;
  1634. end;
  1635. if (pd_implemen in pdflags) and
  1636. not(pd_implemen in proc_direcdata[p].pd_flags) then
  1637. begin
  1638. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1639. exit;
  1640. end;
  1641. if (pd_procvar in pdflags) and
  1642. not(pd_procvar in proc_direcdata[p].pd_flags) then
  1643. begin
  1644. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1645. exit;
  1646. end;
  1647. { Return the new pd_flags }
  1648. if not(pd_body in proc_direcdata[p].pd_flags) then
  1649. exclude(pdflags,pd_body);
  1650. { Add the correct flag }
  1651. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  1652. { Call the handler }
  1653. if pointer({$ifndef FPCPROCVAR}@{$endif}proc_direcdata[p].handler)<>nil then
  1654. proc_direcdata[p].handler(pd);
  1655. end;
  1656. procedure handle_calling_convention(pd:tabstractprocdef);
  1657. begin
  1658. { set the default calling convention if none provided }
  1659. if not(po_hascallingconvention in pd.procoptions) then
  1660. pd.proccalloption:=aktdefproccall
  1661. else
  1662. begin
  1663. if pd.proccalloption=pocall_none then
  1664. internalerror(200309081);
  1665. end;
  1666. { handle proccall specific settings }
  1667. case pd.proccalloption of
  1668. pocall_cdecl :
  1669. begin
  1670. { set mangledname }
  1671. if (pd.deftype=procdef) then
  1672. begin
  1673. if not tprocdef(pd).has_mangledname then
  1674. begin
  1675. if assigned(tprocdef(pd)._class) then
  1676. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd)._class.objrealname^+'_'+tprocdef(pd).procsym.realname)
  1677. else
  1678. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd).procsym.realname);
  1679. end;
  1680. { check C cdecl para types }
  1681. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_c_para,nil);
  1682. end;
  1683. end;
  1684. pocall_cppdecl :
  1685. begin
  1686. { set mangledname }
  1687. if (pd.deftype=procdef) then
  1688. begin
  1689. if not tprocdef(pd).has_mangledname then
  1690. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd).cplusplusmangledname);
  1691. { check C cdecl para types }
  1692. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_c_para,nil);
  1693. end;
  1694. end;
  1695. pocall_compilerproc :
  1696. begin
  1697. if (pd.deftype<>procdef) then
  1698. internalerror(200110232);
  1699. if (target_info.system<>system_i386_watcom) then
  1700. tprocdef(pd).setmangledname(lower(tprocdef(pd).procsym.name));
  1701. end;
  1702. pocall_far16 :
  1703. begin
  1704. { Temporary stub, must be rewritten to support OS/2 far16 }
  1705. Message1(parser_w_proc_directive_ignored,'FAR16');
  1706. end;
  1707. pocall_inline :
  1708. begin
  1709. if not(cs_support_inline in aktmoduleswitches) then
  1710. begin
  1711. Message(parser_e_proc_inline_not_supported);
  1712. pd.proccalloption:=pocall_default;
  1713. end;
  1714. end;
  1715. end;
  1716. { For varargs directive also cdecl and external must be defined }
  1717. if (po_varargs in pd.procoptions) then
  1718. begin
  1719. { check first for external in the interface, if available there
  1720. then the cdecl must also be there since there is no implementation
  1721. available to contain it }
  1722. if parse_only then
  1723. begin
  1724. { if external is available, then cdecl must also be available,
  1725. procvars don't need external }
  1726. if not((po_external in pd.procoptions) or
  1727. (pd.deftype=procvardef)) and
  1728. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1729. Message(parser_e_varargs_need_cdecl_and_external);
  1730. end
  1731. else
  1732. begin
  1733. { both must be defined now }
  1734. if not((po_external in pd.procoptions) or
  1735. (pd.deftype=procvardef)) or
  1736. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1737. Message(parser_e_varargs_need_cdecl_and_external);
  1738. end;
  1739. end;
  1740. { add mangledname to external list }
  1741. if (pd.deftype=procdef) and
  1742. (po_external in pd.procoptions) and
  1743. target_info.DllScanSupported then
  1744. current_module.externals.insert(tExternalsItem.create(tprocdef(pd).mangledname));
  1745. end;
  1746. procedure calc_parast(pd:tabstractprocdef);
  1747. var
  1748. currpara : tparaitem;
  1749. begin
  1750. { insert hidden high parameters }
  1751. insert_hidden_para(pd);
  1752. { insert hidden self parameter }
  1753. insert_self_and_vmt_para(pd);
  1754. { insert funcret parameter if required }
  1755. insert_funcret_para(pd);
  1756. { insert parentfp parameter if required }
  1757. insert_parentfp_para(pd);
  1758. currpara:=tparaitem(pd.para.first);
  1759. while assigned(currpara) do
  1760. begin
  1761. if not(assigned(currpara.parasym) and (currpara.parasym.typ=varsym)) then
  1762. internalerror(200304232);
  1763. { connect parasym to paraitem }
  1764. tvarsym(currpara.parasym).paraitem:=currpara;
  1765. { We need a local copy for a value parameter when only the
  1766. address is pushed. Open arrays and Array of Const are
  1767. an exception because they are allocated at runtime and the
  1768. address that is pushed is patched }
  1769. if (currpara.paratyp=vs_value) and
  1770. paramanager.push_addr_param(currpara.paratyp,currpara.paratype.def,pd.proccalloption) and
  1771. not(is_open_array(currpara.paratype.def) or
  1772. is_array_of_const(currpara.paratype.def)) then
  1773. include(tvarsym(currpara.parasym).varoptions,vo_has_local_copy);
  1774. currpara:=tparaitem(currpara.next);
  1775. end;
  1776. end;
  1777. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  1778. {
  1779. Parse the procedure directives. It does not matter if procedure directives
  1780. are written using ;procdir; or ['procdir'] syntax.
  1781. }
  1782. var
  1783. res : boolean;
  1784. begin
  1785. while token in [_ID,_LECKKLAMMER] do
  1786. begin
  1787. if try_to_consume(_LECKKLAMMER) then
  1788. begin
  1789. repeat
  1790. parse_proc_direc(pd,pdflags);
  1791. until not try_to_consume(_COMMA);
  1792. consume(_RECKKLAMMER);
  1793. { we always expect at least '[];' }
  1794. res:=true;
  1795. end
  1796. else
  1797. begin
  1798. res:=parse_proc_direc(pd,pdflags);
  1799. end;
  1800. { A procedure directive normally followed by a semicolon, but in
  1801. a const section or reading a type we should stop when _EQUAL is found,
  1802. because a constant/default value follows }
  1803. if res then
  1804. begin
  1805. if (block_type in [bt_const,bt_type]) and
  1806. (token=_EQUAL) then
  1807. break;
  1808. { support procedure proc;stdcall export; }
  1809. if not(is_proc_directive(token,(pd.deftype=procvardef))) then
  1810. consume(_SEMICOLON);
  1811. end
  1812. else
  1813. break;
  1814. end;
  1815. end;
  1816. procedure parse_var_proc_directives(sym:tsym);
  1817. var
  1818. pdflags : tpdflags;
  1819. pd : tabstractprocdef;
  1820. begin
  1821. pdflags:=[pd_procvar];
  1822. pd:=nil;
  1823. case sym.typ of
  1824. varsym :
  1825. pd:=tabstractprocdef(tvarsym(sym).vartype.def);
  1826. typedconstsym :
  1827. pd:=tabstractprocdef(ttypedconstsym(sym).typedconsttype.def);
  1828. typesym :
  1829. pd:=tabstractprocdef(ttypesym(sym).restype.def);
  1830. else
  1831. internalerror(2003042617);
  1832. end;
  1833. if pd.deftype<>procvardef then
  1834. internalerror(2003042618);
  1835. { names should never be used anyway }
  1836. parse_proc_directives(pd,pdflags);
  1837. end;
  1838. procedure parse_object_proc_directives(pd:tabstractprocdef);
  1839. var
  1840. pdflags : tpdflags;
  1841. begin
  1842. pdflags:=[pd_object];
  1843. parse_proc_directives(pd,pdflags);
  1844. end;
  1845. function proc_add_definition(var pd:tprocdef):boolean;
  1846. {
  1847. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  1848. forwarddef is found and reused it returns true
  1849. }
  1850. var
  1851. hd : tprocdef;
  1852. ad,fd : tsym;
  1853. s1,s2 : stringid;
  1854. i : cardinal;
  1855. forwardfound : boolean;
  1856. po_comp : tprocoptions;
  1857. aprocsym : tprocsym;
  1858. begin
  1859. forwardfound:=false;
  1860. aprocsym:=tprocsym(pd.procsym);
  1861. { check overloaded functions if the same function already exists }
  1862. for i:=1 to aprocsym.procdef_count do
  1863. begin
  1864. hd:=aprocsym.procdef[i];
  1865. { Skip overloaded definitions that are declared in other
  1866. units }
  1867. if hd.procsym<>aprocsym then
  1868. continue;
  1869. { check the parameters, for delphi/tp it is possible to
  1870. leave the parameters away in the implementation (forwarddef=false).
  1871. But for an overload declared function this is not allowed }
  1872. if { check if empty implementation arguments match is allowed }
  1873. (
  1874. not(m_repeat_forward in aktmodeswitches) and
  1875. not(pd.forwarddef) and
  1876. (pd.maxparacount=0) and
  1877. not(po_overload in hd.procoptions)
  1878. ) or
  1879. { check arguments }
  1880. (
  1881. (compare_paras(pd.para,hd.para,cp_none,[cpo_comparedefaultvalue])>=te_equal) and
  1882. { for operators equal_paras is not enough !! }
  1883. ((pd.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1884. equal_defs(hd.rettype.def,pd.rettype.def))
  1885. ) then
  1886. begin
  1887. { Check if we've found the forwarddef, if found then
  1888. we need to update the forward def with the current
  1889. implementation settings }
  1890. if hd.forwarddef then
  1891. begin
  1892. forwardfound:=true;
  1893. { Check if the procedure type and return type are correct,
  1894. also the parameters must match also with the type }
  1895. if (hd.proctypeoption<>pd.proctypeoption) or
  1896. (
  1897. (m_repeat_forward in aktmodeswitches) and
  1898. (not((pd.maxparacount=0) or
  1899. (compare_paras(pd.para,hd.para,cp_all,[cpo_comparedefaultvalue])>=te_equal)))
  1900. ) or
  1901. (
  1902. ((m_repeat_forward in aktmodeswitches) or
  1903. not(is_void(pd.rettype.def))) and
  1904. (not equal_defs(hd.rettype.def,pd.rettype.def))) then
  1905. begin
  1906. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,
  1907. pd.fullprocname(false));
  1908. aprocsym.write_parameter_lists(pd);
  1909. break;
  1910. end;
  1911. { Check if both are declared forward }
  1912. if hd.forwarddef and pd.forwarddef then
  1913. begin
  1914. MessagePos1(pd.fileinfo,parser_e_function_already_declared_public_forward,
  1915. pd.fullprocname(false));
  1916. end;
  1917. { internconst or internproc only need to be defined once }
  1918. if (hd.proccalloption=pocall_internproc) then
  1919. pd.proccalloption:=hd.proccalloption
  1920. else
  1921. if (pd.proccalloption=pocall_internproc) then
  1922. hd.proccalloption:=pd.proccalloption;
  1923. if (po_internconst in hd.procoptions) then
  1924. include(pd.procoptions,po_internconst)
  1925. else if (po_internconst in pd.procoptions) then
  1926. include(hd.procoptions,po_internconst);
  1927. { Check calling convention }
  1928. if (hd.proccalloption<>pd.proccalloption) then
  1929. begin
  1930. { In delphi it is possible to specify the calling
  1931. convention in the interface or implementation if
  1932. there was no convention specified in the other
  1933. part }
  1934. if (m_delphi in aktmodeswitches) then
  1935. begin
  1936. if not(po_hascallingconvention in pd.procoptions) then
  1937. pd.proccalloption:=hd.proccalloption
  1938. else
  1939. if not(po_hascallingconvention in hd.procoptions) then
  1940. hd.proccalloption:=pd.proccalloption
  1941. else
  1942. begin
  1943. MessagePos(pd.fileinfo,parser_e_call_convention_dont_match_forward);
  1944. aprocsym.write_parameter_lists(pd);
  1945. { restore interface settings }
  1946. pd.proccalloption:=hd.proccalloption;
  1947. end;
  1948. end
  1949. else
  1950. begin
  1951. MessagePos(pd.fileinfo,parser_e_call_convention_dont_match_forward);
  1952. aprocsym.write_parameter_lists(pd);
  1953. { restore interface settings }
  1954. pd.proccalloption:=hd.proccalloption;
  1955. end;
  1956. end;
  1957. { Check procedure options, Delphi requires that class is
  1958. repeated in the implementation for class methods }
  1959. if (m_fpc in aktmodeswitches) then
  1960. po_comp:=[po_varargs,po_methodpointer,po_interrupt]
  1961. else
  1962. po_comp:=[po_classmethod,po_methodpointer];
  1963. if ((po_comp * hd.procoptions)<>(po_comp * pd.procoptions)) then
  1964. begin
  1965. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,
  1966. pd.fullprocname(false));
  1967. aprocsym.write_parameter_lists(pd);
  1968. { This error is non-fatal, we can recover }
  1969. end;
  1970. { Check manglednames }
  1971. if (m_repeat_forward in aktmodeswitches) or
  1972. (pd.minparacount>0) then
  1973. begin
  1974. { If mangled names are equal then they have the same amount of arguments }
  1975. { We can check the names of the arguments }
  1976. { both symtables are in the same order from left to right }
  1977. ad:=tsym(hd.parast.symindex.first);
  1978. fd:=tsym(pd.parast.symindex.first);
  1979. repeat
  1980. { skip default parameter constsyms }
  1981. while assigned(ad) and (ad.typ<>varsym) do
  1982. ad:=tsym(ad.indexnext);
  1983. while assigned(fd) and (fd.typ<>varsym) do
  1984. fd:=tsym(fd.indexnext);
  1985. { stop when one of the two lists is at the end }
  1986. if not assigned(ad) or not assigned(fd) then
  1987. break;
  1988. { retrieve names, remove reg for register parameters }
  1989. s1:=ad.name;
  1990. s2:=fd.name;
  1991. { compare names }
  1992. if (s1<>s2) then
  1993. begin
  1994. MessagePos3(pd.fileinfo,parser_e_header_different_var_names,
  1995. aprocsym.name,s1,s2);
  1996. break;
  1997. end;
  1998. ad:=tsym(ad.indexnext);
  1999. fd:=tsym(fd.indexnext);
  2000. until false;
  2001. if assigned(ad) xor assigned(fd) then
  2002. internalerror(200204178);
  2003. end;
  2004. { Everything is checked, now we can update the forward declaration
  2005. with the new data from the implementation }
  2006. hd.forwarddef:=pd.forwarddef;
  2007. hd.hasforward:=true;
  2008. hd.procoptions:=hd.procoptions+pd.procoptions;
  2009. if hd.extnumber=65535 then
  2010. hd.extnumber:=pd.extnumber;
  2011. while not pd.aliasnames.empty do
  2012. hd.aliasnames.insert(pd.aliasnames.getfirst);
  2013. { update fileinfo so position references the implementation,
  2014. also update funcretsym if it is already generated }
  2015. hd.fileinfo:=pd.fileinfo;
  2016. if assigned(hd.funcretsym) then
  2017. hd.funcretsym.fileinfo:=pd.fileinfo;
  2018. { update mangledname if the implementation has a fixed mangledname set }
  2019. if pd.has_mangledname then
  2020. begin
  2021. { rename also asmsymbol first, because the name can already be used }
  2022. objectlibrary.renameasmsymbol(hd.mangledname,pd.mangledname);
  2023. hd.setmangledname(pd.mangledname);
  2024. end;
  2025. { for compilerproc defines we need to rename and update the
  2026. symbolname to lowercase }
  2027. if (pd.proccalloption=pocall_compilerproc) then
  2028. begin
  2029. { rename to lowercase so users can't access it }
  2030. aprocsym.owner.rename(aprocsym.name,lower(aprocsym.name));
  2031. { also update the realname that is stored in the ppu }
  2032. stringdispose(aprocsym._realname);
  2033. aprocsym._realname:=stringdup('$'+aprocsym.name);
  2034. { the mangeled name is already changed by the pd_compilerproc }
  2035. { handler. It must be done immediately because if we have a }
  2036. { call to a compilerproc before it's implementation is }
  2037. { encountered, it must already use the new mangled name (JM) }
  2038. end;
  2039. { the procdef will be released by the symtable, we release
  2040. at least the parast }
  2041. pd.releasemem;
  2042. pd:=hd;
  2043. end
  2044. else
  2045. begin
  2046. { abstract methods aren't forward defined, but this }
  2047. { needs another error message }
  2048. if (po_abstractmethod in hd.procoptions) then
  2049. MessagePos(pd.fileinfo,parser_e_abstract_no_definition)
  2050. else
  2051. MessagePos(pd.fileinfo,parser_e_overloaded_have_same_parameters);
  2052. end;
  2053. { we found one proc with the same arguments, there are no others
  2054. so we can stop }
  2055. break;
  2056. end;
  2057. { check for allowing overload directive }
  2058. if not(m_fpc in aktmodeswitches) then
  2059. begin
  2060. { overload directive turns on overloading }
  2061. if ((po_overload in pd.procoptions) or
  2062. (po_overload in hd.procoptions)) then
  2063. begin
  2064. { check if all procs have overloading, but not if the proc is a method or
  2065. already declared forward, then the check is already done }
  2066. if not(hd.hasforward or
  2067. assigned(pd._class) or
  2068. (pd.forwarddef<>hd.forwarddef) or
  2069. ((po_overload in pd.procoptions) and
  2070. (po_overload in hd.procoptions))) then
  2071. begin
  2072. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,aprocsym.realname);
  2073. break;
  2074. end;
  2075. end
  2076. else
  2077. begin
  2078. if not(hd.forwarddef) then
  2079. begin
  2080. MessagePos(pd.fileinfo,parser_e_procedure_overloading_is_off);
  2081. break;
  2082. end;
  2083. end;
  2084. end; { equal arguments }
  2085. end;
  2086. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2087. list }
  2088. if not forwardfound then
  2089. begin
  2090. aprocsym.addprocdef(pd);
  2091. { add overloadnumber for unique naming, the overloadcount is
  2092. counted per module and 0 for the first procedure }
  2093. pd.overloadnumber:=aprocsym.overloadcount;
  2094. inc(aprocsym.overloadcount);
  2095. end;
  2096. proc_add_definition:=forwardfound;
  2097. end;
  2098. end.
  2099. {
  2100. $Log$
  2101. Revision 1.182 2004-06-20 08:55:30 florian
  2102. * logs truncated
  2103. Revision 1.181 2004/06/16 20:07:09 florian
  2104. * dwarf branch merged
  2105. Revision 1.180 2004/05/23 20:54:39 peter
  2106. * fixed 3114
  2107. Revision 1.179 2004/05/23 19:06:26 peter
  2108. * expect : after function when it is a forwarddef
  2109. Revision 1.178 2004/05/12 13:21:09 karoly
  2110. * few small changes to add syscall support to M68k/Amiga target
  2111. Revision 1.177 2004/05/11 22:52:48 olle
  2112. * Moved import_implicit_external to symsym
  2113. }